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

RawPathRouting #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

RawPathRouting #55

wants to merge 3 commits into from

Conversation

imkira
Copy link

@imkira imkira commented Feb 9, 2015

Thanks in advance for your awesome router.

golang's http package does (by default) percent-decode the URI path in requests, which cases problems during routing if the requested path contains %2f in named/catch-all parameters.
For instance, if I define the route GET /dirs/:dir/files/:file, and make a request like GET /dirs/dir1%2fdir2/files/file instead of having that route triggered, the NotFound handler will be executed because httprouter uses req.URL.Path which happens to expand to /dirs/dir1/dir2/files/file.

The following thread mentions the importance of this detail.
https://code.google.com/p/go/issues/detail?id=2782

This patch adds the option RawPathRouting to Router. It is disabled by default because it is rarely necessary and because it forces the user to decode parameters manually if enabled. There are already other routers doing this, namely httptreemux

Would it be possible to merge this into your repo?

@@ -59,7 +77,7 @@ func TestRouter(t *testing.T) {

w := new(mockResponseWriter)

req, _ := http.NewRequest("GET", "/user/gopher", nil)
req := newRequest(t, "GET", "/user/gopher")
Copy link
Owner

Choose a reason for hiding this comment

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

Why did you change everything to newRequest instead of http.NewRequest, even for unaffected tests?

Copy link
Author

Choose a reason for hiding this comment

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

The reason I did this for the new tests is because parsing of http requests is internally handled differently in net/http when requests come from a server.

https://github.com/golang/go/blob/master/src/net/http/server.go#L613
https://github.com/golang/go/blob/master/src/net/http/request.go#L592

When I looked at net/http's code, I noticed that public function ReadRequest builds a url.Request structure from an io.Reader, parsing the request as-is (in raw form) and the request is never altered until being delivered to the http function handler. Making a request using http.NewRequest does not handle requests in raw form so one would also need to call url.ParseRequestURI and fix other fields to be coherent. I thought that would be difficult to maintain and to keep up with golang updates. The only external requirement now is using http.ReadRequest so it shouldn't be difficult to maintain.
The other alternative to all of this would be to fully mock http listeners, transports, etc so that the request would go through all steps of the http stack from client to server and then from server to http handler. I didn't do this because that would be a lot more work and because it would better fit in a PR for that purpose and not this one.

The reason I also changed all old tests to use newRequest is to make all tests homogeneous and to be able to specify requests in raw form (not parsed).

@julienschmidt julienschmidt added this to the v2 milestone May 27, 2015
@julienschmidt julienschmidt self-assigned this May 27, 2015
@julienschmidt julienschmidt changed the title added: support for RawPathRouting in Router RawPathRouting Oct 20, 2016
@samsalisbury
Copy link

Any reason this hasn't been merged yet @julienschmidt? Has this functionality been included in some other way?

similark pushed a commit to similarweb/httprouter that referenced this pull request May 9, 2023
* Update release workflow

Signed-off-by: Lucas Santos <lhs.santoss@gmail.com>

* Update canary workflow

Signed-off-by: Lucas Santos <lhs.santoss@gmail.com>

* Update .github/workflows/build_canary.yml

Signed-off-by: Lucas Santos <lhs.santoss@gmail.com>

Co-authored-by: Aaron Schlesinger <70865+arschles@users.noreply.github.com>

* Update .github/workflows/build_canary.yml

Signed-off-by: Lucas Santos <lhs.santoss@gmail.com>

Co-authored-by: Aaron Schlesinger <70865+arschles@users.noreply.github.com>

* Update .github/workflows/build_release.yml

Signed-off-by: Lucas Santos <lhs.santoss@gmail.com>

Co-authored-by: Aaron Schlesinger <70865+arschles@users.noreply.github.com>

* Update .github/workflows/build_canary.yml

Signed-off-by: Lucas Santos <lhs.santoss@gmail.com>

Co-authored-by: Aaron Schlesinger <70865+arschles@users.noreply.github.com>

* Update .github/workflows/build_release.yml

Signed-off-by: Lucas Santos <lhs.santoss@gmail.com>

Co-authored-by: Aaron Schlesinger <70865+arschles@users.noreply.github.com>

* Update .github/workflows/build_release.yml

Signed-off-by: Lucas Santos <lhs.santoss@gmail.com>

Co-authored-by: Aaron Schlesinger <70865+arschles@users.noreply.github.com>

* Remove output variables

Signed-off-by: Lucas Santos <lhs.santoss@gmail.com>

Co-authored-by: Aaron Schlesinger <70865+arschles@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
v2
Routing
Development

Successfully merging this pull request may close these issues.

None yet

3 participants