Example #1
0
        private void ProceedControl(MyWebRequest req)
        {
            if (!req.Headers["SOAPACTION"].Trim('"').StartsWith(serviceType, StringComparison.OrdinalIgnoreCase))
            {
                throw new HttpException(500, "Service type mismatch");
            }
            XmlDocument xDoc = new XmlDocument();

            using (MemoryStream stream = req.GetContent())
            {
                xDoc.Load(stream);
                XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xDoc.NameTable);
                namespaceManager.AddNamespace("soapNam", "http://schemas.xmlsoap.org/soap/envelope/");
                XmlNode bodyNode = xDoc.SelectSingleNode("/soapNam:Envelope/soapNam:Body/*[1]", namespaceManager);

                if (bodyNode == null)
                {
                    throw new HttpException(500, "Body node of SOAP message not found");
                }
                if (P2pProxyApp.Debug)
                {
                    P2pProxyApp.Log.Write("Run method " + GetType().Name + "." + bodyNode.LocalName, TypeMessage.Info);
                }
                MethodInfo method = GetType().GetMethod(bodyNode.LocalName, BindingFlags.Instance | BindingFlags.NonPublic);
                if (method == null)
                {
                    throw new HttpException(401, "Invalid Action " + GetType().Name + "." + bodyNode.LocalName);
                }
                string[] outParam = method.GetCustomAttributes(typeof(UpnpServiceArgument), true).Cast <UpnpServiceArgument>().OrderBy(
                    a => a.Index).Select(a => a.Name).ToArray();
                ParameterInfo[] paramDef = method.GetParameters();

                req.SetSoap(bodyNode.LocalName, serviceType, outParam);

                object[] paramVal = new object[paramDef.Length];
                paramVal[0] = req;
                for (int i = 1; i < paramDef.Length; i++)
                {
                    string param_name =
                        ((AliasAttribute)paramDef[i].GetCustomAttributes(typeof(AliasAttribute), true)[0]).Name;
                    XmlNode paramNode = bodyNode.SelectSingleNode(param_name);
                    if (paramNode == null)
                    {
                        throw new HttpException(402, "Invalid Args " + bodyNode.LocalName + " " + param_name);
                    }

                    paramVal[i] = paramNode.InnerXml;
                }
                try
                {
                    method.Invoke(this, paramVal);
                }
                catch (ArgumentException)
                {
                    throw new HttpException(402, "Invalid Args " + bodyNode.LocalName + " " + string.Join("|", paramVal));
                }
                catch (TargetParameterCountException)
                {
                    throw new HttpException(402, "Invalid Args " + bodyNode.LocalName + " " + string.Join("|", paramVal));
                }
                catch (TargetInvocationException ex)
                {
                    if (ex.InnerException is SoapException)
                    {
                        throw ex.InnerException;
                    }
                    P2pProxyApp.Log.Write(ex.Message, TypeMessage.Error);
                    throw new SoapException(501, "Action Failed");
                }
            }
        }