/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 breezy/transport/http/response.py

  • Committer: Jelmer Vernooij
  • Date: 2018-08-01 19:37:07 UTC
  • mto: This revision was merged to the branch mainline in revision 7058.
  • Revision ID: jelmer@jelmer.uk-20180801193707-dyecqwo0y2z4cpk9
Fix a few more tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
    )
40
40
from ...sixish import (
41
41
    BytesIO,
 
42
    PY3,
42
43
    )
43
44
 
44
45
 
65
66
        Dummy implementation for consistency with the 'file' API.
66
67
        """
67
68
 
68
 
    def read(self, size=-1):
 
69
    def __enter__(self):
 
70
        return self
 
71
 
 
72
    def __exit__(self, exc_type, exc_val, exc_tb):
 
73
        return False # propogate exceptions.
 
74
 
 
75
    def read(self, size=None):
69
76
        """Read size bytes from the current position in the file.
70
77
 
71
78
        :param size:  The number of bytes to read.  Leave unspecified or pass
72
79
            -1 to read to EOF.
73
80
        """
74
 
        data =  self._file.read(size)
 
81
        if size is None and not PY3:
 
82
            size = -1
 
83
        data = self._file.read(size)
75
84
        self._pos += len(data)
76
85
        return data
77
86
 
164
173
        The file should be at the beginning of the body, the first range
165
174
        definition is read and taken into account.
166
175
        """
 
176
        if not isinstance(boundary, bytes):
 
177
            raise TypeError(boundary)
167
178
        self._boundary = boundary
168
179
        # Decode the headers and setup the first range
169
180
        self.read_boundary()
171
182
 
172
183
    def read_boundary(self):
173
184
        """Read the boundary headers defining a new range"""
174
 
        boundary_line = '\r\n'
175
 
        while boundary_line == '\r\n':
 
185
        boundary_line = b'\r\n'
 
186
        while boundary_line == b'\r\n':
176
187
            # RFC2616 19.2 Additional CRLFs may precede the first boundary
177
188
            # string entity.
178
189
            # To be on the safe side we allow it before any boundary line
179
190
            boundary_line = self._file.readline()
180
191
 
181
 
        if boundary_line == '':
 
192
        if boundary_line == b'':
182
193
            # A timeout in the proxy server caused the response to end early.
183
194
            # See launchpad bug 198646.
184
195
            raise errors.HttpBoundaryMissing(
185
196
                self._path,
186
197
                self._boundary)
187
198
 
188
 
        if boundary_line != '--' + self._boundary + '\r\n':
 
199
        if boundary_line != b'--' + self._boundary + b'\r\n':
189
200
            # email_utils.unquote() incorrectly unquotes strings enclosed in <>
190
201
            # IIS 6 and 7 incorrectly wrap boundary strings in <>
191
202
            # together they make a beautiful bug, which we will be gracious
192
203
            # about here
193
204
            if (self._unquote_boundary(boundary_line) !=
194
 
                '--' + self._boundary + '\r\n'):
 
205
                b'--' + self._boundary + b'\r\n'):
195
206
                raise errors.InvalidHttpResponse(
196
207
                    self._path,
197
208
                    "Expected a boundary (%s) line, got '%s'"
206
217
        Parse the headers including the empty line following them so that we
207
218
        are ready to read the data itself.
208
219
        """
209
 
        self._headers = http_client.HTTPMessage(self._file, seekable=0)
 
220
        if PY3:
 
221
            self._headers = http_client.parse_headers(self._file)
 
222
        else:
 
223
            self._headers = http_client.HTTPMessage(self._file, seekable=0)
210
224
        # Extract the range definition
211
 
        content_range = self._headers.getheader('content-range', None)
 
225
        content_range = self._headers.get('content-range', None)
212
226
        if content_range is None:
213
227
            raise errors.InvalidHttpResponse(
214
228
                self._path,
281
295
        if (self._size > 0
282
296
            and self._pos == self._start + self._size):
283
297
            if size == 0:
284
 
                return ''
 
298
                return b''
285
299
            else:
286
300
                self._seek_to_next_range()
287
301
        elif self._pos < self._start:
372
386
        rfile = ResponseFile(url, data)
373
387
    elif code == 206:
374
388
        rfile = RangeFile(url, data)
375
 
        content_type = msg.getheader('content-type', None)
 
389
        content_type = msg.get('content-type', None)
376
390
        if content_type is None:
377
391
            # When there is no content-type header we treat the response as
378
392
            # being of type 'application/octet-stream' as per RFC2616 section
381
395
            content_type = 'application/octet-stream'
382
396
            is_multipart = False
383
397
        else:
384
 
            is_multipart = (msg.getmaintype() == 'multipart'
385
 
                            and msg.getsubtype() == 'byteranges')
 
398
            if PY3:
 
399
                is_multipart = (msg.get_content_maintype() == 'multipart'
 
400
                                and msg.get_content_subtype() == 'byteranges')
 
401
            else:
 
402
                is_multipart = (msg.getmaintype() == 'multipart'
 
403
                                and msg.getsubtype() == 'byteranges')
386
404
 
387
405
        if is_multipart:
388
406
            # Full fledged multipart response
389
 
            rfile.set_boundary(msg.getparam('boundary'))
 
407
            if PY3:
 
408
                boundary = msg.get_param('boundary')
 
409
            else:
 
410
                boundary = msg.getparam('boundary')
 
411
            rfile.set_boundary(boundary.encode('ascii'))
390
412
        else:
391
413
            # A response to a range request, but not multipart
392
 
            content_range = msg.getheader('content-range', None)
 
414
            content_range = msg.get('content-range', None)
393
415
            if content_range is None:
394
416
                raise errors.InvalidHttpResponse(url,
395
417
                    'Missing the Content-Range header in a 206 range response')