~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
responses.
22
22
"""
23
23
 
 
24
from __future__ import absolute_import
24
25
 
 
26
import os
25
27
import httplib
26
28
from cStringIO import StringIO
27
29
import rfc822
32
34
    )
33
35
 
34
36
 
 
37
class ResponseFile(object):
 
38
    """A wrapper around the http socket containing the result of a GET request.
 
39
 
 
40
    Only read() and seek() (forward) are supported.
 
41
 
 
42
    """
 
43
    def __init__(self, path, infile):
 
44
        """Constructor.
 
45
 
 
46
        :param path: File url, for error reports.
 
47
 
 
48
        :param infile: File-like socket set at body start.
 
49
        """
 
50
        self._path = path
 
51
        self._file = infile
 
52
        self._pos = 0
 
53
 
 
54
    def close(self):
 
55
        """Close this file.
 
56
 
 
57
        Dummy implementation for consistency with the 'file' API.
 
58
        """
 
59
 
 
60
    def read(self, size=-1):
 
61
        """Read size bytes from the current position in the file.
 
62
 
 
63
        :param size:  The number of bytes to read.  Leave unspecified or pass
 
64
            -1 to read to EOF.
 
65
        """
 
66
        data =  self._file.read(size)
 
67
        self._pos += len(data)
 
68
        return data
 
69
 
 
70
    def readline(self):
 
71
        data = self._file.readline()
 
72
        self._pos += len(data)
 
73
        return data
 
74
 
 
75
    def __iter__(self):
 
76
        while True:
 
77
            line = self.readline()
 
78
            if not line:
 
79
                return
 
80
            yield line
 
81
 
 
82
    def tell(self):
 
83
        return self._pos
 
84
 
 
85
    def seek(self, offset, whence=os.SEEK_SET):
 
86
        if whence == os.SEEK_SET:
 
87
            if offset < self._pos:
 
88
                raise AssertionError(
 
89
                    "Can't seek backwards, pos: %s, offset: %s"
 
90
                    % (self._pos, offset))
 
91
            to_discard = offset - self._pos
 
92
        elif whence == os.SEEK_CUR:
 
93
            to_discard = offset
 
94
        else:
 
95
            raise AssertionError("Can't seek backwards")
 
96
        if to_discard:
 
97
            # Just discard the unwanted bytes
 
98
            self.read(to_discard)
 
99
 
35
100
# A RangeFile expects the following grammar (simplified to outline the
36
101
# assumptions we rely upon).
37
102
 
38
 
# file: whole_file
39
 
#     | single_range
 
103
# file: single_range
40
104
#     | multiple_range
41
105
 
42
 
# whole_file: [content_length_header] data
43
 
 
44
106
# single_range: content_range_header data
45
107
 
46
108
# multiple_range: boundary_header boundary (content_range_header data boundary)+
47
109
 
48
 
class RangeFile(object):
 
110
class RangeFile(ResponseFile):
49
111
    """File-like object that allow access to partial available data.
50
112
 
51
113
    All accesses should happen sequentially since the acquisition occurs during
70
132
        """Constructor.
71
133
 
72
134
        :param path: File url, for error reports.
 
135
 
73
136
        :param infile: File-like socket set at body start.
74
137
        """
75
 
        self._path = path
76
 
        self._file = infile
 
138
        super(RangeFile, self).__init__(path, infile)
77
139
        self._boundary = None
78
140
        # When using multi parts response, this will be set with the headers
79
141
        # associated with the range currently read.
227
289
                    % (size, self._start, self._size))
228
290
 
229
291
        # read data from file
230
 
        buffer = StringIO()
 
292
        buf = StringIO()
231
293
        limited = size
232
294
        if self._size > 0:
233
295
            # Don't read past the range definition
234
296
            limited = self._start + self._size - self._pos
235
297
            if size >= 0:
236
298
                limited = min(limited, size)
237
 
        osutils.pumpfile(self._file, buffer, limited, self._max_read_size)
238
 
        data = buffer.getvalue()
 
299
        osutils.pumpfile(self._file, buf, limited, self._max_read_size)
 
300
        data = buf.getvalue()
239
301
 
240
302
        # Update _pos respecting the data effectively read
241
303
        self._pos += len(data)
297
359
    :return: A file-like object that can seek()+read() the
298
360
             ranges indicated by the headers.
299
361
    """
300
 
    rfile = RangeFile(url, data)
301
362
    if code == 200:
302
363
        # A whole file
303
 
        size = msg.getheader('content-length', None)
304
 
        if size is None:
305
 
            size = -1
306
 
        else:
307
 
            size = int(size)
308
 
        rfile.set_range(0, size)
 
364
        rfile = ResponseFile(url, data)
309
365
    elif code == 206:
 
366
        rfile = RangeFile(url, data)
310
367
        content_type = msg.getheader('content-type', None)
311
368
        if content_type is None:
312
369
            # When there is no content-type header we treat the response as