http.Request and Content-Length

2,957 views
Skip to first unread message

Russ Ross

unread,
Jun 8, 2011, 3:30:23 PM6/8/11
to golang-nuts
I am using an http.Request object to upload a file using the HTTP PUT
method. Everything is fine except when attempting to upload a zero-
length file. In this case the Content-Length field is omitted from
the request, and the server responds with a "411 Length Required"
error.

I have tried setting req.ContentLength = 0, and manually adding
req.Header.Set("Content-Length", "0"), but in the latter case the
header is stripped out of the request at transfer time.

I know that PUT requests are not terribly common, so this case was
probably overlooked when testing the library; it seems like a bug to
me. Any suggestions for a workaround?

Brad Fitzpatrick

unread,
Jun 8, 2011, 3:48:42 PM6/8/11
to Russ Ross, golang-nuts
On Wed, Jun 8, 2011 at 12:30 PM, Russ Ross <russ...@gmail.com> wrote:
I am using an http.Request object to upload a file using the HTTP PUT
method.  Everything is fine except when attempting to upload a zero-
length file.  In this case the Content-Length field is omitted from
the request, and the server responds with a "411 Length Required"
error.

To be clear, this part of your bug report is your webserver's "bug":  it's speaking a 12 year old version of HTTP and not accepting a chunked encoding request body.
 
I have tried setting req.ContentLength = 0, and manually adding
req.Header.Set("Content-Length", "0"), but in the latter case the
header is stripped out of the request at transfer time.

... but this part should work, and I thought we even had a test for that, since I once hit this too.

I know that PUT requests are not terribly common,

I don't think this is related to PUT in any way.  Other methods like POST should be identical.
 
so this case was
probably overlooked when testing the library; it seems like a bug to
me.  Any suggestions for a workaround?

Let me look into it now.

Russ Ross

unread,
Jun 8, 2011, 4:13:43 PM6/8/11
to golang-nuts
On Jun 8, 1:48 pm, Brad Fitzpatrick <bradf...@golang.org> wrote:
> On Wed, Jun 8, 2011 at 12:30 PM, Russ Ross <russr...@gmail.com> wrote:
> > I am using an http.Request object to upload a file using the HTTP PUT
> > method.  Everything is fine except when attempting to upload a zero-
> > length file.  In this case the Content-Length field is omitted from
> > the request, and the server responds with a "411 Length Required"
> > error.
>
> To be clear, this part of your bug report is your webserver's "bug":  it's
> speaking a 12 year old version of HTTP and not accepting a chunked encoding
> request body.

The server is Amazon S3, which does state that "Transfer-Encoding:
chunked is not supported". Please forgive my ignorance of HTTP, but I
don't see anything in the headers that indicate a Transfer-Encoding; a
quick glance over http/transfer.go suggested that the Content-Length
field was being removed to accommodate a chunked encoding, but I
didn't knowingly initiate one.

Here is a sample request, produced using req.Write(os.Stdout), edited
to remove <private> data:

PUT /foo.txt HTTP/1.1
Host: <bucketname>.s3.amazonaws.com
User-Agent: Go http package
Authorization: AWS <request signature>
Content-Type: text/plain
Date: Wed, 08 Jun 2011 14:00:40 MDT
X-Amz-Acl: public-read
X-Amz-Meta-Gid: 1000
X-Amz-Meta-Mode: 0100644
X-Amz-Meta-Mtime: 1307563195.459921506 (Wed Jun 8 13:59:55 MDT 2011)
X-Amz-Meta-Uid: 1000

When I change the file to be of length 1, the only change is the
Content-Length field and the body itself.

I have tried supplying nil as the body of the request (when calling
http.NewRequest), and providing an io.Reader with 0 bytes. The result
is the same in either case.


> > so this case was
> > probably overlooked when testing the library; it seems like a bug to
> > me.  Any suggestions for a workaround?
>
> Let me look into it now.

Thank you! If I can provide anything else that would be helpful,
please let me know.

Brad Fitzpatrick

unread,
Jun 8, 2011, 6:14:54 PM6/8/11
to Russ Ross, golang-nuts
Reply all
Reply to author
Forward
0 new messages