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

RESOLVED FIXED in mozilla17

Status

()

Core
Networking: HTTP
RESOLVED FIXED
9 years ago
a year ago

People

(Reporter: zextra, Assigned: michal)

Tracking

unspecified
mozilla17
x86
Windows XP
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(URL)

Attachments

(1 attachment, 2 obsolete attachments)

(Reporter)

Description

9 years ago
User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.43 Safari/525.19
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) 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:

http://localhost:1313/index.php?action=test

Example response:

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

test({"volid":"6C9DDEE2","oldsid":"57cabb368034fa07b18e5daf032d1547","newsid":""
,"ver":"1.1"})
-----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).

http://www.w3.org/Protocols/HTTP/1.0/spec.html#Content-Length

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

Comment 1

9 years ago
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 http://www.fdn.fr/~arenevier/bugs/mozilla_475444.php On that url, I only see 37 characters.
(Reporter)

Comment 2

9 years ago
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.

Comment 3

9 years ago
Hi,
looks like Connection:close is the triggering header:
http://www.fdn.fr/~arenevier/bugs/mozilla_475444-2.php
(I also tested with content-type: application/json, but results are the same)

Updated

9 years ago
(Reporter)

Comment 4

9 years ago
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:

http://zextra.ath.cx:54321/index.php?action=none

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

none({"volid":"6C9DDEE2","oldsid":"5dcbf65634d694e521069d29e72c95cb","newsid":"","ver":"1.1"})
--------end-------

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)

Updated

9 years ago
Assignee: nobody → michal
(Assignee)

Comment 5

9 years ago
This seems to be a feature and not a bug:
http://hg.mozilla.org/mozilla-central/annotate/94678a2f5ae9/netwerk/protocol/http/src/nsHttpTransaction.cpp#l934

But maybe the behavior should be different for HTTP/1.1, see https://bugzilla.mozilla.org/show_bug.cgi?id=83960#c8
(Assignee)

Comment 6

9 years ago
Created attachment 367937 [details] [diff] [review]
patch v1

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)
(Assignee)

Updated

9 years ago
Status: UNCONFIRMED → ASSIGNED
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).
(Assignee)

Updated

9 years ago
Depends on: 484027
Keywords: checkin-needed
http://hg.mozilla.org/mozilla-central/rev/905b72e27bb9
Status: ASSIGNED → RESOLVED
Last Resolved: 9 years ago
Keywords: checkin-needed
Resolution: --- → FIXED
Target Milestone: --- → mozilla1.9.2a1
This is causing lots of talos crashes, e.g.
http://tinderbox.mozilla.org/showlog.cgi?log=Firefox/1238943422.1238945288.3595.gz

Going to backout...

Updated

9 years ago
Status: RESOLVED → REOPENED
Resolution: FIXED → ---
(Assignee)

Comment 13

9 years ago
Created attachment 371357 [details] [diff] [review]
patch v2

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:

  [diff]
  showfunc = true
  unified = 8

r=me
Attachment #371357 - Flags: review?(bzbarsky) → review+
(Assignee)

Comment 15

9 years ago
Created attachment 371486 [details] [diff] [review]
patch v3 - unified diff with function names

Right! I had in my .hgrc

[defaults]
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+
(Assignee)

Comment 16

5 years ago
https://hg.mozilla.org/integration/mozilla-inbound/rev/a6c9088eb267
https://hg.mozilla.org/mozilla-central/rev/a6c9088eb267
Status: REOPENED → RESOLVED
Last Resolved: 9 years ago5 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.