如何解决即使使用SkipClean
我有以下小程序。即使使用SkipClean后,它也不接受编码的双斜杠(%2F%2F),而单编码的斜杠可以正常工作。
有人可以建议这里出什么问题吗?
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! %2Fds
curl http://localhost:10801/hi%2F%2Fds
将输出Hello world! %2F%2Fds
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。