]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - www/pages/torrent/client/zurllib.py
1 # Written by John Hoffman
2 # see LICENSE.txt for license information
4 from httplib
import HTTPConnection
, HTTPSConnection
, HTTPException
5 from urlparse
import urlparse
6 from bencode
import bdecode
8 from gzip
import GzipFile
9 from StringIO
import StringIO
10 from urllib
import quote
, unquote
11 from __init__
import product_name
, version_short
13 VERSION
= product_name
+'/'+version_short
17 class btHTTPcon(HTTPConnection
): # attempt to add automatic connection timeout
19 HTTPConnection
.connect(self
)
21 self
.sock
.settimeout(30)
25 class btHTTPScon(HTTPSConnection
): # attempt to add automatic connection timeout
27 HTTPSConnection
.connect(self
)
29 self
.sock
.settimeout(30)
34 def __init__(self
, url
):
36 self
._open
(url
.strip())
37 self
.error_return
= None
41 if self
.tries
> MAX_REDIRECTS
:
42 raise IOError, ('http error', 500,
43 "Internal Server Error: Redirect Recursion")
44 (scheme
, netloc
, path
, pars
, query
, fragment
) = urlparse(url
)
45 if scheme
!= 'http' and scheme
!= 'https':
46 raise IOError, ('url error', 'unknown url type', scheme
, url
)
55 self
.connection
= btHTTPcon(netloc
)
57 self
.connection
= btHTTPScon(netloc
)
58 self
.connection
.request('GET', url
, None,
59 { 'User-Agent': VERSION
,
60 'Accept-Encoding': 'gzip' } )
61 self
.response
= self
.connection
.getresponse()
62 except HTTPException
, e
:
63 raise IOError, ('http error', str(e
))
64 status
= self
.response
.status
65 if status
in (301,302):
67 self
.connection
.close()
70 self
._open
(self
.response
.getheader('Location'))
76 if d
.has_key('failure reason'):
77 self
.error_return
= data
81 raise IOError, ('http error', status
, self
.response
.reason
)
85 return self
.error_return
89 data
= self
.response
.read()
90 if self
.response
.getheader('Content-Encoding','').find('gzip') >= 0:
92 compressed
= StringIO(data
)
93 f
= GzipFile(fileobj
= compressed
)
96 raise IOError, ('http error', 'got corrupt response')
100 self
.connection
.close()