The Wayback Machine - https://web.archive.org/web/20210121042221/https://github.com/gorilla/mux/issues/578
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

[bug] mux.Router.ServeHTTP redirection has bug #578

Closed
lynguyen4-tiki opened this issue Jul 8, 2020 · 4 comments
Closed

[bug] mux.Router.ServeHTTP redirection has bug #578

lynguyen4-tiki opened this issue Jul 8, 2020 · 4 comments
Labels

Comments

@lynguyen4-tiki
Copy link

@lynguyen4-tiki lynguyen4-tiki commented Jul 8, 2020

Describe the bug
After cleanPath, it writes into header to redirect request

                if p := cleanPath(path); p != path {

			// Added 3 lines (Philip Schlump) - It was dropping the query string and #whatever from query.
			// This matches with fix in go 1.2 r.c. 4 for same problem.  Go Issue:
			// http://code.google.com/p/go/issues/detail?id=5252
			url := *req.URL
			url.Path = p
			p = url.String()

			w.Header().Set("Location", p)
			w.WriteHeader(http.StatusMovedPermanently)
			return
		}

But it doesn't set method, so redirection calls GET method by default, and error will occur if I call POST method with path like http://my.domain//api

Code example:

package main

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

func main() {
         r := mux.NewRouter()
	r.Methods("GET").Path("/user/{userID:[0-9]+}").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		fmt.Println("GET Call")
	})
	r.Methods("POST").Path("/user").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		fmt.Println("POST Call")
	})

	http.Handle("/", r)
	panic(http.ListenAndServe(":8080", nil))
}

Call: curl --location --request POST 'http://localhost:8080//user' to see bug

@lynguyen4-tiki lynguyen4-tiki added the bug label Jul 8, 2020
@elithrar
Copy link
Member

@elithrar elithrar commented Jul 8, 2020

@rittneje
Copy link

@rittneje rittneje commented Sep 15, 2020

Why is it using 301 here instead of 308 (Permanent Redirect)?

@elithrar
Copy link
Member

@elithrar elithrar commented Sep 15, 2020

As discussed in other issues on this topic, the HTTP 308 code was introduced after mux was written. There are also clients that do not handle HTTP 308 correctly.

@stale
Copy link

@stale stale bot commented Dec 4, 2020

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 Dec 4, 2020
@stale stale bot closed this Dec 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.