Skip to content

Commit 00db919

Browse files
author
j.s@google.com
committed
Adds a get_headers function to be used in Python2.2 and 2.3 since httplib.HTTPResponse does not have getheaders in those versions of Python. Resolves issue 335.
1 parent 724e0d7 commit 00db919

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/atom/http_core.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ class ProxyError(Error):
5050
MIME_BOUNDARY = 'END_OF_PART'
5151

5252

53+
def get_headers(http_response):
54+
"""Retrieves all HTTP headers from an HTTP response from the server.
55+
56+
This method is provided for backwards compatibility for Python2.2 and 2.3.
57+
The httplib.HTTPResponse object in 2.2 and 2.3 does not have a getheaders
58+
method so this function will use getheaders if available, but if not it
59+
will retrieve a few using getheader.
60+
"""
61+
if hasattr(http_response, 'getheaders'):
62+
return http_response.getheaders()
63+
else:
64+
headers = []
65+
for header in (
66+
'location', 'content-type', 'content-length', 'age', 'allow',
67+
'cache-control', 'content-location', 'content-encoding', 'date',
68+
'etag', 'expires', 'last-modified', 'pragma', 'server',
69+
'set-cookie', 'transfer-encoding', 'vary', 'via', 'warning',
70+
'www-authenticate', 'gdata-version'):
71+
value = http_response.getheader(header, None)
72+
if value is not None:
73+
headers.append((header, value))
74+
return headers
75+
76+
5377
class HttpRequest(object):
5478
"""Contains all of the parameters for an HTTP 1.1 request.
5579
@@ -377,7 +401,7 @@ def _dump_response(http_response):
377401
"""
378402
output = 'HttpResponse\n status: %s\n reason: %s\n headers:' % (
379403
http_response.status, http_response.reason)
380-
headers = http_response.getheaders()
404+
headers = get_headers(http_response)
381405
if isinstance(headers, dict):
382406
for header, value in headers.iteritems():
383407
output += ' %s: %s\n' % (header, value)

src/atom/mock_http_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def request(self, http_request):
8686
scrubbed_response = _scrub_response(response)
8787
self.add_response(request, scrubbed_response.status,
8888
scrubbed_response.reason,
89-
dict(scrubbed_response.getheaders()),
89+
dict(atom.http_core.get_headers(scrubbed_response)),
9090
scrubbed_response.read())
9191
# Return the recording which we just added.
9292
return self._recordings[-1][1]

src/gdata/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def error_from_response(message, http_response, error_class,
116116
error.status = http_response.status
117117
error.reason = http_response.reason
118118
error.body = body
119-
error.headers = http_response.getheaders()
119+
error.headers = atom.http_core.get_headers(http_response)
120120
return error
121121

122122

tests/gdata_tests/live_client_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def test_create_update_delete(self):
6262
http_request.add_body_part(str(blog_post), 'application/atom+xml')
6363

6464
def entry_from_string_wrapper(response):
65+
self.assert_(response.getheader('content-type') is not None)
66+
self.assert_(response.getheader('gdata-version') is not None)
6567
return atom.EntryFromString(response.read())
6668

6769
entry = self.client.request('POST',

0 commit comments

Comments
 (0)