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

Route.Host -matching will ignore any provided port from getHost(), if… #447

Merged
merged 1 commit into from
May 17, 2019

Conversation

cognusion
Copy link
Contributor

… a port isn't specified in the template

In lieu of checking the template pattern on every Match request, a bool is added to the routeRegexp, and set if the routeRegexp is a host AND there is no ":" in the template. I dislike extending the type, but I'd dislike doing a string match on every single Match, even more.

All existing mux tests pass. Additionally my application-level (Convey) positive and negative tests pass. Feel free to comment: You know your code better than I do.

=== RUN   TestPathHostPortUnset

  When a request is made, with a host and port specified, the correct match is found (no port specified) ✔✔✔✔✔


16 total assertions

--- PASS: TestPathHostPortUnset (0.00s)
=== RUN   TestPathHostPortSet

  When a request is made, with a host and port specified, and the port does match, a match is found ✔✔✔✔✔


21 total assertions

--- PASS: TestPathHostPortSet (0.00s)
=== RUN   TestPathHostPortSetNope

  When a request is made, with a host and port specified, and the port does not match, a match is NOT found ✔✔✔


24 total assertions

--- PASS: TestPathHostPortSetNope (0.00s)

… a port isn't specified in the template

In lieu of checking the template pattern on every Match request, a bool is added to the routeRegexp, and set
if the routeRegexp is a host AND there is no ":" in the template. I dislike extending the type, but I'd dislike
doing a string match on every single Match, even more.
@yogo1212
Copy link

works for me

@stale
Copy link

stale bot commented May 11, 2019

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.

@stale stale bot added the stale label May 11, 2019
@elithrar
Copy link
Contributor

Sorry, I missed this: can you show application code before/after that explains this better?

I understand what the change does, but not why.

@stale stale bot removed the stale label May 11, 2019
@yogo1212
Copy link

@elithrar
Without this change, r.Host("test.host.com") won't match requests with the Host: test.host.com:1234.
In order to make that case work, this PR assumes that, unless a port specified (e.g. r.Host("test.host.com:1234")), any port is allowed to match.

@cognusion
Copy link
Contributor Author

cognusion commented May 16, 2019

@elithrar ,

As @yogo1212 properly described, a previous PR #383 horrifically broke a good swath of the Gorilla-using Internet by deciding that if you didn't specify a port in the route, you can't specify a port on the Request and have it match. This PR allows that PR to exist in-spirit, without the awful requirement of specifying ports for every route. Without either reverting #383 or implementing this patch, if your route is r.Host("a.b.c") then http://a.b.c:80/ won't work but http://a.b.c/ will. With this patch, unless you specify the port on the route, it assumes you don't give a flying leap what port the request tacks on.

@elithrar
Copy link
Contributor

Understood - #383 broke more than we anticipated (users should pin + test deps, but we also need to do a better job of checking impact).

Reviewing the PR today and can (hopefully) merge + tag a new version tonight.

@elithrar elithrar merged commit ed099d4 into gorilla:master May 17, 2019
@elithrar
Copy link
Contributor

@yogo1212
Copy link

@elithrar thank you for the notice <3

thaJeztah added a commit to thaJeztah/cli that referenced this pull request Jun 20, 2019
full diff: gorilla/mux@v1.7.0...v1.7.2

includes:

 - gorilla/mux#457 adding Router.Name to create new Route
 - gorilla/mux#447 host:port matching does not require a :port to be specified

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
thaJeztah added a commit to thaJeztah/docker that referenced this pull request Jun 20, 2019
full diff: gorilla/mux@v1.7.0...v1.7.2

includes:

 - gorilla/mux#457 adding Router.Name to create new Route
 - gorilla/mux#447 host:port matching does not require a :port to be specified

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
docker-jenkins pushed a commit to docker-archive/docker-ce that referenced this pull request Jun 26, 2019
full diff: gorilla/mux@v1.7.0...v1.7.2

