Skip to content

Commit e137606

Browse files
committed
Support python interception for httplib
1 parent 94c0887 commit e137606

File tree

3 files changed

+73
-24
lines changed

3 files changed

+73
-24
lines changed

overrides/pythonpath/httplib.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from httptoolkit_intercept import preload_real_module
2+
3+
preload_real_module('httplib')
4+
5+
import httplib, os, functools
6+
7+
# Re-export all public fields
8+
from httplib import *
9+
# Load a few extra notable private fields, for max compatibility
10+
from httplib import __file__, __doc__
11+
12+
_httpProxy = os.environ['HTTP_PROXY']
13+
[_proxyHost, _proxyPort] = _httpProxy.split('://')[1].split(':')
14+
_certPath = os.environ['SSL_CERT_FILE']
15+
16+
_http_connection_init = HTTPConnection.__init__
17+
@functools.wraps(_http_connection_init)
18+
def _new_http_connection_init(self, host, port=80, *k, **kw):
19+
_http_connection_init(self, _proxyHost, _proxyPort, *k, **kw)
20+
self.set_tunnel(host, port)
21+
HTTPConnection.__init__ = _new_http_connection_init
22+
23+
def _build_default_context():
24+
import ssl
25+
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
26+
context.options |= ssl.OP_NO_SSLv2
27+
context.options |= ssl.OP_NO_SSLv3
28+
return context
29+
30+
_https_connection_init = HTTPSConnection.__init__
31+
@functools.wraps(_https_connection_init)
32+
def _new_https_connection_init(self, host, port=443, *k, **kw):
33+
context = None
34+
if 'context' in kw:
35+
context = kw.get('context')
36+
elif len(k) > 7:
37+
context = k[7]
38+
39+
if context == None:
40+
context = kw['context'] = _build_default_context()
41+
42+
context.load_verify_locations(_certPath)
43+
44+
_https_connection_init(self, _proxyHost, _proxyPort, *k, **kw)
45+
self.set_tunnel(host, port)
46+
HTTPSConnection.__init__ = _new_https_connection_init
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
try:
2+
import importlib
3+
if not hasattr(importlib, 'reload'):
4+
raise Exception('wrong importlib')
5+
except:
6+
import imp
7+
importlib = imp
8+
9+
def preload_real_module(moduleName):
10+
# Re-importing the real module at the top level of an override fails after deleting it from
11+
# sys.modules['httplib'] in Python 2. Some interesting issues there ofc, but doing this
12+
# instead works nicely in both Python 2 & 3.
13+
import sys, os
14+
15+
override_path = os.path.dirname(os.path.abspath(__file__))
16+
# print('override path %s' % override_path)
17+
original_sys_path = list(sys.path)
18+
# print('sys_path %s' % original_sys_path)
19+
sys.path = [p for p in sys.path if p != override_path and p != '']
20+
# print('new sys_path %s' % sys.path)
21+
22+
del sys.modules[moduleName]
23+
__import__(moduleName)
24+
25+
sys.path = original_sys_path

overrides/pythonpath/stripe.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
1-
try:
2-
import importlib
3-
if not hasattr(importlib, 'reload'):
4-
raise Exception('wrong importlib')
5-
except:
6-
import imp
7-
importlib = imp
8-
9-
def _load_real_stripe():
10-
# Importing this at the top level breaks after deleting sys.modules['stripe'],
11-
# in Python 2. Some interesting internal issues there, but this works in Py 2 & 3.
12-
import sys, os
13-
14-
override_path = os.path.dirname(os.path.abspath(__file__))
15-
16-
original_sys_path = list(sys.path)
17-
18-
sys.path = [p for p in sys.path if p != override_path]
19-
del sys.modules['stripe']
20-
import stripe
21-
22-
sys.path = original_sys_path
23-
24-
_load_real_stripe()
1+
from httptoolkit_intercept import preload_real_module
2+
preload_real_module('stripe')
253

264
import stripe, os
275
stripe.ca_bundle_path = os.environ['SSL_CERT_FILE']

0 commit comments

Comments
 (0)