/// <summary> /// Returns the <see cref="UniqueVisitor" /> instance from the current /// HTTP context for the end user making the request generating a new /// instance as necessary. /// </summary> /// <returns>The <see cref="UniqueVisitor" /> instance.</returns> /// <exception cref="InvalidOperationException">Thrown if the method is not called within the processing context of a HTTP request.</exception> /// <remarks> /// <para> /// This method retrieves the visitor identifier from the <b>__lt-uv</b> /// cookie in the request and if necessary, adds newly generated cookies /// to the HTTP response. /// </para> /// <note> /// Unique visitor instances <b>will not</b> be created for known web crawlers. /// </note> /// </remarks> public static UniqueVisitor GetUniqueVisitor() { HttpCookie cookie; UniqueVisitor visitor; if (HttpContext.Current == null) { throw new InvalidOperationException(NoHttpContextMsg); } if (HttpContext.Current.Request.Browser.Crawler) { return(null); } cookie = GetCookie(UniqueVisitorCookie); if (cookie == null) { // There is no visitor cookie so generate a new visitor ID // and send it back in the HTTP response with an approximate // 100 year lifetime (essentially forever). visitor = new UniqueVisitor(); cookie = new HttpCookie(UniqueVisitorCookie, visitor.ToString()); cookie.Expires = DateTime.UtcNow + TimeSpan.FromDays(365 * 100); SetCookie(cookie); return(visitor); } try { return(new UniqueVisitor(cookie.Value)); } catch (Exception e) { SysLog.LogException(e, "Probable invalid HTTP visitor cookie. Generating a new cookie."); visitor = new UniqueVisitor(); cookie = new HttpCookie(UniqueVisitorCookie, visitor.ToString()); cookie.Expires = DateTime.UtcNow + TimeSpan.FromDays(365 * 100); SetCookie(cookie); return(visitor); } }
/// <summary> /// Constructor. /// </summary> /// <param name="visitorCookie">The unique visitor cookie or <c>null</c>.</param> /// <param name="logTag">The log tag.</param> /// <param name="logEntry">The log entry.</param> internal LogAnalyzerArgs(string visitorCookie, string logTag, string logEntry) { this.VisitorCookie = visitorCookie; this.LogTag = logTag; this.LogEntry = logEntry; if (!logTag.StartsWith("PageView") && !logTag.StartsWith("BotView")) { return; // Not for a HTTP request. } // Process the HTTP request line and headers. using (var reader = new StringReader(logEntry)) { string line; string[] fields; char[] headerSplit = new char[] { ':' }; line = reader.ReadLine(); fields = line.Split(' '); Method = fields[0].ToUpper(); Path = fields[1]; Headers = new ArgCollection(ArgCollectionType.Unconstrained); for (line = reader.ReadLine(); line != null; line = reader.ReadLine()) { fields = line.Split(headerSplit, 2); Headers[fields[0].Trim()] = fields[1].Trim(); } Headers.IsReadOnly = true; } // Extract the common header values. this.Host = Headers["Host"]; IPAddress address; if (Headers.ContainsKey("X-Remote-Address") && IPAddress.TryParse(Headers["X-Remote-Address"], out address)) { this.RemoteAddress = address; } if (Headers.ContainsKey("Referer")) { try { this.Referer = new Uri(Headers["Referer"]); } catch { // Ignore } } // Try to extract the standard LillTek unique visitor cookie if we didn't have // one passed. string cookieHeader; if (VisitorCookie == null && Headers.TryGetValue("Cookie", out cookieHeader)) { try { var cookies = new ArgCollection(cookieHeader, '=', ';'); string visitor; if (cookies.TryGetValue(WebHelper.UniqueVisitorCookie, out visitor)) { try { VisitorCookie = new UniqueVisitor(visitor).ID.ToString("D").ToUpper(); } catch { VisitorCookie = visitor; } } } catch (Exception e) { SysLog.LogException(e); } } }