1
# Copyright (C) 2006, 2007 Canonical Ltd
1
# Copyright (C) 2006-2011 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
24
from __future__ import absolute_import
26
28
from cStringIO import StringIO
29
31
from bzrlib import (
37
class ResponseFile(object):
38
"""A wrapper around the http socket containing the result of a GET request.
40
Only read() and seek() (forward) are supported.
42
def __init__(self, path, infile):
45
:param path: File url, for error reports.
47
:param infile: File-like socket set at body start.
56
Dummy implementation for consistency with the 'file' API.
59
def read(self, size=-1):
60
"""Read size bytes from the current position in the file.
62
:param size: The number of bytes to read. Leave unspecified or pass
65
data = self._file.read(size)
66
self._pos += len(data)
69
def seek(self, offset, whence=os.SEEK_SET):
70
if whence == os.SEEK_SET:
71
if offset < self._pos:
72
raise AsserttionError(
73
"Can't seek backwards, pos: %s, offset: %s"
74
% (self._pos, offfset))
75
to_discard = offset - self._pos
76
elif whence == os.SEEK_CUR:
79
raise AssertionError("Can't seek backwards")
81
# Just discard the unwanted bytes
36
84
# A RangeFile expects the following grammar (simplified to outline the
37
85
# assumptions we rely upon).
43
# whole_file: [content_length_header] data
45
90
# single_range: content_range_header data
47
92
# multiple_range: boundary_header boundary (content_range_header data boundary)+
49
class RangeFile(object):
94
class RangeFile(ResponseFile):
50
95
"""File-like object that allow access to partial available data.
52
97
All accesses should happen sequentially since the acquisition occurs during
73
118
:param path: File url, for error reports.
74
120
:param infile: File-like socket set at body start.
122
super(RangeFile, self).__init__(path, infile)
78
123
self._boundary = None
79
124
# When using multi parts response, this will be set with the headers
80
125
# associated with the range currently read.
109
154
# To be on the safe side we allow it before any boundary line
110
155
boundary_line = self._file.readline()
157
if boundary_line == '':
158
# A timeout in the proxy server caused the response to end early.
159
# See launchpad bug 198646.
160
raise errors.HttpBoundaryMissing(
112
164
if boundary_line != '--' + self._boundary + '\r\n':
113
165
# rfc822.unquote() incorrectly unquotes strings enclosed in <>
114
166
# IIS 6 and 7 incorrectly wrap boundary strings in <>
291
343
:return: A file-like object that can seek()+read() the
292
344
ranges indicated by the headers.
294
rfile = RangeFile(url, data)
297
size = msg.getheader('content-length', None)
302
rfile.set_range(0, size)
348
rfile = ResponseFile(url, data)
303
349
elif code == 206:
350
rfile = RangeFile(url, data)
304
351
content_type = msg.getheader('content-type', None)
305
352
if content_type is None:
306
353
# When there is no content-type header we treat the response as