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,
 
 
64
class RangeFile(object):
 
 
65
    """File-like object that allow access to partial available data.
 
 
67
    Specified by a set of ranges.
 
 
70
    def __init__(self, path, input_file):
 
 
75
        self._data = input_file.read()
 
 
77
    def _add_range(self, ent_start, ent_end, data_start):
 
 
78
        """Add an entity range.
 
 
80
        :param ent_start: Start offset of entity
 
 
81
        :param ent_end: End offset of entity (inclusive)
 
 
82
        :param data_start: Start offset of data in data stream.
 
 
84
        self._ranges.append(ResponseRange(ent_start, ent_end, data_start))
 
 
85
        self._len = max(self._len, ent_end)
 
 
87
    def _finish_ranges(self):
 
 
91
        """Read size bytes from the current position in the file.
 
 
93
        Reading across ranges is not supported.
 
 
95
        # find the last range which has a start <= pos
 
 
96
        i = bisect(self._ranges, self._pos) - 1
 
 
98
        if i < 0 or self._pos > self._ranges[i]._ent_end:
 
 
99
            raise errors.InvalidRange(self._path, self._pos)
 
 
103
        # mutter('found range %s %s for pos %s', i, self._ranges[i], self._pos)
 
 
105
        if (self._pos + size - 1) > r._ent_end:
 
 
106
            raise errors.InvalidRange(self._path, self._pos)
 
 
108
        start = r._data_start + (self._pos - r._ent_start)
 
 
110
        # mutter("range read %d bytes at %d == %d-%d", size, self._pos,
 
 
112
        self._pos += (end-start)
 
 
113
        return self._data[start:end]
 
 
115
    def seek(self, offset, whence=0):
 
 
121
            self._pos = self._len + offset
 
 
123
            raise ValueError("Invalid value %s for whence." % whence)
 
 
132
class HttpRangeResponse(RangeFile):
 
 
133
    """A single-range HTTP response."""
 
 
135
    # TODO: jam 20060706 Consider compiling these regexes on demand
 
 
136
    _CONTENT_RANGE_RE = re.compile(
 
 
137
        '\s*([^\s]+)\s+([0-9]+)-([0-9]+)/([0-9]+)\s*$')
 
 
139
    def __init__(self, path, content_range, input_file):
 
 
140
        # mutter("parsing 206 non-multipart response for %s", path)
 
 
141
        RangeFile.__init__(self, path, input_file)
 
 
142
        start, end = self._parse_range(content_range, path)
 
 
143
        self._add_range(start, end, 0)
 
 
144
        self._finish_ranges()
 
 
147
    def _parse_range(range, path='<unknown>'):
 
 
148
        """Parse an http Content-range header and return start + end
 
 
150
        :param range: The value for Content-range
 
 
151
        :param path: Provide to give better error messages.
 
 
152
        :return: (start, end) A tuple of integers
 
 
154
        match = HttpRangeResponse._CONTENT_RANGE_RE.match(range)
 
 
156
            raise errors.InvalidHttpRange(path, range,
 
 
157
                                          "Invalid Content-range")
 
 
159
        rtype, start, end, total = match.groups()
 
 
162
            raise errors.InvalidHttpRange(path, range,
 
 
163
                    "Unsupported range type '%s'" % (rtype,))
 
 
168
        except ValueError, e:
 
 
169
            raise errors.InvalidHttpRange(path, range, str(e))
 
 
174
class HttpMultipartRangeResponse(RangeFile):
 
 
175
    """A multi-range HTTP response."""
 
 
177
    _CONTENT_TYPE_RE = re.compile(
 
 
178
        '^\s*multipart/byteranges\s*;\s*boundary\s*=\s*(.*?)\s*$')
 
 
180
    # Start with --<boundary>\r\n
 
 
181
    # and ignore all headers ending in \r\n
 
 
182
    # except for content-range:
 
 
183
    # and find the two trailing \r\n separators
 
 
