~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2005-06-02 02:37:54 UTC
  • Revision ID: mbp@sourcefrog.net-20050602023754-fc3d4c02877ba29d
doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Michael Ellerman
2
 
#           modified by John Arbash Meinel (Canonical Ltd)
3
 
#
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.
8
 
#
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.
13
 
#
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
17
 
 
18
 
"""Handlers for HTTP Responses.
19
 
 
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
22
 
responses.
23
 
"""
24
 
 
25
 
 
26
 
from bisect import bisect
27
 
from cStringIO import StringIO
28
 
import re
29
 
 
30
 
from bzrlib import errors
31
 
from bzrlib.trace import mutter
32
 
 
33
 
 
34
 
class ResponseRange(object):
35
 
    """A range in a RangeFile-object."""
36
 
 
37
 
    __slots__ = ['_ent_start', '_ent_end', '_data_start']
38
 
 
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
43
 
 
44
 
    def __cmp__(self, other):
45
 
        """Compare this to other.
46
 
 
47
 
        We need this both for sorting, and so that we can
48
 
        bisect the list of ranges.
49
 
        """
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)
54
 
        else:
55
 
            return cmp((self._ent_start, self._ent_end, self._data_start),
56
 
                       (other._ent_start, other._ent_end, other._data_start))
57
 
 
58
 
    def __str__(self):
59
 
        return "%s(%s-%s,%s)" % (self.__class__.__name__,
60
 
                                 self._ent_start, self._ent_end,
61
 
                                 self._data_start)
62
 
 
63
 
 
64
 
class RangeFile(object):
65
 
    """File-like object that allow access to partial available data.
66
 
 
67
 
    Specified by a set of ranges.
68
 
    """
69
 
 
70
 
    def __init__(self, path, input_file):
71
 
        self._path = path
72
 
        self._pos = 0
73
 
        self._len = 0
74
 
        self._ranges = []
75
 
        self._data = input_file.read()
76
 
 
77
 
    def _add_range(self, ent_start, ent_end, data_start):
78
 
        """Add an entity range.
79
 
 
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.
83
 
        """
84
 
        self._ranges.append(ResponseRange(ent_start, ent_end, data_start))
85
 
        self._len = max(self._len, ent_end)
86
 
 
87
 
    def _finish_ranges(self):
88
 
        self._ranges.sort()
89
 
 
90
 
    def read(self, size):
91
 
        """Read size bytes from the current position in the file.
92
 
 
93
 
        Reading across ranges is not supported.
94
 
        """
95
 
        # find the last range which has a start <= pos
96
 
        i = bisect(self._ranges, self._pos) - 1
97
 
 
98
 
        if i < 0 or self._pos > self._ranges[i]._ent_end:
99
 
            raise errors.InvalidRange(self._path, self._pos)
100
 
 
101
 
        r = self._ranges[i]
102
 
 
103
 
        # mutter('found range %s %s for pos %s', i, self._ranges[i], self._pos)
104
 
 
105
 
        if (self._pos + size - 1) > r._ent_end:
106
 
            raise errors.InvalidRange(self._path, self._pos)
107
 
 
108
 
        start = r._data_start + (self._pos - r._ent_start)
109
 
        end   = start + size
110
 
        # mutter("range read %d bytes at %d == %d-%d", size, self._pos,
111
 
        #         start, end)
112
 
        self._pos += (end-start)
113
 
        return self._data[start:end]
114
 
 
115
 
    def seek(self, offset, whence=0):
116
 
        if whence == 0:
117
 
            self._pos = offset
118
 
        elif whence == 1:
119
 
            self._pos += offset
120
 
        elif whence == 2:
121
 
            self._pos = self._len + offset
122
 
        else:
123
 
            raise ValueError("Invalid value %s for whence." % whence)
124
 
 
125
 
        if self._pos < 0:
126
 
            self._pos = 0
127
 
 
128
 
    def tell(self):
129
 
        return self._pos
130
 
 
131
 
 
132
 
class HttpRangeResponse(RangeFile):
133
 
    """A single-range HTTP response."""
134
 
 
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*$')
138
 
 
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()
145
 
 
146
 
    @staticmethod
