Skip to content

Commit a637be8

Browse files
committed
Fixes for full successful re-run of unit tests against Python 2.7 and
3.4
1 parent 5af49f3 commit a637be8

File tree

7 files changed

+52
-42
lines changed

7 files changed

+52
-42
lines changed

ndg/httpsclient/https.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
if sys.version_info[0] > 2:
1818
from http.client import HTTPS_PORT
1919
from http.client import HTTPConnection
20+
21+
from urllib.request import AbstractHTTPHandler
2022
else:
2123
from httplib import HTTPS_PORT
2224
from httplib import HTTPConnection
2325

24-
from urllib2 import AbstractHTTPHandler
26+
from urllib2 import AbstractHTTPHandler
2527

2628

2729
from OpenSSL import SSL
@@ -114,7 +116,7 @@ def __init__(self, ssl_context, debuglevel=0):
114116
ssl_context)
115117
self.ssl_context = ssl_context
116118
else:
117-
self.ssl_context = SSL.Context(SSL.SSLv23_METHOD)
119+
self.ssl_context = SSL.Context(SSL.TLSv1_METHOD)
118120

119121
def https_open(self, req):
120122
"""Opens HTTPS request

ndg/httpsclient/ssl_context_util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
__license__ = "BSD - see LICENSE file in top-level directory"
99
__contact__ = "[email protected]"
1010
__revision__ = '$Id$'
11+
import sys
12+
1113
if sys.version_info[0] > 2:
1214
import urllib.parse as urlparse_
1315
else:

ndg/httpsclient/ssl_peer_verification.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ndg.httpsclient.subj_alt_name import SubjectAltName
1515
from pyasn1.codec.der import decoder as der_decoder
1616
SUBJ_ALT_NAME_SUPPORT = True
17+
1718
except ImportError as e:
1819
SUBJ_ALT_NAME_SUPPORT = False
1920
SUBJ_ALT_NAME_SUPPORT_MSG = (
@@ -156,12 +157,12 @@ def __call__(self, connection, peerCert, errorStatus, errorDepth,
156157
return preverifyOK
157158

158159
def get_verify_server_cert_func(self):
159-
def verify_server_cert(connection, peerCert, errorStatus, errorDepth,
160-
preverifyOK):
161-
return self.__call__(connection, peerCert, errorStatus,
162-
errorDepth, preverifyOK)
163-
164-
return verify_server_cert
160+
def verify_server_cert(connection, peerCert, errorStatus, errorDepth,
161+
preverifyOK):
162+
return self.__call__(connection, peerCert, errorStatus,
163+
errorDepth, preverifyOK)
164+
165+
return verify_server_cert
165166

166167
@classmethod
167168
def _get_subj_alt_name(cls, peer_cert):

ndg/httpsclient/ssl_socket.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
import sys
1616

1717
if sys.version_info[0] > 2:
18-
from io import StringIO
18+
# Make use of byte buffer for Python 3
19+
from io import BytesIO as BufferIO_
1920
else:
20-
from cStringIO import StringIO
21+
from cStringIO import StringIO as BufferIO_
2122

2223
from OpenSSL import SSL
2324

@@ -74,14 +75,12 @@ def buf_size(self, value):
7475
def close(self):
7576
"""Shutdown the SSL connection and call the close method of the
7677
underlying socket"""
77-
# try:
78-
# self.__ssl_conn.shutdown()
79-
# except SSL.Error:
80-
# # Make errors on shutdown non-fatal
81-
# pass
82-
8378
if self._makefile_refs < 1:
84-
self.__ssl_conn.shutdown()
79+
try:
80+
self.__ssl_conn.shutdown()
81+
except (SSL.Error, SSL.SysCallError):
82+
# Make errors on shutdown non-fatal
83+
pass
8584
else:
8685
self._makefile_refs -= 1
8786

@@ -241,7 +240,7 @@ def makefile(self, *args):
241240
_buf_size = self.buf_size
242241

243242
i=0
244-
stream = StringIO()
243+
stream = BufferIO_()
245244
startTime = datetime.utcnow()
246245
try:
247246
dat = self.__ssl_conn.recv(_buf_size)
@@ -266,17 +265,6 @@ def makefile(self, *args):
266265
stream.seek(0)
267266

268267
return stream
269-
270-
# def makefile(self, mode='r', bufsize=-1):
271-
#
272-
# """Make and return a file-like object that
273-
# works with the SSL connection. Just use the code
274-
# from the socket module."""
275-
#
276-
# self._makefile_refs += 1
277-
# # close=True so as to decrement the reference count when done with
278-
# # the file-like object.
279-
# return socket._fileobject(self.socket, mode, bufsize, close=True)
280268

