Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # 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

Comments

@lynguyen4-tiki
Copy link

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

@elithrar
Copy link
Contributor

elithrar commented Jul 8, 2020 via email

@rittneje
Copy link

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

@elithrar
Copy link
Contributor

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 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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants