From a21b87dd00cf11ab93faef641547412bfdcfed8e Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Sat, 9 Oct 2021 01:53:41 +0000 Subject: [PATCH] Refactor auth/path handling in preparation for GET stream --- http2irc.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/http2irc.py b/http2irc.py index c71e082..03f6ad7 100644 --- a/http2irc.py +++ b/http2irc.py @@ -804,7 +804,7 @@ class WebServer: self._app = aiohttp.web.Application() self._app.add_routes([ aiohttp.web.get('/status', self.get_status), - aiohttp.web.post('/{path:.+}', self.post) + aiohttp.web.post('/{path:.+}', functools.partial(self._path_request, func = self.post)), ]) self.update_config(config) @@ -833,18 +833,22 @@ class WebServer: self.logger.info(f'Received request {id(request)} from {request.remote!r} for {request.path!r}') return (aiohttp.web.Response if (self.ircClient.lastRecvTime or 0) > time.time() - 600 else aiohttp.web.HTTPInternalServerError)() - async def post(self, request): - self.logger.info(f'Received request {id(request)} from {request.remote!r} for {request.path!r} with body {(await request.read())!r}') + async def _path_request(self, request, func): + self.logger.info(f'Received request {id(request)} from {request.remote!r} for {request.method} {request.path!r} with body {(await request.read())!r}') try: - channel, auth, module, moduleargs, overlongmode = self._paths[request.path] + pathConfig = self._paths[request.path] except KeyError: self.logger.info(f'Bad request {id(request)}: no path {request.path!r}') raise aiohttp.web.HTTPNotFound() + auth = pathConfig[1] if auth: authHeader = request.headers.get('Authorization') if not authHeader or authHeader != auth: self.logger.info(f'Bad request {id(request)}: authentication failed: {authHeader!r} != {auth}') raise aiohttp.web.HTTPForbidden() + return (await func(request, *pathConfig)) + + async def post(self, request, channel, auth, module, moduleargs, overlongmode): if module is not None: self.logger.debug(f'Processing request {id(request)} using {module!r}') try: