Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mangum/handlers/abstract_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions mangum/handlers/aws_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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""
Expand Down
6 changes: 4 additions & 2 deletions mangum/handlers/aws_http_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,6 +86,8 @@ def request(self) -> Request:

if not path:
path = "/"
else:
path = self._strip_base_path(path)

return Request(
method=http_method,
Expand Down