/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: 2019-05-29 03:28:14 UTC
  • mfrom: (7303 work)
  • mto: This revision was merged to the branch mainline in revision 7305.
  • Revision ID: jelmer@jelmer.uk-20190529032814-fzqbrgf9647u9ptq
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
from __future__ import absolute_import
25
25
 
 
26
import cgi
26
27
import os
27
28
try:
28
29
    import http.client as http_client
369
370
        return self._pos
370
371
 
371
372
 
372
 
def handle_response(url, code, msg, data):
 
373
def handle_response(url, code, getheader, data):
373
374
    """Interpret the code & headers and wrap the provided data in a RangeFile.
374
375
 
375
376
    This is a factory method which returns an appropriate RangeFile based on
377
378
 
378
379
    :param url: The url being processed. Mostly for error reporting
379
380
    :param code: The integer HTTP response code
380
 
    :param msg: An HTTPMessage containing the headers for the response
 
381
    :param getheader: Function for retrieving header
381
382
    :param data: A file-like object that can be read() to get the
382
383
                 requested data
383
384
    :return: A file-like object that can seek()+read() the
388
389
        rfile = ResponseFile(url, data)
389
390
    elif code == 206:
390
391
        rfile = RangeFile(url, data)
391
 
        content_type = msg.get('content-type', None)
392
 
        if content_type is None:
393
 
            # When there is no content-type header we treat the response as
394
 
            # being of type 'application/octet-stream' as per RFC2616 section
395
 
            # 7.2.1.
396
 
            # Therefore it is obviously not multipart
397
 
            content_type = 'application/octet-stream'
398
 
            is_multipart = False
399
 
        else:
400
 
            if PY3:
401
 
                is_multipart = (msg.get_content_maintype() == 'multipart'
402
 
                                and msg.get_content_subtype() == 'byteranges')
403
 
            else:
404
 
                is_multipart = (msg.getmaintype() == 'multipart'
405
 
                                and msg.getsubtype() == 'byteranges')
406
 
 
407
 
        if is_multipart:
408
 
            # Full fledged multipart response
409
 
            if PY3:
410
 
                boundary = msg.get_param('boundary')
411
 
            else:
412
 
                boundary = msg.getparam('boundary')
413
 
            rfile.set_boundary(boundary.encode('ascii'))
 
392
        # When there is no content-type header we treat the response as
 
393
        # being of type 'application/octet-stream' as per RFC2616 section
 
394
        # 7.2.1.
 
395
        # Therefore it is obviously not multipart
 
396
        content_type = getheader('content-type', 'application/octet-stream')
 
397
        mimetype, options = cgi.parse_header(content_type)
 
398
        if mimetype == 'multipart/byteranges':
 
399
            rfile.set_boundary(options['boundary'].encode('ascii'))
414
400
        else:
415
401
            # A response to a range request, but not multipart
416
 
            content_range = msg.get('content-range', None)
 
402
            content_range = getheader('content-range', None)
417
403
            if content_range is None:
418
 
                raise errors.InvalidHttpResponse(url,
419
 
                                                 'Missing the Content-Range header in a 206 range response')
 
404
                raise errors.InvalidHttpResponse(
 
405
                    url, 'Missing the Content-Range header in a 206 range response')
420
406
            rfile.set_range_from_header(content_range)
421
407
    else:
422
408
        raise errors.InvalidHttpResponse(url,