Skip to content

Commit 8f4e6b9

Browse files
xpaynXavier Payn
andauthored
Fix unhandled api_gateway_base_path in AwsHttpGateway (#204)
Co-authored-by: Xavier Payn <xavier.payn@adaptive-channel.com>
1 parent a462f95 commit 8f4e6b9

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

mangum/handlers/abstract_handler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ def from_trigger(
101101
if "version" in trigger_event and "requestContext" in trigger_event:
102102
from . import AwsHttpGateway
103103

104-
return AwsHttpGateway(trigger_event, trigger_context, **kwargs)
104+
return AwsHttpGateway(
105+
trigger_event, trigger_context, **kwargs # type: ignore
106+
)
105107

106108
if "resource" in trigger_event:
107109
from . import AwsApiGateway

mangum/handlers/aws_api_gateway.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,8 @@ def request(self) -> Request:
7474

7575
if not path:
7676
path = "/"
77-
elif self.api_gateway_base_path and self.api_gateway_base_path != "/":
78-
if not self.api_gateway_base_path.startswith("/"):
79-
self.api_gateway_base_path = f"/{self.api_gateway_base_path}"
80-
if path.startswith(self.api_gateway_base_path):
81-
path = path[len(self.api_gateway_base_path) :]
77+
else:
78+
path = self._strip_base_path(path)
8279

8380
return Request(
8481
method=http_method,
@@ -93,6 +90,14 @@ def request(self) -> Request:
9390
event_type=self.TYPE,
9491
)
9592

93+
def _strip_base_path(self, path: str) -> str:
94+
if self.api_gateway_base_path and self.api_gateway_base_path != "/":
95+
if not self.api_gateway_base_path.startswith("/"):
96+
self.api_gateway_base_path = f"/{self.api_gateway_base_path}"
97+
if path.startswith(self.api_gateway_base_path):
98+
path = path[len(self.api_gateway_base_path) :]
99+
return path
100+
96101
@property
97102
def body(self) -> bytes:
98103
body = self.trigger_event.get("body", b"") or b""

mangum/handlers/aws_http_gateway.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import urllib.parse
33
from typing import Dict, Any
44

5-
from .abstract_handler import AbstractHandler
5+
from . import AwsApiGateway
66
from .. import Response, Request
77

88

9-
class AwsHttpGateway(AbstractHandler):
9+
class AwsHttpGateway(AwsApiGateway):
1010
"""
1111
Handles AWS HTTP Gateway events (v1.0 and v2.0), transforming them into ASGI Scope
1212
and handling responses
@@ -86,6 +86,8 @@ def request(self) -> Request:
8686

8787
if not path:
8888
path = "/"
89+
else:
90+
path = self._strip_base_path(path)
8991

9092
return Request(
9193
method=http_method,

0 commit comments

Comments
 (0)