MessageImpl.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // MessageImpl.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Runtime.Serialization;
  31. using System.Xml;
  32. using System.IO;
  33. namespace System.ServiceModel.Channels
  34. {
  35. internal class XmlReaderMessage : Message
  36. {
  37. MessageVersion version;
  38. XmlDictionaryReader reader;
  39. MessageHeaders headers;
  40. MessageProperties properties = new MessageProperties ();
  41. bool is_empty, is_fault, body_started, body_consumed;
  42. int max_headers;
  43. Dictionary<XmlQualifiedName,string> attributes;
  44. public XmlReaderMessage (MessageVersion version, XmlDictionaryReader reader, int maxSizeOfHeaders)
  45. {
  46. this.version = version;
  47. this.reader = reader;
  48. this.max_headers = maxSizeOfHeaders;
  49. ReadEnvelopeStart ();
  50. }
  51. public override MessageHeaders Headers {
  52. get {
  53. if (headers == null)
  54. ReadHeaders ();
  55. return headers;
  56. }
  57. }
  58. public override bool IsEmpty {
  59. get {
  60. ReadBodyStart ();
  61. return is_empty;
  62. }
  63. }
  64. public override bool IsFault {
  65. get {
  66. ReadBodyStart ();
  67. return is_fault;
  68. }
  69. }
  70. public override MessageProperties Properties {
  71. get { return properties; }
  72. }
  73. public override MessageVersion Version {
  74. get { return version; }
  75. }
  76. protected override MessageBuffer OnCreateBufferedCopy (
  77. int maxBufferSize)
  78. {
  79. ReadBodyStart ();
  80. var headers = new MessageHeaders (Headers);
  81. var props = new MessageProperties (Properties);
  82. return new DefaultMessageBuffer (maxBufferSize, headers, props, new XmlReaderBodyWriter (reader), IsFault, attributes);
  83. }
  84. protected override string OnGetBodyAttribute (
  85. string localName, string ns)
  86. {
  87. ReadBodyStart ();
  88. string v;
  89. return attributes.TryGetValue (new XmlQualifiedName (localName, ns), out v) ? v : null;
  90. }
  91. protected override XmlDictionaryReader OnGetReaderAtBodyContents ()
  92. {
  93. if (reader.ReadState == ReadState.Closed)
  94. return reader; // silly, but that's what our test expects.
  95. ReadBodyStart ();
  96. if (is_empty)
  97. throw new InvalidOperationException ("The message body is empty.");
  98. body_consumed = true;
  99. return reader;
  100. }
  101. protected override void OnWriteBodyContents (
  102. XmlDictionaryWriter writer)
  103. {
  104. XmlDictionaryReader reader = GetReaderAtBodyContents ();
  105. while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement)
  106. writer.WriteNode (reader, false);
  107. }
  108. static readonly char [] whitespaceChars = new char [] {' ', '\t', '\r', '\n'};
  109. void ReadEnvelopeStart ()
  110. {
  111. reader.MoveToContent ();
  112. if (reader.IsEmptyElement)
  113. throw new ArgumentException ("Missing message content XML.");
  114. reader.ReadStartElement ("Envelope", Version.Envelope.Namespace);
  115. // SOAP Header
  116. reader.MoveToContent ();
  117. }
  118. void ReadHeaders ()
  119. {
  120. if (headers != null)
  121. throw new InvalidOperationException ("XmlReader at headers is already consumed.");
  122. string envNS = Version.Envelope.Namespace;
  123. headers = new MessageHeaders (version);
  124. if (reader.LocalName != "Header" || reader.NamespaceURI != envNS)
  125. return;
  126. bool isEmptyHeader = reader.IsEmptyElement;
  127. reader.ReadStartElement ("Header", envNS);
  128. reader.MoveToContent ();
  129. if (isEmptyHeader)
  130. return;
  131. int nHeaders = 0;
  132. while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement) {
  133. if (reader.NodeType == XmlNodeType.Element) {
  134. if (nHeaders++ == max_headers)
  135. throw new InvalidOperationException (String.Format ("Message header size has exceeded the maximum header size {0}", max_headers));
  136. headers.Add (new MessageHeader.RawMessageHeader (reader, envNS));
  137. }
  138. else
  139. reader.Skip ();
  140. // FIXME: handle UnderstoodHeaders as well.
  141. reader.MoveToContent ();
  142. }
  143. reader.ReadEndElement ();
  144. reader.MoveToContent ();
  145. }
  146. void ReadBodyStart ()
  147. {
  148. if (body_consumed)
  149. throw new InvalidOperationException ("The message body XmlReader is already consumed.");
  150. if (body_started)
  151. return;
  152. // read headers in advance.
  153. if (headers == null)
  154. ReadHeaders ();
  155. // SOAP Body
  156. body_started = true;
  157. is_empty = reader.IsEmptyElement;
  158. attributes = new Dictionary<XmlQualifiedName,string> ();
  159. reader.MoveToContent ();
  160. if (reader.MoveToFirstAttribute ()) {
  161. do {
  162. attributes [new XmlQualifiedName (reader.LocalName, reader.NamespaceURI)] = reader.Value;
  163. } while (reader.MoveToNextAttribute ());
  164. reader.MoveToElement ();
  165. }
  166. reader.ReadStartElement ("Body", Version.Envelope.Namespace);
  167. if (reader.NodeType == XmlNodeType.EndElement) {
  168. is_empty = true;
  169. reader.Read ();
  170. } else {
  171. reader.MoveToContent ();
  172. if (reader.NodeType == XmlNodeType.Element &&
  173. reader.LocalName == "Fault" &&
  174. reader.NamespaceURI == Version.Envelope.Namespace)
  175. is_fault = true;
  176. }
  177. }
  178. }
  179. internal abstract class MessageImplBase : Message
  180. {
  181. MessageHeaders headers;
  182. MessageProperties properties = new MessageProperties ();
  183. Dictionary<XmlQualifiedName,string> attributes;
  184. public MessageImplBase (MessageVersion version, string action, Dictionary<XmlQualifiedName,string> attributes)
  185. {
  186. headers = new MessageHeaders (version);
  187. if (action != null)
  188. headers.Action = action;
  189. this.attributes = attributes;
  190. }
  191. internal Dictionary<XmlQualifiedName,string> Attributes {
  192. get { return attributes; }
  193. }
  194. public override MessageHeaders Headers {
  195. get { return headers; }
  196. }
  197. public override MessageProperties Properties {
  198. get { return properties; }
  199. }
  200. public override MessageVersion Version {
  201. get { return Headers.MessageVersion; }
  202. }
  203. protected override string OnGetBodyAttribute (
  204. string localName, string ns)
  205. {
  206. string v;
  207. return attributes.TryGetValue (new XmlQualifiedName (localName, ns), out v) ? v : null;
  208. }
  209. protected override void OnWriteStartBody (
  210. XmlDictionaryWriter writer)
  211. {
  212. var dic = Constants.SoapDictionary;
  213. writer.WriteStartElement ("s", dic.Add ("Body"), dic.Add (Version.Envelope.Namespace));
  214. foreach (var p in Attributes)
  215. writer.WriteAttributeString (p.Key.Name, p.Key.Namespace, p.Value);
  216. }
  217. }
  218. internal class EmptyMessage : MessageImplBase
  219. {
  220. static readonly Dictionary<XmlQualifiedName,string> empty_attributes = new Dictionary<XmlQualifiedName,string> ();
  221. public EmptyMessage (MessageVersion version, string action)
  222. : base (version, action, empty_attributes)
  223. {
  224. }
  225. public override bool IsEmpty {
  226. get { return true; }
  227. }
  228. protected override void OnWriteBodyContents (
  229. XmlDictionaryWriter writer)
  230. {
  231. }
  232. protected override MessageBuffer OnCreateBufferedCopy (
  233. int maxBufferSize)
  234. {
  235. return new DefaultMessageBuffer (Headers, Properties, Attributes);
  236. }
  237. }
  238. internal class SimpleMessage : MessageImplBase
  239. {
  240. BodyWriter body;
  241. bool is_fault;
  242. public SimpleMessage (MessageVersion version,
  243. string action, BodyWriter body, bool isFault, Dictionary<XmlQualifiedName,string> attributes)
  244. : base (version, action, attributes)
  245. {
  246. this.body = body;
  247. this.is_fault = isFault;
  248. }
  249. public override bool IsEmpty {
  250. get { return false; }
  251. }
  252. public override bool IsFault {
  253. get { return is_fault; }
  254. }
  255. protected override void OnWriteBodyContents (
  256. XmlDictionaryWriter writer)
  257. {
  258. body.WriteBodyContents (writer);
  259. }
  260. protected override MessageBuffer OnCreateBufferedCopy (
  261. int maxBufferSize)
  262. {
  263. var headers = new MessageHeaders (Headers);
  264. var props = new MessageProperties (Properties);
  265. var atts = new Dictionary<XmlQualifiedName,string> (Attributes);
  266. return new DefaultMessageBuffer (maxBufferSize, headers, props, body.CreateBufferedCopy (maxBufferSize), IsFault, atts);
  267. }
  268. }
  269. }