接受图像头请求

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

Accept image header request

问题

我写了一个路由函数,只接受图像请求。看一下下面的代码。

// 添加图像路由路径
func addImage(path string, handler httpImageFunc, name ...string) {
//log.Println("添加图像")
if len(name) > 0 {
router.Handle(path, httpImageFunc(handler)).
Methods("GET").
Headers("Accept", "image/").
Name(name[0])
} else {
router.Handle(path, httpImageFunc(handler)).
Methods("GET").
Headers("Accept", "image/
")
}
}

如你所见,请求只接受带有头部("Accept", "image/*")的请求。
但是Chrome浏览器发送的请求头是:

Accept:image/webp,/;q=0.8

而Firefox发送的请求头是:

Accept:image/png,image/;q=0.8,/*;q=0.5

在这种情况下,当Firefox和Chrome发出图像请求时,它们不会通过路由器。
我应该怎么做,让请求能够通过呢?我正在使用Gorilla Mux作为路由器。

英文:

I wrote a route function, that accept only image request. Look at the following code.

// Add image route paths
func addImage(path string, handler httpImageFunc, name ...string) {
	//log.Println("Add image")
	if len(name) > 0 {
		router.Handle(path, httpImageFunc(handler)).
			Methods("GET").
			Headers("Accept", "image/*").
			Name(name[0])
	} else {
		router.Handle(path, httpImageFunc(handler)).
			Methods("GET").
			Headers("Accept", "image/*")
	}

}

As you can see, the requests accept only with header ("Accept", "image/*").
But chrome browser send a request with header

> Accept:image/webp,/;q=0.8

and firefox with

> Accept:image/png,image/;q=0.8,/*;q=0.5

In this case, when firefox and chrome make an image request, it does not go through the router.
What do I have to do, that the request would be get through? I am using gorilla mux as router.

答案1

得分: 1

使用MatcherFunc并自己检查标头,这样你就可以匹配"image/"。

英文:

Use MatcherFunc and inspect the header yourself so you can match on "image/".

huangapple
  • 本文由 发表于 2015年2月19日 15:03:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/28600424.html
匿名

发表评论

匿名网友

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

确定