Opened 11 years ago

Closed 9 years ago

#2012 defect closed duplicate (duplicate)

HTTP HEAD Request fails if Content-length given

Reported by: garrison Owned by:
Priority: normal Milestone:
Component: web Keywords:
Cc: Branch:
Author:

Description

I would expect the following snippet of code to work:

from twisted.internet import reactor
from twisted.web import client

def x(result):
    reactor.stop()
    return result

uri = 'http://www.gnu.org/licenses/gpl.txt'
head = client.HTTPClientFactory(uri, 'HEAD')
scheme, host, port, path = client._parse(uri)
reactor.connectTCP(host, port, head)
head.deferred.addCallback(x).addErrback(x)

reactor.run()

Unfortunately, it fails with a partial download request. Apparently, since the resource in question has a fixed Content-length, the HTTPClient/HTTPPageGetter expects there to be content after the headers. However, since it is a head request, there is obviously going to be no content after the headers, so HTTPPageGetter thinks things failed.

There are two possibilities. One is that I am misusing HTTPClientFactory by expecting it to do this HEAD request. The other possibility is that there is a bug in twisted. I could submit a few possible patches that would fix this problem, but I am not sure where the cleanest place to do it is. The main point of failure is on the following line (128) in web/client.py:

elif self.length != None and self.length != 0:

My temporary solution has been to also continue without error in the case that self.factory.method == 'HEAD'. Another possibility is simply to leave self.length unset if method == 'HEAD'.

Attachments (1)

twisted-web-head-content-length.diff (1.0 KB) - added by mbergsma 11 years ago.
Suggested fix

Download all attachments as: .zip

Change History (3)

Changed 11 years ago by mbergsma

Suggested fix

comment:1 Changed 9 years ago by Jean-Paul Calderone

Resolution: duplicate
Status: newclosed

#2750 was a duplicate of this and has been resolved.

comment:2 Changed 7 years ago by <automation>

Owner: jknight deleted
Note: See TracTickets for help on using tickets.