summaryrefslogtreecommitdiff
path: root/M2Crypto/m2urllib.py
diff options
context:
space:
mode:
authorNg Pheng Siong <ngps@netmemetic.com>1999-09-12 14:34:41 +0000
committerNg Pheng Siong <ngps@netmemetic.com>1999-09-12 14:34:41 +0000
commit7068f5eba2940e4bdb4484312a307a7bf6199d15 (patch)
tree77bdc7b45bca9141cdba9586ae5b47d9b1e8686e /M2Crypto/m2urllib.py
parentc051249b5230e38dd7e40321c6d38c6177e49ac2 (diff)
downloadm2crypto-7068f5eba2940e4bdb4484312a307a7bf6199d15.tar.gz
Initial revision
git-svn-id: http://svn.osafoundation.org/m2crypto/trunk@11 2715db39-9adf-0310-9c64-84f055769b4b
Diffstat (limited to 'M2Crypto/m2urllib.py')
-rw-r--r--M2Crypto/m2urllib.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/M2Crypto/m2urllib.py b/M2Crypto/m2urllib.py
new file mode 100644
index 0000000..3053072
--- /dev/null
+++ b/M2Crypto/m2urllib.py
@@ -0,0 +1,76 @@
+"""M2Crypto enhancement to Python's urllib for handling 'https' url's.
+
+Copyright (c) 1999 Ng Pheng Siong. All rights reserved.
+
+Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
+The Netherlands. """
+
+RCS_id='$Id: m2urllib.py,v 1.1 1999/09/12 14:34:41 ngps Exp $'
+
+from urllib import *
+
+import SSL
+import httpslib
+
+DEFAULT_PROTOCOL='sslv3'
+
+# Cut-&-pasted almost verbatim from urllib's open_http().
+# Achtung: following code indents by space, _not_ by tabs.
+def open_https(self, url, data=None):
+ self.ctx=SSL.Context(DEFAULT_PROTOCOL)
+ user_passwd = None
+ if type(url) is type(""):
+ host, selector = splithost(url)
+ if host:
+ user_passwd, host = splituser(host)
+ host = unquote(host)
+ realhost = host
+ else:
+ host, selector = url
+ urltype, rest = splittype(selector)
+ url = rest
+ user_passwd = None
+ if string.lower(urltype) != 'http':
+ realhost = None
+ else:
+ realhost, rest = splithost(rest)
+ if realhost:
+ user_passwd, realhost = splituser(realhost)
+ if user_passwd:
+ selector = "%s://%s%s" % (urltype, realhost, rest)
+ #print "proxy via http:", host, selector
+ if not host: raise IOError, ('http error', 'no host given')
+ if user_passwd:
+ import base64
+ auth = string.strip(base64.encodestring(user_passwd))
+ else:
+ auth = None
+ # Here!
+ h = httpslib.HTTPS(self.ctx, host)
+ #h.debuglevel=1
+ # Here!
+ if data is not None:
+ h.putrequest('POST', selector)
+ h.putheader('Content-type', 'application/x-www-form-urlencoded')
+ h.putheader('Content-length', '%d' % len(data))
+ else:
+ h.putrequest('GET', selector)
+ if auth: h.putheader('Authorization', 'Basic %s' % auth)
+ if realhost: h.putheader('Host', realhost)
+ for args in self.addheaders: apply(h.putheader, args)
+ h.endheaders()
+ if data is not None:
+ h.send(data + '\r\n')
+ errcode, errmsg, headers = h.getreply()
+ fp = h.getfile()
+ if errcode == 200:
+ return addinfourl(fp, headers, "http:" + url)
+ else:
+ if data is None:
+ return self.http_error(url, fp, errcode, errmsg, headers)
+ else:
+ return self.http_error(url, fp, errcode, errmsg, headers, data)
+
+# Minor brain surgery.
+URLopener.open_https=open_https
+