Esempio n. 1
0
        private IResponse ProcessSoapRequest(IRequest request)
        {
            var soap = new XmlDocument();
              soap.LoadXml(request.Body);
              var sparams = new RawHeaders();
              var body = soap.SelectSingleNode("//soap:Body", namespaceMgr);
              if (body == null) {
            throw new HttpStatusException(HttpCode.InternalError);
              }
              var method = body.FirstChild;
              foreach (var p in method.ChildNodes) {
            var e = p as XmlElement;
            if (e == null) {
              continue;
            }
            sparams.Add(e.LocalName, e.InnerText.Trim());
              }
              var env = new XmlDocument();
              env.AppendChild(env.CreateXmlDeclaration("1.0", "utf-8", "yes"));
              var envelope = env.CreateElement("SOAP-ENV", "Envelope", NS_SOAPENV);
              env.AppendChild(envelope);
              envelope.SetAttribute(
            "encodingStyle", NS_SOAPENV,
            "http://schemas.xmlsoap.org/soap/encoding/");

              var rbody = env.CreateElement("SOAP-ENV:Body", NS_SOAPENV);
              env.DocumentElement?.AppendChild(rbody);

              var code = HttpCode.Ok;
              try {
            IEnumerable<KeyValuePair<string, string>> result;
            switch (method.LocalName) {
            case "GetSearchCapabilities":
              result = HandleGetSearchCapabilities();
              break;
            case "GetSortCapabilities":
              result = HandleGetSortCapabilities();
              break;
            case "GetSystemUpdateID":
              result = HandleGetSystemUpdateID();
              break;
            case "Browse":
              result = HandleBrowse(request, sparams);
              break;
            case "X_GetFeatureList":
              result = HandleXGetFeatureList();
              break;
            case "X_SetBookmark":
              result = HandleXSetBookmark(sparams);
              break;
            case "GetCurrentConnectionIDs":
              result = HandleGetCurrentConnectionIDs();
              break;
            case "GetCurrentConnectionInfo":
              result = HandleGetCurrentConnectionInfo();
              break;
            case "GetProtocolInfo":
              result = HandleGetProtocolInfo();
              break;
            case "IsAuthorized":
              result = HandleIsAuthorized();
              break;
            case "IsValidated":
              result = HandleIsValidated();
              break;
            case "RegisterDevice":
              result = HandleRegisterDevice();
              break;
            default:
              throw new HttpStatusException(HttpCode.NotFound);
            }
            var response = env.CreateElement($"u:{method.LocalName}Response", method.NamespaceURI);
            rbody.AppendChild(response);

            foreach (var i in result) {
              var ri = env.CreateElement(i.Key);
              ri.InnerText = i.Value;
              response.AppendChild(ri);
            }
              }
              catch (Exception ex) {
            code = HttpCode.InternalError;
            var fault = env.CreateElement("SOAP-ENV", "Fault", NS_SOAPENV);
            var faultCode = env.CreateElement("faultcode");
            faultCode.InnerText = "500";
            fault.AppendChild(faultCode);
            var faultString = env.CreateElement("faultstring");
            faultString.InnerText = ex.ToString();
            fault.AppendChild(faultString);
            var detail = env.CreateDocumentFragment();
            detail.InnerXml =
              "<detail><UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\"><errorCode>401</errorCode><errorDescription>Invalid Action</errorDescription></UPnPError></detail>";
            fault.AppendChild(detail);
            rbody.AppendChild(fault);
            WarnFormat(
              "Invalid call: Action: {0}, Params: {1}, Problem {2}",
              method.LocalName, sparams, ex.Message);
              }

              var rv = new StringResponse(code, "text/xml", env.OuterXml);
              rv.Headers.Add("EXT", string.Empty);
              return rv;
        }
Esempio n. 2
0
        private IResponse ProcessSoapRequest(IRequest request)
        {
            var soap = new XmlDocument();

            soap.LoadXml(request.Body);
            var sparams = new RawHeaders();
            var body    = soap.SelectSingleNode("//soap:Body", namespaceMgr);

            if (body == null)
            {
                throw new HttpStatusException(HttpCode.InternalError);
            }
            var method = body.FirstChild;

            foreach (var p in method.ChildNodes)
            {
                var e = p as XmlElement;
                if (e == null)
                {
                    continue;
                }
                sparams.Add(e.LocalName, e.InnerText.Trim());
            }

            var env = new XmlDocument();

            env.AppendChild(env.CreateXmlDeclaration("1.0", "utf-8", "yes"));
            var envelope = env.CreateElement("SOAP-ENV", "Envelope", NS_SOAPENV);

            env.AppendChild(envelope);
            envelope.SetAttribute(
                "encodingStyle", NS_SOAPENV,
                "http://schemas.xmlsoap.org/soap/encoding/");

            var rbody = env.CreateElement("SOAP-ENV:Body", NS_SOAPENV);

            env.DocumentElement?.AppendChild(rbody);

            var code = HttpCode.Ok;

            try
            {
                IEnumerable <KeyValuePair <string, string> > result;
                switch (method.LocalName)
                {
                case "GetSearchCapabilities":
                    result = HandleGetSearchCapabilities();
                    break;

                case "GetSortCapabilities":
                    result = HandleGetSortCapabilities();
                    break;

                case "GetSystemUpdateID":
                    result = HandleGetSystemUpdateID();
                    break;

                case "Browse":
                    result = HandleBrowse(request, sparams);
                    break;

                case "X_GetFeatureList":
                    result = HandleXGetFeatureList();
                    break;

                case "X_SetBookmark":
                    result = HandleXSetBookmark(sparams);
                    break;

                case "GetCurrentConnectionIDs":
                    result = HandleGetCurrentConnectionIDs();
                    break;

                case "GetCurrentConnectionInfo":
                    result = HandleGetCurrentConnectionInfo();
                    break;

                case "GetProtocolInfo":
                    result = HandleGetProtocolInfo();
                    break;

                case "IsAuthorized":
                    result = HandleIsAuthorized();
                    break;

                case "IsValidated":
                    result = HandleIsValidated();
                    break;

                case "RegisterDevice":
                    result = HandleRegisterDevice();
                    break;

                default:
                    throw new HttpStatusException(HttpCode.NotFound);
                }

                var response = env.CreateElement($"u:{method.LocalName}Response", method.NamespaceURI);
                rbody.AppendChild(response);

                foreach (var i in result)
                {
                    var ri = env.CreateElement(i.Key);
                    ri.InnerText = i.Value;
                    response.AppendChild(ri);
                }
            }
            catch (Exception ex)
            {
                code = HttpCode.InternalError;
                var fault     = env.CreateElement("SOAP-ENV", "Fault", NS_SOAPENV);
                var faultCode = env.CreateElement("faultcode");
                faultCode.InnerText = "500";
                fault.AppendChild(faultCode);
                var faultString = env.CreateElement("faultstring");
                faultString.InnerText = ex.ToString();
                fault.AppendChild(faultString);
                var detail = env.CreateDocumentFragment();
                detail.InnerXml =
                    "<detail><UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\"><errorCode>401</errorCode><errorDescription>Invalid Action</errorDescription></UPnPError></detail>";
                fault.AppendChild(detail);
                rbody.AppendChild(fault);
                WarnFormat(
                    "Invalid call: Action: {0}, Params: {1}, Problem {2}",
                    method.LocalName, sparams, ex.Message);
            }

            var rv = new StringResponse(code, "text/xml", env.OuterXml);

            rv.Headers.Add("EXT", string.Empty);
            return(rv);
        }