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)
71 int idx = cts.IndexOf (';');
75 content_type = cts.Substring (0, idx);
77 content_type = content_type.Trim ();
78 for (start = idx + 1; idx != -1;)
80 idx = cts.IndexOf (";", start);
83 body = cts.Substring (start);
86 body = cts.Substring (start, idx - start);
90 if (body.StartsWith ("charset="))
92 encoding = body.Substring (8);
93 encoding = encoding.TrimStart (trimChars).TrimEnd (trimChars);
97 return Encoding.GetEncoding (encoding);
100 public static void WriteSoapMessage (XmlTextWriter xtw, SoapTypeStubInfo info, SoapBindingUse methodUse, XmlSerializer bodySerializer, object bodyContent, SoapHeaderCollection headers)
102 xtw.WriteStartDocument ();
103 xtw.WriteStartElement ("soap", "Envelope", WebServiceHelper.SoapEnvelopeNamespace);
104 xtw.WriteAttributeString ("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
105 xtw.WriteAttributeString ("xmlns", "xsd", null, XmlSchema.Namespace);
110 foreach (SoapHeader header in headers)
112 XmlSerializer ser = info.GetHeaderSerializer (header.GetType(), methodUse);
113 xtw.WriteStartElement ("soap", "Header", WebServiceHelper.SoapEnvelopeNamespace);
114 ser.Serialize (xtw, header);
115 xtw.WriteEndElement ();
120 xtw.WriteStartElement ("soap", "Body", WebServiceHelper.SoapEnvelopeNamespace);
122 if (methodUse == SoapBindingUse.Encoded)
123 xtw.WriteAttributeString ("encodingStyle", WebServiceHelper.SoapEnvelopeNamespace, "http://schemas.xmlsoap.org/soap/encoding/");
125 bodySerializer.Serialize (xtw, bodyContent);
127 xtw.WriteEndElement ();
128 xtw.WriteEndElement ();
132 public static void ReadSoapMessage (XmlTextReader xmlReader, SoapTypeStubInfo typeStubInfo, SoapBindingUse methodUse, XmlSerializer bodySerializer, out object body, out SoapHeaderCollection headers)
134 xmlReader.MoveToContent ();
\r
135 xmlReader.ReadStartElement ("Envelope", WebServiceHelper.SoapEnvelopeNamespace);
\r
137 headers = ReadHeaders (typeStubInfo, methodUse, xmlReader);
\r
139 xmlReader.MoveToContent ();
\r
140 xmlReader.ReadStartElement ("Body", WebServiceHelper.SoapEnvelopeNamespace);
\r
141 xmlReader.MoveToContent ();
143 if (xmlReader.LocalName == "Fault" && xmlReader.NamespaceURI == SoapEnvelopeNamespace)
144 bodySerializer = Fault.Serializer;
146 body = bodySerializer.Deserialize (xmlReader);
\r
149 static SoapHeaderCollection ReadHeaders (SoapTypeStubInfo typeStubInfo, SoapBindingUse methodUse, XmlTextReader xmlReader)
151 SoapHeaderCollection headers = new SoapHeaderCollection ();
152 while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace))
154 if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Header"
155 && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace && !xmlReader.IsEmptyElement)
157 xmlReader.ReadStartElement ();
158 xmlReader.MoveToContent ();
159 XmlQualifiedName qname = new XmlQualifiedName (xmlReader.LocalName, xmlReader.NamespaceURI);
160 XmlSerializer headerSerializer = typeStubInfo.GetHeaderSerializer (qname, methodUse);
161 if (headerSerializer != null)
163 SoapHeader header = (SoapHeader) headerSerializer.Deserialize (xmlReader);
164 headers.Add (header);
168 while (xmlReader.NodeType == XmlNodeType.EndElement)
169 xmlReader.Skip (); // TODO: Check if the header has mustUnderstand=true
179 public static void InvalidOperation (string message, WebResponse response, Encoding enc)
181 if (response == null)
182 throw new InvalidOperationException (message);
187 StringBuilder sb = new StringBuilder ();
189 sb.Append ("\r\nResponse error message:\r\n--\r\n");
192 StreamReader resp = new StreamReader (response.GetResponseStream (), enc);
193 sb.Append (resp.ReadToEnd ());
194 } catch (Exception) {
197 throw new InvalidOperationException (sb.ToString ());