147
 
    def _parse_range(range, path='<unknown>'):
148
 
        """Parse an http Content-range header and return start + end
149
 
 
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
153
 
        """
154
 
        match = HttpRangeResponse._CONTENT_RANGE_RE.match(range)
155
 
        if not match:
156
 
            raise errors.InvalidHttpRange(path, range,
157
 
                                          "Invalid Content-range")
158
 
 
159
 
        rtype, start, end, total = match.groups()
160
 
 
161
 
        if rtype != 'bytes':
162
 
            raise errors.InvalidHttpRange(path, range,
163
 
                    "Unsupported range type '%s'" % (rtype,))
164
 
 
165
 
        try:
166
 
            start = int(start)
167
 
            end = int(end)
168
 
        except ValueError, e:
169
 
            raise errors.InvalidHttpRange(path, range, str(e))
170
 
 
171
 
        return start, end
172
 
 
173
 
 
174
 
class HttpMultipartRangeResponse(RangeFile):
175
 
    """A multi-range HTTP response."""
176
 
    
177
 
    _CONTENT_TYPE_RE = re.compile(
178
 
        '^\s*multipart/byteranges\s*;\s*boundary\s*=\s*(.*?)\s*$')
179
 
    
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)
188
 
    _BOUNDARY_PATT = (
189
 
        "^--%s(?:\r\n(?:(?:content-range:([^\r]+))|[^\r]+))+\r\n\r\n")
190
 
 
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)
196
 
 
197
 
        self.boundary_regex = self._parse_boundary(content_type, path)
198
 
 
199
 
        for match in self.boundary_regex.finditer(self._data):
200
 
            ent_start, ent_end = HttpRangeResponse._parse_range(match.group(1),
201
 
                                                                path)
202
 
            self._add_range(ent_start, ent_end, match.end())
203
 
 
204
 
        self._finish_ranges()
205
 
 
206
 
    @staticmethod
207
 
    def _parse_boundary(ctype, path='<unknown>'):
208
 
        """Parse the Content-type field.
209
 
        
210
 
        This expects a multipart Content-type, and returns a
211
 
        regex which is capable of finding the boundaries
212
 
        in the multipart data.
213
 
        """
214
 
        match = HttpMultipartRangeResponse._CONTENT_TYPE_RE.match(ctype)
215
 
        if not match:
216
 
            raise errors.InvalidHttpContentType(path, ctype,
217
 
                    "Expected multipart/byteranges with boundary")
218
 
 
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)
224
 
 
225
 
 
226
 
def _is_multipart(content_type):
227
 
    return content_type.startswith('multipart/byteranges;')
228
 
 
229
 
 
230
 
def handle_response(url, code, headers, data):
231
 
    """Interpret the code & headers and return a HTTP response.
232
 
 
233
 
    This is a factory method which returns an appropriate HTTP response
234
 
    based on the code & headers it's given.
235
 
 
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
240
 
                 requested data
241
 
    :return: A file-like object that can seek()+read() the 
242
 
             ranges indicated by the headers.
243
 
    """
244
 
 
245
 
    if code == 206:
246
 
        try:
247
 
            content_type = headers['Content-Type']
248
 
        except KeyError:
249
 
            raise errors.InvalidHttpContentType(url, '',
250
 
                msg='Missing Content-Type')
251
 
 
252
 
        if _is_multipart(content_type):
253
 
            # Full fledged multipart response
254
 
            return HttpMultipartRangeResponse(url, content_type, data)
255
 
        else:
256
 
            # A response to a range request, but not multipart
257
 
            try:
258
 
                content_range = headers['Content-Range']
259
 
            except KeyError:
260
 
                raise errors.InvalidHttpResponse(url,
261
 
                    'Missing the Content-Range header in a 206 range response')
262
 
            return HttpRangeResponse(url, content_range, data)
263
 
    elif code == 200:
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)
267
 
        if tell is None:
268
 
            return StringIO(data.read())
269
 
        return data
270
 
    elif code == 404:
271
 
        raise errors.NoSuchFile(url)
272
 
 
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
276
 
    else:
277
 
        raise errors.InvalidHttpResponse(url, "Unknown response code %s" 
278
 
                                              % (code,))
279