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.
375
376
This is a factory method which returns an appropriate RangeFile based on
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
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
396
# Therefore it is obviously not multipart
397
content_type = 'application/octet-stream'
401
is_multipart = (msg.get_content_maintype() == 'multipart'
402
and msg.get_content_subtype() == 'byteranges')
404
is_multipart = (msg.getmaintype() == 'multipart'
405
and msg.getsubtype() == 'byteranges')
408
# Full fledged multipart response
410
boundary = msg.get_param('boundary')
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
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'))
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)
422
408
raise errors.InvalidHttpResponse(url,