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

Mux not properly parsing ampersand in url query paramters #238

Closed
micahkwillard opened this issue Mar 2, 2017 · 3 comments · Fixed by #270
Closed

Mux not properly parsing ampersand in url query paramters #238

micahkwillard opened this issue Mar 2, 2017 · 3 comments · Fixed by #270

Comments

@micahkwillard
Copy link

When I execute and perform a request on the following code at url: localhost:3000/?query=Business%20Management%20%26%20Administration:

package main

import (
	"fmt"
	"io"
	"net/http"
	"github.com/gorilla/mux"
	"log"
)

func main() {
	handler := func(w http.ResponseWriter, r *http.Request) {
		fmt.Println()
		fmt.Println(r.URL.Query())
		fmt.Println(r.URL.Query().Get("query"))
		fmt.Println(r.URL.RawQuery)
		fmt.Println("Called: " + r.URL.String())
		fmt.Println("^^^")
		fmt.Println()
		io.WriteString(w, "<html><body>H World!</body></html>")
	}

	r := mux.NewRouter()
	r.HandleFunc("/", handler)
	http.Handle("/", r)
	log.Fatal(http.ListenAndServe(":3000", r))
}

I get the following results for my prints:

map[query:[Business Management ]  Administration:[]]
Business Management 
query=Business%20Management%20&%20Administration
Called: /?query=Business%20Management%20&%20Administration
^^^

I'm not 100% sure if it is mux, but something seems to not properly handle the ampersand. In the print for the "r.URL.RawQuery" you can see a literal "&" where a url encoded ampersand should be ("%26") and that messes up the parsing of everything else. Am I mistaken?

I created a httptest program without mux (here) and everything came out right, so that is why I am thinking something in mux might be the cause. I apologize in advance if this is something simple that I am simply missing. Thanks for your time!

@micahkwillard
Copy link
Author

I'm also now noticing a similar issue with encoded "?" (%3F). If I set up Query params and an encoded "?" is present, I'll get a 404 since mux won't match the path.

@micahkwillard
Copy link
Author

Okay, This is definitely broken. LUCKILY, there is a gorilla mux way of fixing it. You have to use the route.Queries() and give it a {name:pattern} for it to use (using {query:.+} fixes my above issue). If you give route.Queries() just {name}, it will fail with the example url I gave above. Hope this helps someone down the road.

@elithrar
Copy link
Contributor

elithrar commented Mar 28, 2017 via email

ChrisHines added a commit to ChrisHines/mux that referenced this issue May 30, 2017
kisielk pushed a commit that referenced this issue Jun 2, 2017
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 a pull request may close this issue.

2 participants