ContextProtocol.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.Xml;
  9. using System.Net;
  10. using System.IO;
  11. using System.Text;
  12. using System.Globalization;
  13. using System.Runtime.Serialization;
  14. using System.ServiceModel.Diagnostics;
  15. using System.Diagnostics;
  16. abstract class ContextProtocol
  17. {
  18. ContextExchangeMechanism contextExchangeMechanism;
  19. protected ContextProtocol(ContextExchangeMechanism contextExchangeMechanism)
  20. {
  21. if (!ContextExchangeMechanismHelper.IsDefined(contextExchangeMechanism))
  22. {
  23. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("contextExchangeMechanism"));
  24. }
  25. this.contextExchangeMechanism = contextExchangeMechanism;
  26. }
  27. protected ContextExchangeMechanism ContextExchangeMechanism
  28. {
  29. get { return this.contextExchangeMechanism; }
  30. }
  31. public abstract void OnIncomingMessage(Message message);
  32. public abstract void OnOutgoingMessage(Message message, RequestContext requestContext);
  33. protected void OnSendSoapContextHeader(Message message, ContextMessageProperty context)
  34. {
  35. if (message == null)
  36. {
  37. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  38. }
  39. if (context == null)
  40. {
  41. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  42. }
  43. if (context.Context.Count > 0)
  44. {
  45. message.Headers.Add(new ContextMessageHeader(context.Context));
  46. }
  47. if (DiagnosticUtility.ShouldTraceVerbose)
  48. {
  49. TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.ContextProtocolContextAddedToMessage,
  50. SR.GetString(SR.TraceCodeContextProtocolContextAddedToMessage), this);
  51. }
  52. }
  53. internal static class HttpCookieToolbox
  54. {
  55. public const string ContextHttpCookieName = "WscContext";
  56. public const string RemoveContextHttpCookieHeader = ContextHttpCookieName + ";Max-Age=0";
  57. public static string EncodeContextAsHttpSetCookieHeader(ContextMessageProperty context, Uri uri)
  58. {
  59. if (uri == null)
  60. {
  61. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("uri");
  62. }
  63. if (context == null)
  64. {
  65. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  66. }
  67. MemoryStream stream = new MemoryStream();
  68. XmlWriterSettings writerSettings = new XmlWriterSettings();
  69. writerSettings.OmitXmlDeclaration = true;
  70. XmlWriter writer = XmlWriter.Create(stream, writerSettings);
  71. ContextMessageHeader contextHeader = new ContextMessageHeader(context.Context);
  72. contextHeader.WriteHeader(writer, MessageVersion.Default);
  73. writer.Flush();
  74. string result = string.Format(
  75. CultureInfo.InvariantCulture,
  76. "{0}=\"{1}\";Path={2}",
  77. HttpCookieToolbox.ContextHttpCookieName,
  78. Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length),
  79. uri.AbsolutePath);
  80. return result;
  81. }
  82. public static bool TryCreateFromHttpCookieHeader(string httpCookieHeader, out ContextMessageProperty context)
  83. {
  84. if (httpCookieHeader == null)
  85. {
  86. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("httpCookieHeader");
  87. }
  88. context = null;
  89. foreach (string token in httpCookieHeader.Split(';'))
  90. {
  91. string trimmedToken = token.Trim();
  92. if (trimmedToken.StartsWith(HttpCookieToolbox.ContextHttpCookieName, StringComparison.Ordinal))
  93. {
  94. int equalsSignIndex = trimmedToken.IndexOf('=');
  95. if (equalsSignIndex < 0)
  96. {
  97. context = new ContextMessageProperty();
  98. break;
  99. }
  100. if (equalsSignIndex < (trimmedToken.Length - 1))
  101. {
  102. string value = trimmedToken.Substring(equalsSignIndex + 1).Trim();
  103. if (value.Length > 1 && (value[0] == '"') && (value[value.Length - 1] == '"'))
  104. {
  105. value = value.Substring(1, value.Length - 2);
  106. }
  107. try
  108. {
  109. context = ContextMessageHeader.ParseContextHeader(
  110. XmlReader.Create(new MemoryStream(Convert.FromBase64String(value))));
  111. break;
  112. }
  113. catch (SerializationException e)
  114. {
  115. DiagnosticUtility.TraceHandledException(e, TraceEventType.Warning);
  116. }
  117. catch (ProtocolException pe)
  118. {
  119. DiagnosticUtility.TraceHandledException(pe, TraceEventType.Warning);
  120. }
  121. }
  122. }
  123. }
  124. return context != null;
  125. }
  126. }
  127. }
  128. }