New test.
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / WebServiceHelper.cs
1 //
2 // System.Web.Services.Protocols.WebServiceHelper.cs
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // Copyright (C) Ximian, Inc. 2003
8 //
9
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
31 using System;
32 using System.IO;
33 using System.Net;
34 using System.Text;
35 using System.Xml;
36 using System.Xml.Schema;
37 using System.Xml.Serialization;
38 using System.Web.Services.Description;
39
40 namespace System.Web.Services.Protocols
41 {
42         internal class WebServiceHelper
43         {
44                 public const string SoapEnvelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
45                 static readonly char [] trimChars = { '"', '\'' };
46                 static readonly bool prettyXml;
47                 
48                 static WebServiceHelper ()
49                 {
50                         string pxml = Environment.GetEnvironmentVariable ("MONO_WEBSERVICES_PRETTYXML");
51                         prettyXml = (pxml != null && pxml != "no");
52                 }
53                 
54                 public static XmlTextWriter CreateXmlWriter (Stream s)
55                 {
56                         // What a waste of UTF8encoders, but it has to be thread safe.
57                         XmlTextWriter xtw = new XmlTextWriter (s, new UTF8Encoding (false));
58                                 
59                         if (prettyXml)
60                                 xtw.Formatting = Formatting.Indented;
61                                 
62                         return xtw;
63                 }
64                 
65                 public static Encoding GetContentEncoding (string cts, out string content_type)
66                 {
67                         string encoding;
68
69                         if (cts == null) cts = "";
70                         
71                         encoding = "utf-8";
72                         int start = 0;
73                         int idx = cts.IndexOf (';');
74                         if (idx == -1)
75                                 content_type = cts;
76                         else
77                                 content_type = cts.Substring (0, idx);
78
79                         content_type = content_type.Trim ();
80                         for (start = idx + 1; idx != -1;)
81                         {
82                                 idx = cts.IndexOf (";", start);
83                                 string body;
84                                 if (idx == -1)
85                                         body = cts.Substring (start);
86                                 else 
87                                 {
88                                         body = cts.Substring (start, idx - start);
89                                         start = idx + 1;
90                                 }
91                                 body = body.Trim ();
92                                 if (body.StartsWith ("charset="))
93                                 {
94                                         encoding = body.Substring (8);
95                                         encoding = encoding.TrimStart (trimChars).TrimEnd (trimChars);
96                                 }
97                         }
98
99                         return Encoding.GetEncoding (encoding);
100                 }
101
102                 public static void WriteSoapMessage (XmlTextWriter xtw, SoapMethodStubInfo method, SoapHeaderDirection dir, object bodyContent, SoapHeaderCollection headers)
103                 {
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);
109                 }
110                 
111                 public static void WriteSoapMessage (XmlTextWriter xtw, SoapBindingUse methodUse, XmlSerializer bodySerializer, XmlSerializer headerSerializer, object bodyContent, object[] headers)
112                 {
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);
117
118                         // Serialize headers
119                         if (headers != null)
120                         {
121                                 xtw.WriteStartElement ("soap", "Header", WebServiceHelper.SoapEnvelopeNamespace);
122                                 headerSerializer.Serialize (xtw, headers);
123                                 xtw.WriteEndElement ();
124                         }
125
126                         // Serialize body
127                         xtw.WriteStartElement ("soap", "Body", WebServiceHelper.SoapEnvelopeNamespace);
128                         
129                         if (methodUse == SoapBindingUse.Encoded)
130                                 xtw.WriteAttributeString ("encodingStyle", WebServiceHelper.SoapEnvelopeNamespace, "http://schemas.xmlsoap.org/soap/encoding/");
131                                 
132                         bodySerializer.Serialize (xtw, bodyContent);
133
134                         xtw.WriteEndElement ();
135                         xtw.WriteEndElement ();
136                         xtw.Flush ();
137                 }
138
139                 public static void ReadSoapMessage (XmlTextReader xmlReader, SoapMethodStubInfo method, SoapHeaderDirection dir, out object body, out SoapHeaderCollection headers)
140                 {
141                         XmlSerializer bodySerializer = method.GetBodySerializer (dir);
142                         XmlSerializer headerSerializer = method.GetHeaderSerializer (dir);
143                         ReadSoapMessage (xmlReader, bodySerializer, headerSerializer, out body, out headers);
144                 }
145                 
146                 public static void ReadSoapMessage (XmlTextReader xmlReader, XmlSerializer bodySerializer, XmlSerializer headerSerializer, out object body, out SoapHeaderCollection headers)
147                 {
148                         xmlReader.MoveToContent ();\r
149                         xmlReader.ReadStartElement ("Envelope", WebServiceHelper.SoapEnvelopeNamespace);\r
150 \r
151                         headers = ReadHeaders (xmlReader, headerSerializer);\r
152 \r
153                         xmlReader.MoveToContent ();\r
154                         xmlReader.ReadStartElement ("Body", WebServiceHelper.SoapEnvelopeNamespace);\r
155                         xmlReader.MoveToContent ();
156                         
157                         if (xmlReader.LocalName == "Fault" && xmlReader.NamespaceURI == SoapEnvelopeNamespace)
158                                 bodySerializer = Fault.Serializer;
159 \r
160                         body = bodySerializer.Deserialize (xmlReader);\r
161                 }
162
163                 static SoapHeaderCollection ReadHeaders (XmlTextReader xmlReader, XmlSerializer headerSerializer)
164                 {
165                         SoapHeaderCollection headers = null;
166                         while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace))
167                         {
168                                 if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Header" 
169                                     && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace && !xmlReader.IsEmptyElement
170                                     && headerSerializer != null)
171                                 {
172                                         xmlReader.ReadStartElement ();
173                                         xmlReader.MoveToContent ();
174                                         
175                                         HeaderSerializationHelper uh = new HeaderSerializationHelper (headerSerializer);
176                                         headers = uh.Deserialize (xmlReader);
177                                         
178                                         while (xmlReader.NodeType != XmlNodeType.EndElement)
179                                                 xmlReader.Skip ();
180                                                 
181                                         xmlReader.ReadEndElement ();
182                                 }
183                                 else
184                                         xmlReader.Skip ();
185                         }
186                         if (headers != null)
187                                 return headers;
188                         else
189                                 return new SoapHeaderCollection ();
190                 }
191                 
192                 class HeaderSerializationHelper
193                 {
194                         SoapHeaderCollection headers;
195                         XmlSerializer headerSerializer;
196                         
197                         public HeaderSerializationHelper (XmlSerializer headerSerializer)
198                         {
199                                 this.headers = new SoapHeaderCollection ();
200                                 this.headerSerializer = headerSerializer;
201                         }
202                         
203                         public SoapHeaderCollection Deserialize (XmlTextReader xmlReader)
204                         {
205                                 try {
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);
210                                         return headers;
211                                 } finally {
212                                         headerSerializer.UnknownElement -= new XmlElementEventHandler (OnAddUnknownHeader);
213                                 }
214                         }
215                         
216                         void OnAddUnknownHeader (object sender, XmlElementEventArgs e)
217                         {
218                                 headers.Add (new SoapUnknownHeader (e.Element));
219                         }
220                 }
221
222                 public static void InvalidOperation (string message, WebResponse response, Encoding enc)
223                 {
224                         if (response == null)
225                                 throw new InvalidOperationException (message);
226
227                         if (enc == null)
228                                 enc = Encoding.UTF8;
229
230                         StringBuilder sb = new StringBuilder ();
231                         sb.Append (message);
232                         if (response.ContentLength > 0) {
233                                 sb.Append ("\r\nResponse error message:\r\n--\r\n");
234
235                                 try {
236                                         StreamReader resp = new StreamReader (response.GetResponseStream (), enc);
237                                         sb.Append (resp.ReadToEnd ());
238                                 } catch (Exception) {
239                                 }
240                         }
241
242                         throw new InvalidOperationException (sb.ToString ());
243                 }
244         }
245 }