Beispiel #1
0
        private void XmlRpcHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
        {
            XmlRpcRequest  rpcRequest  = null;
            XmlRpcResponse rpcResponse = null;

            try
            { rpcRequest = m_xmlrpcDeserializer.Deserialize(new StreamReader(request.Body)) as XmlRpcRequest; }
            catch (SystemException ex)
            { m_log.Warn("Failed to deserialize incoming XML-RPC request: " + ex.Message); }

            if (rpcRequest != null)
            {
                response.ContentType = "text/xml";
                response.Encoding    = Encoding.UTF8;
                response.Chunked     = false;

                XmlRpcCallback callback;
                if (m_xmlrpcCallbacks.TryGetValue(rpcRequest.MethodName, out callback))
                {
                    // TODO: Add IHttpClientContext.RemoteEndPoint
                    rpcRequest.Params.Add(null); //rpcRequest.Params.Add(client.RemoteEndPoint);
                    rpcRequest.Params.Add(request.Uri);

                    try
                    {
                        rpcResponse = callback(rpcRequest, request);

                        string responseString = XmlRpcResponseSerializer.Singleton.Serialize(rpcResponse);
                        byte[] buffer         = Encoding.UTF8.GetBytes(responseString);
                        // Set the content-length, otherwise the LL viewer freaks out
                        response.ContentLength = buffer.Length;
                        response.Body.Write(buffer, 0, buffer.Length);
                        response.Body.Flush();
                    }
                    catch (Exception ex)
                    {
                        m_log.ErrorFormat("XML-RPC method [{0}] threw exception: {1}", rpcRequest.MethodName, ex);

                        rpcResponse = new XmlRpcResponse();
                        rpcResponse.SetFault(INTERNAL_ERROR, String.Format("Requested method [{0}] threw exception: {1}", rpcRequest.MethodName, ex.Message));
                        XmlRpcResponseSerializer.Singleton.Serialize(new XmlTextWriter(response.Body, Encoding.UTF8), rpcResponse);
                    }
                }
                else
                {
                    m_log.WarnFormat("XML-RPC method [{0}] not found", rpcRequest.MethodName);

                    rpcResponse = new XmlRpcResponse();
                    rpcResponse.SetFault(METHOD_NOT_FOUND, String.Format("Requested method [{0}] not found", rpcRequest.MethodName));
                    XmlRpcResponseSerializer.Singleton.Serialize(new XmlTextWriter(response.Body, Encoding.UTF8), rpcResponse);
                }
            }
            else
            {
                m_log.Warn("Bad XML-RPC request");
                response.Status = HttpStatusCode.BadRequest;
            }
        }
Beispiel #2
0
        /// <summary>Classes Main method.</summary>
        /// <remarks>This method opens an XML file as a <c>StreamReader</c> and then asks
        /// <c>XmlRpcRequestDeserializer.Parse</c> to deserialize it into an <c>XmlRpcRequest</c>. The
        /// resultant request is now displayed.
        /// </remarks>
        public static void Main(String[] args)
        {
            Logger.Delegate = new Logger.LoggerDelegate(WriteEntry);
            XmlRpcRequestDeserializer deserializer = new XmlRpcRequestDeserializer();

            Console.WriteLine("Attempting to deserialize " + args[0]);
            StreamReader  input = new StreamReader(args[0]);
            XmlRpcRequest req   = (XmlRpcRequest)deserializer.Deserialize(input);

            Console.WriteLine(req);
        }