Opened 12 years ago

Closed 10 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:


I would expect the following snippet of code to work:

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

def x(result):
    return result

uri = ''
head = client.HTTPClientFactory(uri, 'HEAD')
scheme, host, port, path = client._parse(uri)
reactor.connectTCP(host, port, head)

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/

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 12 years ago.
Suggested fix

Download all attachments as: .zip

Change History (3)

Changed 12 years ago by mbergsma

Suggested fix

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

Resolution: duplicate
Status: newclosed

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

comment:2 Changed 8 years ago by <automation>

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