2 // System.Web.Services.Protocols.WebServiceHelper.cs
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // Copyright (C) Ximian, Inc. 2003
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
36 using System.Xml.Schema;
37 using System.Xml.Serialization;
38 using System.Web.Services.Description;
40 namespace System.Web.Services.Protocols
42 internal class WebServiceHelper
44 public const string SoapEnvelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
45 static readonly char [] trimChars = { '"', '\'' };
46 static readonly bool prettyXml;
48 static WebServiceHelper ()
50 string pxml = Environment.GetEnvironmentVariable ("MONO_WEBSERVICES_PRETTYXML");
51 prettyXml = (pxml != null && pxml != "no");
54 public static XmlTextWriter CreateXmlWriter (Stream s)
56 // What a waste of UTF8encoders, but it has to be thread safe.
57 XmlTextWriter xtw = new XmlTextWriter (s, new UTF8Encoding (false));
60 xtw.Formatting = Formatting.Indented;
65 public static Encoding GetContentEncoding (string cts, out string content_type)
69 if (cts == null) cts = "";
73 int idx = cts.IndexOf (';');
77 content_type = cts.Substring (0, idx);
79 content_type = content_type.Trim ();
80 for (start = idx + 1; idx != -1;)
82 idx = cts.IndexOf (";", start);
85 body = cts.Substring (start);
88 body = cts.Substring (start, idx - start);
92 if (body.StartsWith ("charset="))
94 encoding = body.Substring (8);
95 encoding = encoding.TrimStart (trimChars).TrimEnd (trimChars);
99 return Encoding.GetEncoding (encoding);
102 public static void WriteSoapMessage (XmlTextWriter xtw, SoapMethodStubInfo method, SoapHeaderDirection dir, object bodyContent, SoapHeaderCollection headers)
104 SoapBindingUse methodUse = dir == SoapHeaderDirection.Fault ? SoapBindingUse.Literal : method.Use;
105 XmlSerializer bodySerializer = method.GetBodySerializer (dir);
106 XmlSerializer headerSerializer = method.GetHeaderSerializer (dir);
107 object[] headerArray = method.GetHeaderValueArray (dir, headers);
108 WriteSoapMessage (xtw, methodUse, bodySerializer, headerSerializer, bodyContent, headerArray);
111 public static void WriteSoapMessage (XmlTextWriter xtw, SoapBindingUse methodUse, XmlSerializer bodySerializer, XmlSerializer headerSerializer, object bodyContent, object[] headers)
113 xtw.WriteStartDocument ();
114 xtw.WriteStartElement ("soap", "Envelope", WebServiceHelper.SoapEnvelopeNamespace);
115 xtw.WriteAttributeString ("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
116 xtw.WriteAttributeString ("xmlns", "xsd", null, XmlSchema.Namespace);
121 xtw.WriteStartElement ("soap", "Header", WebServiceHelper.SoapEnvelopeNamespace);
122 headerSerializer.Serialize (xtw, headers);
123 xtw.WriteEndElement ();
127 xtw.WriteStartElement ("soap", "Body", WebServiceHelper.SoapEnvelopeNamespace);
129 if (methodUse == SoapBindingUse.Encoded)
130 xtw.WriteAttributeString ("encodingStyle", WebServiceHelper.SoapEnvelopeNamespace, "http://schemas.xmlsoap.org/soap/encoding/");
132 bodySerializer.Serialize (xtw, bodyContent);
134 xtw.WriteEndElement ();
135 xtw.WriteEndElement ();
139 public static void ReadSoapMessage (XmlTextReader xmlReader, SoapMethodStubInfo method, SoapHeaderDirection dir, out object body, out SoapHeaderCollection headers)
141 XmlSerializer bodySerializer = method.GetBodySerializer (dir);
142 XmlSerializer headerSerializer = method.GetHeaderSerializer (dir);
143 ReadSoapMessage (xmlReader, bodySerializer, headerSerializer, out body, out headers);
146 public static void ReadSoapMessage (XmlTextReader xmlReader, XmlSerializer bodySerializer, XmlSerializer headerSerializer, out object body, out SoapHeaderCollection headers)
148 xmlReader.MoveToContent ();
\r
149 xmlReader.ReadStartElement ("Envelope", WebServiceHelper.SoapEnvelopeNamespace);
\r
151 headers = ReadHeaders (xmlReader, headerSerializer);
\r
153 xmlReader.MoveToContent ();
\r
154 xmlReader.ReadStartElement ("Body", WebServiceHelper.SoapEnvelopeNamespace);
\r
155 xmlReader.MoveToContent ();
157 if (xmlReader.LocalName == "Fault" && xmlReader.NamespaceURI == SoapEnvelopeNamespace)
158 bodySerializer = Fault.Serializer;
160 body = bodySerializer.Deserialize (xmlReader);
\r
163 static SoapHeaderCollection ReadHeaders (XmlTextReader xmlReader, XmlSerializer headerSerializer)
165 SoapHeaderCollection headers = null;
166 while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace))
168 if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Header"
169 && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace && !xmlReader.IsEmptyElement
170 && headerSerializer != null)
172 xmlReader.ReadStartElement ();
173 xmlReader.MoveToContent ();
175 HeaderSerializationHelper uh = new HeaderSerializationHelper (headerSerializer);
176 headers = uh.Deserialize (xmlReader);
178 while (xmlReader.NodeType != XmlNodeType.EndElement)
181 xmlReader.ReadEndElement ();
189 return new SoapHeaderCollection ();
192 class HeaderSerializationHelper
194 SoapHeaderCollection headers;
195 XmlSerializer headerSerializer;
197 public HeaderSerializationHelper (XmlSerializer headerSerializer)
199 this.headers = new SoapHeaderCollection ();
200 this.headerSerializer = headerSerializer;
203 public SoapHeaderCollection Deserialize (XmlTextReader xmlReader)
206 headerSerializer.UnknownElement += new XmlElementEventHandler (OnAddUnknownHeader);
207 object[] headerArray = (object[]) headerSerializer.Deserialize (xmlReader);
208 foreach (SoapHeader h in headerArray)
209 if (h != null) headers.Add (h);
212 headerSerializer.UnknownElement -= new XmlElementEventHandler (OnAddUnknownHeader);
216 void OnAddUnknownHeader (object sender, XmlElementEventArgs e)
218 headers.Add (new SoapUnknownHeader (e.Element));
222 public static void InvalidOperation (string message, WebResponse response, Encoding enc)
224 if (response == null)
225 throw new InvalidOperationException (message);
230 StringBuilder sb = new StringBuilder ();
232 if (response.ContentLength > 0) {
233 sb.Append ("\r\nResponse error message:\r\n--\r\n");
236 StreamReader resp = new StreamReader (response.GetResponseStream (), enc);
237 sb.Append (resp.ReadToEnd ());
238 } catch (Exception) {
242 throw new InvalidOperationException (sb.ToString ());