public static MetaWeblogResult AddSecurityFault(this MetaWeblogResult result)
        {
            var faultStruct = new FaultStruct();

            faultStruct.faultCode   = "11"; // invalid access
            faultStruct.faultString = "Authentication Failed";
            result.Fault            = faultStruct;

            return(result);
        }
        public static MetaWeblogResult AddValidatonFault(this MetaWeblogResult result)
        {
            var faultStruct = new FaultStruct();

            faultStruct.faultCode   = "802"; // invalid access
            faultStruct.faultString = "invalid request";
            result.Fault            = faultStruct;

            return(result);
        }
Exemplo n.º 3
0
        public void SerializeFaultResponse(Stream stm, XmlRpcFaultException faultEx)
        {
            var fs = new FaultStruct
            {
                faultCode   = faultEx.FaultCode,
                faultString = faultEx.FaultString
            };

            var xtw = new XmlTextWriter(stm, Configuration.XmlEncoding);

            Configuration.ConfigureXmlFormat(xtw);

            xtw.WriteStartDocument();
            xtw.WriteStartElement("", "methodResponse", "");
            xtw.WriteStartElement("", "fault", "");

            Serialize(xtw, fs);

            xtw.WriteEndElement();
            xtw.WriteEndElement();
            xtw.Flush();
        }