184
    # indicating the start of the text
 
 
185
    # TODO: jam 20060706 This requires exact conformance
 
 
186
    #       to the spec, we probably could relax the requirement
 
 
187
    #       of \r\n, and use something more like (\r?\n)
 
 
189
        "^--%s(?:\r\n(?:(?:content-range:([^\r]+))|[^\r]+))+\r\n\r\n")
 
 
191
    def __init__(self, path, content_type, input_file):
 
 
192
        # mutter("parsing 206 multipart response for %s", path)
 
 
193
        # TODO: jam 20060706 Is it valid to initialize a
 
 
194
        #       grandparent without initializing parent?
 
 
195
        RangeFile.__init__(self, path, input_file)
 
 
197
        self.boundary_regex = self._parse_boundary(content_type, path)
 
 
199
        for match in self.boundary_regex.finditer(self._data):
 
 
200
            ent_start, ent_end = HttpRangeResponse._parse_range(match.group(1),
 
 
202
            self._add_range(ent_start, ent_end, match.end())
 
 
204
        self._finish_ranges()
 
 
207
    def _parse_boundary(ctype, path='<unknown>'):
 
 
208
        """Parse the Content-type field.
 
 
210
        This expects a multipart Content-type, and returns a
 
 
211
        regex which is capable of finding the boundaries
 
 
212
        in the multipart data.
 
 
214
        match = HttpMultipartRangeResponse._CONTENT_TYPE_RE.match(ctype)
 
 
216
            raise errors.InvalidHttpContentType(path, ctype,
 
 
217
                    "Expected multipart/byteranges with boundary")
 
 
219
        boundary = match.group(1)
 
 
220
        # mutter('multipart boundary is %s', boundary)
 
 
221
        pattern = HttpMultipartRangeResponse._BOUNDARY_PATT
 
 
222
        return re.compile(pattern % re.escape(boundary),
 
 
223
                          re.IGNORECASE | re.MULTILINE)
 
 
226
def _is_multipart(content_type):
 
 
227
    return content_type.startswith('multipart/byteranges;')
 
 
230
def handle_response(url, code, headers, data):
 
 
231
    """Interpret the code & headers and return a HTTP response.
 
 
233
    This is a factory method which returns an appropriate HTTP response
 
 
234
    based on the code & headers it's given.
 
 
236
    :param url: The url being processed. Mostly for error reporting
 
 
237
    :param code: The integer HTTP response code
 
 
238
    :param headers: A dict-like object that contains the HTTP response headers
 
 
239
    :param data: A file-like object that can be read() to get the
 
 
241
    :return: A file-like object that can seek()+read() the 
 
 
242
             ranges indicated by the headers.
 
 
247
            content_type = headers['Content-Type']
 
 
249
            raise errors.InvalidHttpContentType(url, '',
 
 
250
                msg='Missing Content-Type')
 
 
252
        if _is_multipart(content_type):
 
 
253
            # Full fledged multipart response
 
 
254
            return HttpMultipartRangeResponse(url, content_type, data)
 
 
256
            # A response to a range request, but not multipart
 
 
258
                content_range = headers['Content-Range']
 
 
260
                raise errors.InvalidHttpResponse(url,
 
 
261
                    'Missing the Content-Range header in a 206 range response')
 
 
262
            return HttpRangeResponse(url, content_range, data)
 
 
264
        # A regular non-range response, unfortunately the result from
 
 
265
        # urllib doesn't support seek, so we wrap it in a StringIO
 
 
266
        tell = getattr(data, 'tell', None)
 
 
268
            return StringIO(data.read())
 
 
271
        raise errors.NoSuchFile(url)
 
 
273
    # TODO: jam 20060713 Properly handle redirects (302 Found, etc)
 
 
274
    #       The '_get' code says to follow redirects, we probably 
 
 
275
    #       should actually handle the return values
 
 
277
        raise errors.InvalidHttpResponse(url, "Unknown response code %s"