Skip to content

Commit 586ae64

Browse files
author
Paul Sokolovsky
committed
urequests: If error happens while parsing response headers, close socket.
Because otherwise, user doesn't get any response object, so cannot close socket, and it leaks.
1 parent 7a469b2 commit 586ae64

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

urequests/urequests.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,43 +50,48 @@ def request(method, url, data=None, json=None, headers={}, stream=None):
5050

5151
ai = usocket.getaddrinfo(host, port)
5252
addr = ai[0][-1]
53+
5354
s = usocket.socket()
54-
s.connect(addr)
55-
if proto == "https:":
56-
s = ussl.wrap_socket(s, server_hostname=host)
57-
s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
58-
if not "Host" in headers:
59-
s.write(b"Host: %s\r\n" % host)
60-
# Iterate over keys to avoid tuple alloc
61-
for k in headers:
62-
s.write(k)
63-
s.write(b": ")
64-
s.write(headers[k])
55+
try:
56+
s.connect(addr)
57+
if proto == "https:":
58+
s = ussl.wrap_socket(s, server_hostname=host)
59+
s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
60+
if not "Host" in headers:
61+
s.write(b"Host: %s\r\n" % host)
62+
# Iterate over keys to avoid tuple alloc
63+
for k in headers:
64+
s.write(k)
65+
s.write(b": ")
66+
s.write(headers[k])
67+
s.write(b"\r\n")
68+
if json is not None:
69+
assert data is None
70+
import ujson
71+
data = ujson.dumps(json)
72+
if data:
73+
s.write(b"Content-Length: %d\r\n" % len(data))
6574
s.write(b"\r\n")
66-
if json is not None:
67-
assert data is None
68-
import ujson
69-
data = ujson.dumps(json)
70-
if data:
71-
s.write(b"Content-Length: %d\r\n" % len(data))
72-
s.write(b"\r\n")
73-
if data:
74-
s.write(data)
75-
76-
l = s.readline()
77-
protover, status, msg = l.split(None, 2)
78-
status = int(status)
79-
#print(protover, status, msg)
80-
while True:
75+
if data:
76+
s.write(data)
77+
8178
l = s.readline()
82-
if not l or l == b"\r\n":
83-
break
84-
#print(l)
85-
if l.startswith(b"Transfer-Encoding:"):
86-
if b"chunked" in l:
87-
raise ValueError("Unsupported " + l)
88-
elif l.startswith(b"Location:") and not 200 <= status <= 299:
89-
raise NotImplementedError("Redirects not yet supported")
79+
protover, status, msg = l.split(None, 2)
80+
status = int(status)
81+
#print(protover, status, msg)
82+
while True:
83+
l = s.readline()
84+
if not l or l == b"\r\n":
85+
break
86+
#print(l)
87+
if l.startswith(b"Transfer-Encoding:"):
88+
if b"chunked" in l:
89+
raise ValueError("Unsupported " + l)
90+
elif l.startswith(b"Location:") and not 200 <= status <= 299:
91+
raise NotImplementedError("Redirects not yet supported")
92+
except OSError:
93+
s.close()
94+
raise
9095

9196
resp = Response(s)
9297
resp.status_code = status

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy