Switch to compiler-tester
[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, SoapTypeStubInfo info, SoapBindingUse methodUse, XmlSerializer bodySerializer, object bodyContent, SoapHeaderCollection headers)
103                 {
104                         xtw.WriteStartDocument ();
105                         xtw.WriteStartElement ("soap", "Envelope", WebServiceHelper.SoapEnvelopeNamespace);
106                         xtw.WriteAttributeString ("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
107                         xtw.WriteAttributeString ("xmlns", "xsd", null, XmlSchema.Namespace);
108
109                         // Serialize headers
110                         if (headers != null)
111                         {
112                                 foreach (SoapHeader header in headers) 
113                                 {
114                                         XmlSerializer ser = info.GetHeaderSerializer (header.GetType(), methodUse);
115                                         xtw.WriteStartElement ("soap", "Header", WebServiceHelper.SoapEnvelopeNamespace);
116                                         ser.Serialize (xtw, header);
117                                         xtw.WriteEndElement ();
118                                 }
119                         }
120
121                         // Serialize body
122                         xtw.WriteStartElement ("soap", "Body", WebServiceHelper.SoapEnvelopeNamespace);
123                         
124                         if (methodUse == SoapBindingUse.Encoded)
125                                 xtw.WriteAttributeString ("encodingStyle", WebServiceHelper.SoapEnvelopeNamespace, "http://schemas.xmlsoap.org/soap/encoding/");
126                                 
127                         bodySerializer.Serialize (xtw, bodyContent);
128
129                         xtw.WriteEndElement ();
130                         xtw.WriteEndElement ();
131                         xtw.Flush ();
132                 }
133
134                 public static void ReadSoapMessage (XmlTextReader xmlReader, SoapTypeStubInfo typeStubInfo, SoapBindingUse methodUse, XmlSerializer bodySerializer, out object body, out SoapHeaderCollection headers)
135                 {
136                         xmlReader.MoveToContent ();\r
137                         xmlReader.ReadStartElement ("Envelope", WebServiceHelper.SoapEnvelopeNamespace);\r
138 \r
139                         headers = ReadHeaders (typeStubInfo, methodUse, xmlReader);\r
140 \r
141                         xmlReader.MoveToContent ();\r
142                         xmlReader.ReadStartElement ("Body", WebServiceHelper.SoapEnvelopeNamespace);\r
143                         xmlReader.MoveToContent ();
144                         
145                         if (xmlReader.LocalName == "Fault" && xmlReader.NamespaceURI == SoapEnvelopeNamespace)
146                                 bodySerializer = Fault.Serializer;
147 \r
148                         body = bodySerializer.Deserialize (xmlReader);\r
149                 }
150
151                 static SoapHeaderCollection ReadHeaders (SoapTypeStubInfo typeStubInfo, SoapBindingUse methodUse, XmlTextReader xmlReader)
152                 {
153                         SoapHeaderCollection headers = new SoapHeaderCollection ();
154                         while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace))
155                         {
156                                 if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Header" 
157                                     && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace && !xmlReader.IsEmptyElement)
158                                 {
159                                         xmlReader.ReadStartElement ();
160                                         xmlReader.MoveToContent ();
161                                         
162                                         while (xmlReader.NodeType != XmlNodeType.Element && xmlReader.NodeType != XmlNodeType.EndElement)
163                                                 xmlReader.Skip ();
164                                                 
165                                         xmlReader.MoveToContent ();
166                                         
167                                         if (xmlReader.NodeType == XmlNodeType.Element) {
168                                                 XmlQualifiedName qname = new XmlQualifiedName (xmlReader.LocalName, xmlReader.NamespaceURI);
169                                                 XmlSerializer headerSerializer = typeStubInfo.GetHeaderSerializer (qname, methodUse);
170                                                 if (headerSerializer != null)
171                                                 {
172                                                         SoapHeader header = (SoapHeader) headerSerializer.Deserialize (xmlReader);
173                                                         headers.Add (header);
174                                                 }
175                                                 else
176                                                 {
177                                                         XmlDocument doc = new XmlDocument ();
178                                                         XmlElement elem = (XmlElement) doc.ReadNode (xmlReader);
179                                                         headers.Add (new SoapUnknownHeader (elem));
180                                                 }
181                                         }
182                                         
183                                         while (xmlReader.NodeType != XmlNodeType.EndElement)
184                                                 xmlReader.Skip ();
185                                                 
186                                         xmlReader.ReadEndElement ();
187                                 }
188                                 else
189                                         xmlReader.Skip ();
190                         }
191                         return headers;
192                 }
193
194                 public static void InvalidOperation (string message, WebResponse response, Encoding enc)
195                 {
196                         if (response == null)
197                                 throw new InvalidOperationException (message);
198
199                         if (enc == null)
200                                 enc = Encoding.UTF8;
201
202                         StringBuilder sb = new StringBuilder ();
203                         sb.Append (message);
204                         if (response.ContentLength > 0) {
205                                 sb.Append ("\r\nResponse error message:\r\n--\r\n");
206
207                                 try {
208                                         StreamReader resp = new StreamReader (response.GetResponseStream (), enc);
209                                         sb.Append (resp.ReadToEnd ());
210                                 } catch (Exception) {
211                                 }
212                         }
213
214                         throw new InvalidOperationException (sb.ToString ());
215                 }
216         }
217 }