Skip to content

Commit

Permalink
Fix unhandled api_gateway_base_path in AwsHttpGateway (#204)
Browse files Browse the repository at this point in the history
Co-authored-by: Xavier Payn <[email protected]>
  • Loading branch information
xpayn and Xavier Payn authored Oct 6, 2021
1 parent a462f95 commit 8f4e6b9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
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

0 comments on commit 8f4e6b9

Please sign in to comment.