Skip to content

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

@lynguyen4-tiki

Description

@lynguyen4-tiki

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions