From 8f4e6b947ba07cc89138f6d81842a281566e13b0 Mon Sep 17 00:00:00 2001 From: Xavier Payn Date: Wed, 6 Oct 2021 12:32:40 +0200 Subject: [PATCH] Fix unhandled api_gateway_base_path in AwsHttpGateway (#204) Co-authored-by: Xavier Payn --- mangum/handlers/abstract_handler.py | 4 +++- mangum/handlers/aws_api_gateway.py | 15 ++++++++++----- mangum/handlers/aws_http_gateway.py | 6 ++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/mangum/handlers/abstract_handler.py b/mangum/handlers/abstract_handler.py index 96ef1176..25c294d9 100644 --- a/mangum/handlers/abstract_handler.py +++ b/mangum/handlers/abstract_handler.py @@ -101,7 +101,9 @@ def from_trigger( if "version" in trigger_event and "requestContext" in trigger_event: from . import AwsHttpGateway - return AwsHttpGateway(trigger_event, trigger_context, **kwargs) + return AwsHttpGateway( + trigger_event, trigger_context, **kwargs # type: ignore + ) if "resource" in trigger_event: from . import AwsApiGateway diff --git a/mangum/handlers/aws_api_gateway.py b/mangum/handlers/aws_api_gateway.py index f6e05ae6..eb4a51f6 100644 --- a/mangum/handlers/aws_api_gateway.py +++ b/mangum/handlers/aws_api_gateway.py @@ -74,11 +74,8 @@ def request(self) -> Request: if not path: path = "/" - elif self.api_gateway_base_path and self.api_gateway_base_path != "/": - if not self.api_gateway_base_path.startswith("/"): - self.api_gateway_base_path = f"/{self.api_gateway_base_path}" - if path.startswith(self.api_gateway_base_path): - path = path[len(self.api_gateway_base_path) :] + else: + path = self._strip_base_path(path) return Request( method=http_method, @@ -93,6 +90,14 @@ def request(self) -> Request: event_type=self.TYPE, ) + def _strip_base_path(self, path: str) -> str: + if self.api_gateway_base_path and self.api_gateway_base_path != "/": + if not self.api_gateway_base_path.startswith("/"): + self.api_gateway_base_path = f"/{self.api_gateway_base_path}" + if path.startswith(self.api_gateway_base_path): + path = path[len(self.api_gateway_base_path) :] + return path + @property def body(self) -> bytes: body = self.trigger_event.get("body", b"") or b"" diff --git a/mangum/handlers/aws_http_gateway.py b/mangum/handlers/aws_http_gateway.py index 513c4942..2d8982da 100644 --- a/mangum/handlers/aws_http_gateway.py +++ b/mangum/handlers/aws_http_gateway.py @@ -2,11 +2,11 @@ import urllib.parse from typing import Dict, Any -from .abstract_handler import AbstractHandler +from . import AwsApiGateway from .. import Response, Request -class AwsHttpGateway(AbstractHandler): +class AwsHttpGateway(AwsApiGateway): """ Handles AWS HTTP Gateway events (v1.0 and v2.0), transforming them into ASGI Scope and handling responses @@ -86,6 +86,8 @@ def request(self) -> Request: if not path: path = "/" + else: + path = self._strip_base_path(path) return Request( method=http_method,