281269
def getsockname(self):
282270
"""

ndg/httpsclient/test/test_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ class TestUtilsModule(unittest.TestCase):
2323
'''Test ndg.httpsclient.utils module'''
2424

2525
def test01_configuration(self):
26-
config = Configuration(SSL.Context(SSL.SSLv3_METHOD), True)
26+
config = Configuration(SSL.Context(SSL.TLSv1_METHOD), True)
2727
self.assertTrue(config.ssl_context)
2828
self.assertEqual(config.debug, True)
2929

3030
def test02_fetch_from_url(self):
31-
config = Configuration(SSL.Context(SSL.SSLv3_METHOD), True)
31+
config = Configuration(SSL.Context(SSL.TLSv1_METHOD), True)
3232
res = fetch_from_url(Constants.TEST_URI, config)
3333
self.assertTrue(res)
3434

3535
def test03_open_url(self):
36-
config = Configuration(SSL.Context(SSL.SSLv3_METHOD), True)
36+
config = Configuration(SSL.Context(SSL.TLSv1_METHOD), True)
3737
res = open_url(Constants.TEST_URI, config)
3838
self.assertEqual(res[0], 200,
3939
'open_url for %r failed' % Constants.TEST_URI)
@@ -57,5 +57,6 @@ def test04__should_use_proxy(self):
5757
else:
5858
del os.environ['no_proxy']
5959

60+
6061
if __name__ == "__main__":
6162
unittest.main()

ndg/httpsclient/urllib2_build_opener.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
# Py 2 <=> 3 compatibility for class type checking
1414
if sys.version_info[0] > 2:
1515
class_type_ = type
16+
from urllib.request import (ProxyHandler, UnknownHandler,
17+
HTTPDefaultErrorHandler, FTPHandler,
18+
FileHandler, HTTPErrorProcessor,
19+
HTTPHandler, OpenerDirector,
20+
HTTPRedirectHandler)
1621
else:
1722
import types
1823
class_type_ = types.ClassType
1924

20-
from urllib2 import (ProxyHandler, UnknownHandler, HTTPDefaultErrorHandler,
21-
FTPHandler, FileHandler, HTTPErrorProcessor, HTTPHandler,
22-
OpenerDirector, HTTPRedirectHandler)
25+
from urllib2 import (ProxyHandler, UnknownHandler, HTTPDefaultErrorHandler,
26+
FTPHandler, FileHandler, HTTPErrorProcessor,
27+
HTTPHandler, OpenerDirector, HTTPRedirectHandler)
2328

2429
from ndg.httpsclient.https import HTTPSContextHandler
2530

@@ -36,7 +41,6 @@ def build_opener(*handlers, **kw):
3641
If any of the handlers passed as arguments are subclasses of the
3742
default handlers, the default handlers will not be used.
3843
"""
39-
import types
4044
def isclass(obj):
4145
return isinstance(obj, class_type_) or hasattr(obj, "__bases__")
4246

ndg/httpsclient/utils.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,29 @@
1111
import logging
1212
from optparse import OptionParser
1313
import os
14-
15-
from urllib2 import (HTTPHandler, HTTPCookieProcessor,
16-
HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm)
14+
import sys
1715

1816
if sys.version_info[0] > 2:
1917
import http.cookiejar as cookiejar_
2018
import http.client as http_client_
2119
from urllib.request import Request as Request_
20+
from urllib.request import HTTPHandler as HTTPHandler_
21+
from urllib.request import HTTPCookieProcessor as HTTPCookieProcessor_
22+
from urllib.request import HTTPBasicAuthHandler as HTTPBasicAuthHandler_
23+
from urllib.request import HTTPPasswordMgrWithDefaultRealm as \
24+
HTTPPasswordMgrWithDefaultRealm_
2225
from urllib.request import ProxyHandler as ProxyHandler_
2326
from urllib.error import HTTPError as HTTPError_
2427
import urllib.parse as urlparse_
2528
else:
2629
import cookielib as cookiejar_
2730
import httplib as http_client_
2831
from urllib2 import Request as Request_
32+
from urllib2 import HTTPHandler as HTTPHandler_
33+
from urllib2 import HTTPCookieProcessor as HTTPCookieProcessor_
34+
from urllib2 import HTTPBasicAuthHandler as HTTPBasicAuthHandler_
35+
from urllib2 import HTTPPasswordMgrWithDefaultRealm as \
36+
HTTPPasswordMgrWithDefaultRealm_
2937
from urllib2 import ProxyHandler as ProxyHandler_
3038
from urllib2 import HTTPError as HTTPError_
3139
import urlparse as urlparse_
@@ -36,7 +44,7 @@
3644

3745
log = logging.getLogger(__name__)
3846

39-
class AccumulatingHTTPCookieProcessor(HTTPCookieProcessor):
47+
class AccumulatingHTTPCookieProcessor(HTTPCookieProcessor_):
4048
"""Cookie processor that adds new cookies (instead of replacing the existing
4149
ones as HTTPCookieProcessor does)
4250
"""
@@ -171,14 +179,14 @@ def open_url(url, config, data=None, handlers=None):
171179
handlers.append(cookie_handler)
172180

173181
if config.debug:
174-
http_handler = HTTPHandler(debuglevel=debuglevel)
182+
http_handler = HTTPHandler_(debuglevel=debuglevel)
175183
https_handler = HTTPSContextHandler(config.ssl_context,
176184
debuglevel=debuglevel)
177185
handlers.extend([http_handler, https_handler])
178186

179187
if config.http_basicauth:
180188
# currently only supports http basic auth
181-
auth_handler = HTTPBasicAuthHandler(HTTPPasswordMgrWithDefaultRealm())
189+
auth_handler = HTTPBasicAuthHandler_(HTTPPasswordMgrWithDefaultRealm_())
182190
auth_handler.add_password(realm=None, uri=url,
183191
user=config.httpauth[0],
184192
passwd=config.httpauth[1])
@@ -208,6 +216,10 @@ def open_url(url, config, data=None, handlers=None):
208216
return_code = 0
209217
return_message = ''
210218
response = None
219+
220+
# FIXME
221+
response = opener.open(request)
222+
211223
try:
212224
response = opener.open(request)
213225
return_message = response.msg

0 commit comments

Comments
 (0)