14 # 59 Temple Place, Suite 330, |
14 # 59 Temple Place, Suite 330, |
15 # Boston, MA 02111-1307 USA |
15 # Boston, MA 02111-1307 USA |
16 |
16 |
17 # This file is part of urlgrabber, a high-level cross-protocol url-grabber |
17 # This file is part of urlgrabber, a high-level cross-protocol url-grabber |
18 # Copyright 2002-2004 Michael D. Stenner, Ryan Tomayko |
18 # Copyright 2002-2004 Michael D. Stenner, Ryan Tomayko |
|
19 |
|
20 # Modified by Benoit Boissinot: |
|
21 # - fix for digest auth (inspired from urllib2.py @ Python v2.4) |
19 |
22 |
20 """An HTTP handler for urllib2 that supports HTTP 1.1 and keepalive. |
23 """An HTTP handler for urllib2 that supports HTTP 1.1 and keepalive. |
21 |
24 |
22 >>> import urllib2 |
25 >>> import urllib2 |
23 >>> from keepalive import HTTPHandler |
26 >>> from keepalive import HTTPHandler |
300 if DEBUG: DEBUG.info("re-using connection to %s (%d)", host, id(h)) |
303 if DEBUG: DEBUG.info("re-using connection to %s (%d)", host, id(h)) |
301 |
304 |
302 return r |
305 return r |
303 |
306 |
304 def _start_transaction(self, h, req): |
307 def _start_transaction(self, h, req): |
305 try: |
308 headers = req.headers.copy() |
306 if req.has_data(): |
309 body = req.data |
307 data = req.get_data() |
310 if sys.version_info >= (2, 4): |
308 h.putrequest('POST', req.get_selector()) |
311 headers.update(req.unredirected_hdrs) |
309 if not req.headers.has_key('Content-type'): |
312 try: |
310 h.putheader('Content-type', |
313 h.request(req.get_method(), req.get_selector(), body, headers) |
311 'application/x-www-form-urlencoded') |
314 except socket.error, err: # XXX what error? |
312 if not req.headers.has_key('Content-length'): |
|
313 h.putheader('Content-length', '%d' % len(data)) |
|
314 else: |
|
315 h.putrequest('GET', req.get_selector()) |
|
316 except (socket.error, httplib.HTTPException), err: |
|
317 raise urllib2.URLError(err) |
315 raise urllib2.URLError(err) |
318 |
|
319 for args in self.parent.addheaders: |
|
320 h.putheader(*args) |
|
321 for k, v in req.headers.items(): |
|
322 h.putheader(k, v) |
|
323 h.endheaders() |
|
324 if req.has_data(): |
|
325 h.send(data) |
|
326 |
316 |
327 class HTTPResponse(httplib.HTTPResponse): |
317 class HTTPResponse(httplib.HTTPResponse): |
328 # we need to subclass HTTPResponse in order to |
318 # we need to subclass HTTPResponse in order to |
329 # 1) add readline() and readlines() methods |
319 # 1) add readline() and readlines() methods |
330 # 2) add close_connection() methods |
320 # 2) add close_connection() methods |