我一直在使用golang的默认值
http.ServeMux
用于http路由处理。
wrap := func(h func(t *MyStruct, w http.ResponseWriter, r *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
h(t, w, r)
}
}
// Register handlers with default mux
httpMux := http.NewServeMux()
httpMux.HandleFunc("/", wrap(payloadHandler))
假设此服务器可以通过
http://example.com/
我客户的请求中很少有路径问题
http://example.com/api//module
(注意额外的斜杠)重定向为
301 Moved Permanently
. 探索golang的http内部
ServeMux.Handler(r *Request)
功能,似乎是有意的。
path := cleanPath(r.URL.Path)
// if there is any change between actual and cleaned path, the request is 301 ed
if path != r.URL.Path {
_, pattern = mux.handler(host, path)
url := *r.URL
url.Path = path
return RedirectHandler(url.String(), StatusMovedPermanently), pattern
}
我还研究了其他类似的问题。
go-web-server-is-automatically-redirecting-post-requests
以上qn存在冗余问题
/
在寄存器模式本身中,但我的用例不在寄存器模式中(在一些与寄存器模式无关的嵌套路径中)
问题是,因为我客户的要求是
POST
,浏览器使用新的
GET
具有精确查询参数和POST正文的请求。但是HTTP方法中的更改会导致请求失败。
我已经指示客户修复多余的
/
在url中,但修复可能需要几(?)在所有客户端位置部署数周。
这些也是多余的
/
处理得很好
Apache Tomcat
,但仅在golang服务器中失败。那么,这是我用例中的预期行为吗(多余的
/
我在想办法超越
Handler
的函数
ServeMux
,但它将不再有用
处理程序
内部通话。如果希望禁用此301行为,我们将不胜感激。
相关链接
http-post-method-is-actally-sending-a-get