1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-01-28 15:36:13 +01:00

Merge pull request #490 from lethliel/python3_oscssl_module

[python3] make oscssl.py python3 ready.
This commit is contained in:
Marco Strigl 2019-04-15 15:05:04 +02:00 committed by GitHub
commit 05d90f9e96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,9 +5,8 @@
from __future__ import print_function from __future__ import print_function
import M2Crypto.httpslib
from M2Crypto.SSL.Checker import SSLVerificationError from M2Crypto.SSL.Checker import SSLVerificationError
from M2Crypto import m2, SSL from M2Crypto import m2, SSL, httpslib
import M2Crypto.m2urllib2 import M2Crypto.m2urllib2
import socket import socket
import sys import sys
@ -185,22 +184,28 @@ class myHTTPSHandler(M2Crypto.m2urllib2.HTTPSHandler):
# "do_open()" and "https_open()" so that we just need to override # "do_open()" and "https_open()" so that we just need to override
# the small "https_open()" method...) # the small "https_open()" method...)
def https_open(self, req): def https_open(self, req):
host = req.get_host() # https://docs.python.org/3.3/library/urllib.request.html#urllib.request.Request.get_host
try: # up to python-3.2
host = req.get_host()
except AttributeError: # from python-3.3
host = req.host
if not host: if not host:
raise M2Crypto.m2urllib2.URLError('no host given: ' + req.get_full_url()) raise M2Crypto.m2urllib2.URLError('no host given')
# Our change: Check to see if we're using a proxy. # Our change: Check to see if we're using a proxy.
# Then create an appropriate ssl-aware connection. # Then create an appropriate ssl-aware connection.
full_url = req.get_full_url() full_url = req.get_full_url()
target_host = urlparse(full_url)[1] target_host = urlparse(full_url)[1]
if (target_host != host): if target_host != host:
h = myProxyHTTPSConnection(host = host, appname = self.appname, ssl_context = self.ctx) request_uri = urldefrag(full_url)[0]
# M2Crypto.ProxyHTTPSConnection.putrequest expects a fullurl h = httpslib.ProxyHTTPSConnection(host=host, ssl_context=self.ctx)
selector = full_url
else: else:
h = myHTTPSConnection(host = host, appname = self.appname, ssl_context = self.ctx) try: # up to python-3.2
selector = req.get_selector() request_uri = req.get_selector()
except AttributeError: # from python-3.3
request_uri = req.selector
h = httpslib.HTTPSConnection(host=host, ssl_context=self.ctx)
# End our change # End our change
h.set_debuglevel(self._debuglevel) h.set_debuglevel(self._debuglevel)
@ -214,10 +219,9 @@ class myHTTPSHandler(M2Crypto.m2urllib2.HTTPSHandler):
# request. # request.
headers["Connection"] = "close" headers["Connection"] = "close"
try: try:
h.request(req.get_method(), selector, req.data, headers) h.request(req.get_method(), request_uri, req.data, headers)
r = h.getresponse() r = h.getresponse()
except socket.error as err: # XXX what error? except socket.error as err: # XXX what error?
err.filename = full_url
raise M2Crypto.m2urllib2.URLError(err) raise M2Crypto.m2urllib2.URLError(err)
# Pick apart the HTTPResponse object to get the addinfourl # Pick apart the HTTPResponse object to get the addinfourl
@ -227,18 +231,26 @@ class myHTTPSHandler(M2Crypto.m2urllib2.HTTPSHandler):
# for Windows. That adapter calls recv(), so delegate recv() # for Windows. That adapter calls recv(), so delegate recv()
# to read(). This weird wrapping allows the returned object to # to read(). This weird wrapping allows the returned object to
# have readline() and readlines() methods. # have readline() and readlines() methods.
# XXX It might be better to extract the read buffering code
# out of socket._fileobject() and into a base class.
r.recv = r.read r.recv = r.read
fp = socket._fileobject(r) if (sys.version_info < (3, 0)):
fp = socket._fileobject(r, close=True)
else:
r._decref_socketios = lambda: None
r.ssl = h.sock.ssl
r._timeout = -1.0
# hack to bypass python3 bug with 0 buffer size and
# http/client.py readinto method for response class
if r.length is not None and r.length == 0:
r.readinto = lambda b: 0
r.recv_into = r.readinto
fp = socket.SocketIO(r, 'rb')
resp = addinfourl(fp, r.msg, req.get_full_url()) resp = addinfourl(fp, r.msg, req.get_full_url())
resp.code = r.status resp.code = r.status
resp.msg = r.reason resp.msg = r.reason
return resp return resp
class myHTTPSConnection(M2Crypto.httpslib.HTTPSConnection): class myHTTPSConnection(M2Crypto.httpslib.HTTPSConnection):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.appname = kwargs.pop('appname', 'generic') self.appname = kwargs.pop('appname', 'generic')