Exemplo n.º 4
0
        public void SerializeFaultResponse(
            Stream stm,
            XmlRpcFaultException faultEx)
        {
            FaultStruct fs = new FaultStruct();

            fs.faultCode   = faultEx.FaultCode;
            fs.faultString = faultEx.FaultString;
            XmlWriter xtw = XmlRpcXmlWriter.Create(stm, XmlRpcFormatSettings);

            xtw.WriteStartDocument(false);
            xtw.WriteStartElement("", "methodResponse", "");
            xtw.WriteStartElement("", "fault", "");
            Serialize(xtw, fs, new MappingActions {
                NullMappingAction = NullMappingAction.Error
            });
            WriteFullEndElement(xtw);
            WriteFullEndElement(xtw);
            xtw.Flush();


            #region ×¢ÊÍ

            /*
             * FaultStruct fs=new FaultStruct();
             * fs.faultCode = faultEx.FaultCode;
             * fs.faultString = faultEx.FaultString;
             *
             * XmlWriter xw = XmlWriter.Create(stm);
             * xw.WriteStartDocument();
             *
             * xw.WriteStartElement("", "methodResponse", "");
             * xw.WriteStartElement("", "fault", "");
             * xw.WriteStartElement("value");
             *
             * xw.WriteStartElement("struct");
             *
             * //Member ״̬¿ªÊ¼
             * xw.WriteStartElement("member");
             * xw.WriteStartElement("name");
             * xw.WriteValue("faultCode");
             * xw.WriteEndElement();
             *
             * xw.WriteStartElement("value");
             * xw.WriteStartElement("int");
             * xw.WriteValue(faultEx.FaultCode);
             * xw.WriteEndElement();
             * xw.WriteEndElement();
             *
             * //Member½áÊø
             * xw.WriteEndElement();
             *
             * //Member×Ö·û¿ªÊ¼
             * xw.WriteStartElement("member");
             * xw.WriteStartElement("name");
             * xw.WriteValue("faultString");
             * xw.WriteEndElement();
             *
             * xw.WriteStartElement("value");
             * xw.WriteStartElement("string");
             * xw.WriteValue(faultEx.FaultString);
             * xw.WriteEndElement();
             * xw.WriteEndElement();
             *
             * //Member½áÊø
             * xw.WriteEndElement();
             *
             * //struct½áÊø
             * xw.WriteEndElement();
             *
             * //value½áÊø
             * xw.WriteEndElement();
             *
             *
             * //fault½áÊø
             * xw.WriteEndElement();
             *
             * //methodResponse½áÊø
             * xw.WriteEndElement();
             *
             *
             * // xw.WriteAttributeString("faultCode", fs.faultCode.ToString());
             * //xw.WriteAttributeString("faultString", fs.faultString);
             * // xw.WriteEndElement();
             * // xw.WriteEndElement();
             *
             * xw.WriteEndDocument();
             * xw.Flush();
             *
             * /*
             * <?xml version="1.0" encoding="UTF-8"?>
             * <methodResponse>
             * <fault>
             * <value>
             *  <struct>
             *      <member>
             *          <name>faultCode</name>
             *          <value><int>´íÎóºÅ</int></value>
             *      </member>
             *      <member>
             *          <name>faultString</name>
             *          <value><string>ʧ°ÜÔ­Òò</string></value>
             *      </member>
             *  </struct>
             * </value>
             * </fault>
             * </methodResponse>
             */
            #endregion
        }
        public virtual async Task <IActionResult> Index()
        {
            CancellationToken cancellationToken = HttpContext?.RequestAborted ?? CancellationToken.None;

            var dumpFileBasePath = HostingEnvironment.ContentRootPath
                                   + ApiOptions.AppRootDumpFolderVPath.Replace('/', Path.DirectorySeparatorChar);

            XDocument        postedXml = null;
            XDocument        resultXml;
            MetaWeblogResult outCome;
            FaultStruct      faultStruct;

            try
            {
                using (HttpContext.Request.Body)

                {
                    string tmp;
                    using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
                    {
                        tmp = await reader.ReadToEndAsync();
                    }

                    postedXml = XDocument.Parse(tmp, LoadOptions.None);
                }
            }
            catch (Exception ex)
            {
                Log.LogError(ex.Message, ex);

                if (ApiOptions.DumpRequestXmlToDisk)
                {
                    var requestFileName = dumpFileBasePath + "request-with-error" + Utils.GetDateTimeStringForFileName(true) + ".txt";
                    using (StreamWriter s = System.IO.File.CreateText(requestFileName))
                    {
                        //postedXml.Save(s, SaveOptions.None);
                        await HttpContext.Request.Body.CopyToAsync(s.BaseStream);
                    }
                }

                outCome                 = new MetaWeblogResult();
                faultStruct             = new FaultStruct();
                faultStruct.faultCode   = "802"; // invalid access
                faultStruct.faultString = "invalid access";
                outCome.Fault           = faultStruct;
                resultXml               = ResultFormatter.Format(outCome);
                return(new XmlResult(resultXml));
            }


            var metaWeblogRequest = RequestParser.ParseRequest(postedXml);

            if (ApiOptions.DumpRequestXmlToDisk)
            {
                var requestFileName = dumpFileBasePath + "request-"
                                      + Utils.GetDateTimeStringForFileName(true) + "-"
                                      + metaWeblogRequest.MethodName.Replace(".", "-")
                                      + ".xml";
                using (StreamWriter s = System.IO.File.CreateText(requestFileName))
                {
                    postedXml.Save(s, SaveOptions.None);
                }
            }

            var permissions = await Security.ValiatePermissions(metaWeblogRequest, cancellationToken);

            if (string.IsNullOrEmpty(metaWeblogRequest.BlogId))
            {
                metaWeblogRequest.BlogId = permissions.BlogId;
            }

            if ((!permissions.CanEditPosts) && (!permissions.CanEditPages))
            {
                outCome   = new MetaWeblogResult();
                resultXml = ResultFormatter.Format(outCome);
                return(new XmlResult(resultXml));
            }

            var isValid = await RequestValidator.IsValid(metaWeblogRequest, cancellationToken);

            if (!isValid)
            {
                outCome = new MetaWeblogResult();
                outCome.AddValidatonFault();
                resultXml = ResultFormatter.Format(outCome);
                return(new XmlResult(resultXml));
            }

            outCome = await RequestProcessor.ProcessRequest(
                metaWeblogRequest,
                permissions,
                cancellationToken);

            resultXml = ResultFormatter.Format(outCome);

            if (ApiOptions.DumpResponseXmlToDisk)
            {
                var reseponseFileName = dumpFileBasePath + "response-"
                                        + Utils.GetDateTimeStringForFileName(true) + "-"
                                        + outCome.Method.Replace(".", "-")
                                        + ".xml";

                using (StreamWriter s = System.IO.File.CreateText(reseponseFileName))
                {
                    resultXml.Save(s, SaveOptions.None);
                }
            }

            return(new XmlResult(resultXml));
        }