Repository navigation
Add support for hostname routing - #48
Merged
Merged
Conversation
- joinHostPath: efficient 0 allocation concat host & path
…handle url with domain. Special care must be taken to safely extract param value from the url.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #48 +/- ##
==========================================
+ Coverage 90.75% 91.98% +1.23%
==========================================
Files 18 19 +1
Lines 2779 3131 +352
==========================================
+ Hits 2522 2880 +358
+ Misses 196 194 -2
+ Partials 61 57 -4 ☔ View full report in Codecov by Sentry. 🚨 Try these New Features:
|
tigerwill90
marked this pull request as ready for review
November 15, 2024 23:48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces hostname support that allow routes to be match by domain names. Fox can now handle routes both with and without specific hostname, bringing flexibility to environments with varying domain requirements.
Key feature
http.ServerMux).{sub}.example.com/or{sub1}.{sub2}.example.com/. Catch-all wildcard such as*{any}.exemple.com/are not supported for security reason.Validation & Restrictions
Any characters, or combination of bits (as octets), are permitted in DNS names. However, there is a preferred form that is required by most applications as described in RFC 3696. This preferred form has been the only one permitted in the names of top-level domains, or TLDs. In general, it is also the only form permitted in most second-level names registered in TLDs.
In consequence, hostnames are validated to conform to the LDH (letters, digits, hyphens) standard. This ensures compatibility and avoids security risks, particularly with homograph attacks. User that need to support more flexible (non ASCII) domain should convert label in "punycode" form (see Internationalized Domain Names) before registering the route. Wildcard segments within hostnames, such as
{a}.b.c/, are exempt from LDH validation since they act as placeholders rather than actual domain labels. As such, they do not count toward the hard limit of 63 characters per label, nor the 255-character limit for the full hostname (including periods).The DNS specification also permits a trailing period to be used to denote the root, e.g.,
a.b.canda.b.c.are equivalent, but the latter is more explicit and is required to be accepted by applications. Fox will reject route registered with trailing period. However, the router will automatically strip any trailing period from incoming request host so it can match the route regardless of an trailing period. Note that FQDN (with trailing period) does not play well with golang TLS stdlib (see traefik/traefik#9157 (comment)).Priority rules
The router can transition instantly and transparently, at runtime, from path-only mode to hostname-prioritized mode without any additional configuration or action. If any route with a hostname is registered, the router automatically switches to prioritize hostname matching. Conversely, if no hostname-specific routes are registered, the router reverts to path-priority mode, ensuring optimal and adaptive routing behavior.
Exemple
Performance
Fox vs. Standard Mux with a static hostname (
adproxy.tf1.fr) is approximately 35% faster, though the performance gap narrows for very long domain names. Note that standard mux does not support wildcard in hostname.Very few routers support hostname matching. When comparing Fox to Gorilla Mux with a static hostname (
adproxy.tf1.fr), Fox is approximately 96% faster:Comparing to
masterbranch, the new hostname routing feature introduces a modest 5% performance overhead on path-only route.Other Notable Changes
This PR introduces a significant performance improvement for infix wildcard handling. Previously, route lookups with infix wildcards were not zero-allocation in all cases. See the lookup function implementation for details.
The limitation stemmed from the need to create an
inodeduring the lookup phase, as references toinodeobjects had to remain valid until the function completed to avoid subtle race conditions. This prevented the use of object pooling during lookups.To resolve this,
inodeobjects are now precomputed during the route insertion, update, or deletion phases. This approach leverages the fact that we can determine the split points in advance, avoiding allocations entirely during the lookup phase.Results: