minor: try to extract from both referer and referrer header and url query parameter

Former-commit-id: c0ed0916f35ee9cffe0b267e34d5708c1d38082b
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-06-11 01:58:35 +03:00
parent 07f678eac0
commit b4fcaab459
2 changed files with 18 additions and 10 deletions

View File

@ -8,8 +8,8 @@ func main() {
app := iris.New() app := iris.New()
app.Get("/", func(ctx iris.Context) { app.Get("/", func(ctx iris.Context) {
// GetReferrer extracts and returns the information from the "Referrer" header as specified // GetReferrer extracts and returns the information from the "Referer" (or "Referrer") header
// in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy or by the URL query parameter "referrer". // and url query parameter as specified in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy.
r := ctx.GetReferrer() r := ctx.GetReferrer()
switch r.Type { switch r.Type {
case iris.ReferrerSearch: case iris.ReferrerSearch:

View File

@ -405,9 +405,8 @@ type Context interface {
IsHTTP2() bool IsHTTP2() bool
// IsGRPC reports whether the request came from a gRPC client. // IsGRPC reports whether the request came from a gRPC client.
IsGRPC() bool IsGRPC() bool
// GetReferrer extracts and returns the information from the "Referrer" header as specified // GetReferrer extracts and returns the information from the "Referer" (or "Referrer") header
// in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy // and url query parameter as specified in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy.
// or by the URL query parameter "referrer".
GetReferrer() Referrer GetReferrer() Referrer
// SetLanguage force-sets the language for i18n, can be used inside a middleare. // SetLanguage force-sets the language for i18n, can be used inside a middleare.
// It has the highest priority over the rest and if it is empty then it is ignored, // It has the highest priority over the rest and if it is empty then it is ignored,
@ -2031,16 +2030,25 @@ const (
// unnecessary but good to know the default values upfront. // unnecessary but good to know the default values upfront.
var emptyReferrer = Referrer{Type: ReferrerInvalid, GoogleType: ReferrerNotGoogleSearch} var emptyReferrer = Referrer{Type: ReferrerInvalid, GoogleType: ReferrerNotGoogleSearch}
// GetReferrer extracts and returns the information from the "Referrer" header as specified // GetReferrer extracts and returns the information from the "Referer" (or "Referrer") header
// in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy // and url query parameter as specified in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy.
// or by the URL query parameter "referrer".
func (ctx *context) GetReferrer() Referrer { func (ctx *context) GetReferrer() Referrer {
// the underline net/http follows the https://tools.ietf.org/html/rfc7231#section-5.5.2, // the underline net/http follows the https://tools.ietf.org/html/rfc7231#section-5.5.2,
// so there is nothing special left to do. // so there is nothing special left to do.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
refURL := ctx.GetHeader("Referrer") refURL := ctx.GetHeader("Referer")
if refURL == "" { if refURL == "" {
refURL = ctx.URLParam("referrer") refURL = ctx.GetHeader("Referrer")
if refURL == "" {
refURL = ctx.URLParam("referer")
if refURL == "" {
refURL = ctx.URLParam("referrer")
}
}
}
if refURL == "" {
return emptyReferrer
} }
if ref := goreferrer.DefaultRules.Parse(refURL); ref.Type > goreferrer.Invalid { if ref := goreferrer.DefaultRules.Parse(refURL); ref.Type > goreferrer.Invalid {