05/15/06: Rails / lighttpd gotcha: AJAX stalling? Check your Content-Length header

So neither Ruby on Rails nor lighttpd set the Content-Length header in HTTP responses on their own. That means that the browser doesn't know when it's gotten the whole response, so it has to keep the connection open in case more bytes come down. In normal web usage this doesn't cause a problem (*) since browsers are smart enough to display partial results. But for AJAX responses it means that, sometimes (**) the onComplete handler (***) doesn't get called for over a minute. The onInteractive handler gets called quickly, but that doesn't help, since usually you do stuff during onComplete when you're guaranteed to have the full response.

Here's the fix in Rails. Put the following in your application.rb:

 after_filter :set_content_length

 def set_content_length
   @response.headers['Content-Length'] = @response.body.length
Anyone who wants to fix the above to check for an existing content-length, be my guest :-)

(**) The stalling happens sporadically, not sure with what pattern. In one app we saw it maybe one in 5 requests; some of those were actually HTTP requests so the

(*) Occasionally the page will be fully visible but the cursor will still be a pointer-plus-spinning-hourglass. And this may cause performance problems in high-volume sites.

(***) onInteractive and onComplete are prototype.js event callbacks; the corresponding standard XMLHttpRequest "ready states" are INTERACTIVE and COMPLETE; "ready state" is a parameter to the onReadyStateChange callback.

(****) Apache is nice enough to tack on a proper Content-Length for you, so if you're running behind Apache you're safe from this problem.

Comments made

I tried your solution, but looking at the headers as received by the client, they don't seem to have been transmitted. Is it possible that lighttpd actually strips the Content-Length from the header after it is set by Rails? Well, I assume that is what is happening because lighttpd seems to insist on sending everything with "Transfer-Encoding: chunked". I wonder what version of lighttpd you had/have your solution working on? I'm on lighttpd 1.4.10. Btw, I also posted a question concerning this (and citing your blog) on .
06/26/06 23:13:21

Even the college sports teams inevitably started to produce snapbacks of their schools and universities. This led to the snapback cap gaining enormous popularity during the 1980's through the 1990's, even with the fitted hats being extremely well taken.
02/27/12 17:36:10
People should be able to wear the hat in a range of situations as well, whether are out neighborhood with the kids, playing a round of golf, or walking throughout the grocery store.
03/13/12 20:16:24
Probably the easiest way to identify fake <a href="">True Religion Outlet</a> is the shape of the pocket flaps. On originals <a href="">True Religion Jeans Outlet</a> both the back pockets and the front coin pocket are curved with a pronounced point in the center.
03/26/12 20:30:51

Nothing provides more satisfaction than purchasing a branded accessory at discounted prices. And if the accessory you are buying are the world Fake Oakley Sunglasses, then you've got all the more reason to feel excited. Why? Because you're going to own a glamorous pair of shades which will ensure everlasting protection for your eyes. How? This is due to the brand's commitment in undertaking research to develop cutting-edge optical technologies in their glasses. As a result, Replica Oakley Sunglasses are today considered one of the best when it comes to vision care.
04/01/12 00:52:06

Add comment

Sorry, but due to blog comment spam, I have to ask you to create an account before you post a comment. Please log in (using the form on the top right of the page) or click here to create an account: Create an account!