Skip to content

Commit

Permalink
Omit Content-Length on chunked transfer
Browse files Browse the repository at this point in the history
Content-Length and Transfer-Encoding conflict according to the HTTP
spec. This fixes bug 981332.

[ This is backported from 223fbee ]

Change-Id: I62f2bcabd9712361dd8837e39a577c4add052d0f
  • Loading branch information
novas0x2a committed Apr 24, 2012
1 parent 871ea1e commit bad506d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 5 additions & 1 deletion glance/common/client.py
Expand Up @@ -198,8 +198,12 @@ def _do_request(self, method, action, body=None, headers=None,
# Chunk it, baby...
c.putrequest(method, action)

# According to HTTP/1.1, Content-Length and Transfer-Encoding
# conflict.
for header, value in headers.items():
c.putheader(header, value)
if header.lower() != 'content-length':
c.putheader(header, value)

c.putheader('Transfer-Encoding', 'chunked')
c.endheaders()

Expand Down
9 changes: 6 additions & 3 deletions glance/tests/stubs.py
Expand Up @@ -95,7 +95,7 @@ def connect(self):
def close(self):
return True

def request(self, method, url, body=None, headers={}):
def request(self, method, url, body=None, headers=None):
self.req = webob.Request.blank("/" + url.lstrip("/"))
self.req.method = method
if headers:
Expand Down Expand Up @@ -138,15 +138,18 @@ def putheader(self, key, value):
self.req.headers[key] = value

def endheaders(self):
pass
hl = [i.lower() for i in self.req.headers.keys()]
assert not ('content-length' in hl and
'transfer-encoding' in hl), \
'Content-Length and Transfer-Encoding are mutually exclusive'

def send(self, data):
# send() is called during chunked-transfer encoding, and
# data is of the form %x\r\n%s\r\n. Strip off the %x and
# only write the actual data in tests.
self.req.body += data.split("\r\n")[1]

def request(self, method, url, body=None, headers={}):
def request(self, method, url, body=None, headers=None):
self.req = webob.Request.blank("/" + url.lstrip("/"))
self.req.method = method
if headers:
Expand Down

0 comments on commit bad506d

Please sign in to comment.