ClientContextProtocol.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Net;
  9. using System.Runtime;
  10. using System.ServiceModel;
  11. using System.ServiceModel.Diagnostics;
  12. class ClientContextProtocol : ContextProtocol, IContextManager
  13. {
  14. ContextMessageProperty context;
  15. bool contextInitialized;
  16. bool contextManagementEnabled;
  17. CookieContainer cookieContainer;
  18. IChannel owner;
  19. object thisLock;
  20. Uri uri;
  21. Uri callbackAddress;
  22. public ClientContextProtocol(ContextExchangeMechanism contextExchangeMechanism, Uri uri, IChannel owner, Uri callbackAddress, bool contextManagementEnabled)
  23. : base(contextExchangeMechanism)
  24. {
  25. if (contextExchangeMechanism == ContextExchangeMechanism.HttpCookie)
  26. {
  27. this.cookieContainer = new CookieContainer();
  28. }
  29. this.context = ContextMessageProperty.Empty;
  30. this.contextManagementEnabled = contextManagementEnabled;
  31. this.owner = owner;
  32. this.thisLock = new object();
  33. this.uri = uri;
  34. this.callbackAddress = callbackAddress;
  35. }
  36. protected Uri Uri
  37. {
  38. get
  39. {
  40. return this.uri;
  41. }
  42. }
  43. bool IContextManager.Enabled
  44. {
  45. get
  46. {
  47. return this.contextManagementEnabled;
  48. }
  49. set
  50. {
  51. if (this.owner.State != CommunicationState.Created)
  52. {
  53. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  54. new InvalidOperationException(
  55. SR.GetString(SR.ChannelIsOpen)
  56. ));
  57. }
  58. this.contextManagementEnabled = value;
  59. }
  60. }
  61. public IDictionary<string, string> GetContext()
  62. {
  63. if (!this.contextManagementEnabled)
  64. {
  65. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  66. new InvalidOperationException(
  67. SR.GetString(SR.ContextManagementNotEnabled)
  68. ));
  69. }
  70. return new Dictionary<string, string>(this.GetCurrentContext().Context);
  71. }
  72. public override void OnIncomingMessage(Message message)
  73. {
  74. if (message == null)
  75. {
  76. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  77. }
  78. ContextMessageProperty incomingContext = null;
  79. if (this.ContextExchangeMechanism == ContextExchangeMechanism.HttpCookie)
  80. {
  81. incomingContext = this.OnReceiveHttpCookies(message);
  82. }
  83. else
  84. {
  85. incomingContext = this.OnReceiveSoapContextHeader(message);
  86. }
  87. if (incomingContext != null)
  88. {
  89. if (this.contextManagementEnabled)
  90. {
  91. EnsureInvariants(true, incomingContext);
  92. }
  93. else
  94. {
  95. incomingContext.AddOrReplaceInMessage(message);
  96. }
  97. }
  98. // verify that the callback context was not sent on an incoming message
  99. if (message.Headers.FindHeader(CallbackContextMessageHeader.CallbackContextHeaderName, CallbackContextMessageHeader.CallbackContextHeaderNamespace) != -1)
  100. {
  101. throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new ProtocolException(SR.GetString(SR.CallbackContextNotExpectedOnIncomingMessageAtClient, message.Headers.Action, CallbackContextMessageHeader.CallbackContextHeaderName, CallbackContextMessageHeader.CallbackContextHeaderNamespace)));
  102. }
  103. }
  104. public override void OnOutgoingMessage(Message message, RequestContext requestContext)
  105. {
  106. if (message == null)
  107. {
  108. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  109. }
  110. ContextMessageProperty outgoingContext = null;
  111. if (ContextMessageProperty.TryGet(message, out outgoingContext))
  112. {
  113. if (this.contextManagementEnabled)
  114. {
  115. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  116. new InvalidOperationException(
  117. SR.GetString(SR.InvalidMessageContext)));
  118. }
  119. }
  120. if (this.ContextExchangeMechanism == ContextExchangeMechanism.ContextSoapHeader)
  121. {
  122. if (this.contextManagementEnabled)
  123. {
  124. outgoingContext = GetCurrentContext();
  125. }
  126. if (outgoingContext != null)
  127. {
  128. this.OnSendSoapContextHeader(message, outgoingContext);
  129. }
  130. }
  131. else if (this.ContextExchangeMechanism == ContextExchangeMechanism.HttpCookie)
  132. {
  133. if (this.contextManagementEnabled)
  134. {
  135. this.OnSendHttpCookies(message, null);
  136. }
  137. else
  138. {
  139. this.OnSendHttpCookies(message, outgoingContext);
  140. }
  141. }
  142. // serialize the callback context if the property was supplied
  143. CallbackContextMessageProperty callbackContext;
  144. if (CallbackContextMessageProperty.TryGet(message, out callbackContext))
  145. {
  146. // see if the callbackaddress is already set on the CCMP, if it is set use that
  147. // else if callbackaddress is set on the binding, use that
  148. EndpointAddress callbackAddress = callbackContext.CallbackAddress;
  149. if (callbackAddress == null && this.callbackAddress != null)
  150. {
  151. callbackAddress = callbackContext.CreateCallbackAddress(this.callbackAddress);
  152. }
  153. // add the CallbackContextMessageHeader only if we have a valid CallbackAddress
  154. if (callbackAddress != null)
  155. {
  156. if (this.ContextExchangeMechanism != ContextExchangeMechanism.ContextSoapHeader)
  157. {
  158. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CallbackContextOnlySupportedInSoap)));
  159. }
  160. message.Headers.Add(new CallbackContextMessageHeader(callbackAddress, message.Version.Addressing));
  161. }
  162. }
  163. }
  164. public void SetContext(IDictionary<string, string> context)
  165. {
  166. if (context == null)
  167. {
  168. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  169. }
  170. ContextMessageProperty newContext = new ContextMessageProperty(context);
  171. EnsureInvariants(false, newContext);
  172. if (this.ContextExchangeMechanism == ContextExchangeMechanism.HttpCookie)
  173. {
  174. lock (this.cookieContainer)
  175. {
  176. this.cookieContainer.SetCookies(this.Uri, GetCookieHeaderFromContext(newContext));
  177. }
  178. }
  179. }
  180. //Called to update local context
  181. //1) From SetContext(to update client provided context)
  182. //2) From OnReceive*(to update server issued)
  183. void EnsureInvariants(bool isServerIssued, ContextMessageProperty newContext)
  184. {
  185. //Cannot SetContext when ContextManagement not enabled.
  186. if (!this.contextManagementEnabled)
  187. {
  188. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  189. new InvalidOperationException(
  190. SR.GetString(SR.ContextManagementNotEnabled)
  191. ));
  192. }
  193. //Cannot reset context after initialized in server case.
  194. if ((isServerIssued && !this.contextInitialized) ||
  195. (this.owner.State == CommunicationState.Created))
  196. {
  197. lock (this.thisLock)
  198. {
  199. if ((isServerIssued && !this.contextInitialized) ||
  200. (this.owner.State == CommunicationState.Created))
  201. {
  202. this.context = newContext;
  203. this.contextInitialized = true;
  204. return;
  205. }
  206. }
  207. }
  208. if (isServerIssued)
  209. {
  210. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  211. new ProtocolException(
  212. SR.GetString(SR.InvalidContextReceived)
  213. ));
  214. }
  215. else
  216. {
  217. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  218. new InvalidOperationException(
  219. SR.GetString(SR.CachedContextIsImmutable)
  220. ));
  221. }
  222. }
  223. string GetCookieHeaderFromContext(ContextMessageProperty contextMessageProperty)
  224. {
  225. if (contextMessageProperty.Context.Count == 0)
  226. {
  227. return HttpCookieToolbox.RemoveContextHttpCookieHeader;
  228. }
  229. else
  230. {
  231. return HttpCookieToolbox.EncodeContextAsHttpSetCookieHeader(contextMessageProperty, this.Uri);
  232. }
  233. }
  234. ContextMessageProperty GetCurrentContext()
  235. {
  236. ContextMessageProperty result;
  237. if (this.cookieContainer != null)
  238. {
  239. lock (this.cookieContainer)
  240. {
  241. // This is to allow for the possibility of that the cookie has expired
  242. if (this.cookieContainer.GetCookies(this.Uri)[HttpCookieToolbox.ContextHttpCookieName] == null)
  243. {
  244. result = ContextMessageProperty.Empty;
  245. }
  246. else
  247. {
  248. result = this.context;
  249. }
  250. }
  251. }
  252. else
  253. {
  254. result = this.context;
  255. }
  256. return result;
  257. }
  258. ContextMessageProperty OnReceiveHttpCookies(Message message)
  259. {
  260. ContextMessageProperty newContext = null;
  261. object property;
  262. if (message.Properties.TryGetValue(HttpResponseMessageProperty.Name, out property))
  263. {
  264. HttpResponseMessageProperty httpResponse = property as HttpResponseMessageProperty;
  265. if (httpResponse != null)
  266. {
  267. string setCookieHeader = httpResponse.Headers[HttpResponseHeader.SetCookie];
  268. if (!string.IsNullOrEmpty(setCookieHeader))
  269. {
  270. lock (this.cookieContainer)
  271. {
  272. if (!string.IsNullOrEmpty(setCookieHeader))
  273. {
  274. this.cookieContainer.SetCookies(this.Uri, setCookieHeader);
  275. HttpCookieToolbox.TryCreateFromHttpCookieHeader(setCookieHeader, out newContext);
  276. }
  277. if (!this.contextManagementEnabled)
  278. {
  279. this.cookieContainer.SetCookies(this.Uri, HttpCookieToolbox.RemoveContextHttpCookieHeader);
  280. }
  281. }
  282. }
  283. }
  284. }
  285. return newContext;
  286. }
  287. ContextMessageProperty OnReceiveSoapContextHeader(Message message)
  288. {
  289. ContextMessageProperty messageProperty = ContextMessageHeader.GetContextFromHeaderIfExists(message);
  290. if (messageProperty != null)
  291. {
  292. if (DiagnosticUtility.ShouldTraceVerbose)
  293. {
  294. TraceUtility.TraceEvent(System.Diagnostics.TraceEventType.Verbose,
  295. TraceCode.ContextProtocolContextRetrievedFromMessage, SR.GetString(SR.TraceCodeContextProtocolContextRetrievedFromMessage),
  296. this);
  297. }
  298. }
  299. return messageProperty;
  300. }
  301. void OnSendHttpCookies(Message message, ContextMessageProperty context)
  302. {
  303. string cookieHeader = null;
  304. if (this.contextManagementEnabled || context == null)
  305. {
  306. Fx.Assert(context == null, "Context should be null");
  307. lock (this.cookieContainer)
  308. {
  309. cookieHeader = this.cookieContainer.GetCookieHeader(this.Uri);
  310. }
  311. }
  312. else
  313. {
  314. if (context != null) //User provided context is not null.
  315. {
  316. string contextCookieHeader = this.GetCookieHeaderFromContext(context);
  317. lock (this.cookieContainer)
  318. {
  319. this.cookieContainer.SetCookies(this.Uri, contextCookieHeader);
  320. cookieHeader = this.cookieContainer.GetCookieHeader(this.Uri);
  321. this.cookieContainer.SetCookies(this.Uri, HttpCookieToolbox.RemoveContextHttpCookieHeader);
  322. }
  323. }
  324. }
  325. if (!string.IsNullOrEmpty(cookieHeader))
  326. {
  327. object tmpProperty;
  328. HttpRequestMessageProperty property = null;
  329. if (message.Properties.TryGetValue(HttpRequestMessageProperty.Name, out tmpProperty))
  330. {
  331. property = tmpProperty as HttpRequestMessageProperty;
  332. }
  333. if (property == null)
  334. {
  335. property = new HttpRequestMessageProperty();
  336. message.Properties.Add(HttpRequestMessageProperty.Name, property);
  337. }
  338. property.Headers.Add(HttpRequestHeader.Cookie, cookieHeader);
  339. }
  340. }
  341. }
  342. }