Compare commits

...

3 Commits

Author SHA1 Message Date
Juhani Krekelä e186c7a85d Add 1 min timeout for no new data transferred 2021-10-15 13:06:56 +03:00
Juhani Krekelä 81fb82479a Fix logic for separating port and host for IPv6 literal addresses 2021-10-15 13:05:43 +03:00
Juhani Krekelä e854fb1ad3 Don't pass through connection: keep-alive 2021-10-15 12:56:58 +03:00
1 changed files with 9 additions and 7 deletions

View File

@ -227,10 +227,10 @@ def proxy(sock, host):
del username del username
del password del password
# Remove headers that don't need forwarding # Remove headers that don't need forwarding or are overwritten
headers = dict((key, value) for key, value in headers.items() if not key.startswith(b'proxy-')) headers = dict((key, value) for key, value in headers.items() if not key.startswith(b'proxy-') and not key in (b'connection', b'keep-alive'))
# TODO: connection: close headers[b'connection'] = b' close'
# Split url into its constituents # Split url into its constituents
fields = url.split(b'://', 1) fields = url.split(b'://', 1)
@ -245,7 +245,7 @@ def proxy(sock, host):
elif b':' not in remote_host: elif b':' not in remote_host:
port = None port = None
else: else:
remote_host, port = remote_host.rsplit(b':') remote_host, port = remote_host.rsplit(b':', 1)
try: try:
port = int(port) port = int(port)
if port < 1 or port > 0xffff: raise ValueError if port < 1 or port > 0xffff: raise ValueError
@ -376,18 +376,20 @@ def proxy(sock, host):
del request_data del request_data
# TODO: Timeout
# TODO: Un-https links # TODO: Un-https links
# TODO: Keep sending request body, if any
print('', file=sys.stderr) print('', file=sys.stderr)
sock.settimeout(60)
remote_sock.settimeout(60)
while True: while True:
try: try:
data = remote_sock.recv(1024) data = remote_sock.recv(1024)
except ConnectionResetError: except (ConnectionResetError, socket.timeout):
return return
if data == b'': break if data == b'': break
try: try:
sock.sendall(data) sock.sendall(data)
except (ConnectionResetError, BrokenPipeError): except (ConnectionResetError, BrokenPipeError, socket.timeout):
break break
remote_sock.close() remote_sock.close()