Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Issue accurate Content-Length whenever possible #86

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

camflint
Copy link

@camflint camflint commented Jul 28, 2016

There are several scenarios where we are serving content without setting the Content-Length header. Today have a safeguard in place to force the connection close in the absence of a Content-Length, since HTTP 1.1 uses keepalive connections by default. However in many of these cases we know or can easily determine the content length ahead of time.

Example: Normal request for a file

GET /document.txt HTTP/1.1

HTTP/1.1 200 OK
Content-Length: 25

This is a test document

Example: Range requests

GET /document.txt HTTP/1.1
Range:bytes=10-

HTTP/1.1 200 OK
Content-Range: bytes 10-24/25
Content-Length: 15

test document
GET /document.txt HTTP/1.1
Range: bytes=1-2,5-7,6-10

HTTP/1.1 200 OK
Content-Length: 249

--fc07c753-75b4-4643-8c9a-3479f4184951
Content-Type: text/plain
Content-Range: bytes 1-2/25

hi
--fc07c753-75b4-4643-8c9a-3479f4184951
Content-Type: text/plain
Content-Range: bytes 5-10/25

is a t
--fc07c753-75b4-4643-8c9a-3479f4184951--

Example: sub pipe

N.B.: sub.txt is {{host}} {{domains[]}} {{ports[http][0]}}, and Content-Length is computed after, not before substitution.

GET /sub.txt?pipe=sub HTTP/1.1

HTTP/1.1 200 OK
Content-Length: 25

localhost localhost 19336

Example: slice pipe

GET /document.txt?pipe=slice(1,10) HTTP/1.1

HTTP/1.1 200 OK
Content-Length: 9

his is a 

Example: HEAD request

N.B.: As per HTTP spec, Content-Length should specify the size of the entity body which would be returned by a GET to the same resource.

HEAD /document.txt HTTP/1.1

HTTP/1.1 200 OK
Content-Length: 25

Example: header pipe overriding Content-Length

GET /document.txt?pipe=header(Content-Length,882) HTTP/1.1

HTTP/1.1 200 OK
Content-Length: 882

This is a test document

Note in particular as shown in the last example that the default Content-Length does not overwrite any explicitly-specified Content-Length header (set by pipes, .header files, Python etc.) This keeps in the spirit of wptserve being a well-behaved HTTP server by default, but which can be customized even to send invalid HTTP responses in order to test how browsers handle them.

Ref: #17


This change is Reviewable

camflint added 4 commits July 27, 2016 17:31
Some headers allow commas in their values, for example:

 Cache-Control: no-store, no-cache

wptserve already supports escaping commas in header values by prepending
the comma with a backslash ('\'), so this change just adds mention of that
in the docs.
* test_response tries to override end_headers in order that the body would
be written as the header value, but it forgets to append the '\r\n'
separator between the headers and the body, such that the response is
malformed, and the X-Test header cannot even be parsed properly.

* test_stash needs a stash server in order to pass.
There are several scenarios where we are serving content without setting
the Content-Length header. Today have a safeguard in place to force the
connection close in the absence of a Content-Length, since HTTP 1.1 uses
keepalive connections by default. However in many of these cases we know or
can easily determine the content length ahead of time, so we may as well
be consistent about it.

Note that the default Content-Length will not overwrite any explicitly-
specified Content-Length header (set by pipes, .header files, Python etc.)
This keeps in the spirit of wptserve being a well-behaved HTTP server by
default, but which can be customized even to send invalid HTTP responses
in order to test how browsers handle them.
@camflint camflint changed the title Issue default Content-Length wherever possible Issue accurate Content-Length whenever possible Aug 1, 2016
@jdm
Copy link
Contributor

jdm commented Jan 20, 2017

@jgraham This seems like a useful improvement if it were rebased.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants