File: MessageRpc.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 (269 lines) | stat: -rw-r--r-- 10,352 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
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
260
261
262
263
264
265
266
267
268
269
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//----------------------------------------------------------------

namespace System.ServiceModel.Routing
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Globalization;
    using System.Runtime;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    using System.ServiceModel.Security;
    using System.Transactions;
    using SR2 = System.ServiceModel.Routing.SR;
    using System.Security.Principal;
    using System.Runtime.Diagnostics;
    using System.ServiceModel.Diagnostics;

    // This class wraps a Message, MessageBuffer (if requested), and the OperationContext
    // The message is not buffered if nobody calls MessageRpc.CreateBuffer.  If the message
    // is buffered, then we hang on to the buffer so it can be reused and Clone the message
    // which became invalid when we buffered this message.
    class MessageRpc
    {
        const int ERROR_BAD_IMPERSONATION_LEVEL = 1346;

        Message originalMessage;
        Message clonedMessage;
        MessageBuffer messageBuffer;
        string uniqueID;
        Transaction transaction;
        ReceiveContext receiveContext;
        IList<SendOperation> operations;
        WindowsIdentity windowsIdentity;
        EventTraceActivity eventTraceActivity;

        public MessageRpc(Message message, OperationContext operationContext, bool impersonationRequired)
        {
            Fx.Assert(message != null, "message cannot be null");
            Fx.Assert(operationContext != null, "operationContext cannot be null");

            this.originalMessage = message;
            this.OperationContext = operationContext;

            if (Fx.Trace.IsEtwProviderEnabled)
            {
                this.eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message);
            }

            //passing in true causes this to return null if the thread is not impersonating.
            this.windowsIdentity = WindowsIdentity.GetCurrent(true);
            if (impersonationRequired && !AspNetEnvironment.Current.AspNetCompatibilityEnabled)
            {
                if (this.windowsIdentity == null || this.windowsIdentity.ImpersonationLevel != TokenImpersonationLevel.Impersonation)
                {
                    //Temporarily revert impersonation to process token to throw an exception
                    IDisposable autoRevert = null;
                    try
                    {
                        try { }
                        finally
                        {
                            autoRevert = WindowsIdentity.Impersonate(IntPtr.Zero);
                        }

                        Win32Exception errorDetail = new Win32Exception(ERROR_BAD_IMPERSONATION_LEVEL);
                        throw FxTrace.Exception.AsError(new SecurityNegotiationException(errorDetail.Message));
                    }
                    finally
                    {
                        if (autoRevert != null)
                        {
                            autoRevert.Dispose();
                        }
                    }
                }
            }

            ReceiveContext.TryGet(message, out this.receiveContext);

            this.transaction = Transaction.Current;
            if (this.transaction == null)
            {
                this.transaction = TransactionMessageProperty.TryGetTransaction(message);
            }
        }

        internal bool Impersonating
        {
            get { return this.windowsIdentity != null; }
        }

        internal EventTraceActivity EventTraceActivity
        {
            get
            {
                return this.eventTraceActivity;
            }
        }

        public OperationContext OperationContext
        {
            get;
            private set;
        }

        public string UniqueID
        {
            get
            {
                if (this.uniqueID == null)
                {
                    if (this.Message.Version != MessageVersion.None &&
                        this.Message.Headers.MessageId != null)
                    {
                        this.uniqueID = this.originalMessage.Headers.MessageId.ToString();
                    }
                    else
                    {
                        this.uniqueID = this.GetHashCode().ToString(CultureInfo.InvariantCulture);
                    }
                }
                return this.uniqueID;
            }
        }

        public Message Message
        {
            get
            {
                // If we've created a MessageBuffer then the originalMessage has already been consumed
                if (this.messageBuffer != null)
                {
                    Fx.Assert(this.clonedMessage != null, "Need to set clonedMessage if we buffered the message");
                    return this.clonedMessage;
                }
                else
                {
                    // Haven't buffered, can use the original.
                    return this.originalMessage;
                }
            }
        }

        public ReceiveContext ReceiveContext
        {
            get { return this.receiveContext; }
        }

        public IList<SendOperation> Operations
        {
            get { return this.operations; }
        }

        public Transaction Transaction
        {
            get { return this.transaction; }
        }

        public MessageBuffer CreateBuffer()
        {
            if (this.messageBuffer == null)
            {
                this.messageBuffer = this.originalMessage.CreateBufferedCopy(int.MaxValue);
                this.clonedMessage = this.messageBuffer.CreateMessage();
            }
            return this.messageBuffer;
        }

        public IDisposable PrepareCall()
        {
            return new CallState(this);
        }

        public void RouteToSingleEndpoint<TContract>(RoutingConfiguration routingConfig)
        {
            IEnumerable<ServiceEndpoint> result;
            if (routingConfig.RouteOnHeadersOnly)
            {
                if (TD.RoutingServiceFilterTableMatchStartIsEnabled()) { TD.RoutingServiceFilterTableMatchStart(this.eventTraceActivity); }
                routingConfig.InternalFilterTable.GetMatchingValue(this.Message, out result);
                if (TD.RoutingServiceFilterTableMatchStopIsEnabled()) { TD.RoutingServiceFilterTableMatchStop(this.eventTraceActivity); }
            }
            else
            {
                MessageBuffer buffer = this.CreateBuffer();

                if (TD.RoutingServiceFilterTableMatchStartIsEnabled()) { TD.RoutingServiceFilterTableMatchStart(this.eventTraceActivity); }
                routingConfig.InternalFilterTable.GetMatchingValue(buffer, out result);
                if (TD.RoutingServiceFilterTableMatchStopIsEnabled()) { TD.RoutingServiceFilterTableMatchStop(this.eventTraceActivity); }
            }

            if (result == null)
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.NoFilterMatched));
            }

            if (TD.RoutingServiceMessageRoutedToEndpointsIsEnabled())
            {
                TD.RoutingServiceMessageRoutedToEndpoints(this.eventTraceActivity, this.UniqueID, "1");
            }

            this.operations = new List<SendOperation>(1);
            this.operations.Add(new SendOperation(result, typeof(TContract), this.OperationContext));
        }

        public void RouteToEndpoints<TContract>(RoutingConfiguration routingConfig)
        {
            List<IEnumerable<ServiceEndpoint>> endpointLists = new List<IEnumerable<ServiceEndpoint>>();
            if (routingConfig.RouteOnHeadersOnly)
            {
                if (TD.RoutingServiceFilterTableMatchStartIsEnabled()) { TD.RoutingServiceFilterTableMatchStart(this.eventTraceActivity); }
                routingConfig.InternalFilterTable.GetMatchingValues(this.Message, endpointLists);
                if (TD.RoutingServiceFilterTableMatchStopIsEnabled()) { TD.RoutingServiceFilterTableMatchStop(this.eventTraceActivity); }
            }
            else
            {
                MessageBuffer messageBuffer = this.CreateBuffer();

                if (TD.RoutingServiceFilterTableMatchStartIsEnabled()) { TD.RoutingServiceFilterTableMatchStart(this.eventTraceActivity); }
                routingConfig.InternalFilterTable.GetMatchingValues(messageBuffer, endpointLists);
                if (TD.RoutingServiceFilterTableMatchStopIsEnabled()) { TD.RoutingServiceFilterTableMatchStop(this.eventTraceActivity); }
            }
            
            if (TD.RoutingServiceMessageRoutedToEndpointsIsEnabled())
            {
                TD.RoutingServiceMessageRoutedToEndpoints(this.eventTraceActivity, this.UniqueID, endpointLists.Count.ToString(TD.Culture));
            }
            if (endpointLists.Count == 0)
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.NoFilterMatched));
            }
            this.operations = new List<SendOperation>(endpointLists.Count);
            foreach (IEnumerable<ServiceEndpoint> endpointList in endpointLists)
            {
                this.operations.Add(new SendOperation(endpointList, typeof(TContract), this.OperationContext));
            }
        }

        class CallState : IDisposable
        {
            OperationContextScope nullContextScope;
            WindowsImpersonationContext impersonation;

            public CallState (MessageRpc messageRpc)
            {
                this.nullContextScope = new OperationContextScope((OperationContext)null);

                if (messageRpc.windowsIdentity != null)
                {
                    this.impersonation = messageRpc.windowsIdentity.Impersonate();
                }
            }

            void IDisposable.Dispose()
            {
                if (this.impersonation != null)
                {
                    this.impersonation.Dispose();
                }
                this.nullContextScope.Dispose();
            }
        }
    }
}