~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: John Arbash Meinel
  • Date: 2006-10-06 06:27:06 UTC
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20061006062706-e84640cea080b6e9
shave off another 10ms by avoiding importing merge from working tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2006 Michael Ellerman
 
2
#           modified by John Arbash Meinel (Canonical Ltd)
2
3
#
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
59
60
                                 self._ent_start, self._ent_end,
60
61
                                 self._data_start)
61
62
 
 
63
    __repr__ = __str__
 
64
 
62
65
 
63
66
class RangeFile(object):
64
67
    """File-like object that allow access to partial available data.
95
98
        i = bisect(self._ranges, self._pos) - 1
96
99
 
97
100
        if i < 0 or self._pos > self._ranges[i]._ent_end:
 
101
            mutter('Bisect for pos: %s failed. Found offset: %d, ranges:%s',
 
102
                   self._pos, i, self._ranges)
98
103
            raise errors.InvalidRange(self._path, self._pos)
99
104
 
100
105
        r = self._ranges[i]
133
138
 
134
139
    # TODO: jam 20060706 Consider compiling these regexes on demand
135
140
    _CONTENT_RANGE_RE = re.compile(
136
 
        '\s*([^\s]+)\s+([0-9]+)-([0-9]+)/([0-9]+)\s*$')
 
141
        r'\s*([^\s]+)\s+([0-9]+)-([0-9]+)/([0-9]+)\s*$')
137
142
 
138
143
    def __init__(self, path, content_range, input_file):
139
144
        # mutter("parsing 206 non-multipart response for %s", path)
174
179
    """A multi-range HTTP response."""
175
180
    
176
181
    _CONTENT_TYPE_RE = re.compile(
177
 
        '^\s*multipart/byteranges\s*;\s*boundary\s*=\s*(.*?)\s*$')
 
182
        r'^\s*multipart/byteranges\s*;\s*boundary\s*=\s*("?)([^"]*?)\1\s*$')
178
183
    
179
184
    # Start with --<boundary>\r\n
180
185
    # and ignore all headers ending in \r\n
194
199
        RangeFile.__init__(self, path, input_file)
195
200
 
196
201
        self.boundary_regex = self._parse_boundary(content_type, path)
 
202
        # mutter('response:\n%r', self._data)
197
203
 
198
204
        for match in self.boundary_regex.finditer(self._data):
199
 
            ent_start, ent_end = HttpRangeResponse._parse_range(match.group(1), path)
 
205
            ent_start, ent_end = HttpRangeResponse._parse_range(match.group(1),
 
206
                                                                path)
200
207
            self._add_range(ent_start, ent_end, match.end())
201
208
 
202
209
        self._finish_ranges()
214
221
            raise errors.InvalidHttpContentType(path, ctype,
215
222
                    "Expected multipart/byteranges with boundary")
216
223
 
217
 
        boundary = match.group(1)
 
224
        boundary = match.group(2)
218
225
        # mutter('multipart boundary is %s', boundary)
219
226
        pattern = HttpMultipartRangeResponse._BOUNDARY_PATT
220
227
        return re.compile(pattern % re.escape(boundary),
245
252
            content_type = headers['Content-Type']
246
253
        except KeyError:
247
254
            raise errors.InvalidHttpContentType(url, '',
248
 
                msg = 'Missing Content-Type')
 
255
                msg='Missing Content-Type')
249
256
 
250
257
        if _is_multipart(content_type):
251
258
            # Full fledged multipart response
268
275
    elif code == 404:
269
276
        raise errors.NoSuchFile(url)
270
277
 
271
 
    raise errors.InvalidHttpResponse(url, "Unknown response code %s" % (code,))
 
278
    # TODO: jam 20060713 Properly handle redirects (302 Found, etc)
 
279
    #       The '_get' code says to follow redirects, we probably 
 
280
    #       should actually handle the return values
 
281
    else:
 
282
        raise errors.InvalidHttpResponse(url, "Unknown response code %s" 
 
283
                                              % (code,))
272
284