|
11 | 11 |
|
12 | 12 | import cookielib |
13 | 13 | import os |
| 14 | +import StringIO |
14 | 15 | import urllib2 |
| 16 | +import urlparse |
15 | 17 | import xmlrpclib |
16 | 18 |
|
| 19 | +import pycurl |
| 20 | + |
17 | 21 | from bugzilla import __version__, log |
18 | 22 | from bugzilla.bug import _Bug, _User |
19 | 23 |
|
@@ -95,43 +99,73 @@ def _build_cookiejar(cookiefile): |
95 | 99 | return retcj |
96 | 100 |
|
97 | 101 |
|
98 | | -# CookieTransport code mostly borrowed from pybugz |
99 | | -class _CookieTransport(xmlrpclib.Transport): |
100 | | - def __init__(self, uri, cookiejar, use_datetime=0): |
101 | | - self.verbose = 0 |
102 | | - |
103 | | - # python 2.4 compat |
| 102 | +class _CURLTransport(xmlrpclib.Transport): |
| 103 | + def __init__(self, url, cookiejar, |
| 104 | + sslverify=True, sslcafile=None, debug=0): |
104 | 105 | if hasattr(xmlrpclib.Transport, "__init__"): |
105 | | - xmlrpclib.Transport.__init__(self, use_datetime=use_datetime) |
| 106 | + xmlrpclib.Transport.__init__(self, use_datetime=False) |
| 107 | + |
| 108 | + self.verbose = debug |
| 109 | + |
| 110 | + # transport constructor needs full url too, as xmlrpc does not pass |
| 111 | + # scheme to request |
| 112 | + self.scheme = urlparse.urlparse(url)[0] |
| 113 | + if self.scheme not in ["http", "https"]: |
| 114 | + raise Exception("Invalid URL scheme: %s (%s)" % (self.scheme, url)) |
| 115 | + |
| 116 | + self.c = pycurl.Curl() |
| 117 | + self.c.setopt(pycurl.POST, 1) |
| 118 | + self.c.setopt(pycurl.CONNECTTIMEOUT, 30) |
| 119 | + self.c.setopt(pycurl.HTTPHEADER, [ |
| 120 | + "Content-Type: text/xml", |
| 121 | + ]) |
| 122 | + self.c.setopt(pycurl.VERBOSE, debug) |
| 123 | + |
| 124 | + self.set_cookiejar(cookiejar) |
| 125 | + |
| 126 | + # ssl settings |
| 127 | + if self.scheme == "https": |
| 128 | + # override curl built-in ca file setting |
| 129 | + if sslcafile is not None: |
| 130 | + self.c.setopt(pycurl.CAINFO, sslcafile) |
| 131 | + |
| 132 | + # disable ssl verification |
| 133 | + if not sslverify: |
| 134 | + self.c.setopt(pycurl.SSL_VERIFYPEER, 0) |
| 135 | + self.c.setopt(pycurl.SSL_VERIFYHOST, 0) |
| 136 | + |
| 137 | + def set_cookiejar(self, cj): |
| 138 | + self.c.setopt(pycurl.COOKIEFILE, cj.filename or "") |
| 139 | + self.c.setopt(pycurl.COOKIEJAR, cj.filename or "") |
| 140 | + |
| 141 | + def get_cookies(self): |
| 142 | + return self.c.getinfo(pycurl.INFO_COOKIELIST) |
| 143 | + |
| 144 | + def open_helper(self, url, request_body): |
| 145 | + self.c.setopt(pycurl.URL, url) |
| 146 | + self.c.setopt(pycurl.POSTFIELDS, request_body) |
| 147 | + |
| 148 | + b = StringIO.StringIO() |
| 149 | + self.c.setopt(pycurl.WRITEFUNCTION, b.write) |
| 150 | + try: |
| 151 | + self.c.perform() |
| 152 | + except pycurl.error, e: |
| 153 | + raise xmlrpclib.ProtocolError(url, e[0], e[1], None) |
106 | 154 |
|
107 | | - self.uri = uri |
108 | | - self.opener = urllib2.build_opener() |
109 | | - self.opener.add_handler(urllib2.HTTPCookieProcessor(cookiejar)) |
| 155 | + b.seek(0) |
| 156 | + return b |
110 | 157 |
|
111 | 158 | def request(self, host, handler, request_body, verbose=0): |
112 | | - req = urllib2.Request(self.uri) |
113 | | - req.add_header('User-Agent', self.user_agent) |
114 | | - req.add_header('Content-Type', 'text/xml') |
| 159 | + self.verbose = verbose |
| 160 | + url = "%s://%s%s" % (self.scheme, host, handler) |
115 | 161 |
|
116 | | - if hasattr(self, 'accept_gzip_encoding') and self.accept_gzip_encoding: |
117 | | - req.add_header('Accept-Encoding', 'gzip') |
| 162 | + # xmlrpclib fails to escape \r |
| 163 | + request_body = request_body.replace('\r', '
') |
118 | 164 |
|
119 | | - req.add_data(request_body) |
| 165 | + stringio = self.open_helper(url, request_body) |
| 166 | + return self.parse_response(stringio) |
120 | 167 |
|
121 | | - resp = self.opener.open(req) |
122 | 168 |
|
123 | | - # In Python 2, resp is a urllib.addinfourl instance, which does not |
124 | | - # have the getheader method that parse_response expects. |
125 | | - if not hasattr(resp, 'getheader'): |
126 | | - resp.getheader = resp.headers.getheader |
127 | | - |
128 | | - if resp.code == 200: |
129 | | - self.verbose = verbose |
130 | | - return self.parse_response(resp) |
131 | | - |
132 | | - resp.close() |
133 | | - raise xmlrpclib.ProtocolError(self.uri, resp.status, |
134 | | - resp.reason, resp.msg) |
135 | 169 |
|
136 | 170 |
|
137 | 171 | class BugzillaError(Exception): |
@@ -186,8 +220,6 @@ def url_to_query(url): |
186 | 220 | Given a big huge bugzilla query URL, returns a query dict that can |
187 | 221 | be passed along to the Bugzilla.query() method. |
188 | 222 | ''' |
189 | | - import urlparse |
190 | | - |
191 | 223 | q = {} |
192 | 224 | (ignore, ignore, path, |
193 | 225 | ignore, query, ignore) = urlparse.urlparse(url) |
@@ -219,13 +251,16 @@ def fix_url(url): |
219 | 251 | url = url + '/xmlrpc.cgi' |
220 | 252 | return url |
221 | 253 |
|
222 | | - def __init__(self, url=None, user=None, password=None, cookiefile=-1): |
| 254 | + def __init__(self, url=None, user=None, password=None, cookiefile=-1, |
| 255 | + sslverify=True): |
223 | 256 | # Settings the user might want to tweak |
224 | 257 | self.user = user or '' |
225 | 258 | self.password = password or '' |
226 | 259 | self.url = '' |
227 | 260 |
|
| 261 | + self._transport = None |
228 | 262 | self._cookiejar = None |
| 263 | + self._sslverify = bool(sslverify) |
229 | 264 |
|
230 | 265 | self.logged_in = False |
231 | 266 |
|
@@ -371,9 +406,11 @@ def connect(self, url=None): |
371 | 406 | url = self.url |
372 | 407 | url = self.fix_url(url) |
373 | 408 |
|
374 | | - transport = _CookieTransport(url, self._cookiejar) |
375 | | - transport.user_agent = self.user_agent |
376 | | - self._proxy = xmlrpclib.ServerProxy(url, transport) |
| 409 | + self._transport = _CURLTransport(url, self._cookiejar, |
| 410 | + sslverify=self._sslverify) |
| 411 | + self._transport.user_agent = self.user_agent |
| 412 | + self._proxy = xmlrpclib.ServerProxy(url, self._transport) |
| 413 | + |
377 | 414 |
|
378 | 415 | self.url = url |
379 | 416 | # we've changed URLs - reload config |
@@ -431,8 +468,6 @@ def login(self, user=None, password=None): |
431 | 468 | except xmlrpclib.Fault: |
432 | 469 | r = False |
433 | 470 |
|
434 | | - if r and self._cookiejar.filename is not None: |
435 | | - self._cookiejar.save() |
436 | 471 | return r |
437 | 472 |
|
438 | 473 | def logout(self): |
@@ -1178,18 +1213,32 @@ def openattachment(self, attachid): |
1178 | 1213 | '''Get the contents of the attachment with the given attachment ID. |
1179 | 1214 | Returns a file-like object.''' |
1180 | 1215 | att_uri = self._attachment_uri(attachid) |
1181 | | - opener = urllib2.build_opener( |
1182 | | - urllib2.HTTPCookieProcessor(self._cookiejar)) |
1183 | | - att = opener.open(att_uri) |
1184 | 1216 |
|
1185 | | - # RFC 2183 defines the content-disposition header, if you're curious |
1186 | | - disp = att.headers['content-disposition'].split(';') |
| 1217 | + headers = {} |
| 1218 | + ret = StringIO.StringIO() |
| 1219 | + |
| 1220 | + def headers_cb(buf): |
| 1221 | + if not ":" in buf: |
| 1222 | + return |
| 1223 | + name, val = buf.split(":", 1) |
| 1224 | + headers[name.lower()] = val |
| 1225 | + |
| 1226 | + c = pycurl.Curl() |
| 1227 | + c.setopt(pycurl.URL, att_uri) |
| 1228 | + c.setopt(pycurl.WRITEFUNCTION, ret.write) |
| 1229 | + c.setopt(pycurl.HEADERFUNCTION, headers_cb) |
| 1230 | + c.setopt(pycurl.COOKIEFILE, self._cookiejar.filename or "") |
| 1231 | + c.perform() |
| 1232 | + c.close() |
| 1233 | + |
| 1234 | + disp = headers['content-disposition'].split(';') |
1187 | 1235 | disp.pop(0) |
1188 | 1236 | parms = dict([p.strip().split("=", 1) for p in disp]) |
1189 | | - # Parameter values can be quoted/encoded as per RFC 2231 |
1190 | | - att.name = _decode_rfc2231_value(parms['filename']) |
| 1237 | + ret.name = _decode_rfc2231_value(parms['filename']) |
| 1238 | + |
1191 | 1239 | # Hooray, now we have a file-like object with .read() and .name |
1192 | | - return att |
| 1240 | + ret.seek(0) |
| 1241 | + return ret |
1193 | 1242 |
|
1194 | 1243 | def updateattachmentflags(self, bugid, attachid, flagname, **kwargs): |
1195 | 1244 | ''' |
|
0 commit comments