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

contrib/dimfeld/httptreemux.v5: fix route and name for 30X redirects #2664

Closed

Conversation

devilleweppenaar
Copy link
Contributor

@devilleweppenaar devilleweppenaar commented Apr 18, 2024

The httptreemux router has a redirect behaviour that is invoked when a request URL and matched route only differs in a trailing slash. The default behaviour is to respond with a 301 (moved permanently) to redirect the client to the exact path of the matched handler. We previously patched one of the two scenarios where this occurs in #2332. The changes in this commit addresses the other scenario for the 301 redirect, but also the 307 and 308 redirect options. Several tests were included to cover the various scenarios.

We also standardize the resource name in the event that there is a trailing slash missmatch between request URL and matched handler. We always truncate the trailing slash in the resource name. We no longer do this as part of these changes.

Fixes #2663

What does this PR do?

There are two main changes in this PR:

  1. Fix of the other scenario that was missed when this issue was first raised as contrib/dimfeld/httptreemux.v5: parameterize redirects due to trailing slash #2332.
  2. Standardized the route and resource name by trimming any unnecessary trailing slashes.

Extensive tests have been added for the described changes.

Motivation

Fixes #2663.

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • System-Tests covering this feature have been added and enabled with the va.b.c-dev version tag.
  • There is a benchmark for any new code, or changes to existing code.
  • If this interacts with the agent in a new way, a system test has been added.
  • Add an appropriate team label so this PR gets put in the right place for the release notes.
  • Non-trivial go.mod changes, e.g. adding new modules, are reviewed by @DataDog/dd-trace-go-guild.

Unsure? Have a question? Request a review!

contrib/dimfeld/httptreemux.v5/httptreemux.go Outdated Show resolved Hide resolved
contrib/dimfeld/httptreemux.v5/httptreemux.go Outdated Show resolved Hide resolved
contrib/dimfeld/httptreemux.v5/httptreemux.go Outdated Show resolved Hide resolved
The httptreemux router has a redirect behaviour that is invoked when
a request URL and matched route only differs in a trailing slash. The
default behaviour is to respond with a 301 (moved permanently) to
redirect the client to the exact path of the matched handler. We
previously patched one of the two scenarios where this occurs in DataDog#2332.
The changes in this commit addresses the other scenario for the 301
redirect, but also the 307 and 308 redirect options. Several tests
were included to cover the various scenarios.

We also standardize the resource name in the event that there is a
trailing slash missmatch between request URL and matched handler. We
always truncate the trailing slash in the resource name.

Fixes DataDog#2663
Simplied the unnecessarily verbose function comments of
isSupportedRedirectStatus and routerRedirectEnabled.
The new tests added to assert the redirect behaviour for
the four redirect modes supported by the router had
redundant test cases.

These test cases were reduced down to three scenarios per
redirect mode:
1. path not found
2. path with parameterized URL excluding a trailing slash
3. path with parameterized URL inclduing a trailing slash
When handling the supported redirect modes of the router
we were incorrectly trimming trailing slashes from the
route path matched by the router. The previous behaviour
is restored where we return the exact path matched by
the router.
Copy link
Member

@darccio darccio left a comment

Choose a reason for hiding this comment

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

LGTM

@devilleweppenaar
Copy link
Contributor Author

@rarguelloF I have addressed the feedback. Please take a look when you have a moment. Also let me know if there are any follow up actions you'd like me to look at.

Copy link
Contributor

@rarguelloF rarguelloF left a comment

Choose a reason for hiding this comment

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

LGTM!

@darccio darccio added the apm:ecosystem contrib/* related feature requests or bugs label Apr 30, 2024
@devilleweppenaar
Copy link
Contributor Author

Hi folks. Happy Monday! Just checking in if there's anything required from me on this PR at this stage?

@darccio
Copy link
Member

darccio commented May 6, 2024

@devillecodes No, it just needs to be merged. Allow us to take care of it during this week, before the beginning of v1.64.0 release cc @nsrip-dd

@darccio
Copy link
Member

darccio commented May 8, 2024

@devillecodes Please, can you merge main into your PR? I ran the tests locally and in draft PR #2685 (as external contributors' PRs don't run all the CI). Everything is green.

To avoid blocking the start of the release train, we merged #2685 instead. It's a copy of your PR, with a small temporary fix for another issue unrelated to your PR (it's present in other PRs that we want to merge before the train starts).

@darccio darccio closed this May 8, 2024
@devilleweppenaar
Copy link
Contributor Author

Amazing! Thanks folks! 🙌 Appreciate the efforts in getting this merged! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apm:ecosystem contrib/* related feature requests or bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] contrib/dimfeld/httptreemux.v5: trailing slash requests are redirected with raw URL being tracked
4 participants