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