From d576730d0806300f5596cd5499776d5d68d045ff Mon Sep 17 00:00:00 2001 From: Richard Tweed Date: Sun, 7 Jun 2020 22:16:35 +0100 Subject: [PATCH] Do not cache user agent (#98) --- http/http.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/http/http.go b/http/http.go index 09d1fca..7c9263a 100644 --- a/http/http.go +++ b/http/http.go @@ -94,6 +94,16 @@ func ipFromRequest(headers []string, r *http.Request) (net.IP, error) { return ip, nil } +func userAgentFromRequest(r *http.Request) *useragent.UserAgent { + var userAgent *useragent.UserAgent + userAgentRaw := r.UserAgent() + if userAgentRaw != "" { + parsed := useragent.Parse(userAgentRaw) + userAgent = &parsed + } + return userAgent +} + func (s *Server) newResponse(r *http.Request) (Response, error) { ip, err := ipFromRequest(s.IPHeaders, r) if err != nil { @@ -101,6 +111,8 @@ func (s *Server) newResponse(r *http.Request) (Response, error) { } response, ok := s.cache.Get(ip) if ok { + // Not Caching the userAgent as it can vary for a given IP + response.UserAgent = userAgentFromRequest(r) return *response, nil } ipDecimal := iputil.ToDecimal(ip) @@ -115,12 +127,7 @@ func (s *Server) newResponse(r *http.Request) (Response, error) { if asn.AutonomousSystemNumber > 0 { autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber) } - var userAgent *useragent.UserAgent - userAgentRaw := r.UserAgent() - if userAgentRaw != "" { - parsed := useragent.Parse(userAgentRaw) - userAgent = &parsed - } + userAgent := userAgentFromRequest(r) response = &Response{ IP: ip, IPDecimal: ipDecimal,