1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""http/https transport using pycurl"""
19
# TODO: test reporting of http errors
21
from StringIO import StringIO
24
from bzrlib.errors import (TransportNotPossible, NoSuchFile,
25
TransportError, ConnectionError,
27
from bzrlib.trace import mutter
28
from bzrlib.transport import Transport
29
from bzrlib.transport.http import HttpTransportBase, extract_auth, HttpServer
33
except ImportError, e:
34
mutter("failed to import pycurl: %s", e)
35
raise DependencyNotPresent('pycurl', e)
38
class PyCurlTransport(HttpTransportBase):
39
"""http client transport using pycurl
41
PyCurl is a Python binding to the C "curl" multiprotocol client.
43
This transport can be significantly faster than the builtin Python client.
44
Advantages include: DNS caching, connection keepalive, and ability to
45
set headers to allow caching.
48
def __init__(self, base):
49
super(PyCurlTransport, self).__init__(base)
50
mutter('using pycurl %s' % pycurl.version)
52
def should_cache(self):
53
"""Return True if the data pulled across should be cached locally.
57
def has(self, relpath):
59
abspath = self._real_abspath(relpath)
60
curl.setopt(pycurl.URL, abspath)
61
curl.setopt(pycurl.FOLLOWLOCATION, 1) # follow redirect responses
62
self._set_curl_options(curl)
63
# don't want the body - ie just do a HEAD request
64
curl.setopt(pycurl.NOBODY, 1)
65
self._curl_perform(curl)
66
code = curl.getinfo(pycurl.HTTP_CODE)
67
if code == 404: # not found
69
elif code in (200, 302): # "ok", "found"
72
raise TransportError('http error %d probing for %s' %
73
(code, curl.getinfo(pycurl.EFFECTIVE_URL)))
75
def _get(self, relpath, ranges):
77
abspath = self._real_abspath(relpath)
79
curl.setopt(pycurl.URL, abspath)
80
self._set_curl_options(curl)
81
curl.setopt(pycurl.WRITEFUNCTION, sio.write)
82
curl.setopt(pycurl.NOBODY, 0)
83
if ranges is not None:
84
assert len(ranges) == 1
85
# multiple ranges not supported yet because we can't decode the
87
curl.setopt(pycurl.RANGE, '%d-%d' % ranges[0])
88
self._curl_perform(curl)
89
code = curl.getinfo(pycurl.HTTP_CODE)
91
raise NoSuchFile(abspath)
95
elif code == 206 and (ranges is not None):
99
raise TransportError('http error %d acccessing %s' %
100
(code, curl.getinfo(pycurl.EFFECTIVE_URL)))
102
def _set_curl_options(self, curl):
103
"""Set options for all requests"""
104
# There's no way in http/1.0 to say "must revalidate"; we don't want
105
# to force it to always retrieve. so just turn off the default Pragma
107
headers = ['Cache-control: must-revalidate',
109
## curl.setopt(pycurl.VERBOSE, 1)
110
# TODO: maybe show a summary of the pycurl version
111
ua_str = 'bzr/%s (pycurl)' % (bzrlib.__version__)
112
curl.setopt(pycurl.USERAGENT, ua_str)
113
curl.setopt(pycurl.HTTPHEADER, headers)
114
curl.setopt(pycurl.FOLLOWLOCATION, 1) # follow redirect responses
116
def _curl_perform(self, curl):
117
"""Perform curl operation and translate exceptions."""
120
except pycurl.error, e:
121
# XXX: There seem to be no symbolic constants for these values.
123
# couldn't resolve host
124
raise NoSuchFile(curl.getinfo(pycurl.EFFECTIVE_URL), e)
127
class HttpServer_PyCurl(HttpServer):
128
"""Subclass of HttpServer that gives http+pycurl urls.
130
This is for use in testing: connections to this server will always go
131
through pycurl where possible.
134
# urls returned by this server should require the pycurl client impl
135
_url_protocol = 'http+pycurl'
138
def get_test_permutations():
139
"""Return the permutations to be used in testing."""
140
return [(PyCurlTransport, HttpServer_PyCurl),