1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
//
// System.Web.Services.Protocols.HttpSoapWebServiceHandler.cs
//
// Author:
// Lluis Sanchez Gual (lluis@ximian.com)
//
// Copyright (C) Ximian, Inc. 2003
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Net;
using System.Web;
using System.Xml;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
using System.Web.Services.Description;
namespace System.Web.Services.Protocols
{
internal class HttpSoapWebServiceHandler: WebServiceHandler
{
SoapTypeStubInfo _typeStubInfo;
SoapExtension[] _extensionChainHighPrio;
SoapExtension[] _extensionChainMedPrio;
SoapExtension[] _extensionChainLowPrio;
SoapMethodStubInfo methodInfo;
SoapServerMessage requestMessage = null;
public HttpSoapWebServiceHandler (Type type): base (type)
{
_typeStubInfo = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (ServiceType, "Soap");
}
public override bool IsReusable
{
get { return false; }
}
internal override MethodStubInfo GetRequestMethod (HttpContext context)
{
try
{
requestMessage = DeserializeRequest (context);
return methodInfo;
}
catch (Exception ex)
{
SerializeFault (context, requestMessage, ex);
return null;
}
}
public override void ProcessRequest (HttpContext context)
{
Context = context;
SoapServerMessage responseMessage = null;
try
{
if (requestMessage == null) {
requestMessage = DeserializeRequest (context);
}
if (methodInfo != null && methodInfo.OneWay) {
context.Response.BufferOutput = false;
context.Response.StatusCode = 202;
context.Response.Flush ();
context.Response.Close ();
Invoke (context, requestMessage);
} else {
responseMessage = Invoke (context, requestMessage);
SerializeResponse (context.Response, responseMessage);
}
}
catch (Exception ex)
{
if (methodInfo != null && methodInfo.OneWay) {
context.Response.StatusCode = 500;
context.Response.Flush ();
context.Response.Close ();
} else {
SerializeFault (context, requestMessage, ex);
}
}
finally {
IDisposable disp = requestMessage.Server as IDisposable;
requestMessage = null;
if (disp != null)
disp.Dispose();
}
}
SoapServerMessage DeserializeRequest (HttpContext context)
{
HttpRequest request = context.Request;
Stream stream = request.InputStream;
//using (stream)
//{
string soapAction = null;
string ctype;
Encoding encoding = WebServiceHelper.GetContentEncoding (request.ContentType, out ctype);
#if NET_2_0
if (ctype != "text/xml" && ctype != "application/soap+xml")
#else
if (ctype != "text/xml")
#endif
throw new WebException ("Content is not XML: " + ctype);
object server = CreateServerInstance ();
SoapServerMessage message = new SoapServerMessage (request, server, stream);
message.SetStage (SoapMessageStage.BeforeDeserialize);
message.ContentType = ctype;
#if NET_2_0
object soapVer = context.Items ["WebServiceSoapVersion"];
if (soapVer != null)
message.SetSoapVersion ((SoapProtocolVersion) soapVer);
#endif
// If the routing style is SoapAction, then we can get the method information now
// and set it to the SoapMessage
if (_typeStubInfo.RoutingStyle == SoapServiceRoutingStyle.SoapAction)
{
string headerName = message.IsSoap12 ? "action" : "SOAPAction";
soapAction = message.IsSoap12 ? WebServiceHelper.GetContextAction(request.ContentType) : request.Headers [headerName];
if (soapAction == null) {
if (!message.IsSoap12)
throw new SoapException ("Missing SOAPAction header", WebServiceHelper.ClientFaultCode (message.IsSoap12));
}
else
{
methodInfo = _typeStubInfo.GetMethodForSoapAction (soapAction);
if (methodInfo == null) throw new SoapException ("Server did not recognize the value of HTTP header " + headerName + ": " + soapAction, WebServiceHelper.ClientFaultCode (message.IsSoap12));
message.MethodStubInfo = methodInfo;
}
}
// Execute the high priority global extensions. Do not try to execute the medium and
// low priority extensions because if the routing style is RequestElement we still
// don't have method information
_extensionChainHighPrio = SoapExtension.CreateExtensionChain (_typeStubInfo.SoapExtensions[0]);
stream = SoapExtension.ExecuteChainStream (_extensionChainHighPrio, stream);
SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, stream, false);
// If the routing style is RequestElement, try to get the method name from the
// stream processed by the high priority extensions
if (_typeStubInfo.RoutingStyle == SoapServiceRoutingStyle.RequestElement || (message.IsSoap12 && soapAction == null))
{
MemoryStream mstream;
byte[] buffer = null;
if (stream.CanSeek)
{
buffer = new byte [stream.Length];
for (int n=0; n<stream.Length;)
n += stream.Read (buffer, n, (int)stream.Length-n);
mstream = new MemoryStream (buffer);
}
else
{
buffer = new byte [500];
mstream = new MemoryStream ();
int len;
while ((len = stream.Read (buffer, 0, 500)) > 0)
mstream.Write (buffer, 0, len);
mstream.Position = 0;
buffer = mstream.ToArray ();
}
soapAction = ReadActionFromRequestElement (new MemoryStream (buffer), encoding, message.IsSoap12);
stream = mstream;
methodInfo = (SoapMethodStubInfo) _typeStubInfo.GetMethod (soapAction);
message.MethodStubInfo = methodInfo;
}
// Whatever routing style we used, we should now have the method information.
// We can now notify the remaining extensions
if (methodInfo == null) throw new SoapException ("Method '" + soapAction + "' not defined in the web service '" + _typeStubInfo.LogicalType.WebServiceName + "'", WebServiceHelper.ClientFaultCode (message.IsSoap12));
_extensionChainMedPrio = SoapExtension.CreateExtensionChain (methodInfo.SoapExtensions);
_extensionChainLowPrio = SoapExtension.CreateExtensionChain (_typeStubInfo.SoapExtensions[1]);
stream = SoapExtension.ExecuteChainStream (_extensionChainMedPrio, stream);
stream = SoapExtension.ExecuteChainStream (_extensionChainLowPrio, stream);
SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, stream, false);
SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, stream, false);
// Deserialize the request
StreamReader reader = new StreamReader (stream, encoding, false);
XmlTextReader xmlReader = new XmlTextReader (reader);
try
{
object content;
SoapHeaderCollection headers;
WebServiceHelper.ReadSoapMessage (xmlReader, methodInfo, SoapHeaderDirection.In, message.IsSoap12, out content, out headers);
message.InParameters = (object []) content;
message.SetHeaders (headers);
}
catch (Exception ex)
{
throw new SoapException ("Could not deserialize Soap message", WebServiceHelper.ClientFaultCode (message.IsSoap12), ex);
}
// Notify the extensions after deserialization
message.SetStage (SoapMessageStage.AfterDeserialize);
SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, stream, false);
SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, stream, false);
SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, stream, false);
return message;
//}
}
string ReadActionFromRequestElement (Stream stream, Encoding encoding, bool soap12)
{
string envNS = soap12 ?
WebServiceHelper.Soap12EnvelopeNamespace :
WebServiceHelper.SoapEnvelopeNamespace;
try
{
StreamReader reader = new StreamReader (stream, encoding, false);
XmlTextReader xmlReader = new XmlTextReader (reader);
xmlReader.MoveToContent ();
xmlReader.ReadStartElement ("Envelope", envNS);
while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" && xmlReader.NamespaceURI == envNS))
xmlReader.Skip ();
xmlReader.ReadStartElement ("Body", envNS);
xmlReader.MoveToContent ();
return xmlReader.LocalName;
}
catch (Exception ex)
{
string errmsg = "The root element for the request could not be determined. ";
errmsg += "When RoutingStyle is set to RequestElement, SoapExtensions configured ";
errmsg += "via an attribute on the method cannot modify the request stream before it is read. ";
errmsg += "The extension must be configured via the SoapExtensionTypes element in web.config ";
errmsg += "or the request must arrive at the server as clear text.";
throw new SoapException (errmsg, WebServiceHelper.ServerFaultCode (soap12), ex);
}
}
void SerializeResponse (HttpResponse response, SoapServerMessage message)
{
SoapMethodStubInfo methodInfo = message.MethodStubInfo;
if ((message.ContentEncoding != null) && (message.ContentEncoding.Length > 0))
response.AppendHeader("Content-Encoding", message.ContentEncoding);
response.ContentType = message.IsSoap12 ?
"application/soap+xml; charset=utf-8" :
"text/xml; charset=utf-8";
if (message.Exception != null) response.StatusCode = 500;
Stream responseStream = response.OutputStream;
Stream outStream = responseStream;
bool bufferResponse = (methodInfo == null || methodInfo.MethodAttribute.BufferResponse);
response.BufferOutput = bufferResponse;
try
{
// While serializing, process extensions in reverse order
if (bufferResponse)
{
outStream = SoapExtension.ExecuteChainStream (_extensionChainHighPrio, outStream);
outStream = SoapExtension.ExecuteChainStream (_extensionChainMedPrio, outStream);
outStream = SoapExtension.ExecuteChainStream (_extensionChainLowPrio, outStream);
message.SetStage (SoapMessageStage.BeforeSerialize);
SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, outStream, true);
SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, outStream, true);
SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, outStream, true);
}
XmlTextWriter xtw = WebServiceHelper.CreateXmlWriter (outStream);
if (message.Exception == null)
WebServiceHelper.WriteSoapMessage (xtw, methodInfo, SoapHeaderDirection.Out, message.OutParameters, message.Headers, message.IsSoap12);
else if (methodInfo != null) {
#if NET_2_0
if (message.IsSoap12)
WebServiceHelper.WriteSoapMessage (xtw, methodInfo, SoapHeaderDirection.Fault, new Soap12Fault (message.Exception), message.Headers, message.IsSoap12);
else
#endif
{
WebServiceHelper.WriteSoapMessage (xtw, methodInfo, SoapHeaderDirection.Fault, new Fault (message.Exception), message.Headers, message.IsSoap12);
}
}
else {
#if NET_2_0
if (message.IsSoap12)
WebServiceHelper.WriteSoapMessage (xtw, SoapBindingUse.Literal, Soap12Fault.Serializer, null, new Soap12Fault (message.Exception), null, message.IsSoap12);
else
#endif
{
WebServiceHelper.WriteSoapMessage (xtw, SoapBindingUse.Literal, Fault.Serializer, null, new Fault (message.Exception), null, message.IsSoap12);
}
}
if (bufferResponse)
{
message.SetStage (SoapMessageStage.AfterSerialize);
SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, outStream, true);
SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, outStream, true);
SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, outStream, true);
}
xtw.Flush ();
}
catch (Exception ex)
{
// If the response is buffered, we can discard the response and
// serialize a new Fault message as response.
if (bufferResponse) throw ex;
// If it is not buffered, we can't rollback what has been sent,
// so we can only close the connection and return.
responseStream.Close ();
return;
}
}
void SerializeFault (HttpContext context, SoapServerMessage requestMessage, Exception ex)
{
SoapException soex = ex as SoapException;
if (soex == null) soex = new SoapException (ex.ToString (), WebServiceHelper.ServerFaultCode (requestMessage != null && requestMessage.IsSoap12), ex);
SoapServerMessage faultMessage;
if (requestMessage != null)
faultMessage = new SoapServerMessage (context.Request, soex, requestMessage.MethodStubInfo, requestMessage.Server, requestMessage.Stream);
else
faultMessage = new SoapServerMessage (context.Request, soex, null, null, null);
#if NET_2_0
object soapVer = context.Items ["WebServiceSoapVersion"];
if (soapVer != null)
faultMessage.SetSoapVersion ((SoapProtocolVersion) soapVer);
#endif
SerializeResponse (context.Response, faultMessage);
context.Response.End ();
return;
}
private SoapServerMessage Invoke (HttpContext ctx, SoapServerMessage requestMessage)
{
SoapMethodStubInfo methodInfo = requestMessage.MethodStubInfo;
// Assign header values to web service members
requestMessage.UpdateHeaderValues (requestMessage.Server, methodInfo.Headers);
// Fill an array with the input parameters at the right position
object[] parameters = new object[methodInfo.MethodInfo.Parameters.Length];
ParameterInfo[] inParams = methodInfo.MethodInfo.InParameters;
for (int n=0; n<inParams.Length; n++)
parameters [inParams[n].Position] = requestMessage.InParameters [n];
// Invoke the method
try
{
object[] results = methodInfo.MethodInfo.Invoke (requestMessage.Server, parameters);
requestMessage.OutParameters = results;
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
// Check that headers with MustUnderstand flag have been understood
foreach (SoapHeader header in requestMessage.Headers)
{
if (header.MustUnderstand && !header.DidUnderstand)
throw new SoapHeaderException ("Header not understood: " + header.GetType(), WebServiceHelper.MustUnderstandFaultCode (requestMessage.IsSoap12));
}
// Collect headers that must be sent to the client
requestMessage.CollectHeaders (requestMessage.Server, methodInfo.Headers, SoapHeaderDirection.Out);
return requestMessage;
}
}
}
|