From b4fcaab459bdc9014e19029ef00d9b314b2cd9c3 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 11 Jun 2020 01:58:35 +0300 Subject: [PATCH] minor: try to extract from both referer and referrer header and url query parameter Former-commit-id: c0ed0916f35ee9cffe0b267e34d5708c1d38082b --- _examples/request-referrer/main.go | 4 ++-- context/context.go | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/_examples/request-referrer/main.go b/_examples/request-referrer/main.go index f471bd98..adfbe3b7 100644 --- a/_examples/request-referrer/main.go +++ b/_examples/request-referrer/main.go @@ -8,8 +8,8 @@ func main() { app := iris.New() app.Get("/", func(ctx iris.Context) { - // GetReferrer extracts and returns the information from the "Referrer" header as specified - // in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy or by the URL query parameter "referrer". + // GetReferrer extracts and returns the information from the "Referer" (or "Referrer") header + // and url query parameter as specified in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy. r := ctx.GetReferrer() switch r.Type { case iris.ReferrerSearch: diff --git a/context/context.go b/context/context.go index 146bc1e1..695956a5 100644 --- a/context/context.go +++ b/context/context.go @@ -405,9 +405,8 @@ type Context interface { IsHTTP2() bool // IsGRPC reports whether the request came from a gRPC client. IsGRPC() bool - // GetReferrer extracts and returns the information from the "Referrer" header as specified - // in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy - // or by the URL query parameter "referrer". + // GetReferrer extracts and returns the information from the "Referer" (or "Referrer") header + // and url query parameter as specified in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy. GetReferrer() Referrer // 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, @@ -2031,16 +2030,25 @@ const ( // unnecessary but good to know the default values upfront. var emptyReferrer = Referrer{Type: ReferrerInvalid, GoogleType: ReferrerNotGoogleSearch} -// GetReferrer extracts and returns the information from the "Referrer" header as specified -// in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy -// or by the URL query parameter "referrer". +// GetReferrer extracts and returns the information from the "Referer" (or "Referrer") header +// and url query parameter as specified in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy. func (ctx *context) GetReferrer() Referrer { // the underline net/http follows the https://tools.ietf.org/html/rfc7231#section-5.5.2, // so there is nothing special left to do. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy - refURL := ctx.GetHeader("Referrer") + refURL := ctx.GetHeader("Referer") 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 {