/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 bzrlib/transport/http/_urllib2_wrappers.py

  • Committer: Martin Pool
  • Date: 2007-12-14 07:35:49 UTC
  • mfrom: (3109 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3111.
  • Revision ID: mbp@sourcefrog.net-20071214073549-wpekccrsxjv77yze
Merge 1.0final back to trunk and bump to 1.1dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
    # Some responses have bodies in which we have no interest
79
79
    _body_ignored_responses = [301,302, 303, 307, 401, 403, 404]
80
80
 
81
 
    def __init__(self, *args, **kwargs):
82
 
        httplib.HTTPResponse.__init__(self, *args, **kwargs)
83
 
 
84
81
    def begin(self):
85
82
        """Begin to read the response from the server.
86
83
 
123
120
            # below we keep the socket with the server opened.
124
121
            self.will_close = False
125
122
 
 
123
    # in finish() below, we may have to discard several MB in the worst
 
124
    # case. To avoid buffering that much, we read and discard by chunks
 
125
    # instead. The underlying file is either a socket or a StringIO, so reading
 
126
    # 8k chunks should be fine.
 
127
    _discarded_buf_size = 8192
 
128
 
126
129
    def finish(self):
127
130
        """Finish reading the body.
128
131
 
131
134
        persistent connection. If we don't use a persistent connection, well,
132
135
        nothing will block the next request since a new connection will be
133
136
        issued anyway.
 
137
 
 
138
        :return: the number of bytes left on the socket (may be None)
134
139
        """
 
140
        pending = None
135
141
        if not self.isclosed():
136
142
            # Make sure nothing was left to be read on the socket
137
 
            data = self.read(self.length)
 
143
            pending = 0
 
144
            while self.length and self.length > self._discarded_buf_size:
 
145
                data = self.read(self._discarded_buf_size)
 
146
                pending += len(data)
 
147
            if self.length:
 
148
                data = self.read(self.length)
 
149
                pending += len(data)
 
150
            if pending:
 
151
                trace.mutter(
 
152
                    "bogus http server didn't give body length,"
 
153
                    "%s bytes left on the socket",
 
154
                    pending)
138
155
            self.close()
 
156
        return pending
139
157
 
140
158
 
141
159
# Not inheriting from 'object' because httplib.HTTPConnection doesn't.
145
163
    response_class = Response
146
164
    strict = 1 # We don't support HTTP/0.9
147
165
 
 
166
    # When we detect a server responding with the whole file to range requests,
 
167
    # we want to warn. But not below a given thresold.
 
168
    _range_warning_thresold = 1024 * 1024
 
169
 
148
170
    def __init__(self):
149
171
        self._response = None
 
172
        self._ranges_received_whole_file = None
150
173
 
151
174
    def _mutter_connect(self):
152
 
        netloc = self.host
153
 
        if self.port is not None:
154
 
            netloc += ':%d' % self.port
 
175
        netloc = '%s:%s' % (self.host, self.port)
155
176
        if self.proxied_host is not None:
156
177
            netloc += '(proxy for %s)' % self.proxied_host
157
178
        trace.mutter('* About to connect() to %s' % netloc)
162
183
        return self._response
163
184
 
164
185
    def cleanup_pipe(self):
165
 
        """Make the connection believes the response have been fully handled.
166
 
 
167
 
        That makes the httplib.HTTPConnection happy
168
 
        """
 
186
        """Make the connection believe the response has been fully processed."""
169
187
        if self._response is not None:
170
 
            self._response.finish()
 
188
            pending = self._response.finish()
 
189
            # Warn the user (once)
 
190
            if (self._ranges_received_whole_file is None
 
191
                and self._response.status == 200
 
192
                and pending and pending > self._range_warning_thresold
 
193
                ):
 
194
                self._ranges_received_whole_file = True
 
195
                trace.warning(
 
196
                    'Got a 200 response when asking for multiple ranges,'
 
197
                    ' does your server at %s:%s support range requests?',
 
198
                    self.host, self.port)
171
199
            self._response = None
172
200
        # Preserve our preciousss
173
201
        sock = self.sock