~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2007-04-04 01:22:11 UTC
  • mfrom: (2393.1.1 bzr.docs)
  • mto: This revision was merged to the branch mainline in revision 2397.
  • Revision ID: mbp@sourcefrog.net-20070404012211-sq269me6bai9m6xk
merge trunk and doc fix from elliot

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
# whether we expect a particular file will be modified after it's committed.
24
24
# It's probably safer to just always revalidate.  mbp 20060321
25
25
 
 
26
# TODO: Some refactoring could be done to avoid the strange idiom
 
27
# used to capture data and headers while setting up the request
 
28
# (and having to pass 'header' to _curl_perform to handle
 
29
# redirections) . This could be achieved by creating a
 
30
# specialized Curl object and returning code, headers and data
 
31
# from _curl_perform.  Not done because we may deprecate pycurl in the
 
32
# future -- vila 20070212
 
33
 
26
34
import os
27
35
from cStringIO import StringIO
28
36
import sys
111
119
        # don't want the body - ie just do a HEAD request
112
120
        # This means "NO BODY" not 'nobody'
113
121
        curl.setopt(pycurl.NOBODY, 1)
 
122
        # But we need headers to handle redirections
 
123
        header = StringIO()
 
124
        curl.setopt(pycurl.HEADERFUNCTION, header.write)
114
125
        # In some erroneous cases, pycurl will emit text on
115
126
        # stdout if we don't catch it (see InvalidStatus tests
116
127
        # for one such occurrence).
117
128
        blackhole = StringIO()
118
129
        curl.setopt(pycurl.WRITEFUNCTION, blackhole.write)
119
 
        self._curl_perform(curl)
 
130
        self._curl_perform(curl, header)
120
131
        code = curl.getinfo(pycurl.HTTP_CODE)
121
132
        if code == 404: # not found
122
133
            return False
123
 
        elif code in (200, 302): # "ok", "found"
 
134
        elif code == 200: # "ok"
124
135
            return True
125
136
        else:
126
137
            self._raise_curl_http_error(curl)
165
176
        """Make a request for the entire file"""
166
177
        curl = self._curl
167
178
        abspath, data, header = self._setup_get_request(curl, relpath)
168
 
        self._curl_perform(curl)
 
179
        self._curl_perform(curl, header)
169
180
 
170
181
        code = curl.getinfo(pycurl.HTTP_CODE)
171
182
        data.seek(0)
188
199
            # Forget ranges, the server can't handle them
189
200
            return self._get_full(relpath)
190
201
 
191
 
        self._curl_perform(curl, ['Range: bytes=%s'
192
 
                                  % self.range_header(ranges, tail_amount)])
 
202
        self._curl_perform(curl, header,
 
203
                           ['Range: bytes=%s'
 
204
                            % self.range_header(ranges, tail_amount)])
193
205
        data.seek(0)
194
206
 
195
207
        code = curl.getinfo(pycurl.HTTP_CODE)
210
222
        abspath, data, header = self._setup_request(curl, '.bzr/smart')
211
223
        # We override the Expect: header so that pycurl will send the POST
212
224
        # body immediately.
213
 
        self._curl_perform(curl,['Expect: '])
 
225
        self._curl_perform(curl, header, ['Expect: '])
214
226
        data.seek(0)
215
227
        code = curl.getinfo(pycurl.HTTP_CODE)
216
228
        headers = _extract_headers(header.getvalue(), abspath)
238
250
        # TODO: maybe include a summary of the pycurl version
239
251
        ua_str = 'bzr/%s (pycurl)' % (bzrlib.__version__,)
240
252
        curl.setopt(pycurl.USERAGENT, ua_str)
241
 
        curl.setopt(pycurl.FOLLOWLOCATION, 1) # follow redirect responses
242
253
        if self.cabundle:
243
254
            curl.setopt(pycurl.CAINFO, self.cabundle)
244
255
 
245
 
    def _curl_perform(self, curl, more_headers=[]):
 
256
    def _curl_perform(self, curl, header, more_headers=[]):
246
257
        """Perform curl operation and translate exceptions."""
247
258
        try:
248
259
            # There's no way in http/1.0 to say "must
275
286
            # jam 20060713 The code didn't use to re-raise the exception here,
276
287
            # but that seemed bogus
277
288
            raise
 
289
        code = curl.getinfo(pycurl.HTTP_CODE)
 
290
        if code in (301, 302, 303, 307):
 
291
            url = curl.getinfo(pycurl.EFFECTIVE_URL)
 
292
            headers = _extract_headers(header.getvalue(), url)
 
293
            redirected_to = headers['Location']
 
294
            raise errors.RedirectRequested(url,
 
295
                                           redirected_to,
 
296
                                           is_permament=(code == 301),
 
297
                                           qual_proto=self._qualified_proto)
278
298
 
279
299
 
280
300
def get_test_permutations():