From 542cdb7140a64f8c63ed088b1bdae65d23200525 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Mon, 2 Dec 2019 19:59:36 +0000 Subject: [PATCH] Use plain text message payloads instead of JSON --- http2irc.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/http2irc.py b/http2irc.py index 018e49a..3382c77 100644 --- a/http2irc.py +++ b/http2irc.py @@ -4,7 +4,6 @@ import asyncio import base64 import collections import concurrent.futures -import json import logging import signal import ssl @@ -223,6 +222,7 @@ class IRCClientProtocol(asyncio.Protocol): logging.debug(f'{id(self)}: got message: {message!r}') if message is None: break + #TODO Split if the message is too long. self.unconfirmedMessages.append((channel, message)) self.send(b'PRIVMSG ' + channel.encode('utf-8') + b' :' + message.encode('utf-8')) await asyncio.sleep(1) # Rate limit @@ -339,28 +339,33 @@ class WebServer: await runner.cleanup() async def post(self, request): - logging.info(f'Received request for {request.path!r} with data {await request.read()!r}') + logging.info(f'Received request for {request.path!r}') try: channel, auth = self._paths[request.path] except KeyError: + logging.info(f'Bad request: no path {request.path!r}') raise aiohttp.web.HTTPNotFound() if auth: authHeader = request.headers.get('Authorization') if not authHeader or authHeader != auth: + logging.info(f'Bad request: authentication failed: {authHeader!r} != {auth}') raise aiohttp.web.HTTPForbidden() try: - data = await request.json() - except (aiohttp.ContentTypeError, json.JSONDecodeError) as e: - logging.error(f'Invalid data received: {await request.read()!r}') + message = await request.text() + except Exception as e: + logging.info(f'Bad request: exception while reading request data: {e!s}') + raise aiohttp.web.HTTPBadRequest() # Yes, it's always the client's fault. :-) + logging.debug(f'Request payload: {message!r}') + # Strip optional [CR] LF at the end of the payload + if message.endswith('\r\n'): + message = message[:-2] + elif message.endswith('\n'): + message = message[:-1] + if '\r' in message or '\n' in message: + logging.info('Bad request: linebreaks in message') raise aiohttp.web.HTTPBadRequest() - if 'message' not in data: - logging.error(f'Message missing: {await request.read()!r}') - raise aiohttp.web.HTTPBadRequest() - if '\r' in data['message'] or '\n' in data['message']: - logging.error(f'Linebreaks in message: {await request.read()!r}') - raise aiohttp.web.HTTPBadRequest() - logging.debug(f'Putting message {data["message"]!r} for {channel} into message queue') - self.messageQueue.put_nowait((channel, data['message'])) + logging.debug(f'Putting message {message!r} for {channel} into message queue') + self.messageQueue.put_nowait((channel, message)) raise aiohttp.web.HTTPOk()