From cf2fa1f0c05ea6a26beddd5941c4699038831e9f Mon Sep 17 00:00:00 2001 From: gnat Date: Thu, 24 Oct 2024 14:59:45 -0700 Subject: [PATCH] fix error with response type being passed as response.body; omit requests originating form status.natalieee.net from the log --- src/lib/patchers.py | 2 +- src/lib/request.py | 4 +++- src/lib/router.py | 2 +- src/main.py | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib/patchers.py b/src/lib/patchers.py index c4097d8..4069506 100644 --- a/src/lib/patchers.py +++ b/src/lib/patchers.py @@ -104,7 +104,7 @@ patchers: List[Patcher] = [ lambda response, request: Response( response.code, response.headers, - re.sub(r'sludge', lambda match: 'sludge' + ' (/slʌd͡ʒ/)' if random.randint(0, 5) < 1 else 'sludge', response.body.decode('utf-8')).encode('utf-8') + re.sub(r'sludge', lambda match: 'sludge' + ' (/slʌd͡ʒ/)' if random.randint(0, 5) < 1 else 'sludge', response.body.decode()).encode('utf-8') ) if 'text/html' in response.headers.values() else response, lambda response, request: Response( response.code, diff --git a/src/lib/request.py b/src/lib/request.py index 68728d0..27c2979 100644 --- a/src/lib/request.py +++ b/src/lib/request.py @@ -44,7 +44,9 @@ class Request: body_start = request_str.find('\r\n\r\n') + 4 body = Body(request_bytes[body_start:], headers.get('Content-Type') or 'text/plain') - log.info(f'received request for {path.path} from {headers.get('X-Real-IP')}') + if not 'Nim httpclient' in headers.get('user-agent'): + log.info(f'received request for {path.path} from {headers.get('X-Real-IP')}') + return cls(method, path, version, headers, body) def match(self): diff --git a/src/lib/router.py b/src/lib/router.py index 09356f9..88eba96 100644 --- a/src/lib/router.py +++ b/src/lib/router.py @@ -52,7 +52,7 @@ routes = [ lambda request, *_: Response( ResponseCode.OK, {'Content-Type': 'text/html'}, - ((parse_file('./home.html', dict(prev='\\/')).encode('utf-8') if not 'Nim httpclient' in request.headers.get('user-agent') else error_page(200)) if request.method == Method.GET else ( + ((parse_file('./home.html', dict(prev='\\/')).encode('utf-8') if not 'Nim httpclient' in request.headers.get('user-agent') else error_page(200).body) if request.method == Method.GET else ( [ (lambda form_data: ( (lambda time: ( diff --git a/src/main.py b/src/main.py index e2fa5f5..9b396fc 100644 --- a/src/main.py +++ b/src/main.py @@ -25,7 +25,7 @@ def handle_client(client: socket.socket, addr: Tuple[str, int]) -> None: .execute(request, client, addr) \ .send(client) - log.info('destroy thread') + log.debug('destroy thread') def main() -> None: http_thread = threading.Thread(name='http', target=serve, args=('0.0.0.0', config['http-port'], handle_client))