kopia lustrzana https://github.com/micropython/micropython-lib
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.pull/208/head
rodzic
cad77291d5
commit
b715ee0cb8
|
@ -22,39 +22,44 @@ def urlopen(url, data=None, method="GET"):
|
||||||
|
|
||||||
ai = usocket.getaddrinfo(host, port)
|
ai = usocket.getaddrinfo(host, port)
|
||||||
addr = ai[0][4]
|
addr = ai[0][4]
|
||||||
|
|
||||||
s = usocket.socket()
|
s = usocket.socket()
|
||||||
s.connect(addr)
|
try:
|
||||||
if proto == "https:":
|
s.connect(addr)
|
||||||
s = ussl.wrap_socket(s, server_hostname=host)
|
if proto == "https:":
|
||||||
|
s = ussl.wrap_socket(s, server_hostname=host)
|
||||||
|
|
||||||
s.write(method)
|
s.write(method)
|
||||||
s.write(b" /")
|
s.write(b" /")
|
||||||
s.write(path)
|
s.write(path)
|
||||||
s.write(b" HTTP/1.0\r\nHost: ")
|
s.write(b" HTTP/1.0\r\nHost: ")
|
||||||
s.write(host)
|
s.write(host)
|
||||||
s.write(b"\r\n")
|
|
||||||
|
|
||||||
if data:
|
|
||||||
s.write(b"Content-Length: ")
|
|
||||||
s.write(str(len(data)))
|
|
||||||
s.write(b"\r\n")
|
s.write(b"\r\n")
|
||||||
s.write(b"\r\n")
|
|
||||||
if data:
|
|
||||||
s.write(data)
|
|
||||||
|
|
||||||
l = s.readline()
|
if data:
|
||||||
protover, status, msg = l.split(None, 2)
|
s.write(b"Content-Length: ")
|
||||||
status = int(status)
|
s.write(str(len(data)))
|
||||||
#print(protover, status, msg)
|
s.write(b"\r\n")
|
||||||
while True:
|
s.write(b"\r\n")
|
||||||
|
if data:
|
||||||
|
s.write(data)
|
||||||
|
|
||||||
l = s.readline()
|
l = s.readline()
|
||||||
if not l or l == b"\r\n":
|
protover, status, msg = l.split(None, 2)
|
||||||
break
|
status = int(status)
|
||||||
#print(l)
|
#print(protover, status, msg)
|
||||||
if l.startswith(b"Transfer-Encoding:"):
|
while True:
|
||||||
if b"chunked" in l:
|
l = s.readline()
|
||||||
raise ValueError("Unsupported " + l)
|
if not l or l == b"\r\n":
|
||||||
elif l.startswith(b"Location:"):
|
break
|
||||||
raise NotImplementedError("Redirects not yet supported")
|
#print(l)
|
||||||
|
if l.startswith(b"Transfer-Encoding:"):
|
||||||
|
if b"chunked" in l:
|
||||||
|
raise ValueError("Unsupported " + l)
|
||||||
|
elif l.startswith(b"Location:"):
|
||||||
|
raise NotImplementedError("Redirects not yet supported")
|
||||||
|
except OSError:
|
||||||
|
s.close()
|
||||||
|
raise
|
||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
Ładowanie…
Reference in New Issue