feat: add referer

This commit is contained in:
franklin 2023-06-01 11:44:14 +02:00
parent 6f5d8f9253
commit 8366985ca3
2 changed files with 18 additions and 0 deletions

View File

@ -48,6 +48,9 @@ const (
// HTTPTrackingIDKey represents the HTTP tracking id if known (e.g. from X-Tracking-ID). // HTTPTrackingIDKey represents the HTTP tracking id if known (e.g. from X-Tracking-ID).
HTTPTrackingIDKey = "http_tracking_id" HTTPTrackingIDKey = "http_tracking_id"
// HTTPRefererKey identifies the address of the web page (i.e., the URI or IRI), from which the resource has been requested.
HTTPRefererKey = "http_referer"
) )
func FHTTPServerName(id string) zap.Field { func FHTTPServerName(id string) zap.Field {
@ -98,6 +101,10 @@ func FHTTPUserAgent(userAgent string) zap.Field {
return zap.String(HTTPUserAgentKey, userAgent) return zap.String(HTTPUserAgentKey, userAgent)
} }
func FHTTPReferer(host string) zap.Field {
return zap.String(HTTPRefererKey, host)
}
func FHTTPHost(host string) zap.Field { func FHTTPHost(host string) zap.Field {
return zap.String(HTTPHostKey, host) return zap.String(HTTPHostKey, host)
} }

View File

@ -77,6 +77,16 @@ func WithHTTPRequestID(l *zap.Logger, r *http.Request) *zap.Logger {
} }
} }
func WithHTTPReferer(l *zap.Logger, r *http.Request) *zap.Logger {
if value := r.Header.Get("X-Referer"); value != "" {
return With(l, FHTTPReferer(value))
} else if value := r.Referer(); value != "" {
return With(l, FHTTPHost(value))
} else {
return l
}
}
func WithHTTPHost(l *zap.Logger, r *http.Request) *zap.Logger { func WithHTTPHost(l *zap.Logger, r *http.Request) *zap.Logger {
if value := r.Header.Get("X-Forwarded-Host"); value != "" { if value := r.Header.Get("X-Forwarded-Host"); value != "" {
return With(l, FHTTPHost(value)) return With(l, FHTTPHost(value))
@ -120,6 +130,7 @@ func WithHTTPClientIP(l *zap.Logger, r *http.Request) *zap.Logger {
func WithHTTPRequest(l *zap.Logger, r *http.Request) *zap.Logger { func WithHTTPRequest(l *zap.Logger, r *http.Request) *zap.Logger {
l = WithHTTPHost(l, r) l = WithHTTPHost(l, r)
l = WithHTTPReferer(l, r)
l = WithHTTPRequestID(l, r) l = WithHTTPRequestID(l, r)
l = WithHTTPSessionID(l, r) l = WithHTTPSessionID(l, r)
l = WithHTTPTrackingID(l, r) l = WithHTTPTrackingID(l, r)