-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: master
Are you sure you want to change the base?
RawPathRouting #55
Conversation
@@ -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") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
Any reason this hasn't been merged yet @julienschmidt? Has this functionality been included in some other way? |
* Update release workflow Signed-off-by: Lucas Santos <[email protected]> * Update canary workflow Signed-off-by: Lucas Santos <[email protected]> * Update .github/workflows/build_canary.yml Signed-off-by: Lucas Santos <[email protected]> Co-authored-by: Aaron Schlesinger <[email protected]> * Update .github/workflows/build_canary.yml Signed-off-by: Lucas Santos <[email protected]> Co-authored-by: Aaron Schlesinger <[email protected]> * Update .github/workflows/build_release.yml Signed-off-by: Lucas Santos <[email protected]> Co-authored-by: Aaron Schlesinger <[email protected]> * Update .github/workflows/build_canary.yml Signed-off-by: Lucas Santos <[email protected]> Co-authored-by: Aaron Schlesinger <[email protected]> * Update .github/workflows/build_release.yml Signed-off-by: Lucas Santos <[email protected]> Co-authored-by: Aaron Schlesinger <[email protected]> * Update .github/workflows/build_release.yml Signed-off-by: Lucas Santos <[email protected]> Co-authored-by: Aaron Schlesinger <[email protected]> * Remove output variables Signed-off-by: Lucas Santos <[email protected]> Co-authored-by: Aaron Schlesinger <[email protected]>
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 likeGET /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 httptreemuxWould it be possible to merge this into your repo?