summaryrefslogtreecommitdiff
path: root/day7/http_handler.py
blob: e5a551b5dc4f8723ca373813229b96daede0f7a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from socket import socket, AF_INET, SOCK_STREAM, SHUT_RDWR
from time import strftime, gmtime

from backend import run
from utils import validate_url, parse_cookies, parse_query, parse_headers, add_headers, NOT_FOUND, BAD_REQUEST
from templater import render_template
from config import *
import db


def log_func(func):
    def wrapper(*args, **kwargs):
        address = kwargs['address'] if 'address' in kwargs else args[0]
        method = kwargs['method'] if 'method' in kwargs else args[1]
        url = kwargs['url'] if 'url' in kwargs else args[2]
        http_ver = kwargs['http_ver'] if 'http_ver' in kwargs else args[3]

        response = func(*args, **kwargs)
        status = response.split('\n').pop(0).split()[1]

        log_line = f'{address[0]}: {strftime("[%d %b %Y %H:%M:%S]", gmtime())} "{method} {url} {http_ver}" Status: {status}'
        print(log_line)

        return response

    return wrapper


@log_func
def process_request(address, method, url, http_ver, headers: dict, query):
    if validate_url(url):
        try:
            cookies = parse_cookies(headers['Cookie'])
        except (ValueError, KeyError):
            cookies = {}

        response = run(method, url, cookies, query)
    else:
        response = add_headers(NOT_FOUND, '404 NOT FOUND')

    return response


def get_request(connection):
    buffer = b''
    request = b''
    while not request.endswith(b'\r\n\r\n'):
        data = connection.recv(1024).split(b'\r\n\r\n')
        if len(data) == 1:
            if not data[0]:
                return None
            request += data[0]
        else:
            request += data[0] + b'\r\n\r\n'
            buffer += data[1]

    method, url, http_ver, headers = parse_headers(request.strip().decode())
    if 'Content-Type' in headers and headers['Content-Type'] == 'application/x-www-form-urlencoded':
        length = int(headers['Content-Length']) - len(buffer)
        while length > 0:
            if length < CHUNK:
                data = connection.recv(length)
                length = 0
            else:
                data = connection.recv(CHUNK)
                length -= CHUNK

            buffer += data[0]

    query = parse_query(buffer.strip().decode()) if buffer else {}

    return method, url, http_ver, headers, query


def get_color():
    color = db.get_cookie('bg_color', 'white')
    if color not in ['green', 'white']:
        color = 'white'
    return color


def handle_connection(connection, address):
    try:
        request = get_request(connection)
    except ValueError as e:
        response = add_headers(BAD_REQUEST, render_template(
            TEXT_TEMPLATE_NAME, color=get_color(),
            text=BAD_REQUEST + ('<br>' + str(e) if db.get_config_entry('show_errors') else '')
        ))
        connection.sendall(response.encode())
        return

    if request is None:
        return

    method, url, http_ver, headers, query = request

    response = process_request(address, method, url, http_ver, headers, query)
    connection.sendall(response.encode())


def main(host, port):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.bind((host, port))
    sock.listen(5)
    print(f'* Server started on http://{host}:{port}/')
    print('* Listening to new connections...')

    while True:
        try:
            connection, address = sock.accept()

            handle_connection(connection, address)

            connection.shutdown(SHUT_RDWR)
            connection.close()
            del connection

        except KeyboardInterrupt:
            print('Stopping server...')

            try:
                connection.shutdown(SHUT_RDWR)
                connection.close()
            except UnboundLocalError:
                pass

            sock.shutdown(SHUT_RDWR)
            sock.close()

            print('Server stopped')
            break


if __name__ == '__main__':
    HOST, PORT = ADDR = '0.0.0.0', 4002
    main(HOST, PORT)