Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PB-796: handle paths without trailing slash without redirection. #440

Merged
merged 3 commits into from
Aug 13, 2024

Conversation

adk-swisstopo
Copy link
Member

@adk-swisstopo adk-swisstopo commented Aug 7, 2024

API Gateway v1 strips trailing slashes from the request paths. We have a few endpoints that use trailing slashes. Specifically:
/api/stac/v1/
/api/stac/v0.9/
/api/stac/admin/*

Django's CommonMiddleware already has a feature to redirect URLs without trailing slash to their "slashed" equivalent when they exist. When combined with API Gateway v1 this causes a redirection loop.

This change introduces CommonMiddlewareWithInternalRedirect which handles the redirection internally to serve the query like if it had hit the "slashed" page directly.

It also updates the test to verify both endpoints work correctly.

Based on discussion in https://ltwiki.adr.admin.ch:8443/display/~adrien.kunysz@swisstopo.ch/STAC+API+trailing+slash+inconsistency

@adk-swisstopo adk-swisstopo force-pushed the feat-PB-796-trailing-slash branch 2 times, most recently from b125272 to a810b67 Compare August 13, 2024 11:32
@adk-swisstopo adk-swisstopo marked this pull request as ready for review August 13, 2024 11:35
Copy link
Contributor

@boecklic boecklic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice solution!

Comment on lines +39 to +43
self.logger.error(
'Not redirecting path that already ends with a slash: %s: %s',
request.path_info,
request
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is a case that should never happen, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not. But if it does I think it's better to return gracefully and log the error than to get stuck in an infinite loop or to return 500 to the user.

API Gateway v1 strips trailing slashes from the request paths.
We have a few endpoints that use trailing slashes. Specifically:

- /api/stac/v1/
- /api/stac/v0.9/
- /api/stac/admin/*

Django's CommonMiddleware already has a feature to redirect URLs without
trailing slash to their "slashed" equivalent when they exist. When combined
with API Gateway v1 this causes a redirection loop.

This change introduces CommonMiddlewareWithInternalRedirect which handles the
redirection internally to serve the query like if it had hit the "slashed" page
directly.

It also updates the test to verify both endpoints work correctly.

Based on discussion in https://ltwiki.adr.admin.ch:8443/display/~adrien.kunysz@swisstopo.ch/STAC+API+trailing+slash+inconsistency
@adk-swisstopo adk-swisstopo merged commit 379f2f8 into develop Aug 13, 2024
3 checks passed
@adk-swisstopo adk-swisstopo deleted the feat-PB-796-trailing-slash branch August 13, 2024 14:17
adk-swisstopo added a commit that referenced this pull request Sep 24, 2024
In #440 we added
CommonMiddlewareWithInternalRedirect to respond to URLs that don't end
with a trailing slash without user-visible redirection. This was primarily
done to handle a limitation of AWS API Gateway v1. We eventually decided to
use API Gateway v2 which does not have this limitation.

Hence, we remove this code that is not useful and update the tests accordingly.
This is basically a revert of #440
although we keep some of the test refactoring/improvements.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants