Skip to content

Unusual path parameter values make mux to choose incorrect handler #308

@sergey-seleznev

Description

@sergey-seleznev

Hi!

I would like to define a number of handlers, that have:

  • same URL with a file path as the rightmost parameter,
  • different HTTP request methods.

Here's a simplified example:

func main() {
	router := mux.NewRouter()
	router.HandleFunc("/files/{path:.*}", getHandler).Methods("GET")
	router.HandleFunc("/files/{path:.*}", postHandler).Methods("POST")
	log.Fatal(http.ListenAndServe(":8080", router))
}

func getHandler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("[GET] " + mux.Vars(r)["path"]))
}

func postHandler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("[POST] " + mux.Vars(r)["path"]))
}

It seems that mux tends to ignore methods and select improper handler in some specific cases:

  • GET http://localhost:8080/files/file.txt => [GET] file.txt (OK)
  • POST http://localhost:8080/files/file.txt => [POST] file.txt (OK)
  • GET http://localhost:8080/files/path/to/file.txt => [GET] path/to/file.txt (OK)
  • POST http://localhost:8080/files/path/to/file.txt => [POST] path/to/file.txt (OK)
  • GET http://localhost:8080/files/path/to//file.txt => [GET] path/to/file.txt (OK)
  • POST http://localhost:8080/files/path/to//file.txt => [GET] path/to/file.txt (unexpected)
  • GET http://localhost:8080/files/path/to/./file.txt => [GET] path/to/file.txt (OK)
  • POST http://localhost:8080/files/path/to/./file.txt => [GET] path/to/file.txt (unexpected)
  • GET http://localhost:8080/files/path/to/../file.txt => [GET] path/file.txt (OK)
  • POST http://localhost:8080/files/path/to/../file.txt => [GET] path/file.txt (unexpected)

Handler declaration order doesn't change the tests results mentioned.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions