go - 即使使用 SkipClean 后也不接受双斜线

标签 go gorilla mux

我有以下小程序。它不接受编码双斜线 (%2F%2F),即使在使用 SkipClean 后,单编码斜线工作正常。
有人可以建议这里出了什么问题吗?

package main

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

func main() {
    r := mux.NewRouter().SkipClean(true).UseEncodedPath()
    r.HandleFunc("/", hiHandler)
    r.HandleFunc("/hi{*}", hiHandler).Methods("GET")
    http.Handle("/", r)
    http.ListenAndServe(":10801", nil)
}

func hiHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello world!")
}

#curl http://localhost:10801/hi%2Fds => 这有效
#curl http://localhost:10801/hi%2F%2Fds => 这不是。给出永久移动错误。

最佳答案

诀窍是以稍微不同的方式运行服务器。

package main

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

func main() {
    r := mux.NewRouter().SkipClean(true).UseEncodedPath()
    r.HandleFunc("/", hiHandler)
    r.HandleFunc("/hi{*}", hiHandler).Methods("GET")

    srv := &http.Server{
        Handler: r,
        Addr:    "localhost:10801",
        // Good practice: enforce timeouts for servers you create!
        WriteTimeout: 15 * time.Second,
        ReadTimeout:  15 * time.Second,
    }

    log.Fatal(srv.ListenAndServe())
}

func hiHandler(w http.ResponseWriter, r *http.Request) {
    // Putting the match of {*} into the output
    v := mux.Vars(r)
    fmt.Fprint(w, "Hello world! ", v["*"])
}
curl http://localhost:10801/hi%2Fds将输出 Hello world! %2Fdscurl http://localhost:10801/hi%2F%2Fds将输出 Hello world! %2F%2Fds .

关于go - 即使使用 SkipClean 后也不接受双斜线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63370135/

相关文章:

multithreading - 在 go 中一次处理许多 GET 请求

go - Go 中的 HTML 模板 - 插入 html 作为管道的值

go - 当多个客户端使用它时,Gorilla Websocket非常慢

当用户在 3 分钟内未向 Go Web 服务器发送数据时,Go session 超时

http - 我应该在 golang 中直接使用 ServeMux 还是 http

go - Go中未定义的返回类型

go - 将嵌入式结构复制到新结构中,而不必复制其所有字段

Go 编译器未定义的方法

android - `gomobile build` 和 `gomobile install` 抛出 "gomobile: EOF"

angularjs - AngularJS的Gorilla CSRF