kopia lustrzana https://github.com/micropython/micropython-lib
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.pull/208/head
rodzic
7a469b229e
commit
586ae64cb0
|
@ -50,43 +50,48 @@ def request(method, url, data=None, json=None, headers={}, stream=None):
|
||||||
|
|
||||||
ai = usocket.getaddrinfo(host, port)
|
ai = usocket.getaddrinfo(host, port)
|
||||||
addr = ai[0][-1]
|
addr = ai[0][-1]
|
||||||
s = usocket.socket()
|
|
||||||
s.connect(addr)
|
|
||||||
if proto == "https:":
|
|
||||||
s = ussl.wrap_socket(s, server_hostname=host)
|
|
||||||
s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
|
|
||||||
if not "Host" in headers:
|
|
||||||
s.write(b"Host: %s\r\n" % host)
|
|
||||||
# Iterate over keys to avoid tuple alloc
|
|
||||||
for k in headers:
|
|
||||||
s.write(k)
|
|
||||||
s.write(b": ")
|
|
||||||
s.write(headers[k])
|
|
||||||
s.write(b"\r\n")
|
|
||||||
if json is not None:
|
|
||||||
assert data is None
|
|
||||||
import ujson
|
|
||||||
data = ujson.dumps(json)
|
|
||||||
if data:
|
|
||||||
s.write(b"Content-Length: %d\r\n" % len(data))
|
|
||||||
s.write(b"\r\n")
|
|
||||||
if data:
|
|
||||||
s.write(data)
|
|
||||||
|
|
||||||
l = s.readline()
|
s = usocket.socket()
|
||||||
protover, status, msg = l.split(None, 2)
|
try:
|
||||||
status = int(status)
|
s.connect(addr)
|
||||||
#print(protover, status, msg)
|
if proto == "https:":
|
||||||
while True:
|
s = ussl.wrap_socket(s, server_hostname=host)
|
||||||
|
s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
|
||||||
|
if not "Host" in headers:
|
||||||
|
s.write(b"Host: %s\r\n" % host)
|
||||||
|
# Iterate over keys to avoid tuple alloc
|
||||||
|
for k in headers:
|
||||||
|
s.write(k)
|
||||||
|
s.write(b": ")
|
||||||
|
s.write(headers[k])
|
||||||
|
s.write(b"\r\n")
|
||||||
|
if json is not None:
|
||||||
|
assert data is None
|
||||||
|
import ujson
|
||||||
|
data = ujson.dumps(json)
|
||||||
|
if data:
|
||||||
|
s.write(b"Content-Length: %d\r\n" % len(data))
|
||||||
|
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:") and not 200 <= status <= 299:
|
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:") and not 200 <= status <= 299:
|
||||||
|
raise NotImplementedError("Redirects not yet supported")
|
||||||
|
except OSError:
|
||||||
|
s.close()
|
||||||
|
raise
|
||||||
|
|
||||||
resp = Response(s)
|
resp = Response(s)
|
||||||
resp.status_code = status
|
resp.status_code = status
|
||||||
|
|
Ładowanie…
Reference in New Issue