Skip to content

Fix precedence of explicit HEAD routes over implicit HEAD#3182

Open
Hedingber wants to merge 2 commits into
Kludex:mainfrom
Hedingber:fix/head-routing-priority
Open

Fix precedence of explicit HEAD routes over implicit HEAD#3182
Hedingber wants to merge 2 commits into
Kludex:mainfrom
Hedingber:fix/head-routing-priority

Conversation

@Hedingber

Copy link
Copy Markdown

Summary

Fixes the routing bug described in Discussion #3128.

Previously, Route added "HEAD" to its methods list when initialized with methods=["GET"]. Because routing resolves sequentially via Match.FULL, a HEAD request would trigger the implicit HEAD fallback on a default GET route rather than continuing the search for an explicitly tailored HEAD handler mapped later in the Router.

This PR:

  • Adds an internal tracking property self.explicit_methods to log methods genuinely passed during route initialization by the user.
  • Falls back to Match.PARTIAL for HEAD requests occurring on routes that didn't intentionally list it in explicit_methods. This politely passes routing scope further down the line to ensure real HEAD handlers still take precedence!
  • Adds a regression test case.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion.
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

naarob pushed a commit to naarob/starlette that referenced this pull request Mar 26, 2026
…D route precedence (Kludex#3182)

fix Kludex#3193: staticfiles.is_not_modified() used tag.strip(' W/') which operates
on a character set, incorrectly trimming ETags ending in 'W' or '/'. Replaced
with a normalize_etag() helper that uses str.startswith('W/') for prefix removal.

fix Kludex#3182: Route.__init__ adds HEAD implicitly when GET is defined, causing the
GET route to steal HEAD requests before any explicit HEAD route can match.
Tracked _explicit_methods and return Match.PARTIAL for implicit HEAD so explicit
HEAD routes registered at the same path can take precedence.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants