/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/transport/http/__init__.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-19 14:17:49 UTC
  • mto: This revision was merged to the branch mainline in revision 1869.
  • Revision ID: john@arbash-meinel.com-20060719141749-5c8a2a433f966f29
Update _extract_headers, make it less generic, and non recursive.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
import BaseHTTPServer, SimpleHTTPServer, socket, time
35
35
import threading
36
36
 
 
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
74
75
    return url
75
76
 
76
77
 
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
79
80
 
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)
82
83
 
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
89
 
            former.
90
 
    :return: mimetools.Message object
 
84
    In the case that there are multiple headers inside the file,
 
85
    the last one is returned.
 
86
 
 
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.
91
92
    """
92
 
    header_file.seek(0, 0)
93
 
    if skip_first:
94
 
        header_file.readline()
95
 
    m = mimetools.Message(header_file)
96
 
    if body_is_header:
97
 
        m.rewindbody()
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,
103
 
                                    body_is_header=True)
 
93
    first_header = True
 
94
    remaining = header_text
 
95
 
 
96
    if not remaining:
 
97
        raise errors.InvalidHttpResponse(url, 'Empty headers')
 
98
 
 
99
    while remaining:
 
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' 
 
106
                    % (first_line,))
 
107
                assert False, 'Opening header line was not HTTP'
 
108
            else:
 
109
                break # We are done parsing
 
110
        first_header = False
 
111
        m = mimetools.Message(header_file)
 
112
 
 
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()
104
119
    return m
105
120
 
106
121