views:

1004

answers:

3

I have developed a solution that relies on an AJAX call to retrieve information and update the client page every 10 seconds. This is working fine, but I am concerned at the scalability of the code, given the number and length of headers being passed from client to server and back again. I have removed a number of redundant headers on the server side, mostly ASP.NET-related, and now I'm attempting to cut down on the headers coming from the client.

The browser used by my company is IE (version 6, to be upgraded to 7 soon). This is an approximation of my current code:

var xmlHTTP = new ActiveXObject('Microsoft.XMLHTTP');

xmlHTTP.onreadystatechange = function() {
    if ((xmlHTTP.readyState == 4) && (xmlHTTP.status == 200)) {
        myCallbackFunction(xmlHTTP);
    }
};

xmlHTTP.open('GET', 'myUrl.aspx');

try {
    xmlHTTP.setRequestHeader("User-Agent", ".");
    xmlHTTP.setRequestHeader("Accept", ".");
    xmlHTTP.setRequestHeader("Accept-Language", ".");
    xmlHTTP.setRequestHeader("Content-Type", ".");
} catch(e) {}

xmlHTTP.send();

Although I've read that it's possible to clear some of these headers, I haven't found a way of doing it that works in IE6. Setting them to null results in a Type Mismatch exception, so I've ended up just replacing them with '.' for the time being. Is there another way of clearing them or an alternative method of reducing the submitted HTTP headers?

Also, there seems to be no way of replacing or shortening the 'Referrer' header at all.

+2  A: 

According to the WD spec

The setRequestHeader() method appends a value if the HTTP header given as argument is already part of the list of request headers.

That is, you could only add headers, not replace them.

This doesn't completely match current browser behaviour, but it may be where browsers will be headed, in which case any efforts on this front are a waste of time in the long term. In any case current browser behaviour with setting headers is very varied and generally can't be relied upon.

There seems to be no way of replacing or shortening the 'Referrer' header at all.

That wouldn't surprise me, given that some people misguidedly use ‘Referer’ [sic] as an access-control mechanism.

You could try to make sure that the current page URL wasn't excessively long, but to be honest all this smells of premature optimisation to me. Whatever you do your request is going to fit within one IP packet, so there's not gonig to be a big visible performance difference.

It may be worthwhile for Mibbit (as mentioned on the blog you linked) to try this stuff, because Mibbit draws a quite staggering amount of traffic, but for a simple company-wide application I don't think the cross-browser-and-proxy-testing-burden:end-user-benefit ratio of messing with the headers is worth it.

bobince
Thanks for your comprehensive answer, bobince.That's a great point about the IP packet size. I will check what this is set to and ensure that we don't span two by mistake.It may have been a case of worrying over nothing, but this will get heavy traffic - many hundreds of users simultaneously.
Dave R.
A: 

IE 6 and older versions use the ActiveXObject created from MSXML.XMLHTTP (actually derived from IXMLHTTPRequest), whereas IE 7 and other modern browsers such as Mozilla use an intrinsic object called XmlHttpRequest. This is probably the reason why you cannot set the Request headers to null for the MSXML implementation but you can for the in-built object.

Therefore, I do not believe that there is any way to collectively clear all the headers. The link to Mibbit that you present only provides a function to set all the headers to null one by one. For normal scenarios, cutting down on headers may prove to be a very very insignificant of reducing traffic load.

That said, I am curious to know why you are setting the request headers to "." rather than an empty string "".

Cerebrus
Thanks Cerebus. I was just using '.' so I could see the value in my debugging session - no ulterior motive :)I have taken your and bobince's advice on board and I'm going ahead without this header manipulation. Thanks to you both for your help.
Dave R.
A: 

I would forgo this kind of micro-optimizations and look into a push model instead. By way of a starting place:

Both of these are typically paired with an XMPP server on the back-end.

Chase Seibert
Thank you for your answer. Unfortunately, the project is nearly complete, so changing the whole stack at this stage isn't appropriate. I also haven't coded in ActionScript before, so that's not really an option. However, I'll be sure to investigate your recommendations in the future. Thanks again.
Dave R.