Example #1
0
		public SoapServerMethod (Type serverType, LogicalMethodInfo methodInfo)
		{
			TypeStubInfo type = TypeStubManager.GetTypeStub (serverType, "Soap");
			info = type.GetMethod (methodInfo.Name) as SoapMethodStubInfo;
			if (info == null)
				throw new InvalidOperationException ("Argument methodInfo does not seem to be a member of the server type.");
		}
		//
		// Constructs the SoapClientMessage
		//
		internal SoapClientMessage (SoapHttpClientProtocol client, SoapMethodStubInfo msi, string url, object [] parameters)
		{
			this.MethodStubInfo = msi;
			this.client = client;
			this.url = url;
			Parameters = parameters;
		}
		internal SoapServerMessage (HttpRequest request, SoapException exception, SoapMethodStubInfo stubInfo, object server, Stream stream)
			: base (stream, exception)
		{
			this.action = request.Headers ["SOAPAction"];
			this.stubInfo = stubInfo;
			this.server = server;
			this.url = request.Url.ToString();
		}
Example #4
0
		//
		// Constructs the SoapClientMessage
		//
		internal SoapClientMessage (SoapHttpClientProtocol client, SoapMethodStubInfo msi, string url, object [] parameters)
		{
			this.MethodStubInfo = msi;
			this.client = client;
			this.url = url;
			Parameters = parameters;
			if (SoapVersion == SoapProtocolVersion.Soap12)
				ContentType = "application/soap+xml";
		}
Example #5
0
		internal SoapServerMessage (HttpRequest request, SoapException exception, SoapMethodStubInfo stubInfo, object server, Stream stream)
			: base (stream, exception)
		{
			this.action = request.Headers ["SOAPAction"];
			if (this.action != null)
				this.action = action.Trim ('"',' ');
			this.stubInfo = stubInfo;
			this.server = server;
			this.url = request.Url.ToString();
			ContentEncoding = request.Headers ["Content-Encoding"];
		}
Example #6
0
		protected override MethodStubInfo CreateMethodStubInfo (TypeStubInfo parent, LogicalMethodInfo lmi, bool isClientProxy)
		{
			SoapMethodStubInfo res = null;
			object [] ats = lmi.GetCustomAttributes (typeof (SoapDocumentMethodAttribute));
			if (ats.Length == 0) ats = lmi.GetCustomAttributes (typeof (SoapRpcMethodAttribute));

			if (ats.Length == 0 && isClientProxy)
				return null;
			else if (ats.Length == 0)
				res = new SoapMethodStubInfo (parent, lmi, null, xmlImporter, soapImporter);
			else
				res = new SoapMethodStubInfo (parent, lmi, ats[0], xmlImporter, soapImporter);
				
			methods_byaction [res.Action] = res;
			return res;
		}
		SoapServerMessage DeserializeRequest (HttpRequest request)
		{
			Stream stream = request.InputStream;

			using (stream)
			{
				string soapAction = null;
				string ctype;
				Encoding encoding = WebServiceHelper.GetContentEncoding (request.ContentType, out ctype);
				if (ctype != "text/xml")
					throw new WebException ("Content is not XML: " + ctype);
					
				server = CreateServerInstance ();

				SoapServerMessage message = new SoapServerMessage (request, server, stream);
				message.SetStage (SoapMessageStage.BeforeDeserialize);
				message.ContentType = ctype;
				message.ContentEncoding = encoding.WebName;

				// 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)
				{
					soapAction = request.Headers ["SOAPAction"];
					if (soapAction == null) throw new SoapException ("Missing SOAPAction header", SoapException.ClientFaultCode);
					methodInfo = _typeStubInfo.GetMethodForSoapAction (soapAction);
					if (methodInfo == null) throw new SoapException ("Server did not recognize the value of HTTP header SOAPAction: " + soapAction, SoapException.ClientFaultCode);
					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, 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)
				{
					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);

					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 + "'", SoapException.ClientFaultCode);

				_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, false);
				SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, false);

				// Deserialize the request

				StreamReader reader = new StreamReader (stream, encoding, false);
				XmlTextReader xmlReader = new XmlTextReader (reader);

				try
				{
					object content;
					SoapHeaderCollection headers;
					WebServiceHelper.ReadSoapMessage (xmlReader, _typeStubInfo, methodInfo.Use, methodInfo.RequestSerializer, out content, out headers);
					message.InParameters = (object []) content;
					message.SetHeaders (headers);
				}
				catch (Exception ex)
				{
					throw new SoapException ("Could not deserialize Soap message", SoapException.ClientFaultCode, ex);
				}

				// Notify the extensions after deserialization

				message.SetStage (SoapMessageStage.AfterDeserialize);
				SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, false);
				SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, false);
				SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, false);

				xmlReader.Close ();

				return message;
			}
		}
