~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Patch Queue Manager
  • Date: 2012-07-24 13:25:42 UTC
  • mfrom: (6538.4.6 mailname-first-line)
  • Revision ID: pqm@pqm.ubuntu.com-20120724132542-5e7aakd1c3t9oggf
(jelmer) Only read the first line from /etc/mailname. (Haw Loeung)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
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
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
28
30
 
29
31
from bzrlib import (
30
32
    errors,
31
 
    trace,
32
33
    osutils,
33
34
    )
34
35
 
35
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
    def __init__(self, path, infile):
 
43
        """Constructor.
 
44
 
 
45
        :param path: File url, for error reports.
 
46
 
 
47
        :param infile: File-like socket set at body start.
 
48
        """
 
49
        self._path = path
 
50
        self._file = infile
 
51
        self._pos = 0
 
52
 
 
53
    def close(self):
 
54
        """Close this file.
 
55
 
 
56
        Dummy implementation for consistency with the 'file' API.
 
57
        """
 
58
 
 
59
    def read(self, size=-1):
 
60
        """Read size bytes from the current position in the file.
 
61
 
 
62
        :param size:  The number of bytes to read.  Leave unspecified or pass
 
63
            -1 to read to EOF.
 
64
        """
 
65
        data =  self._file.read(size)
 
66
        self._pos += len(data)
 
67
        return data
 
68
 
 
69
    def readline(self):
 
70
        data = self._file.readline()
 
71
        self._pos += len(data)
 
72
        return data
 
73
 
 
74
    def tell(self):
 
75
        return self._pos
 
76
 
 
77
    def seek(self, offset, whence=os.SEEK_SET):
 
78
        if whence == os.SEEK_SET:
 
79
            if offset < self._pos:
 
80
                raise AssertionError(
 
81
                    "Can't seek backwards, pos: %s, offset: %s"
 
82
                    % (self._pos, offset))
 
83
            to_discard = offset - self._pos
 
84
        elif whence == os.SEEK_CUR:
 
85
            to_discard = offset
 
86
        else:
 
87
            raise AssertionError("Can't seek backwards")
 
88
        if to_discard:
 
89
            # Just discard the unwanted bytes
 
90
            self.read(to_discard)
 
91
 
36
92
# A RangeFile expects the following grammar (simplified to outline the
37
93
# assumptions we rely upon).
38
94
 
39
 
# file: whole_file
40
 
#     | single_range
 
95
# file: single_range
41
96
#     | multiple_range
42
97
 
43
 
# whole_file: [content_length_header] data
44
 
 
45
98
# single_range: content_range_header data
46
99
 
47
100
# multiple_range: boundary_header boundary (content_range_header data boundary)+
48
101
 
49
 
class RangeFile(object):
 
102
class RangeFile(ResponseFile):
50
103
    """File-like object that allow access to partial available data.
51
104
 
52
105
    All accesses should happen sequentially since the acquisition occurs during
71
124
        """Constructor.
72
125
 
73
126
        :param path: File url, for error reports.
 
127
 
74
128
        :param infile: File-like socket set at body start.
75
129
        """
76
 
        self._path = path
77
 
        self._file = infile
 
130
        super(RangeFile, self).__init__(path, infile)
78
131
        self._boundary = None
79
132
        # When using multi parts response, this will be set with the headers
80
133
        # associated with the range currently read.
109
162
            # To be on the safe side we allow it before any boundary line
110
163
            boundary_line = self._file.readline()
111
164
 
 
165
        if boundary_line == '':
 
166
            # A timeout in the proxy server caused the response to end early.
 
167
            # See launchpad bug 198646.
 
168
            raise errors.HttpBoundaryMissing(
 
169
                self._path,
 
170
                self._boundary)
 
171
 
112
172
        if boundary_line != '--' + self._boundary + '\r\n':
113
173
            # rfc822.unquote() incorrectly unquotes strings enclosed in <>
114
174
            # IIS 6 and 7 incorrectly wrap boundary strings in <>
291
351
    :return: A file-like object that can seek()+read() the
292
352
             ranges indicated by the headers.
293
353
    """
294
 
    rfile = RangeFile(url, data)
295
354
    if code == 200:
296
355
        # A whole file
297
 
        size = msg.getheader('content-length', None)
298
 
        if size is None:
299
 
            size = -1
300
 
        else:
301
 
            size = int(size)
302
 
        rfile.set_range(0, size)
 
356
        rfile = ResponseFile(url, data)
303
357
    elif code == 206:
 
358
        rfile = RangeFile(url, data)
304
359
        content_type = msg.getheader('content-type', None)
305
360
        if content_type is None:
306
361
            # When there is no content-type header we treat the response as