2023-12-29 02:13:10 +03:00
|
|
|
import threading
|
2023-12-22 02:48:34 +03:00
|
|
|
import socket
|
|
|
|
import logging
|
2024-06-04 17:25:11 +03:00
|
|
|
import os
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
import config
|
|
|
|
|
2024-02-25 01:42:17 +03:00
|
|
|
from .file_read import fileiter
|
2024-02-26 02:21:00 +03:00
|
|
|
from .response import Response
|
2024-09-15 15:11:51 +03:00
|
|
|
from .common import HandlerType, HTTPMethod
|
2024-02-25 01:42:17 +03:00
|
|
|
|
2023-12-22 02:48:34 +03:00
|
|
|
|
|
|
|
MAX_REQLINE = 64 * 1024
|
|
|
|
|
2024-07-03 14:06:19 +03:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
class HTTPHandler:
|
|
|
|
def __init__(self, conn, addr):
|
|
|
|
self.conn = conn
|
|
|
|
self.addr = addr
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
self.read_fb = conn.makefile("rb")
|
|
|
|
self.write_fb = conn.makefile("wb")
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2024-09-15 15:11:51 +03:00
|
|
|
self.http_method = ""
|
2024-02-26 02:21:00 +03:00
|
|
|
self.http_address = ""
|
|
|
|
self.http_get_request = {}
|
|
|
|
self.http_headers = {}
|
|
|
|
|
|
|
|
self.data = b""
|
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
self.pre_handle_connection()
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
def pre_handle_connection(self):
|
2024-02-26 02:21:00 +03:00
|
|
|
"""
|
2024-09-15 15:11:51 +03:00
|
|
|
Chain of handle first line of HTTP request
|
2024-02-26 02:21:00 +03:00
|
|
|
"""
|
2023-12-29 02:13:10 +03:00
|
|
|
raw = self.read_fb.readline(MAX_REQLINE + 1)
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
if len(raw) > MAX_REQLINE:
|
2024-07-03 14:06:19 +03:00
|
|
|
log.debug("Request line too long")
|
2023-12-29 02:13:10 +03:00
|
|
|
self.conn.send(b"")
|
|
|
|
self.conn.close()
|
|
|
|
return
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
req_line = raw.decode().rstrip("\r\n")
|
2024-09-15 15:11:51 +03:00
|
|
|
# method, address, protocol = req_line_splited
|
2023-12-29 02:13:10 +03:00
|
|
|
req_line_splited = req_line.split(" ")
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
if len(req_line_splited) != 3:
|
2024-02-26 02:21:00 +03:00
|
|
|
if len(req_line_splited) > 3:
|
2024-07-03 14:06:19 +03:00
|
|
|
log.debug("Request head too long")
|
2024-02-26 02:21:00 +03:00
|
|
|
else:
|
2024-07-03 14:06:19 +03:00
|
|
|
log.debug("Request head too small")
|
2024-09-15 15:11:51 +03:00
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
self.conn.send(b"")
|
|
|
|
self.conn.close()
|
|
|
|
return
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2024-07-03 14:06:19 +03:00
|
|
|
if (req_line_splited[2] == "HTTP/1.0" or
|
|
|
|
req_line_splited[2] == "HTTP/1.1"):
|
2024-09-15 15:11:51 +03:00
|
|
|
|
|
|
|
http_method = req_line_splited[0]
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.conn.send(b"")
|
|
|
|
self.conn.close()
|
|
|
|
return
|
|
|
|
|
|
|
|
if http_method == "GET":
|
|
|
|
self.http_method = HTTPMethod.GET
|
|
|
|
elif http_method == "POST":
|
|
|
|
self.http_method = HTTPMethod.POST
|
|
|
|
elif http_method == "PUT":
|
|
|
|
self.http_method = HTTPMethod.PUT
|
|
|
|
elif http_method == "HEAD":
|
|
|
|
self.http_method = HTTPMethod.HEAD
|
|
|
|
elif http_method == "DELETE":
|
|
|
|
self.http_method = HTTPMethod.DELETE
|
|
|
|
elif http_method == "OPTIONS":
|
|
|
|
self.http_method = HTTPMethod.OPTIONS
|
|
|
|
elif http_method == "TRACE":
|
|
|
|
self.http_method = HTTPMethod.TRACE
|
|
|
|
elif http_method == "PATCH":
|
|
|
|
self.http_method = HTTPMethod.PATCH
|
2023-12-29 02:13:10 +03:00
|
|
|
else:
|
|
|
|
self.conn.send(b"")
|
|
|
|
self.conn.close()
|
2024-02-26 02:21:00 +03:00
|
|
|
return
|
|
|
|
|
2024-09-15 15:11:51 +03:00
|
|
|
if (self.http_method == HTTPMethod.GET and
|
|
|
|
req_line_splited[1].find("?") >= 0):
|
|
|
|
|
2024-02-26 02:21:00 +03:00
|
|
|
addr, get_req = req_line_splited[1].split("?", 1)
|
|
|
|
self.http_address = addr
|
|
|
|
|
|
|
|
for param in get_req.split("&"):
|
|
|
|
if len(param) == 0:
|
|
|
|
continue
|
2024-09-15 15:11:51 +03:00
|
|
|
|
2024-02-26 02:21:00 +03:00
|
|
|
elif param.find("=") >= 0:
|
|
|
|
name, val = param.split("=", 1)
|
2024-09-15 15:11:51 +03:00
|
|
|
|
2024-02-26 02:21:00 +03:00
|
|
|
else:
|
|
|
|
name, val = (param, "")
|
|
|
|
|
|
|
|
self.http_get_request.update({name: val})
|
|
|
|
else:
|
|
|
|
self.http_address = req_line_splited[1]
|
|
|
|
self.http_get_request = {}
|
|
|
|
|
|
|
|
self.http_header_handle()
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
def http_header_handle(self):
|
2024-02-26 02:21:00 +03:00
|
|
|
"""
|
|
|
|
Chain of handle headers in HTTP request
|
|
|
|
"""
|
2023-12-22 02:48:34 +03:00
|
|
|
while True:
|
2023-12-29 02:13:10 +03:00
|
|
|
raw = self.read_fb.readline(MAX_REQLINE + 1)
|
|
|
|
|
|
|
|
if len(raw) > MAX_REQLINE:
|
2024-07-03 14:06:19 +03:00
|
|
|
log.debug("Request header line too long")
|
2023-12-29 02:13:10 +03:00
|
|
|
self.conn.send(b"")
|
|
|
|
self.conn.close()
|
|
|
|
return
|
|
|
|
|
|
|
|
if raw == b"":
|
|
|
|
self.conn.send(b"")
|
|
|
|
self.conn.close()
|
|
|
|
return
|
|
|
|
|
|
|
|
if raw == b"\r\n":
|
|
|
|
self.http_data_handle()
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
decoded_data = raw.decode("UTF-8").rstrip("\r\n")
|
|
|
|
decoded_data_split = decoded_data.split(":", 1)
|
2024-07-03 14:06:19 +03:00
|
|
|
self.http_headers.update(
|
|
|
|
{
|
|
|
|
decoded_data_split[0]: decoded_data_split[1].strip(" ")
|
|
|
|
})
|
2023-12-29 02:13:10 +03:00
|
|
|
|
|
|
|
def http_data_handle(self):
|
2024-02-26 02:21:00 +03:00
|
|
|
"""
|
|
|
|
Chain of receive data partitions HTTP request
|
2023-12-29 02:13:10 +03:00
|
|
|
"""
|
|
|
|
|
2024-09-15 15:11:51 +03:00
|
|
|
if "Content-Length" in self.http_headers:
|
|
|
|
# TODO: if content-length biggest of MAX_REQLINE - partition receive
|
|
|
|
log.debug("Request has contain body, try to read")
|
|
|
|
log.debug("Check Content-Length value type:")
|
2023-12-29 02:13:10 +03:00
|
|
|
|
2024-09-15 15:11:51 +03:00
|
|
|
if self.http_headers["Content-Length"].isdigit():
|
|
|
|
log.debug(" Pass")
|
|
|
|
bytes_to_receive = int(self.http_headers["Content-Length"])
|
|
|
|
else:
|
|
|
|
log.error(" Content-Length is not integer")
|
|
|
|
self.conn.send(b"")
|
|
|
|
self.conn.close()
|
|
|
|
return
|
2023-12-29 02:13:10 +03:00
|
|
|
|
2024-09-15 15:11:51 +03:00
|
|
|
log.debug("Want to receive {} bytes from client".format(bytes_to_receive))
|
|
|
|
self.data = self.read_fb.read(bytes_to_receive)
|
2023-12-29 02:13:10 +03:00
|
|
|
|
2024-09-15 15:11:51 +03:00
|
|
|
log.debug("Request method: %s" % self.http_method)
|
2024-07-03 14:06:19 +03:00
|
|
|
log.debug("Requested address: %s" % self.http_address)
|
|
|
|
log.debug("Request headers: %s" % self.http_headers)
|
|
|
|
log.debug("Request in GET after ?: %s" % self.http_get_request)
|
|
|
|
log.debug("Data: %s" % self.data)
|
2023-12-29 02:13:10 +03:00
|
|
|
|
|
|
|
self.send_form_data()
|
|
|
|
|
|
|
|
def send_form_data(self):
|
2024-02-26 02:21:00 +03:00
|
|
|
"""
|
|
|
|
Chain of handle, pack & send data to client
|
|
|
|
"""
|
2024-02-25 01:42:17 +03:00
|
|
|
found = False
|
2023-12-30 15:45:47 +03:00
|
|
|
|
2024-09-15 15:11:51 +03:00
|
|
|
for path in config.urls:
|
|
|
|
if self.http_address == path.address:
|
|
|
|
if self.http_method in path.methods:
|
2024-02-25 01:42:17 +03:00
|
|
|
found = True
|
|
|
|
|
2024-09-15 15:11:51 +03:00
|
|
|
if path.handler_type == HandlerType.STATIC_FILE:
|
|
|
|
log.debug("Address associated with static file on path {}".format(path.link))
|
|
|
|
r = Response(data=fileiter(path.link))
|
2024-02-25 01:42:17 +03:00
|
|
|
|
|
|
|
for i in r:
|
|
|
|
self.write_fb.write(i)
|
|
|
|
self.write_fb.flush()
|
2024-02-26 02:21:00 +03:00
|
|
|
|
2024-09-15 15:11:51 +03:00
|
|
|
elif path.handler_type == HandlerType.FUNCTION:
|
|
|
|
log.debug("Address associated with function")
|
|
|
|
func = path.link
|
2024-02-26 02:21:00 +03:00
|
|
|
|
|
|
|
if func.__code__.co_argcount == 0:
|
2024-09-15 15:11:51 +03:00
|
|
|
func_return = func()
|
2024-02-26 02:21:00 +03:00
|
|
|
|
|
|
|
elif func.__code__.co_argcount == 1:
|
2024-09-15 15:11:51 +03:00
|
|
|
func_return = func(self.http_headers)
|
2024-02-26 02:21:00 +03:00
|
|
|
|
|
|
|
elif func.__code__.co_argcount == 2:
|
2024-09-15 15:11:51 +03:00
|
|
|
func_return = func(self.http_headers, self.http_get_request)
|
|
|
|
|
|
|
|
elif func.__code__.co_argcount == 3:
|
|
|
|
func_return = func(self.http_headers, self.http_get_request, self.data)
|
2024-02-26 02:21:00 +03:00
|
|
|
|
2024-09-15 15:11:51 +03:00
|
|
|
for i in func_return:
|
2024-02-26 02:21:00 +03:00
|
|
|
self.write_fb.write(i)
|
|
|
|
self.write_fb.flush()
|
|
|
|
|
2024-02-25 01:42:17 +03:00
|
|
|
else:
|
2024-07-03 14:06:19 +03:00
|
|
|
log.warning("Address configured on server, but type not allowed URL type in URLs list")
|
2024-02-26 02:21:00 +03:00
|
|
|
r = Response(status_code=404, data=b"Address configured on server, but type not allowed URL type in URLs list")
|
2024-02-25 01:42:17 +03:00
|
|
|
|
|
|
|
for i in r:
|
|
|
|
self.write_fb.write(i)
|
|
|
|
self.write_fb.flush()
|
|
|
|
|
|
|
|
if not found:
|
|
|
|
r = Response(status_code=404, data=b"Not found!")
|
2023-12-30 15:45:47 +03:00
|
|
|
|
2024-02-25 01:42:17 +03:00
|
|
|
for i in r:
|
|
|
|
self.write_fb.write(i)
|
|
|
|
self.write_fb.flush()
|
2023-12-29 02:13:10 +03:00
|
|
|
|
2023-12-30 15:45:47 +03:00
|
|
|
self.write_fb.close()
|
|
|
|
self.read_fb.close()
|
|
|
|
self.conn.close()
|
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
|
2024-07-03 14:06:19 +03:00
|
|
|
def start_http_server():
|
2023-12-29 02:13:10 +03:00
|
|
|
try:
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as main_server_socket:
|
|
|
|
main_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
|
|
|
|
main_server_socket.bind(("0.0.0.0", config.SETUP["server"]["port"]))
|
|
|
|
main_server_socket.listen()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
conn, addr = main_server_socket.accept()
|
2024-07-03 14:06:19 +03:00
|
|
|
log.info("Accept connection from %s", addr[0])
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
thr_serv_conn = threading.Thread(target=HTTPHandler, args=(conn, addr,))
|
|
|
|
thr_serv_conn.run()
|
2023-12-22 02:48:34 +03:00
|
|
|
|
2023-12-29 02:13:10 +03:00
|
|
|
except KeyboardInterrupt:
|
2024-07-03 14:06:19 +03:00
|
|
|
log.info("Server get keyboard interrupt. Initialize server to stop")
|
|
|
|
log.info("==================== End of log ====================")
|