File: HttpTransportManager.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (259 lines) | stat: -rw-r--r-- 9,157 bytes parent folder | download | duplicates (9)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//----------------------------------------------------------------------------
namespace System.ServiceModel.Channels
{
    using System.Collections.Generic;
    using System.ServiceModel;
    using System.ServiceModel.Diagnostics;
    using System.Runtime;
    using System.ServiceModel.Diagnostics.Application;
    using System.Runtime.Diagnostics;

    abstract class HttpTransportManager : TransportManager, ITransportManagerRegistration
    {
        volatile Dictionary<string, UriPrefixTable<HttpChannelListener>> addressTables;
        readonly HostNameComparisonMode hostNameComparisonMode;
        readonly Uri listenUri;
        readonly string realm;

        internal HttpTransportManager()
        {
            this.addressTables = new Dictionary<string, UriPrefixTable<HttpChannelListener>>();
        }

        internal HttpTransportManager(Uri listenUri, HostNameComparisonMode hostNameComparisonMode)
            : this()
        {
            this.hostNameComparisonMode = hostNameComparisonMode;
            this.listenUri = listenUri;
        }

        internal HttpTransportManager(Uri listenUri, HostNameComparisonMode hostNameComparisonMode, string realm)
            : this(listenUri, hostNameComparisonMode)
        {
            this.realm = realm;
        }

        internal string Realm
        {
            get
            {
                return this.realm;
            }
        }

        public HostNameComparisonMode HostNameComparisonMode
        {
            get
            {
                return this.hostNameComparisonMode;
            }
        }

        // are we hosted in Asp.Net? Default is false.
        internal bool IsHosted
        {
            get;
            set;
        }

        internal override string Scheme
        {
            get
            {
                return Uri.UriSchemeHttp;
            }
        }

        internal virtual UriPrefixTable<ITransportManagerRegistration> TransportManagerTable
        {
            get
            {
                return HttpChannelListener.StaticTransportManagerTable;
            }
        }

        public Uri ListenUri
        {
            get
            {
                return this.listenUri;
            }
        }

        protected void Fault(Exception exception)
        {
            lock (ThisLock)
            {
                foreach (KeyValuePair<string, UriPrefixTable<HttpChannelListener>> pair in this.addressTables)
                {
                    this.Fault(pair.Value, exception);
                }
            }
        }

        internal virtual bool IsCompatible(HttpChannelListener listener)
        {
            return (
                (this.hostNameComparisonMode == listener.HostNameComparisonMode) &&
                (this.realm == listener.Realm)
                );
        }

        internal override void OnClose(TimeSpan timeout)
        {
            Cleanup();
        }

        internal override void OnAbort()
        {
            Cleanup();
            base.OnAbort();
        }

        void Cleanup()
        {
            this.TransportManagerTable.UnregisterUri(this.ListenUri, this.HostNameComparisonMode);
        }

        protected void StartReceiveBytesActivity(ServiceModelActivity activity, Uri requestUri)
        {
            Fx.Assert(DiagnosticUtility.ShouldUseActivity, "should only call this if we're using SM Activities");
            ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityReceiveBytes, requestUri.ToString()), ActivityType.ReceiveBytes);
        }

        protected void TraceMessageReceived(EventTraceActivity eventTraceActivity, Uri listenUri)
        {
            if (TD.HttpMessageReceiveStartIsEnabled())
            {                
                TD.HttpMessageReceiveStart(eventTraceActivity);
            }
        }

        protected bool TryLookupUri(Uri requestUri, string requestMethod,
                    HostNameComparisonMode hostNameComparisonMode, bool isWebSocketRequest, out HttpChannelListener listener)
        {
            listener = null;

            if (isWebSocketRequest)
            {
                Fx.Assert(StringComparer.OrdinalIgnoreCase.Compare(requestMethod, "GET") == 0, "The requestMethod must be GET in WebSocket case.");
                requestMethod = WebSocketTransportSettings.WebSocketMethod;
            }
            
            if (requestMethod == null)
            {
                requestMethod = string.Empty;
            }

            UriPrefixTable<HttpChannelListener> addressTable;
            Dictionary<string, UriPrefixTable<HttpChannelListener>> localAddressTables = addressTables;

            // check for a method match if necessary
            HttpChannelListener methodListener = null;
            if (requestMethod.Length > 0)
            {
                if (localAddressTables.TryGetValue(requestMethod, out addressTable))
                {
                    if (addressTable.TryLookupUri(requestUri, hostNameComparisonMode, out methodListener)
                        && string.Compare(requestUri.AbsolutePath, methodListener.Uri.AbsolutePath, StringComparison.OrdinalIgnoreCase) != 0)
                    {
                        methodListener = null;
                    }
                }
            }
            // and also check the wildcard bucket 
            if (localAddressTables.TryGetValue(string.Empty, out addressTable)
                && addressTable.TryLookupUri(requestUri, hostNameComparisonMode, out listener))
            {
                if (methodListener != null && methodListener.Uri.AbsoluteUri.Length >= listener.Uri.AbsoluteUri.Length)
                {
                    listener = methodListener;
                }
            }
            else
            {
                listener = methodListener;
            }

            return (listener != null);
        }



        internal override void Register(TransportChannelListener channelListener)
        {
            string method = ((HttpChannelListener)channelListener).Method;

            UriPrefixTable<HttpChannelListener> addressTable;
            if (!addressTables.TryGetValue(method, out addressTable))
            {
                lock (ThisLock)
                {
                    if (!addressTables.TryGetValue(method, out addressTable))
                    {
                        Dictionary<string, UriPrefixTable<HttpChannelListener>> newAddressTables =
                            new Dictionary<string, UriPrefixTable<HttpChannelListener>>(addressTables);

                        addressTable = new UriPrefixTable<HttpChannelListener>();
                        newAddressTables[method] = addressTable;

                        addressTables = newAddressTables;
                    }
                }
            }

            addressTable.RegisterUri(channelListener.Uri,
                channelListener.InheritBaseAddressSettings ? hostNameComparisonMode : channelListener.HostNameComparisonModeInternal,
                (HttpChannelListener)channelListener);
        }

        IList<TransportManager> ITransportManagerRegistration.Select(TransportChannelListener channelListener)
        {
            IList<TransportManager> result = null;
            if (this.IsCompatible((HttpChannelListener)channelListener))
            {
                result = new List<TransportManager>();
                result.Add(this);
            }
            return result;
        }

        internal override void Unregister(TransportChannelListener channelListener)
        {
            UriPrefixTable<HttpChannelListener> addressTable;
            if (!addressTables.TryGetValue(((HttpChannelListener)channelListener).Method, out addressTable))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(
                     SR.ListenerFactoryNotRegistered, channelListener.Uri)));
            }

            HostNameComparisonMode registeredMode = channelListener.InheritBaseAddressSettings ? hostNameComparisonMode : channelListener.HostNameComparisonModeInternal;

            EnsureRegistered(addressTable, (HttpChannelListener)channelListener, registeredMode);
            addressTable.UnregisterUri(channelListener.Uri, registeredMode);
        }

        protected class ActivityHolder : IDisposable
        {
            internal HttpRequestContext context;
            internal ServiceModelActivity activity;

            public ActivityHolder(ServiceModelActivity activity, HttpRequestContext requestContext)
            {
                Fx.Assert(requestContext != null, "requestContext cannot be null.");
                this.activity = activity;
                this.context = requestContext;
            }

            public void Dispose()
            {
                if (this.activity != null)
                {
                    this.activity.Dispose();
                }
            }
        }
    }
}