This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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                         encoding = "utf-8";
70                         int start = 0;
71                         int idx = cts.IndexOf (';');
72                         if (idx == -1)
73                                 content_type = cts;
74                         else
75                                 content_type = cts.Substring (0, idx);
76
77                         content_type = content_type.Trim ();
78                         for (start = idx + 1; idx != -1;)
79                         {
80                                 idx = cts.IndexOf (";", start);
81                                 string body;
82                                 if (idx == -1)
83                                         body = cts.Substring (start);
84                                 else 
85                                 {
86                                         body = cts.Substring (start, idx - start);
87                                         start = idx + 1;
88                                 }
89                                 body = body.Trim ();
90                                 if (body.StartsWith ("charset="))
91                                 {
92                                         encoding = body.Substring (8);
93                                         encoding = encoding.TrimStart (trimChars).TrimEnd (trimChars);
94                                 }
95                         }
96
97                         return Encoding.GetEncoding (encoding);
98                 }
99
100                 public static void WriteSoapMessage (XmlTextWriter xtw, SoapTypeStubInfo info, SoapBindingUse methodUse, XmlSerializer bodySerializer, object bodyContent, SoapHeaderCollection headers)
101                 {
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);
106
107                         // Serialize headers
108                         if (headers != null)
109                         {
110                                 foreach (SoapHeader header in headers) 
111                                 {
112                                         XmlSerializer ser = info.GetHeaderSerializer (header.GetType(), methodUse);
113                                         xtw.WriteStartElement ("soap", "Header", WebServiceHelper.SoapEnvelopeNamespace);
114                                         ser.Serialize (xtw, header);
115                                         xtw.WriteEndElement ();
116                                 }
117                         }
118
119                         // Serialize body
120                         xtw.WriteStartElement ("soap", "Body", WebServiceHelper.SoapEnvelopeNamespace);
121                         
122                         if (methodUse == SoapBindingUse.Encoded)
123                                 xtw.WriteAttributeString ("encodingStyle", WebServiceHelper.SoapEnvelopeNamespace, "http://schemas.xmlsoap.org/soap/encoding/");
124                                 
125                         bodySerializer.Serialize (xtw, bodyContent);
126
127                         xtw.WriteEndElement ();
128                         xtw.WriteEndElement ();
129                         xtw.Flush ();
130                 }
131
132                 public static void ReadSoapMessage (XmlTextReader xmlReader, SoapTypeStubInfo typeStubInfo, SoapBindingUse methodUse, XmlSerializer bodySerializer, out object body, out SoapHeaderCollection headers)
133                 {
134                         xmlReader.MoveToContent ();\r
135                         xmlReader.ReadStartElement ("Envelope", WebServiceHelper.SoapEnvelopeNamespace);\r
136 \r
137                         headers = ReadHeaders (typeStubInfo, methodUse, xmlReader);\r
138 \r
139                         xmlReader.MoveToContent ();\r
140                         xmlReader.ReadStartElement ("Body", WebServiceHelper.SoapEnvelopeNamespace);\r
141                         xmlReader.MoveToContent ();
142                         
143                         if (xmlReader.LocalName == "Fault" && xmlReader.NamespaceURI == SoapEnvelopeNamespace)
144                                 bodySerializer = Fault.Serializer;
145 \r
146                         body = bodySerializer.Deserialize (xmlReader);\r
147                 }
148
149                 static SoapHeaderCollection ReadHeaders (SoapTypeStubInfo typeStubInfo, SoapBindingUse methodUse, XmlTextReader xmlReader)
150                 {
151                         SoapHeaderCollection headers = new SoapHeaderCollection ();
152                         while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace))
153                         {
154                                 if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Header" 
155                                     && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace && !xmlReader.IsEmptyElement)
156                                 {
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)
162                                         {
163                                                 SoapHeader header = (SoapHeader) headerSerializer.Deserialize (xmlReader);
164                                                 headers.Add (header);
165                                         }
166                                         else
167                                         {
168                                                 while (xmlReader.NodeType == XmlNodeType.EndElement)
169                                                         xmlReader.Skip ();      // TODO: Check if the header has mustUnderstand=true
170                                                 xmlReader.Skip ();
171                                         }
172                                 }
173                                 else
174                                         xmlReader.Skip ();
175                         }
176                         return headers;
177                 }
178
179                 public static void InvalidOperation (string message, WebResponse response, Encoding enc)
180                 {
181                         if (response == null)
182                                 throw new InvalidOperationException (message);
183
184                         if (enc == null)
185                                 enc = Encoding.UTF8;
186
187                         StringBuilder sb = new StringBuilder ();
188                         sb.Append (message);
189                         sb.Append ("\r\nResponse error message:\r\n--\r\n");
190
191                         try {
192                                 StreamReader resp = new StreamReader (response.GetResponseStream (), enc);
193                                 sb.Append (resp.ReadToEnd ());
194                         } catch (Exception) {
195                         }
196
197                         throw new InvalidOperationException (sb.ToString ());
198                 }
199         }
200 }