Skip to content

Commit b715ee0

Browse files
author
Paul Sokolovsky
committed
urllib.urequest: 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 cad7729 commit b715ee0

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

urllib.urequest/urllib/urequest.py

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,44 @@ def urlopen(url, data=None, method="GET"):
2222

2323
ai = usocket.getaddrinfo(host, port)
2424
addr = ai[0][4]
25+
2526
s = usocket.socket()
26-
s.connect(addr)
27-
if proto == "https:":
28-
s = ussl.wrap_socket(s, server_hostname=host)
29-
30-
s.write(method)
31-
s.write(b" /")
32-
s.write(path)
33-
s.write(b" HTTP/1.0\r\nHost: ")
34-
s.write(host)
35-
s.write(b"\r\n")
36-
37-
if data:
38-
s.write(b"Content-Length: ")
39-
s.write(str(len(data)))
27+
try:
28+
s.connect(addr)
29+
if proto == "https:":
30+
s = ussl.wrap_socket(s, server_hostname=host)
31+
32+
s.write(method)
33+
s.write(b" /")
34+
s.write(path)
35+
s.write(b" HTTP/1.0\r\nHost: ")
36+
s.write(host)
4037
s.write(b"\r\n")
41-
s.write(b"\r\n")
42-
if data:
43-
s.write(data)
44-
45-
l = s.readline()
46-
protover, status, msg = l.split(None, 2)
47-
status = int(status)
48-
#print(protover, status, msg)
49-
while True:
38+
39+
if data:
40+
s.write(b"Content-Length: ")
41+
s.write(str(len(data)))
42+
s.write(b"\r\n")
43+
s.write(b"\r\n")
44+
if data:
45+
s.write(data)
46+
5047
l = s.readline()
51-
if not l or l == b"\r\n":
52-
break
53-
#print(l)
54-
if l.startswith(b"Transfer-Encoding:"):
55-
if b"chunked" in l:
56-
raise ValueError("Unsupported " + l)
57-
elif l.startswith(b"Location:"):
58-
raise NotImplementedError("Redirects not yet supported")
48+
protover, status, msg = l.split(None, 2)
49+
status = int(status)
50+
#print(protover, status, msg)
51+
while True:
52+
l = s.readline()
53+
if not l or l == b"\r\n":
54+
break
55+
#print(l)
56+
if l.startswith(b"Transfer-Encoding:"):
57+
if b"chunked" in l:
58+
raise ValueError("Unsupported " + l)
59+
elif l.startswith(b"Location:"):
60+
raise NotImplementedError("Redirects not yet supported")
61+
except OSError:
62+
s.close()
63+
raise
5964

6065
return s

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