File: BrowserCapabilitiesFactoryBase.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (172 lines) | stat: -rw-r--r-- 6,023 bytes parent folder | download | duplicates (7)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//------------------------------------------------------------------------------
// <copyright file="BrowserCapabilitiesFactoryBase.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

/*
 * Base class for browser capabilities object: just a read-only dictionary
 * holder that supports Init()
 *
 * 


*/

using System.Web.UI;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Util;

namespace System.Web.Configuration {

    public class BrowserCapabilitiesFactoryBase {

        private IDictionary _matchedHeaders;
        private IDictionary _browserElements;
        private object _lock = new object();

        public BrowserCapabilitiesFactoryBase() {
        }

        [EditorBrowsable(EditorBrowsableState.Advanced)]
        protected IDictionary BrowserElements {
            get {
                if (_browserElements == null)
                    lock (_lock) {
                        if (_browserElements == null) {
                            Hashtable browserElements = Hashtable.Synchronized(new Hashtable(StringComparer.OrdinalIgnoreCase));
                            PopulateBrowserElements(browserElements);
                            _browserElements = browserElements;
                        }
                    }

                return _browserElements;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced)]
        protected virtual void PopulateBrowserElements(IDictionary dictionary) {
        }

        internal IDictionary InternalGetMatchedHeaders() {
            return MatchedHeaders;
        }

        internal IDictionary InternalGetBrowserElements() {
            return BrowserElements;
        }

        [EditorBrowsable(EditorBrowsableState.Advanced)]
        protected IDictionary MatchedHeaders {
            get {
                if (_matchedHeaders == null)
                    lock (_lock) {
                        if (_matchedHeaders == null) {
                            Hashtable matchedHeaders = Hashtable.Synchronized(new Hashtable(24, StringComparer.OrdinalIgnoreCase));
                            PopulateMatchedHeaders(matchedHeaders);
                            _matchedHeaders = matchedHeaders;
                        }
                    }

                return _matchedHeaders;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Advanced)]
        protected virtual void PopulateMatchedHeaders(IDictionary dictionary) {
        }

        internal int CompareFilters(string filter1, string filter2) {
            bool isFilter1DefaultFilter = String.IsNullOrEmpty(filter1);
            bool isFilter2DefaultFilter = String.IsNullOrEmpty(filter2);

            IDictionary browsers = BrowserElements;
            bool filter1Exists = (browsers.Contains(filter1)) || isFilter1DefaultFilter;
            bool filter2Exists = (browsers.Contains(filter2)) || isFilter2DefaultFilter;

            if (!filter1Exists) {
                if (!filter2Exists) {
                    return 0;
                }
                else {
                    return -1;
                }
            }
            else {
                if (!filter2Exists) {
                    return 1;
                }
            }

            if (isFilter1DefaultFilter && !isFilter2DefaultFilter) {
                return 1;
            }

            if (isFilter2DefaultFilter && !isFilter1DefaultFilter) {
                return -1;
            }

            if (isFilter1DefaultFilter && isFilter2DefaultFilter) {
                return 0;
            }

            int filter1Depth = (int)((Triplet)BrowserElements[filter1]).Third;
            int filter2Depth = (int)((Triplet)BrowserElements[filter2]).Third;

            return filter2Depth - filter1Depth;
        }

        public virtual void ConfigureBrowserCapabilities(NameValueCollection headers, HttpBrowserCapabilities browserCaps) {
        }

        // CodeGenerator will override this function to declare custom browser capabilities
        public virtual void ConfigureCustomCapabilities(NameValueCollection headers, HttpBrowserCapabilities browserCaps) {
        }

        internal static string GetBrowserCapKey(IDictionary headers, HttpRequest request) {
            StringBuilder sb = new StringBuilder();
            foreach(String key in headers.Keys) {
                if (key.Length == 0) {
                    sb.Append(HttpCapabilitiesDefaultProvider.GetUserAgent(request));
                }
                else {
                    sb.Append(request.Headers[key]);
                }

                sb.Append("\n");
            }

            return sb.ToString();
        }

        internal HttpBrowserCapabilities GetHttpBrowserCapabilities(HttpRequest request) {
            if (request == null)
                throw new ArgumentNullException("request");

            NameValueCollection headers = request.Headers;
            HttpBrowserCapabilities browserCaps = new HttpBrowserCapabilities();
            Hashtable values = new Hashtable(180, StringComparer.OrdinalIgnoreCase);
            values[String.Empty] = HttpCapabilitiesDefaultProvider.GetUserAgent(request);
            browserCaps.Capabilities = values;
            ConfigureBrowserCapabilities(headers, browserCaps);
            ConfigureCustomCapabilities(headers, browserCaps);

            return browserCaps;
        }

        protected bool IsBrowserUnknown(HttpCapabilitiesBase browserCaps) {
            // We want to ignore the "Default" node, which will also be matched.
            if(browserCaps.Browsers == null || browserCaps.Browsers.Count <= 1) {
                return true;
            }

            return false;
        }
    }
}