Example #8
0
		void AddOperationMsgBindings (SoapMethodStubInfo method, MessageBinding msg)
		{
			SoapBodyBinding sbbo = CreateSoapBodyBinding ();
			msg.Extensions.Add (sbbo);
			sbbo.Use = method.Use;
			if (method.Use == SoapBindingUse.Encoded) {
				sbbo.Namespace = ReflectionContext.ServiceDescription.TargetNamespace;
				sbbo.Encoding = EncodingNS;
			}
		}
Example #9
0
		public static void ReadSoapMessage (XmlTextReader xmlReader, SoapMethodStubInfo method, SoapHeaderDirection dir, bool soap12, out object body, out SoapHeaderCollection headers)
		{
			XmlSerializer bodySerializer = method.GetBodySerializer (dir, false);// no need to worry about soap12 arg since no call for Fault anyways here.
			XmlSerializer headerSerializer = method.GetHeaderSerializer (dir);
			ReadSoapMessage (xmlReader, bodySerializer, headerSerializer, soap12, out body, out headers);
		}
Example #10
0
		public static void WriteSoapMessage (XmlTextWriter xtw, SoapMethodStubInfo method, SoapHeaderDirection dir, object bodyContent, SoapHeaderCollection headers, bool soap12)
		{
			SoapBindingUse methodUse = dir == SoapHeaderDirection.Fault ? SoapBindingUse.Literal : method.Use;
			XmlSerializer bodySerializer = method.GetBodySerializer (dir, soap12);
			XmlSerializer headerSerializer = method.GetHeaderSerializer (dir);
			object[] headerArray = method.GetHeaderValueArray (dir, headers);
			WriteSoapMessage (xtw, methodUse, bodySerializer, headerSerializer, bodyContent, headerArray, soap12);
		}
Example #11
0
        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;
            }
        }
Example #12
0
        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);
            //}
        }
Example #13
0
        //
        // TODO:
        //    Handle other web responses (multi-output?)
        //
        object [] ReceiveResponse(WebResponse response, SoapClientMessage message, SoapExtension[] extensions)
        {
            SoapMethodStubInfo msi           = message.MethodStubInfo;
            HttpWebResponse    http_response = response as HttpWebResponse;

            if (http_response != null)
            {
                HttpStatusCode code = http_response.StatusCode;

                if (!(code == HttpStatusCode.Accepted || code == HttpStatusCode.OK || code == HttpStatusCode.InternalServerError))
                {
                    string msg = "The request failed with HTTP status {0}: {1}";
                    msg = String.Format(msg, (int)code, code);
                    throw new WebException(msg, null, WebExceptionStatus.ProtocolError, http_response);
                }
                if (message.OneWay && response.ContentLength <= 0 && (code == HttpStatusCode.Accepted || code == HttpStatusCode.OK))
                {
                    return(new object[0]);
                }
            }

            //
            // Remove optional encoding
            //
            string   ctype;
            Encoding encoding = WebServiceHelper.GetContentEncoding(response.ContentType, out ctype);

            ctype = ctype.ToLower(CultureInfo.InvariantCulture);
#if NET_2_0
            if ((!message.IsSoap12 || ctype != "application/soap+xml") && ctype != "text/xml")
#else
            if (ctype != "text/xml")
#endif
            { WebServiceHelper.InvalidOperation(
                  String.Format("Not supported Content-Type in the response: '{0}'", response.ContentType),
                  response, encoding); }

            message.ContentType     = ctype;
            message.ContentEncoding = encoding.WebName;

            Stream stream = response.GetResponseStream();

            if (extensions != null)
            {
                stream = SoapExtension.ExecuteChainStream(extensions, stream);
                message.SetStage(SoapMessageStage.BeforeDeserialize);
                SoapExtension.ExecuteProcessMessage(extensions, message, stream, false);
            }

            // Deserialize the response

            SoapHeaderCollection headers;
            object content;

            using (StreamReader reader = new StreamReader(stream, encoding, false))
            {
                XmlTextReader xml_reader = new XmlTextReader(reader);

                WebServiceHelper.ReadSoapMessage(xml_reader, msi, SoapHeaderDirection.Out, message.IsSoap12, out content, out headers);
            }

#if NET_2_0
            if (content is Soap12Fault)
            {
                SoapException ex = WebServiceHelper.Soap12FaultToSoapException((Soap12Fault)content);
                message.SetException(ex);
            }
            else
#endif
            if (content is Fault)
            {
                Fault         fault = (Fault)content;
                SoapException ex    = new SoapException(fault.faultstring, fault.faultcode, fault.faultactor, fault.detail);
                message.SetException(ex);
            }
            else
            {
                message.OutParameters = (object[])content;
            }

            message.SetHeaders(headers);
            message.UpdateHeaderValues(this, message.MethodStubInfo.Headers);

            if (extensions != null)
            {
                message.SetStage(SoapMessageStage.AfterDeserialize);
                SoapExtension.ExecuteProcessMessage(extensions, message, stream, false);
            }

            if (message.Exception == null)
            {
                return(message.OutParameters);
            }
            else
            {
                throw message.Exception;
            }
        }