HTTP 引荐来源网址(最初是 **referrer** 的拼写错误) 是一个可选的 HTTP 标头字段,用于标识链接到所请求资源的网页地址(即URI或IRI)。通过检查引荐来源网址,新网页可以查看请求的来源。
在 [wikipedia](https://en.wikipedia.org/wiki/HTTP_referer) 上了解更多信息。
Iris 使用 [Shopify's goreferrer](https://github.com/Shopify/goreferrer/pull/27) 包去公开 `Context.GetReferrer()` 方法。
GetReferrer Context 的方法提取和返回这些信息,从 `"Referer" `头如在 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy 或通过URL查询参数 `"referer"`。
```
GetReferrer() Referrer
```
`Referrer` 如下所示:
```
type (
Referrer struct {
Type ReferrerType
Label string
URL string
Subdomain string
Domain string
Tld string
Path string
Query string
GoogleType ReferrerGoogleSearchType
}
```
`ReferrerType` 是 Referrer.Type 值 (间接,直接,电子邮件,搜索,社交) 的枚举。可用的类型有:
```
ReferrerInvalid
ReferrerIndirect
ReferrerDirect
ReferrerEmail
ReferrerSearch
ReferrerSocial
```
`GoogleType` 可以是以下之一:
```
ReferrerNotGoogleSearch
ReferrerGoogleOrganicSearch
ReferrerGoogleAdwords
```
## 示例
```
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
r := ctx.GetReferrer()
switch r.Type {
case iris.ReferrerSearch:
ctx.Writef("Search %s: %s\n", r.Label, r.Query)
ctx.Writef("Google: %s\n", r.GoogleType)
case iris.ReferrerSocial:
ctx.Writef("Social %s\n", r.Label)
case iris.ReferrerIndirect:
ctx.Writef("Indirect: %s\n", r.URL)
}
})
app.Run(iris.Addr(":8080"))
}
```
如何 `curl`:
```
curl http://localhost:8080?\
referer=https://twitter.com/Xinterio/status/1023566830974251008
curl http://localhost:8080?\
referer=https://www.google.com/search?q=Top+6+golang+web+frameworks\
&oq=Top+6+golang+web+frameworks
```