~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

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