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
# TODO: Transport option to control caching of particular requests; broadly we
22
# would want to offer "caching allowed" or "must revalidate", depending on
23
# whether we expect a particular file will be modified after it's committed.
24
# It's probably safer to just always revalidate. mbp 20060321
27
from StringIO import StringIO
30
from bzrlib.errors import (TransportNotPossible, NoSuchFile,
31
TransportError, ConnectionError,
33
from bzrlib.trace import mutter
34
from bzrlib.transport import Transport
35
from bzrlib.transport.http import HttpTransportBase, extract_auth, HttpServer
39
except ImportError, e:
40
mutter("failed to import pycurl: %s", e)
41
raise DependencyNotPresent('pycurl', e)
44
class PyCurlTransport(HttpTransportBase):
45
"""http client transport using pycurl
47
PyCurl is a Python binding to the C "curl" multiprotocol client.
49
This transport can be significantly faster than the builtin Python client.
50
Advantages include: DNS caching, connection keepalive, and ability to
51
set headers to allow caching.
54
def __init__(self, base):
55
super(PyCurlTransport, self).__init__(base)
56
mutter('using pycurl %s' % pycurl.version)
58
def should_cache(self):
59
"""Return True if the data pulled across should be cached locally.
63
def has(self, relpath):
65
abspath = self._real_abspath(relpath)
66
curl.setopt(pycurl.URL, abspath)
67
curl.setopt(pycurl.FOLLOWLOCATION, 1) # follow redirect responses
68
self._set_curl_options(curl)
69
# don't want the body - ie just do a HEAD request
70
curl.setopt(pycurl.NOBODY, 1)
71
self._curl_perform(curl)
72
code = curl.getinfo(pycurl.HTTP_CODE)
73
if code == 404: # not found
75
elif code in (200, 302): # "ok", "found"
78
self._raise_curl_connection_error(curl)
80
self._raise_curl_http_error(curl)
82
def _get(self, relpath, ranges):
84
abspath = self._real_abspath(relpath)
86
curl.setopt(pycurl.URL, abspath)
87
self._set_curl_options(curl)
88
curl.setopt(pycurl.WRITEFUNCTION, sio.write)
89
curl.setopt(pycurl.NOBODY, 0)
90
if ranges is not None:
91
assert len(ranges) == 1
92
# multiple ranges not supported yet because we can't decode the
94
curl.setopt(pycurl.RANGE, '%d-%d' % ranges[0])
95
self._curl_perform(curl)
96
code = curl.getinfo(pycurl.HTTP_CODE)
98
raise NoSuchFile(abspath)
102
elif code == 206 and (ranges is not None):
106
self._raise_curl_connection_error(curl)
108
self._raise_curl_http_error(curl)
110
def _raise_curl_connection_error(self, curl):
111
curl_errno = curl.getinfo(pycurl.OS_ERRNO)
112
url = curl.getinfo(pycurl.EFFECTIVE_URL)
113
raise ConnectionError('curl connection error (%s) on %s'
114
% (os.strerror(curl_errno), url))
116
def _raise_curl_http_error(self, curl):
117
code = curl.getinfo(pycurl.HTTP_CODE)
118
url = curl.getinfo(pycurl.EFFECTIVE_URL)
119
raise TransportError('http error %d probing for %s' %
122
def _set_curl_options(self, curl):
123
"""Set options for all requests"""
124
# There's no way in http/1.0 to say "must revalidate"; we don't want
125
# to force it to always retrieve. so just turn off the default Pragma
127
headers = ['Cache-control: max-age=0',
129
## curl.setopt(pycurl.VERBOSE, 1)
130
# TODO: maybe include a summary of the pycurl version
131
ua_str = 'bzr/%s (pycurl)' % (bzrlib.__version__)
132
curl.setopt(pycurl.USERAGENT, ua_str)
133
curl.setopt(pycurl.HTTPHEADER, headers)
134
curl.setopt(pycurl.FOLLOWLOCATION, 1) # follow redirect responses
136
def _curl_perform(self, curl):
137
"""Perform curl operation and translate exceptions."""
140
except pycurl.error, e:
141
# XXX: There seem to be no symbolic constants for these values.
143
# couldn't resolve host
144
raise NoSuchFile(curl.getinfo(pycurl.EFFECTIVE_URL), e)
147
class HttpServer_PyCurl(HttpServer):
148
"""Subclass of HttpServer that gives http+pycurl urls.
150
This is for use in testing: connections to this server will always go
151
through pycurl where possible.
154
# urls returned by this server should require the pycurl client impl
155
_url_protocol = 'http+pycurl'
158
def get_test_permutations():
159
"""Return the permutations to be used in testing."""
160
return [(PyCurlTransport, HttpServer_PyCurl),