no need to port compatrequests or old ssl code
This commit is contained in:
parent
c976c3f2b2
commit
96532de199
5 changed files with 41 additions and 50 deletions
|
|
@ -7,14 +7,13 @@ module that is based on the older codepaths in koji. It only provides
|
|||
the bits that koji needs.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
import six.moves.http_client
|
||||
import six.moves.urllib
|
||||
import httplib
|
||||
import urlparse
|
||||
import urllib
|
||||
import sys
|
||||
from .ssl import SSLCommon
|
||||
import six
|
||||
import ssl.SSLCommon
|
||||
try:
|
||||
from .ssl import ssl as pyssl
|
||||
from ssl import ssl as pyssl
|
||||
except ImportError: # pragma: no cover
|
||||
pass
|
||||
|
||||
|
|
@ -26,7 +25,7 @@ class Session(object):
|
|||
|
||||
def post(self, url, data=None, headers=None, stream=None, verify=None,
|
||||
cert=None, timeout=None):
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
if uri[3]:
|
||||
handler = "%s?%s" % (uri[2], uri[3])
|
||||
else:
|
||||
|
|
@ -45,11 +44,7 @@ class Session(object):
|
|||
|
||||
def get_connection(self, uri, cert, verify, timeout):
|
||||
scheme = uri[0]
|
||||
# TODO: unnecessary remerging of values
|
||||
parsed = six.moves.urllib.parse.urlparse('%s://%s' % (uri[0], uri[1]))
|
||||
host = parsed.hostname
|
||||
port = parsed.port
|
||||
#host, port = six.moves.urllib.splitport(uri[1])
|
||||
host, port = urllib.splitport(uri[1])
|
||||
key = (scheme, host, cert, verify, timeout)
|
||||
#if self.connection and self.opts.get('keepalive'):
|
||||
if self.connection: # XXX honor keepalive
|
||||
|
|
@ -60,13 +55,13 @@ class Session(object):
|
|||
# Otherwise we make a new one
|
||||
default_port = 80
|
||||
certs = {}
|
||||
if isinstance(verify, six.string_types):
|
||||
if isinstance(verify, basestring):
|
||||
certs['peer_ca_cert'] = verify
|
||||
if cert:
|
||||
certs['key_and_cert'] = cert
|
||||
ctx = SSLCommon.CreateSSLContext(certs)
|
||||
ctx = ssl.SSLCommon.CreateSSLContext(certs)
|
||||
cnxOpts = {'ssl_context' : ctx}
|
||||
cnxClass = SSLCommon.PlgHTTPSConnection
|
||||
cnxClass = ssl.SSLCommon.PlgHTTPSConnection
|
||||
default_port = 443
|
||||
elif scheme == 'https':
|
||||
cnxOpts = {}
|
||||
|
|
@ -89,11 +84,11 @@ class Session(object):
|
|||
# no verify
|
||||
ctx = pyssl._create_unverified_context()
|
||||
cnxOpts['context'] = ctx
|
||||
cnxClass = six.moves.http_client.HTTPSConnection
|
||||
cnxClass = httplib.HTTPSConnection
|
||||
default_port = 443
|
||||
elif scheme == 'http':
|
||||
cnxOpts = {}
|
||||
cnxClass = six.moves.http_client.HTTPConnection
|
||||
cnxClass = httplib.HTTPConnection
|
||||
else:
|
||||
raise IOError("unsupported protocol: %s" % scheme)
|
||||
|
||||
|
|
@ -128,7 +123,7 @@ class Response(object):
|
|||
|
||||
def raise_for_status(self):
|
||||
if self.response.status >= 400:
|
||||
raise six.moves.http_client.HTTPException("HTTP %s: %s" % (self.response.status,
|
||||
raise httplib.HTTPException("HTTP %s: %s" % (self.response.status,
|
||||
self.response.reason))
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -14,11 +14,10 @@
|
|||
#
|
||||
# Copyright 2005 Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
|
||||
|
||||
from __future__ import absolute_import
|
||||
import os, sys
|
||||
from OpenSSL import SSL
|
||||
from . import SSLConnection
|
||||
import six.moves.http_client
|
||||
import SSLConnection
|
||||
import httplib
|
||||
import socket
|
||||
|
||||
def our_verify(connection, x509, errNum, errDepth, preverifyOK):
|
||||
|
|
@ -47,13 +46,13 @@ def CreateSSLContext(certs):
|
|||
return ctx
|
||||
|
||||
|
||||
class PlgHTTPSConnection(six.moves.http_client.HTTPConnection):
|
||||
class PlgHTTPSConnection(httplib.HTTPConnection):
|
||||
"This class allows communication via SSL."
|
||||
|
||||
response_class = six.moves.http_client.HTTPResponse
|
||||
response_class = httplib.HTTPResponse
|
||||
|
||||
def __init__(self, host, port=None, ssl_context=None, strict=None, timeout=None):
|
||||
six.moves.http_client.HTTPConnection.__init__(self, host, port, strict)
|
||||
httplib.HTTPConnection.__init__(self, host, port, strict)
|
||||
self.ssl_ctx = ssl_context
|
||||
self._timeout = timeout
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@
|
|||
# Modifications by Dan Williams <dcbw@redhat.com>
|
||||
|
||||
|
||||
from __future__ import absolute_import
|
||||
from OpenSSL import SSL
|
||||
import time, socket, select
|
||||
|
||||
|
|
@ -107,7 +106,7 @@ class SSLConnection:
|
|||
|
||||
try:
|
||||
sent = con.send(data, flags)
|
||||
except SSL.SysCallError as e:
|
||||
except SSL.SysCallError, e:
|
||||
if e[0] == 32: # Broken Pipe
|
||||
self.close()
|
||||
sent = 0
|
||||
|
|
@ -143,7 +142,7 @@ class SSLConnection:
|
|||
return None
|
||||
except SSL.WantReadError:
|
||||
time.sleep(0.2)
|
||||
except SSL.SysCallError as e:
|
||||
except SSL.SysCallError, e:
|
||||
if e.args == (-1, 'Unexpected EOF'):
|
||||
break
|
||||
raise
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
# identify this as the ssl module
|
||||
|
||||
# our own ssl submodule masks python's in the main lib, so we import this here
|
||||
from __future__ import absolute_import
|
||||
try:
|
||||
import ssl # python's ssl module
|
||||
except ImportError: # pragma: no cover
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
from __future__ import absolute_import
|
||||
import six.moves.http_client
|
||||
import httplib
|
||||
import mock
|
||||
import unittest
|
||||
import six.moves.urllib
|
||||
import urlparse
|
||||
|
||||
import koji.compatrequests
|
||||
|
||||
|
|
@ -60,7 +59,7 @@ class TestResponse(unittest.TestCase):
|
|||
self.response.response.status = 404
|
||||
self.response.response.reason = 'Not Found'
|
||||
self.response.response.getheader.return_value = 42
|
||||
with self.assertRaises(six.moves.http_client.HTTPException):
|
||||
with self.assertRaises(httplib.HTTPException):
|
||||
self.response.raise_for_status()
|
||||
|
||||
|
||||
|
|
@ -106,7 +105,7 @@ class TestSessionConnection(unittest.TestCase):
|
|||
# no cert, no verify, no timeout
|
||||
session = koji.compatrequests.Session()
|
||||
url = 'http://www.fakedomain234234.org/KOJIHUB?a=1&b=2'
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
|
||||
cnx = session.get_connection(uri, None, None, None)
|
||||
HTTPConnection.assert_called_once_with('www.fakedomain234234.org', 80)
|
||||
|
|
@ -124,7 +123,7 @@ class TestSessionConnection(unittest.TestCase):
|
|||
def test_cached(self):
|
||||
session = koji.compatrequests.Session()
|
||||
url = 'http://www.fakedomain234234.org/KOJIHUB?a=1&b=2'
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
key = ('http', 'www.fakedomain234234.org', None, None, None)
|
||||
cnx = mock.MagicMock()
|
||||
session.connection = (key, cnx)
|
||||
|
|
@ -135,10 +134,10 @@ class TestSessionConnection(unittest.TestCase):
|
|||
def test_badproto(self):
|
||||
session = koji.compatrequests.Session()
|
||||
url = 'nosuchproto://www.fakedomain234234.org/KOJIHUB?a=1&b=2'
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
|
||||
with self.assertRaises(IOError):
|
||||
session.get_connection(uri, None, None, None)
|
||||
ret = session.get_connection(uri, None, None, None)
|
||||
|
||||
@mock.patch('httplib.HTTPConnection')
|
||||
@mock.patch('sys.version_info', new=(2, 7, 12, 'final', 0))
|
||||
|
|
@ -146,12 +145,12 @@ class TestSessionConnection(unittest.TestCase):
|
|||
# no cert, no verify
|
||||
session = koji.compatrequests.Session()
|
||||
url = 'http://www.fakedomain234234.org/KOJIHUB?a=1&b=2'
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
timeout = 1701
|
||||
|
||||
cnx = session.get_connection(uri, None, None, timeout)
|
||||
HTTPConnection.assert_called_once_with('www.fakedomain234234.org', 80, timeout=timeout)
|
||||
key = ('http', 'www.fakedomain234234.org', None, None, timeout)
|
||||
cnx = session.get_connection(uri, None, None, 1701)
|
||||
HTTPConnection.assert_called_once_with('www.fakedomain234234.org', 80, timeout=1701)
|
||||
key = ('http', 'www.fakedomain234234.org', None, None, 1701)
|
||||
self.assertEqual(session.connection, (key, cnx))
|
||||
|
||||
@mock.patch('httplib.HTTPConnection')
|
||||
|
|
@ -160,22 +159,22 @@ class TestSessionConnection(unittest.TestCase):
|
|||
# no cert, no verify
|
||||
session = koji.compatrequests.Session()
|
||||
url = 'http://www.fakedomain234234.org/KOJIHUB?a=1&b=2'
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
timeout = 1701
|
||||
|
||||
cnx = session.get_connection(uri, None, None, timeout)
|
||||
cnx = session.get_connection(uri, None, None, 1701)
|
||||
HTTPConnection.assert_called_once_with('www.fakedomain234234.org', 80)
|
||||
key = ('http', 'www.fakedomain234234.org', None, None, timeout)
|
||||
key = ('http', 'www.fakedomain234234.org', None, None, 1701)
|
||||
self.assertEqual(session.connection, (key, cnx))
|
||||
cnx.connect.assert_called_once()
|
||||
cnx.sock.settimeout.assert_called_with(timeout)
|
||||
cnx.sock.settimeout.assert_called_with(1701)
|
||||
|
||||
@mock.patch('httplib.HTTPSConnection')
|
||||
def test_https(self, HTTPSConnection):
|
||||
# no cert, no verify, no timeout
|
||||
session = koji.compatrequests.Session()
|
||||
url = 'https://www.fakedomain234234.org/KOJIHUB?a=1&b=2'
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
|
||||
cnx = session.get_connection(uri, None, None, None)
|
||||
HTTPSConnection.assert_called_once_with('www.fakedomain234234.org', 443)
|
||||
|
|
@ -188,7 +187,7 @@ class TestSessionConnection(unittest.TestCase):
|
|||
# no verify, no timeout
|
||||
session = koji.compatrequests.Session()
|
||||
url = 'https://www.fakedomain234234.org/KOJIHUB?a=1&b=2'
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
cert = '/path/to/cert/file'
|
||||
context = mock.MagicMock()
|
||||
CreateSSLContext.return_value = context
|
||||
|
|
@ -205,7 +204,7 @@ class TestSessionConnection(unittest.TestCase):
|
|||
# no cert, verify=False, no timeout
|
||||
session = koji.compatrequests.Session()
|
||||
url = 'https://www.fakedomain234234.org/KOJIHUB?a=1&b=2'
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
context = mock.MagicMock()
|
||||
create_unverified_context.return_value = context
|
||||
|
||||
|
|
@ -221,7 +220,7 @@ class TestSessionConnection(unittest.TestCase):
|
|||
# no cert, verify=False, no timeout
|
||||
session = koji.compatrequests.Session()
|
||||
url = 'https://www.fakedomain234234.org/KOJIHUB?a=1&b=2'
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
|
||||
cnx = session.get_connection(uri, None, False, None)
|
||||
HTTPSConnection.assert_called_once_with('www.fakedomain234234.org', 443)
|
||||
|
|
@ -236,7 +235,7 @@ class TestSessionConnection(unittest.TestCase):
|
|||
# no cert, no timeout
|
||||
session = koji.compatrequests.Session()
|
||||
url = 'https://www.fakedomain234234.org/KOJIHUB?a=1&b=2'
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
context = mock.MagicMock()
|
||||
SSLContext.return_value = context
|
||||
verify = '/path/to/verify/cert'
|
||||
|
|
@ -257,7 +256,7 @@ class TestSessionConnection(unittest.TestCase):
|
|||
# no cert, no timeout
|
||||
session = koji.compatrequests.Session()
|
||||
url = 'https://www.fakedomain234234.org/KOJIHUB?a=1&b=2'
|
||||
uri = six.moves.urllib.parse.urlsplit(url)
|
||||
uri = urlparse.urlsplit(url)
|
||||
verify = '/path/to/verify/cert'
|
||||
|
||||
cnx = session.get_connection(uri, None, verify, None)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue