summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/src/System.Web.WebPages/CookieBrowserOverrideStore.cs
blob: 24e1e6cc326dcc5b253f6bc38277c1ffbe35ce21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

namespace System.Web.WebPages
{
    /// <summary>
    /// The default BrowserOverrideStore. Gets overridden user agent for a request from a cookie.
    /// Creates a cookie to set the overridden user agent.
    /// </summary>
    public class CookieBrowserOverrideStore : BrowserOverrideStore
    {
        internal static readonly string BrowserOverrideCookieName = ".ASPXBrowserOverride";
        private readonly int _daysToExpire;

        /// <summary>
        /// Creates the BrowserOverrideStore setting any browser override cookie to expire in 7 days.
        /// </summary>
        public CookieBrowserOverrideStore()
            : this(daysToExpire: 7)
        {
        }

        /// <summary>
        /// Constructor to control the expiration of the browser override cookie.
        /// </summary>
        public CookieBrowserOverrideStore(int daysToExpire)
        {
            _daysToExpire = daysToExpire;
        }

        /// <summary>
        /// Looks for a user agent by searching for the browser override cookie. If no cookie is found
        /// returns null.
        /// </summary>
        public override string GetOverriddenUserAgent(HttpContextBase httpContext)
        {
            // Check the response to see if the cookie has been set somewhere in the current request.
            HttpCookieCollection responseCookies = httpContext.Response.Cookies;
            // NOTE: Only look for the key (via AllKeys) so a new cookie is not automatically created.
            string[] cookieNames = responseCookies.AllKeys;
            // NOTE: use a simple for loop since it performs an order of magnitude faster than .Contains()
            // and this is a hot path that gets executed for every request.
            for (int i = 0; i < cookieNames.Length; i++)
            {
                // HttpCookieCollection uses OrdinalIgnoreCase comparison for its keys
                if (String.Equals(cookieNames[i], BrowserOverrideCookieName, StringComparison.OrdinalIgnoreCase))
                {
                    HttpCookie currentOverriddenBrowserCookie = responseCookies[BrowserOverrideCookieName];

                    if (currentOverriddenBrowserCookie.Value != null)
                    {
                        return currentOverriddenBrowserCookie.Value;
                    }
                    else
                    {
                        // The cookie has no value. It was cleared on the current request so return null.
                        return null;
                    }
                }
            }

            // If there was no cookie found in the response check the request.
            var requestOverrideCookie = httpContext.Request.Cookies[BrowserOverrideCookieName];
            if (requestOverrideCookie != null)
            {
                return requestOverrideCookie.Value;
            }

            return null;
        }

        /// <summary>
        /// Adds a browser override cookie with the set user agent to the response of the current request.
        /// If the user agent is null the browser override cookie is set to expire, otherwise its expiration is set
        /// to daysToExpire, specified when CookieBasedOverrideStore is created.
        /// </summary>
        public override void SetOverriddenUserAgent(HttpContextBase httpContext, string userAgent)
        {
            HttpCookie browserOverrideCookie = new HttpCookie(BrowserOverrideCookieName, HttpUtility.UrlEncode(userAgent));

            if (userAgent == null)
            {
                browserOverrideCookie.Expires = DateTime.Now.AddDays(-1);
            }
            else
            {
                // Only set expiration if the cookie should live longer than the current session
                if (_daysToExpire > 0)
                {
                    browserOverrideCookie.Expires = DateTime.Now.AddDays(_daysToExpire);
                }
            }

            httpContext.Response.Cookies.Remove(BrowserOverrideCookieName);
            httpContext.Response.Cookies.Add(browserOverrideCookie);
        }
    }
}