Go Gin – http “HEAD” 请求方法

huangapple go评论85阅读模式
英文:

Go Gin - http "HEAD" request method

问题

我尝试在中间件中将传入请求的方法设置为"GET",如果请求方法是"HEAD",代码如下所示。

看起来,如果我使用curl -I命令,Gin会将其识别为"GET"请求,但是根据附加的日志显示(底部的日志),它会响应404。

我只是想看看是否可以在路由器级别上实现"HEAD"方法之外的方法来解决这个问题。有什么建议吗?

func CORS() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
		c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "*")
		c.Writer.Header().Set("Access-Control-Allow-Methods", "*")

		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
			return
		}

        // 如果传入请求是"HEAD",则将HTTP方法设置为"GET"
		if c.Request.Method == "HEAD" {
			c.Request.Method = "GET"
		}

		c.Next()
	}
}

gin的日志

英文:

I tried to set http method to "GET" if an incoming request method is "HEAD" in a middleware like below.

It looks like Gin recognizes this as "GET" request if I do curl -I,
but it responds with 404 as the attached log shows (the bottom one).

I just wanted to see if this works without implementing "HEAD" method in a router level.
Any advice?

func CORS() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
		c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "*")
		c.Writer.Header().Set("Access-Control-Allow-Methods", "*")

		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
			return
		}

        //  set http method to "GET" if an incoming request is "HEAD"
		if c.Request.Method == "HEAD" {
			c.Request.Method = "GET"
		}

		c.Next()
	}
}

gin's log

答案1

得分: 0

因为gin中间件是在路由匹配之后获取的处理函数,所以中间件在路由匹配之后执行,因此中间件不能修改路由匹配方法。

通过对http.Handler(gin.Engine)进行装饰,使用http.Server中间件来修改请求方法。

注意:'HEAD'请求没有响应体。如果将'HEAD'转换为'GET'后有响应体,这不符合HTTP协议规范,建议将所有'Get'处理函数注册为'Head'方法。

func main() {
	router := gin.Default()

	s := &http.Server{
		Addr:           ":8080",
		Handler:        http.Handler(func(w http.ResponseWriter, r *http.Request){
			if r.Method == "HEAD" {
				r.Method = "GET"
			}
			router.ServeHTTP(w, r)
		}),
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	s.ListenAndServe()
}
英文:

Because gin middleware is a processing function obtained after route matching, middleware executes after route matching, so middleware cannot modify the route matching method.

By decorating a layer of http.Handler(gin.Engine), use http.Server middleware to modify the request method.

Note: There is no response body for the 'HEAD' request. If there is a response body after converting 'HEAD' to 'GET', which does not conform to the http protocol specification, it is recommended to register all the 'Get' processing functions with the 'Head' method.

func main() {
	router := gin.Default()

	s := &amp;http.Server{
		Addr:           &quot;:8080&quot;,
		Handler:        http.Handler(func(w http.ResponseWriter, r *http.Request){
			if r.Method == &quot;HEAD&quot; {
				r.Method = &quot;GET&quot;
			}
			router.ServeHTTP(w, r)
		}),
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 &lt;&lt; 20,
	}
	s.ListenAndServe()
}

huangapple
  • 本文由 发表于 2021年9月10日 11:24:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/69126867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定