Closed Bug 475444 Opened 15 years ago Closed 11 years ago

HTTP standard violation, Content-length header appears to be ignored under some circumstances


(Core :: Networking: HTTP, defect)

Windows XP
Not set





(Reporter: zextra, Assigned: michal)





(1 file, 2 obsolete files)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/ Safari/525.19
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/2008120122 Firefox/3.0.5

I have my own little application server. Although the way I have discovered this was due to a bug in my own software, here's a rundown of why do I think this is a violation of standard. Of course, it's not up to me to decide if it's a real bug or a feature.

On every valid HTTP request, my server sent valid HTTP response back to the client (I have Firefox as one of my client browsers).

Example of request URI:


Example response:

-----start of response------
HTTP/1.1 200 OK
Content-Length: 37
Connection: close
Content-Type: application/json

-----end of response-------

So, a typical JSON response. But, one might notice that content-length header contains value a lot smaller than actual body size, which is 94 (that's because of a bug in my software, but I don't believe it matters in this case).

Nevertheless, Firefox did grab whole body, irregardless of size specified in content-length. For instance, I have tested same behavior with IE7 and wget, and they both conform to the standard specification (they have grabbed only first 37 bytes of content).

Because these circumstances are quite rare today, I haven't been able to confirm this behavior anywhere else except in my own lab conditions. But it should be as simple as writing 

Reproducible: Always

Steps to Reproduce:
1. make sure that you have a http server which returns smaller content-length value than size of the body is
2. open up a browser
3. enter exact url leading to mis-generated resource in address bar
4. compare received content size to specified content-length specified in header
Actual Results:  
content body is received in full

Expected Results:  
content body should be received up to the length specified in content-length header
Component: General → Networking: HTTP
Product: Firefox → Core
QA Contact: general → networking.http
I cannot reproduce it on linux, neither with 3.0.5, neither with latest trunk.
I've set up a test similar to the one you described (except I use text/plain to make it easier to see in firefox) on On that url, I only see 37 characters.
You're right, same happens for me on given url.

Can you try sending exactly the same headers (including content-type and connection: close) as well? It could be related in some way...

Another suggestion might be to try with content length just slightly smaller than real content length (I know that it shouldn't matter, but who knows).

I'll play more with my local app and report back on my findings.
looks like Connection:close is the triggering header:
(I also tested with content-type: application/json, but results are the same)
I don't understand if you're able to confirm this by sending connection: close or not?

Anyway, here are few more things. What I noticed when I tested output of your first url via telnet, is that web server stops sending data after specified content-length, and since it's keep-alive connection, it waits for some specified interval, flushes rest of the output, and closes the connection.

When connection: close is sent (second url), whole output is sent at once, thus fulfilling required step number 1. But, even then, I'm still unable to reproduce it using firefox.

For the sake of confirmation, I've exposed my app to public, try testing it using following url:

Server response:
HTTP/1.1 200 OK
Content-Length: 88
Connection: close
Content-Type: application/json


It still confirms unexpected behavior for me.

Anyhow, I'm not quite sure how important this very special case and deviation from standard really is... What do you think?
Assignee: nobody → michal
Attached patch patch v1 (obsolete) — Splinter Review
Content-Length header is ignored only in case of HTTP/1.0 when connection isn't persistent.

I tried to write unittest but unfortunately testing httpd doesn't allow to set incorrect Content-Length header. It always computes correct value from bodyInputStream.
Attachment #367937 - Flags: review?(bzbarsky)
Ever confirmed: true
I think Waldo's working on a patch that would let you set bogus content-lengths...
Comment on attachment 367937 [details] [diff] [review]
patch v1

Looks ok, but I'd like Christian to take a look too.
Attachment #367937 - Flags: superreview?(cbiesinger)
Attachment #367937 - Flags: review?(bzbarsky)
Attachment #367937 - Flags: review+
Comment on attachment 367937 [details] [diff] [review]
patch v1

It's not like HTTP 1.1 gets this always right, but all cases I've seen the additional data is just garbage (and is generally on keepalive connections anyway. see bug 254181 et al)
Attachment #367937 - Flags: superreview?(cbiesinger) → superreview+
(In reply to comment #7)
> I think Waldo's working on a patch that would let you set bogus
> content-lengths...

With bug 396226 that'll be possible, but the most future-proof solution will be in bug 484027, which will let you send whatever random junk you want to the HTTP client, in whatever format you want, and the server won't make any attempt to ensure any formatting whatsoever -- you get complete control except possibly for connection timeouts.  That you can set a bogus Content-Length with only the former is a side effect more than an intended result, and eventually that sort of thing will probably be broken when I implement chunked responses and keepalive connections (the latter of which has already been directly requested, so it's not just a hypothetical feature).
Depends on: 484027
Keywords: checkin-needed
Closed: 15 years ago
Keywords: checkin-needed
Resolution: --- → FIXED
Target Milestone: --- → mozilla1.9.2a1
This is causing lots of talos crashes, e.g.

Going to backout...
Resolution: FIXED → ---
Attached patch patch v2 (obsolete) — Splinter Review
The problem is that nsHttpTransaction::TakeResponseHead() nulls out mResponseHead. I didn't notice that and it didn't crash on small web pages :-/
Attachment #367937 - Attachment is obsolete: true
Attachment #371357 - Flags: review?(bzbarsky)
Comment on attachment 371357 [details] [diff] [review]
patch v2

Michal, you probably want to add the following to your .hgrc to make your hg export patches more readable:

  showfunc = true
  unified = 8

Attachment #371357 - Flags: review?(bzbarsky) → review+
Right! I had in my .hgrc

diff=-p -U 8

but this option doesn't take effect for hg export
Attachment #371357 - Attachment is obsolete: true
Attachment #371486 - Flags: superreview?(cbiesinger)
Attachment #371486 - Flags: superreview?(cbiesinger) → superreview+
Closed: 15 years ago11 years ago
Resolution: --- → FIXED
Target Milestone: mozilla1.9.2a1 → mozilla17
You need to log in before you can comment on or make changes to this bug.