~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

Merge bzr.dev, update to use new hooks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
from cStringIO import StringIO
35
35
import httplib
36
36
 
 
37
import bzrlib
37
38
from bzrlib import (
38
39
    debug,
39
40
    errors,
 
41
    trace,
40
42
    )
41
 
import bzrlib
42
 
from bzrlib.trace import mutter
43
43
from bzrlib.transport.http import (
44
44
    ca_bundle,
45
45
    HttpTransportBase,
46
46
    response,
 
47
    unhtml_roughly,
47
48
    )
48
49
 
49
50
try:
50
51
    import pycurl
51
52
except ImportError, e:
52
 
    mutter("failed to import pycurl: %s", e)
 
53
    trace.mutter("failed to import pycurl: %s", e)
53
54
    raise errors.DependencyNotPresent('pycurl', e)
54
55
 
55
56
try:
64
65
    # reported by Alexander Belchenko, 2006-04-26
65
66
    pycurl.Curl()
66
67
except pycurl.error, e:
67
 
    mutter("failed to initialize pycurl: %s", e)
 
68
    trace.mutter("failed to initialize pycurl: %s", e)
68
69
    raise errors.DependencyNotPresent('pycurl', e)
69
70
 
70
71
 
267
268
        # We override the Expect: header so that pycurl will send the POST
268
269
        # body immediately.
269
270
        try:
270
 
            self._curl_perform(curl, header, ['Expect: '])
 
271
            self._curl_perform(curl, header,
 
272
                               ['Expect: ',
 
273
                                'Content-Type: application/octet-stream'])
271
274
        except pycurl.error, e:
272
275
            if e[0] == CURLE_SEND_ERROR:
273
276
                # When talking to an HTTP/1.0 server, getting a 400+ error code
279
282
                # error code and the headers are known to be available, we just
280
283
                # swallow the exception, leaving the upper levels handle the
281
284
                # 400+ error.
282
 
                mutter('got pycurl error in POST: %s, %s, %s, url: %s ',
283
 
                       e[0], e[1], e, abspath)
 
285
                trace.mutter('got pycurl error in POST: %s, %s, %s, url: %s ',
 
286
                             e[0], e[1], e, abspath)
284
287
            else:
285
288
                # Re-raise otherwise
286
289
                raise
290
293
        return code, response.handle_response(abspath, code, msg, data)
291
294
 
292
295
 
293
 
    def _raise_curl_http_error(self, curl, info=None):
 
296
    def _raise_curl_http_error(self, curl, info=None, body=None):
 
297
        """Common curl->bzrlib error translation.
 
298
 
 
299
        Some methods may choose to override this for particular cases.
 
300
 
 
301
        The URL and code are automatically included as appropriate.
 
302
 
 
303
        :param info: Extra information to include in the message.
 
304
 
 
305
        :param body: File-like object from which the body of the page can be
 
306
            read.
 
307
        """
294
308
        code = curl.getinfo(pycurl.HTTP_CODE)
295
309
        url = curl.getinfo(pycurl.EFFECTIVE_URL)
296
 
        # Some error codes can be handled the same way for all
297
 
        # requests
 
310
        if body is not None:
 
311
            response_body = body.read()
 
312
            plaintext_body = unhtml_roughly(response_body)
 
313
        else:
 
314
            response_body = None
 
315
            plaintext_body = ''
298
316
        if code == 403:
299
317
            raise errors.TransportError(
300
318
                'Server refuses to fulfill the request (403 Forbidden)'
301
 
                ' for %s' % url)
 
319
                ' for %s: %s' % (url, plaintext_body))
302
320
        else:
303
321
            if info is None:
304
322
                msg = ''
305
323
            else:
306
324
                msg = ': ' + info
307
325
            raise errors.InvalidHttpResponse(
308
 
                url, 'Unable to handle http code %d%s' % (code,msg))
 
326
                url, 'Unable to handle http code %d%s: %s'
 
327
                % (code, msg, plaintext_body))
309
328
 
310
329
    def _debug_cb(self, kind, text):
311
 
        if kind in (pycurl.INFOTYPE_HEADER_IN, pycurl.INFOTYPE_DATA_IN,
312
 
                    pycurl.INFOTYPE_SSL_DATA_IN):
 
330
        if kind in (pycurl.INFOTYPE_HEADER_IN, pycurl.INFOTYPE_DATA_IN):
313
331
            self._report_activity(len(text), 'read')
314
332
            if (kind == pycurl.INFOTYPE_HEADER_IN
315
333
                and 'http' in debug.debug_flags):
316
 
                mutter('< %s' % text)
317
 
        elif kind in (pycurl.INFOTYPE_HEADER_OUT, pycurl.INFOTYPE_DATA_OUT,
318
 
                      pycurl.INFOTYPE_SSL_DATA_OUT):
 
334
                trace.mutter('< %s' % (text.rstrip(),))
 
335
        elif kind in (pycurl.INFOTYPE_HEADER_OUT, pycurl.INFOTYPE_DATA_OUT):
319
336
            self._report_activity(len(text), 'write')
320
337
            if (kind == pycurl.INFOTYPE_HEADER_OUT
321
338
                and 'http' in debug.debug_flags):
322
 
                mutter('> %s' % text)
 
339
                lines = []
 
340
                for line in text.rstrip().splitlines():
 
341
                    # People are often told to paste -Dhttp output to help
 
342
                    # debug. Don't compromise credentials.
 
343
                    try:
 
344
                        header, details = line.split(':', 1)
 
345
                    except ValueError:
 
346
                        header = None
 
347
                    if header in ('Authorization', 'Proxy-Authorization'):
 
348
                        line = '%s: <masked>' % (header,)
 
349
                    lines.append(line)
 
350
                trace.mutter('> ' + '\n> '.join(lines))
323
351
        elif kind == pycurl.INFOTYPE_TEXT and 'http' in debug.debug_flags:
324
 
            mutter('* %s' % text)
 
352
            trace.mutter('* %s' % text.rstrip())
 
353
        elif (kind in (pycurl.INFOTYPE_TEXT, pycurl.INFOTYPE_SSL_DATA_IN,
 
354
                       pycurl.INFOTYPE_SSL_DATA_OUT)
 
355
              and 'http' in debug.debug_flags):
 
356
            trace.mutter('* %s' % text)
325
357
 
326
358
    def _set_curl_options(self, curl):
327
359
        """Set options for all requests"""
358
390
            curl.perform()
359
391
        except pycurl.error, e:
360
392
            url = curl.getinfo(pycurl.EFFECTIVE_URL)
361
 
            mutter('got pycurl error: %s, %s, %s, url: %s ',
362
 
                    e[0], e[1], e, url)
 
393
            trace.mutter('got pycurl error: %s, %s, %s, url: %s ',
 
394
                         e[0], e[1], e, url)
363
395
            if e[0] in (CURLE_COULDNT_RESOLVE_HOST,
364
396
                        CURLE_COULDNT_RESOLVE_PROXY,
365
397
                        CURLE_COULDNT_CONNECT,
393
425
 
394
426
def get_test_permutations():
395
427
    """Return the permutations to be used in testing."""
396
 
    from bzrlib import tests
 
428
    from bzrlib.tests import features
397
429
    from bzrlib.tests import http_server
398
430
    permutations = [(PyCurlTransport, http_server.HttpServer_PyCurl),]
399
 
    if tests.HTTPSServerFeature.available():
 
431
    if features.HTTPSServerFeature.available():
400
432
        from bzrlib.tests import (
401
433
            https_server,
402
434
            ssl_certs,