2 // System.Web.Services.Protocols.HttpSoapWebServiceHandler.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.
37 using System.Reflection;
38 using System.Xml.Serialization;
39 using System.Web.Services.Description;
41 namespace System.Web.Services.Protocols
43 internal class HttpSoapWebServiceHandler: WebServiceHandler
45 SoapTypeStubInfo _typeStubInfo;
46 SoapExtension[] _extensionChainHighPrio;
47 SoapExtension[] _extensionChainMedPrio;
48 SoapExtension[] _extensionChainLowPrio;
49 SoapMethodStubInfo methodInfo;
50 SoapServerMessage requestMessage = null;
52 public HttpSoapWebServiceHandler (Type type): base (type)
54 _typeStubInfo = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (ServiceType, "Soap");
57 public override bool IsReusable
62 internal override MethodStubInfo GetRequestMethod (HttpContext context)
66 requestMessage = DeserializeRequest (context.Request);
71 SerializeFault (context, requestMessage, ex);
76 public override void ProcessRequest (HttpContext context)
79 SoapServerMessage responseMessage = null;
83 if (requestMessage == null)
84 requestMessage = DeserializeRequest (context.Request);
86 if (methodInfo != null && methodInfo.OneWay) {
87 context.Response.BufferOutput = false;
88 context.Response.StatusCode = 202;
89 context.Response.Flush ();
90 context.Response.Close ();
91 Invoke (context, requestMessage);
93 responseMessage = Invoke (context, requestMessage);
94 SerializeResponse (context.Response, responseMessage);
99 if (methodInfo != null && methodInfo.OneWay) {
100 context.Response.StatusCode = 500;
101 context.Response.Flush ();
102 context.Response.Close ();
104 SerializeFault (context, requestMessage, ex);
108 IDisposable disp = requestMessage.Server as IDisposable;
109 requestMessage = null;
115 SoapServerMessage DeserializeRequest (HttpRequest request)
117 Stream stream = request.InputStream;
121 string soapAction = null;
123 Encoding encoding = WebServiceHelper.GetContentEncoding (request.ContentType, out ctype);
124 if (ctype != "text/xml")
125 throw new WebException ("Content is not XML: " + ctype);
127 object server = CreateServerInstance ();
129 SoapServerMessage message = new SoapServerMessage (request, server, stream);
130 message.SetStage (SoapMessageStage.BeforeDeserialize);
131 message.ContentType = ctype;
133 // If the routing style is SoapAction, then we can get the method information now
134 // and set it to the SoapMessage
136 if (_typeStubInfo.RoutingStyle == SoapServiceRoutingStyle.SoapAction)
138 soapAction = request.Headers ["SOAPAction"];
139 if (soapAction == null) throw new SoapException ("Missing SOAPAction header", SoapException.ClientFaultCode);
140 methodInfo = _typeStubInfo.GetMethodForSoapAction (soapAction);
141 if (methodInfo == null) throw new SoapException ("Server did not recognize the value of HTTP header SOAPAction: " + soapAction, SoapException.ClientFaultCode);
142 message.MethodStubInfo = methodInfo;
145 // Execute the high priority global extensions. Do not try to execute the medium and
146 // low priority extensions because if the routing style is RequestElement we still
147 // don't have method information
149 _extensionChainHighPrio = SoapExtension.CreateExtensionChain (_typeStubInfo.SoapExtensions[0]);
150 stream = SoapExtension.ExecuteChainStream (_extensionChainHighPrio, stream);
151 SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, stream, false);
153 // If the routing style is RequestElement, try to get the method name from the
154 // stream processed by the high priority extensions
156 if (_typeStubInfo.RoutingStyle == SoapServiceRoutingStyle.RequestElement)
158 MemoryStream mstream;
159 byte[] buffer = null;
163 buffer = new byte [stream.Length];
164 for (int n=0; n<stream.Length;)
165 n += stream.Read (buffer, n, (int)stream.Length-n);
166 mstream = new MemoryStream (buffer);
170 buffer = new byte [500];
171 mstream = new MemoryStream ();
174 while ((len = stream.Read (buffer, 0, 500)) > 0)
175 mstream.Write (buffer, 0, len);
176 mstream.Position = 0;
177 buffer = mstream.ToArray ();
180 soapAction = ReadActionFromRequestElement (new MemoryStream (buffer), encoding);
183 methodInfo = (SoapMethodStubInfo) _typeStubInfo.GetMethod (soapAction);
184 message.MethodStubInfo = methodInfo;
187 // Whatever routing style we used, we should now have the method information.
188 // We can now notify the remaining extensions
190 if (methodInfo == null) throw new SoapException ("Method '" + soapAction + "' not defined in the web service '" + _typeStubInfo.LogicalType.WebServiceName + "'", SoapException.ClientFaultCode);
192 _extensionChainMedPrio = SoapExtension.CreateExtensionChain (methodInfo.SoapExtensions);
193 _extensionChainLowPrio = SoapExtension.CreateExtensionChain (_typeStubInfo.SoapExtensions[1]);
195 stream = SoapExtension.ExecuteChainStream (_extensionChainMedPrio, stream);
196 stream = SoapExtension.ExecuteChainStream (_extensionChainLowPrio, stream);
197 SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, stream, false);
\r
198 SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, stream, false);
\r
200 // Deserialize the request
202 StreamReader reader = new StreamReader (stream, encoding, false);
203 XmlTextReader xmlReader = new XmlTextReader (reader);
208 SoapHeaderCollection headers;
\r
209 WebServiceHelper.ReadSoapMessage (xmlReader, methodInfo, SoapHeaderDirection.In, out content, out headers);
\r
210 message.InParameters = (object []) content;
211 message.SetHeaders (headers);
215 throw new SoapException ("Could not deserialize Soap message", SoapException.ClientFaultCode, ex);
218 // Notify the extensions after deserialization
220 message.SetStage (SoapMessageStage.AfterDeserialize);
221 SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, stream, false);
\r
222 SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, stream, false);
\r
223 SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, stream, false);
\r
231 string ReadActionFromRequestElement (Stream stream, Encoding encoding)
235 StreamReader reader = new StreamReader (stream, encoding, false);
236 XmlTextReader xmlReader = new XmlTextReader (reader);
238 xmlReader.MoveToContent ();
239 xmlReader.ReadStartElement ("Envelope", WebServiceHelper.SoapEnvelopeNamespace);
241 while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace))
244 xmlReader.ReadStartElement ("Body", WebServiceHelper.SoapEnvelopeNamespace);
245 xmlReader.MoveToContent ();
247 return xmlReader.LocalName;
251 string errmsg = "The root element for the request could not be determined. ";
252 errmsg += "When RoutingStyle is set to RequestElement, SoapExtensions configured ";
253 errmsg += "via an attribute on the method cannot modify the request stream before it is read. ";
254 errmsg += "The extension must be configured via the SoapExtensionTypes element in web.config ";
255 errmsg += "or the request must arrive at the server as clear text.";
256 throw new SoapException (errmsg, SoapException.ServerFaultCode, ex);
260 void SerializeResponse (HttpResponse response, SoapServerMessage message)
262 SoapMethodStubInfo methodInfo = message.MethodStubInfo;
264 if ((message.ContentEncoding != null) && (message.ContentEncoding.Length > 0))
265 response.AppendHeader("Content-Encoding", message.ContentEncoding);
267 response.ContentType = "text/xml; charset=utf-8";
268 if (message.Exception != null) response.StatusCode = 500;
270 Stream responseStream = response.OutputStream;
271 Stream outStream = responseStream;
272 bool bufferResponse = (methodInfo == null || methodInfo.MethodAttribute.BufferResponse);
273 response.BufferOutput = bufferResponse;
277 // While serializing, process extensions in reverse order
281 outStream = SoapExtension.ExecuteChainStream (_extensionChainHighPrio, outStream);
282 outStream = SoapExtension.ExecuteChainStream (_extensionChainMedPrio, outStream);
283 outStream = SoapExtension.ExecuteChainStream (_extensionChainLowPrio, outStream);
285 message.SetStage (SoapMessageStage.BeforeSerialize);
286 SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, outStream, true);
\r
287 SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, outStream, true);
\r
288 SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, outStream, true);
\r
291 XmlTextWriter xtw = WebServiceHelper.CreateXmlWriter (outStream);
293 if (message.Exception == null)
294 WebServiceHelper.WriteSoapMessage (xtw, methodInfo, SoapHeaderDirection.Out, message.OutParameters, message.Headers);
295 else if (methodInfo != null)
296 WebServiceHelper.WriteSoapMessage (xtw, methodInfo, SoapHeaderDirection.Fault, new Fault (message.Exception), message.Headers);
298 WebServiceHelper.WriteSoapMessage (xtw, SoapBindingUse.Literal, Fault.Serializer, null, new Fault (message.Exception), null);
302 message.SetStage (SoapMessageStage.AfterSerialize);
303 SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, outStream, true);
\r
304 SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, outStream, true);
\r
305 SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, outStream, true);
\r
312 // If the response is buffered, we can discard the response and
313 // serialize a new Fault message as response.
314 if (bufferResponse) throw ex;
316 // If it is not buffered, we can't rollback what has been sent,
317 // so we can only close the connection and return.
318 responseStream.Close ();
323 void SerializeFault (HttpContext context, SoapServerMessage requestMessage, Exception ex)
325 SoapException soex = ex as SoapException;
326 if (soex == null) soex = new SoapException (ex.Message, SoapException.ServerFaultCode, ex);
328 SoapServerMessage faultMessage;
329 if (requestMessage != null)
330 faultMessage = new SoapServerMessage (context.Request, soex, requestMessage.MethodStubInfo, requestMessage.Server, requestMessage.Stream);
332 faultMessage = new SoapServerMessage (context.Request, soex, null, null, null);
334 SerializeResponse (context.Response, faultMessage);
335 context.Response.End ();
339 private SoapServerMessage Invoke (HttpContext ctx, SoapServerMessage requestMessage)
341 SoapMethodStubInfo methodInfo = requestMessage.MethodStubInfo;
343 // Assign header values to web service members
345 requestMessage.UpdateHeaderValues (requestMessage.Server, methodInfo.Headers);
347 // Fill an array with the input parameters at the right position
349 object[] parameters = new object[methodInfo.MethodInfo.Parameters.Length];
350 ParameterInfo[] inParams = methodInfo.MethodInfo.InParameters;
351 for (int n=0; n<inParams.Length; n++)
352 parameters [inParams[n].Position] = requestMessage.InParameters [n];
358 object[] results = methodInfo.MethodInfo.Invoke (requestMessage.Server, parameters);
359 requestMessage.OutParameters = results;
361 catch (TargetInvocationException ex)
363 throw ex.InnerException;
366 // Check that headers with MustUnderstand flag have been understood
368 foreach (SoapHeader header in requestMessage.Headers)
370 if (header.MustUnderstand && !header.DidUnderstand)
371 throw new SoapHeaderException ("Header not understood: " + header.GetType(), SoapException.MustUnderstandFaultCode);
374 // Collect headers that must be sent to the client
375 requestMessage.CollectHeaders (requestMessage.Server, methodInfo.Headers, SoapHeaderDirection.Out);
377 return requestMessage;