Skip to content

Commit 46921d0

Browse files
committed
Complete backwards compatibility:
* Add TLS_MODE to old connect() calls * Provide ip to connect when in HTTP mode
1 parent e6a4c8f commit 46921d0

File tree

7 files changed

+45
-20
lines changed

7 files changed

+45
-20
lines changed

adafruit_requests.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __init__(self, sock, session=None):
100100
http = self._readto(b" ")
101101
if not http:
102102
raise RuntimeError("Unable to read HTTP response.")
103-
self.status_code = int(self._readto(b" "))
103+
self.status_code = int(bytes(self._readto(b" ")))
104104
self.reason = self._readto(b"\r\n")
105105
self._parse_headers()
106106
self._raw = None
@@ -196,7 +196,7 @@ def _readinto(self, buf):
196196
if self._remaining == 0:
197197
self._throw_away(2)
198198
chunk_header = self._readto(b";", b"\r\n")
199-
http_chunk_size = int(chunk_header, 16)
199+
http_chunk_size = int(bytes(chunk_header), 16)
200200
if http_chunk_size == 0:
201201
self._chunked = False
202202
self._parse_headers()
@@ -237,7 +237,7 @@ def close(self):
237237
elif self._chunked:
238238
while True:
239239
chunk_header = self._readto(b";", b"\r\n")
240-
chunk_size = int(chunk_header, 16)
240+
chunk_size = int(bytes(chunk_header), 16)
241241
if chunk_size == 0:
242242
break
243243
self._throw_away(chunk_size + 2)
@@ -391,12 +391,14 @@ def _get_socket(self, host, port, proto, *, timeout=1):
391391
host, port, 0, self._socket_pool.SOCK_STREAM
392392
)[0]
393393
sock = self._socket_pool.socket(addr_info[0], addr_info[1], addr_info[2])
394+
connect_host = addr_info[-1][0]
394395
if proto == "https:":
395396
sock = self._ssl_context.wrap_socket(sock, server_hostname=host)
397+
connect_host = host
396398
sock.settimeout(timeout) # socket read timeout
397399
ok = True
398400
try:
399-
sock.connect((host, port))
401+
sock.connect((connect_host, port))
400402
except MemoryError:
401403
if not any(self._socket_free.items()):
402404
raise
@@ -411,7 +413,7 @@ def _get_socket(self, host, port, proto, *, timeout=1):
411413
if proto == "https:":
412414
sock = self._ssl_context.wrap_socket(sock, server_hostname=host)
413415
sock.settimeout(timeout) # socket read timeout
414-
sock.connect((host, port))
416+
sock.connect((connect_host, port))
415417
self._open_sockets[key] = sock
416418
self._socket_free[sock] = False
417419
return sock
@@ -490,7 +492,7 @@ def request(
490492
socket.send(bytes(data, "utf-8"))
491493

492494
resp = Response(socket, self) # our response
493-
if "location" in resp.headers and not 200 <= resp.status_code <= 299:
495+
if "location" in resp.headers and 300 <= resp.status_code <= 399:
494496
raise NotImplementedError("Redirects not yet supported")
495497

496498
self._last_response = resp
@@ -525,19 +527,31 @@ def delete(self, url, **kw):
525527

526528
_default_session = None # pylint: disable=invalid-name
527529

530+
class _FakeSSLSocket:
531+
def __init__(self, socket, tls_mode):
532+
self._socket = socket
533+
self._mode = tls_mode
534+
self.settimeout = socket.settimeout
535+
self.send = socket.send
536+
self.recv = socket.recv
537+
538+
def connect(self, address):
539+
return self._socket.connect(address, self._mode)
528540

529541
class _FakeSSLContext:
530-
@staticmethod
531-
def wrap_socket(socket, server_hostname=None):
542+
def __init__(self, iface):
543+
self._iface = iface
544+
545+
def wrap_socket(self, socket, server_hostname=None):
532546
"""Return the same socket"""
533547
# pylint: disable=unused-argument
534-
return socket
548+
return _FakeSSLSocket(socket, self._iface.TLS_MODE)
535549

536550

537551
def set_socket(sock, iface=None):
538552
"""Legacy API for setting the socket and network interface. Use a `Session` instead."""
539553
global _default_session # pylint: disable=global-statement,invalid-name
540-
_default_session = Session(sock, _FakeSSLContext())
554+
_default_session = Session(sock, _FakeSSLContext(iface))
541555
if iface:
542556
sock.set_interface(iface)
543557

tests/chunk_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_get_text():
3838
s = adafruit_requests.Session(pool)
3939
r = s.get("http://" + host + path)
4040

41-
sock.connect.assert_called_once_with((host, 80))
41+
sock.connect.assert_called_once_with((ip, 80))
4242
sock.send.assert_has_calls(
4343
[
4444
mock.call(b"GET /testwifi/index.html HTTP/1.1\r\n"),

tests/header_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_json():
2020
headers = {"user-agent": "blinka/1.0.0"}
2121
r = s.get("http://" + host + "/get", headers=headers)
2222

23-
sock.connect.assert_called_once_with((host, 80))
23+
sock.connect.assert_called_once_with((ip, 80))
2424
sent = b"".join(sent).lower()
2525
assert b"user-agent: blinka/1.0.0\r\n" in sent
2626
# The current implementation sends two user agents. Fix it, and uncomment below.

tests/legacy_test.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,21 @@ def test_get_json():
2020
adafruit_requests.set_socket(mocket, mocket.interface)
2121
r = adafruit_requests.get("http://" + host + "/get")
2222

23-
sock.connect.assert_called_once_with((host, 80))
23+
sock.connect.assert_called_once_with((ip, 80))
2424
assert r.json() == response
2525
r.close()
2626

27+
def test_tls_mode():
28+
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
29+
sock = mocket.Mocket(headers + encoded)
30+
mocket.socket.return_value = sock
31+
32+
adafruit_requests.set_socket(mocket, mocket.interface)
33+
r = adafruit_requests.get("https://" + host + "/get")
34+
35+
sock.connect.assert_called_once_with((host, 443), mocket.interface.TLS_MODE)
36+
assert r.json() == response
37+
r.close()
2738

2839
def test_post_string():
2940
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
@@ -33,6 +44,6 @@ def test_post_string():
3344
adafruit_requests.set_socket(mocket, mocket.interface)
3445
data = "31F"
3546
r = adafruit_requests.post("http://" + host + "/post", data=data)
36-
sock.connect.assert_called_once_with((host, 80))
47+
sock.connect.assert_called_once_with((ip, 80))
3748
sock.send.assert_called_with(b"31F")
3849
r.close()

tests/parse_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ def test_json():
2424

2525
s = adafruit_requests.Session(pool)
2626
r = s.get("http://" + host + "/get")
27-
sock.connect.assert_called_once_with((host, 80))
27+
sock.connect.assert_called_once_with((ip, 80))
2828
assert r.json() == response

tests/post_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_method():
2020

2121
s = adafruit_requests.Session(pool)
2222
r = s.post("http://" + host + "/post")
23-
sock.connect.assert_called_once_with((host, 80))
23+
sock.connect.assert_called_once_with((ip, 80))
2424
sock.send.assert_has_calls(
2525
[mock.call(b"POST /post HTTP/1.1\r\n"), mock.call(b"Host: httpbin.org\r\n")]
2626
)
@@ -35,7 +35,7 @@ def test_string():
3535
s = adafruit_requests.Session(pool)
3636
data = "31F"
3737
r = s.post("http://" + host + "/post", data=data)
38-
sock.connect.assert_called_once_with((host, 80))
38+
sock.connect.assert_called_once_with((ip, 80))
3939
sock.send.assert_called_with(b"31F")
4040

4141

@@ -48,7 +48,7 @@ def test_form():
4848
s = adafruit_requests.Session(pool)
4949
data = {"Date": "July 25, 2019"}
5050
r = s.post("http://" + host + "/post", data=data)
51-
sock.connect.assert_called_once_with((host, 80))
51+
sock.connect.assert_called_once_with((ip, 80))
5252
sock.send.assert_called_with(b"Date=July 25, 2019")
5353

5454

@@ -61,5 +61,5 @@ def test_json():
6161
s = adafruit_requests.Session(pool)
6262
json_data = {"Date": "July 25, 2019"}
6363
r = s.post("http://" + host + "/post", json=json_data)
64-
sock.connect.assert_called_once_with((host, 80))
64+
sock.connect.assert_called_once_with((ip, 80))
6565
sock.send.assert_called_with(b'{"Date": "July 25, 2019"}')

tests/protocol_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_get_http_text():
5353
s = adafruit_requests.Session(pool)
5454
r = s.get("http://" + host + path)
5555

56-
sock.connect.assert_called_once_with((host, 80))
56+
sock.connect.assert_called_once_with((ip, 80))
5757
sock.send.assert_has_calls(
5858
[
5959
mock.call(b"GET /testwifi/index.html HTTP/1.1\r\n"),

0 commit comments

Comments
 (0)