Parallerize request processing

This commit is contained in:
Juhani Krekelä 2021-10-15 12:50:33 +03:00
parent c29417f726
commit 8cbaa722b1
1 changed files with 15 additions and 3 deletions

View File

@ -122,6 +122,7 @@ import socket
import ssl
import sys
import time
import threading
def connect(host, port):
try:
@ -229,6 +230,8 @@ def proxy(sock, host):
# Remove headers that don't need forwarding
headers = dict((key, value) for key, value in headers.items() if not key.startswith(b'proxy-'))
# TODO: connection: close
# Split url into its constituents
fields = url.split(b'://', 1)
if len(fields) != 2 or fields[0] not in (b'http', b'https'):
@ -373,6 +376,7 @@ def proxy(sock, host):
del request_data
# TODO: Timeout
# TODO: Un-https links
print('', file=sys.stderr)
while True:
@ -388,6 +392,16 @@ def proxy(sock, host):
remote_sock.close()
class ProxyThread(threading.Thread):
def __init__(self, sock, host):
self.sock = sock
self.host = host
super().__init__()
def run(self):
proxy(self.sock, self.host)
self.sock.close()
def listen(port):
sockets = []
for res in socket.getaddrinfo(None, port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
@ -430,10 +444,8 @@ def listen(port):
while True:
for fd, _ in listening.poll():
# TODO: Threads
conn, (host, *_) = sock_by_fd[fd].accept()
proxy(conn, host)
conn.close()
ProxyThread(conn, host).start()
if __name__ == '__main__':
if len(sys.argv) != 3: