34
34
import BaseHTTPServer, SimpleHTTPServer, socket, time
37
from bzrlib import errors
37
38
from bzrlib.errors import (TransportNotPossible, NoSuchFile,
38
39
TransportError, ConnectionError, InvalidURL)
39
40
from bzrlib.branch import Branch
77
def _extract_headers(header_file, skip_first=True, body_is_header=True):
78
"""Extract the mapping for an rfc822 header
78
def _extract_headers(header_text, url):
79
"""Extract the mapping for an rfc2822 header
80
This is a helper function for the test suite, and for _pycurl.
81
This is a helper function for the test suite and for _pycurl.
81
82
(urllib already parses the headers for us)
83
:param header_file: A file-like object to read
84
:param skip_first: HTTP headers start with the HTTP response as
85
the first line. Skip this line while parsing
86
:param body_is_header: When pycurl gets a redirect request, it saves
87
both the redirect headers and the final headers in the header
88
file. Which means we really want the latter headers, not the
90
:return: mimetools.Message object
84
In the case that there are multiple headers inside the file,
85
the last one is returned.
87
:param header_text: A string of header information.
88
This expects that the first line of a header will always be HTTP ...
89
:param url: The url we are parsing, so we can raise nice errors
90
:return: mimetools.Message object, which basically acts like a case
91
insensitive dictionary.
92
header_file.seek(0, 0)
94
header_file.readline()
95
m = mimetools.Message(header_file)
98
remaining = header_file.read()
99
# Ignore some extra whitespace, but if we have acutal content
100
# lines, then the later content superceeds the eariler.
101
if remaining.strip() != '':
102
return _extract_headers(StringIO(remaining), skip_first=skip_first,
94
remaining = header_text
97
raise errors.InvalidHttpResponse(url, 'Empty headers')
100
header_file = StringIO(remaining)
101
first_line = header_file.readline()
102
if not first_line.startswith('HTTP'):
103
if first_header: # The first header *must* start with HTTP
104
raise errors.InvalidHttpResponse(url,
105
'Opening header line did not start with HTTP: %s'
107
assert False, 'Opening header line was not HTTP'
109
break # We are done parsing
111
m = mimetools.Message(header_file)
113
# mimetools.Message parses the first header up to a blank line
114
# So while there is remaining data, it probably means there is
115
# another header to be parsed.
116
# Get rid of any preceeding whitespace, which if it is all whitespace
117
# will get rid of everything.
118
remaining = header_file.read().lstrip()