TransportBindingElement.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // TransportBindingElement.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. // Martin Baulig <[email protected]>
  7. //
  8. // Copyright (C) 2005, 2007 Novell, Inc. http://www.novell.com
  9. // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Net;
  33. using System.ServiceModel.Channels;
  34. using System.ServiceModel.Description;
  35. using System.ServiceModel.Security;
  36. using System.Xml;
  37. namespace System.ServiceModel.Channels
  38. {
  39. public abstract class TransportBindingElement : BindingElement
  40. {
  41. bool manual_addressing;
  42. long max_buffer_pool_size = 0x80000;
  43. long max_recv_message_size = 0x10000;
  44. protected TransportBindingElement ()
  45. {
  46. }
  47. protected TransportBindingElement (
  48. TransportBindingElement other)
  49. : base (other)
  50. {
  51. manual_addressing = other.manual_addressing;
  52. max_buffer_pool_size = other.max_buffer_pool_size;
  53. max_recv_message_size = other.max_recv_message_size;
  54. }
  55. public virtual bool ManualAddressing {
  56. get { return manual_addressing; }
  57. set { manual_addressing = value; }
  58. }
  59. public virtual long MaxBufferPoolSize {
  60. get { return max_buffer_pool_size; }
  61. set { max_buffer_pool_size = value; }
  62. }
  63. public virtual long MaxReceivedMessageSize {
  64. get { return max_recv_message_size; }
  65. set { max_recv_message_size = value; }
  66. }
  67. public abstract string Scheme { get; }
  68. public override T GetProperty<T> (BindingContext context)
  69. {
  70. if (typeof (T) == typeof (XmlDictionaryReaderQuotas)) {
  71. XmlDictionaryReaderQuotas q =
  72. new XmlDictionaryReaderQuotas ();
  73. q.MaxStringContentLength = (int) MaxReceivedMessageSize;
  74. return (T) (object) q;
  75. }
  76. #if !MOBILE
  77. if (typeof (T) == typeof (ChannelProtectionRequirements))
  78. // blank one, basically it should not be used
  79. // for any secure channels (
  80. return (T) (object) new ChannelProtectionRequirements ();
  81. #endif
  82. if (typeof (T) == typeof (MessageVersion))
  83. return (T) (object) MessageVersion.Soap12WSAddressing10;
  84. return context.GetInnerProperty<T> ();
  85. }
  86. #if !MOBILE && !XAMMAC_4_5
  87. internal static XmlElement CreateTransportBinding (XmlElement transportToken)
  88. {
  89. var doc = new XmlDocument ();
  90. var transportBinding = doc.CreateElement (
  91. "sp", "TransportBinding", PolicyImportHelper.SecurityPolicyNS);
  92. var token = doc.CreateElement (
  93. "sp", "TransportToken", PolicyImportHelper.SecurityPolicyNS);
  94. PolicyImportHelper.AddWrappedPolicyElement (token, transportToken);
  95. var algorithmSuite = doc.CreateElement (
  96. "sp", "AlgorithmSuite", PolicyImportHelper.SecurityPolicyNS);
  97. var basic256 = doc.CreateElement (
  98. "sp", "Basic256", PolicyImportHelper.SecurityPolicyNS);
  99. PolicyImportHelper.AddWrappedPolicyElement (algorithmSuite, basic256);
  100. var layout = doc.CreateElement (
  101. "sp", "Layout", PolicyImportHelper.SecurityPolicyNS);
  102. var strict = doc.CreateElement (
  103. "sp", "Strict", PolicyImportHelper.SecurityPolicyNS);
  104. PolicyImportHelper.AddWrappedPolicyElement (layout, strict);
  105. PolicyImportHelper.AddWrappedPolicyElements (
  106. transportBinding, token, algorithmSuite, layout);
  107. return transportBinding;
  108. }
  109. internal static MessageEncodingBindingElement ExportAddressingPolicy (
  110. PolicyConversionContext context)
  111. {
  112. MessageEncodingBindingElement messageEncodingElement = null;
  113. foreach (var element in context.BindingElements) {
  114. var check = element as MessageEncodingBindingElement;
  115. if (check == null)
  116. continue;
  117. messageEncodingElement = check;
  118. break;
  119. }
  120. var doc = new XmlDocument ();
  121. var assertions = context.GetBindingAssertions ();
  122. if (messageEncodingElement == null) {
  123. assertions.Add (doc.CreateElement (
  124. "wsaw", "UsingAddressing",
  125. "http://www.w3.org/2006/05/addressing/wsdl"));
  126. return null;
  127. }
  128. var addressing = messageEncodingElement.MessageVersion.Addressing;
  129. if (addressing == AddressingVersion.WSAddressingAugust2004)
  130. assertions.Add (doc.CreateElement (
  131. "wsaw", "UsingAddressing",
  132. "http://schemas.xmlsoap.org/ws/2004/08/addressing/policy"));
  133. else if (addressing != AddressingVersion.None)
  134. assertions.Add (doc.CreateElement (
  135. "wsaw", "UsingAddressing",
  136. "http://www.w3.org/2006/05/addressing/wsdl"));
  137. return messageEncodingElement;
  138. }
  139. #endif
  140. }
  141. }