Skip to content

Fix RouteHeaders middleware to handle Host header correctly#1039

Open
nathannewyen wants to merge 1 commit intogo-chi:masterfrom
nathannewyen:fix-route-headers-host
Open

Fix RouteHeaders middleware to handle Host header correctly#1039
nathannewyen wants to merge 1 commit intogo-chi:masterfrom
nathannewyen:fix-route-headers-host

Conversation

@nathannewyen
Copy link
Copy Markdown

Summary

Go's http server promotes the Host header to r.Host and removes it from the Header map. This means r.Header.Get("Host") always returns an empty string, making the Host-based routing example in RouteHeaders non-functional.

This change:

  • Adds special handling for the "Host" header to read from r.Host instead of r.Header.Get("Host")
  • Fixes the documentation example to avoid circular router references which would cause a stack overflow
  • Adds comprehensive tests for Host header routing behavior

Changes

  1. route_headers.go: Added conditional check for "host" header to use r.Host
  2. route_headers.go: Updated example code in comments to work correctly
  3. route_headers_test.go: Added tests covering Host header routing with subdomains and ports

Test Plan

  • All existing tests pass
  • New tests verify Host header is correctly read from r.Host
  • Tests cover subdomain matching with wildcards
  • Tests cover Host with port numbers

Fixes #691

Go's http server promotes the Host header to r.Host and removes it from
the Header map. This means r.Header.Get("Host") always returns empty.

This change adds special handling for the "Host" header to read from
r.Host instead, making the Host-based routing example actually work.

Also fixed the example in the documentation to avoid circular router
references which would cause a stack overflow.

Added tests for Host header routing behavior.

Fixes go-chi#691
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.

Subdomain example in route_headers.go not working

1 participant