1
# Copyright (C) 2006 Michael Ellerman
2
# modified by John Arbash Meinel (Canonical Ltd)
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Handlers for HTTP Responses.
20
The purpose of these classes is to provide a uniform interface for clients
21
to standard HTTP responses, single range responses and multipart range
26
from bisect import bisect
27
from cStringIO import StringIO
30
from bzrlib import errors
31
from bzrlib.trace import mutter
34
class ResponseRange(object):
35
"""A range in a RangeFile-object."""
37
__slots__ = ['_ent_start', '_ent_end', '_data_start']
39
def __init__(self, ent_start, ent_end, data_start):
40
self._ent_start = ent_start
41
self._ent_end = ent_end
42
self._data_start = data_start
44
def __cmp__(self, other):
45
"""Compare this to other.
47
We need this both for sorting, and so that we can
48
bisect the list of ranges.
50
if isinstance(other, int):
51
# Later on we bisect for a starting point
52
# so we allow comparing against a single integer
53
return cmp(self._ent_start, other)
55
return cmp((self._ent_start, self._ent_end, self._data_start),
56
(other._ent_start, other._ent_end, other._data_start))
59
return "%s(%s-%s,%s)" % (self.__class__.__name__,
60
self._ent_start, self._ent_end,
66
class RangeFile(object):
67
"""File-like object that allow access to partial available data.
69
Specified by a set of ranges.
72
def __init__(self, path, input_file):
77
self._data = input_file.read()
79
def _add_range(self, ent_start, ent_end, data_start):
80
"""Add an entity range.
82
:param ent_start: Start offset of entity
83
:param ent_end: End offset of entity (inclusive)
84
:param data_start: Start offset of data in data stream.
86
self._ranges.append(ResponseRange(ent_start, ent_end, data_start))
87
self._len = max(self._len, ent_end)
89
def _finish_ranges(self):
93
"""Read size bytes from the current position in the file.
95
Reading across ranges is not supported.
97
# find the last range which has a start <= pos
98
i = bisect(self._ranges, self._pos) - 1
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)
103
raise errors.InvalidRange(self._path, self._pos)
107
# mutter('found range %s %s for pos %s', i, self._ranges[i], self._pos)
109
if (self._pos + size - 1) > r._ent_end:
110
raise errors.InvalidRange(self._path, self._pos)
112
start = r._data_start + (self._pos - r._ent_start)
114
# mutter("range read %d bytes at %d == %d-%d", size, self._pos,
116
self._pos += (end-start)
117
return self._data[start:end]
119
def seek(self, offset, whence=0):
125
self._pos = self._len + offset
127
raise ValueError("Invalid value %s for whence." % whence)
136
class HttpRangeResponse(RangeFile):
137
"""A single-range HTTP response."""
139
# TODO: jam 20060706 Consider compiling these regexes on demand
140
_CONTENT_RANGE_RE = re.compile(
141
r'\s*([^\s]+)\s+([0-9]+)-([0-9]+)/([0-9]+)\s*$')
143
def __init__(self, path, content_range, input_file):
144
# mutter("parsing 206 non-multipart response for %s", path)
145
RangeFile.__init__(self, path, input_file)
146
start, end = self._parse_range(content_range, path)
147
self._add_range(start, end, 0)
148
self._finish_ranges()
151
def _parse_range(range, path='<unknown>'):
152
"""Parse an http Content-range header and return start + end
154
:param range: The value for Content-range
155
:param path: Provide to give better error messages.
156
:return: (start, end) A tuple of integers
158
match = HttpRangeResponse._CONTENT_RANGE_RE.match(range)
160
raise errors.InvalidHttpRange(path, range,
161
"Invalid Content-range")
163
rtype, start, end, total = match.groups()
166
raise errors.InvalidHttpRange(path, range,
167
"Unsupported range type '%s'" % (rtype,))
172
except ValueError, e:
173
raise errors.InvalidHttpRange(path, range, str(e))
178
class HttpMultipartRangeResponse(RangeFile):
179
"""A multi-range HTTP response."""
181
_CONTENT_TYPE_RE = re.compile(
182
r'^\s*multipart/byteranges\s*;\s*boundary\s*=\s*("?)([^"]*?)\1\s*$')
184
# Start with --<boundary>\r\n
185
# and ignore all headers ending in \r\n
186
# except for content-range:
187
# and find the two trailing \r\n separators
188
# indicating the start of the text
189
# TODO: jam 20060706 This requires exact conformance
190
# to the spec, we probably could relax the requirement
191
# of \r\n, and use something more like (\r?\n)
193
"^--%s(?:\r\n(?:(?:content-range:([^\r]+))|[^\r]+))+\r\n\r\n")
195
def __init__(self, path, content_type, input_file):
196
# mutter("parsing 206 multipart response for %s", path)
197
# TODO: jam 20060706 Is it valid to initialize a
198
# grandparent without initializing parent?
199
RangeFile.__init__(self, path, input_file)
201
self.boundary_regex = self._parse_boundary(content_type, path)
202
# mutter('response:\n%r', self._data)
204
for match in self.boundary_regex.finditer(self._data):
205
ent_start, ent_end = HttpRangeResponse._parse_range(match.group(1),
207
self._add_range(ent_start, ent_end, match.end())
209
self._finish_ranges()
212
def _parse_boundary(ctype, path='<unknown>'):
213
"""Parse the Content-type field.
215
This expects a multipart Content-type, and returns a
216
regex which is capable of finding the boundaries
217
in the multipart data.
219
match = HttpMultipartRangeResponse._CONTENT_TYPE_RE.match(ctype)
221
raise errors.InvalidHttpContentType(path, ctype,
222
"Expected multipart/byteranges with boundary")
224
boundary = match.group(2)
225
# mutter('multipart boundary is %s', boundary)
226
pattern = HttpMultipartRangeResponse._BOUNDARY_PATT
227
return re.compile(pattern % re.escape(boundary),
228
re.IGNORECASE | re.MULTILINE)
231
def _is_multipart(content_type):
232
return content_type.startswith('multipart/byteranges;')
235
def handle_response(url, code, headers, data):
236
"""Interpret the code & headers and return a HTTP response.
238
This is a factory method which returns an appropriate HTTP response
239
based on the code & headers it's given.
241
:param url: The url being processed. Mostly for error reporting
242
:param code: The integer HTTP response code
243
:param headers: A dict-like object that contains the HTTP response headers
244
:param data: A file-like object that can be read() to get the
246
:return: A file-like object that can seek()+read() the
247
ranges indicated by the headers.
252
content_type = headers['Content-Type']
254
raise errors.InvalidHttpContentType(url, '',
255
msg='Missing Content-Type')
257
if _is_multipart(content_type):
258
# Full fledged multipart response
259
return HttpMultipartRangeResponse(url, content_type, data)
261
# A response to a range request, but not multipart
263
content_range = headers['Content-Range']
265
raise errors.InvalidHttpResponse(url,
266
'Missing the Content-Range header in a 206 range response')
267
return HttpRangeResponse(url, content_range, data)
269
# A regular non-range response, unfortunately the result from
270
# urllib doesn't support seek, so we wrap it in a StringIO
271
tell = getattr(data, 'tell', None)
273
return StringIO(data.read())
276
raise errors.NoSuchFile(url)
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
282
raise errors.InvalidHttpResponse(url, "Unknown response code %s"