includes:

 - gorilla/mux#457 adding Router.Name to create new Route
 - gorilla/mux#447 host:port matching does not require a :port to be specified

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Upstream-commit: 25b451e01b8a1b44f45a67cb4731ff61d7b1ebc1
Component: engine
thaJeztah added a commit to thaJeztah/docker that referenced this pull request Aug 27, 2019
full diff: gorilla/mux@v1.7.0...v1.7.2

includes:

 - gorilla/mux#457 adding Router.Name to create new Route
 - gorilla/mux#447 host:port matching does not require a :port to be specified

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 25b451e)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
docker-jenkins pushed a commit to docker-archive/docker-ce that referenced this pull request Sep 6, 2019
full diff: gorilla/mux@v1.7.0...v1.7.2

includes:

 - gorilla/mux#457 adding Router.Name to create new Route
 - gorilla/mux#447 host:port matching does not require a :port to be specified

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Upstream-commit: 81e3457c23630de470c42d909e79dfb159833f7a
Component: cli
aiordache pushed a commit to aiordache/cli that referenced this pull request Sep 20, 2019
full diff: gorilla/mux@v1.7.0...v1.7.2

includes:

 - gorilla/mux#457 adding Router.Name to create new Route
 - gorilla/mux#447 host:port matching does not require a :port to be specified

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
thaJeztah added a commit to thaJeztah/docker that referenced this pull request Sep 20, 2019
full diff: gorilla/mux@v1.7.0...v1.7.2

includes:

 - gorilla/mux#457 adding Router.Name to create new Route
 - gorilla/mux#447 host:port matching does not require a :port to be specified

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 25b451e)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
docker-jenkins pushed a commit to docker-archive/docker-ce that referenced this pull request Sep 24, 2019
full diff: gorilla/mux@v1.7.0...v1.7.2

includes:

 - gorilla/mux#457 adding Router.Name to create new Route
 - gorilla/mux#447 host:port matching does not require a :port to be specified

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 25b451e01b8a1b44f45a67cb4731ff61d7b1ebc1)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Upstream-commit: 486953e2ff065883897746317c318b0bb8d3406d
Component: engine
burnMyDread pushed a commit to burnMyDread/moby that referenced this pull request Oct 21, 2019
full diff: gorilla/mux@v1.7.0...v1.7.2

includes:

 - gorilla/mux#457 adding Router.Name to create new Route
 - gorilla/mux#447 host:port matching does not require a :port to be specified

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: zach <Zachary.Joyner@linux.com>
Copy link
Contributor

@harshavardhana harshavardhana left a comment

Choose a reason for hiding this comment

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

This PR actually caused a regression one more change needed is missing in this PR

git diff
diff --git a/regexp.go b/regexp.go
index 96dd94a..0144842 100644
--- a/regexp.go
+++ b/regexp.go
@@ -325,6 +325,12 @@ func (v routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route) {
        // Store host variables.
        if v.host != nil {
                host := getHost(req)
+               if v.host.wildcardHostPort {
+                       // Don't be strict on the port match
+                       if i := strings.Index(host, ":"); i != -1 {
+                               host = host[:i]
+                       }
+               }
                matches := v.host.regexp.FindStringSubmatchIndex(host)
                if len(matches) > 0 {
                        extractVars(host, matches, v.host.varsN, m.Vars)

harshavardhana added a commit to harshavardhana/mux that referenced this pull request Jul 10, 2020
Continuing from PR gorilla#447 we have to add extra
check to ignore the port as well
harshavardhana added a commit to harshavardhana/mux that referenced this pull request Jul 10, 2020
Continuing from PR gorilla#447 we have to add extra
check to ignore the port as well

add tests to cover this case
elithrar pushed a commit that referenced this pull request Jul 11, 2020
Continuing from PR #447 we have to add extra
check to ignore the port as well

add tests to cover this case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants