Universe.Framework.Servers.HttpServer.BaseHttpServer.HandleXmlRpcRequests C# (CSharp) Method

HandleXmlRpcRequests() private method

Try all the registered xmlrpc handlers when an xmlrpc request is received. Sends back an XMLRPC unknown request response if no handler is registered for the requested method.
private HandleXmlRpcRequests ( OSHttpRequest request, OSHttpResponse response ) : byte[]
request Universe.Framework.Servers.HttpServer.Implementation.OSHttpRequest
response Universe.Framework.Servers.HttpServer.Implementation.OSHttpResponse
return byte[]
        byte [] HandleXmlRpcRequests (OSHttpRequest request, OSHttpResponse response)
        {
            string requestBody = HttpServerHandlerHelpers.ReadString (request.InputStream);

            requestBody = requestBody.Replace ("<base64></base64>", "");
            string responseString = string.Empty;
            XmlRpcRequest xmlRprcRequest = null;

            try {
                xmlRprcRequest = (XmlRpcRequest)(new XmlRpcRequestDeserializer ()).Deserialize (requestBody);
            } catch (XmlException e) {
                MainConsole.Instance.WarnFormat (
                    "[Base HTTP server]: Got XMLRPC request with invalid XML from {0}.  XML was '{1}'.  Sending blank response.  Exception: {2}",
                    request.RemoteIPEndPoint, requestBody, e.ToString ());
            }

            if (xmlRprcRequest != null)
            {
                string methodName = xmlRprcRequest.MethodName;
                if (methodName != null)
                {
                    xmlRprcRequest.Params.Add (request.RemoteIPEndPoint); // Param[1]
                    XmlRpcResponse xmlRpcResponse;

                    XmlRpcMethod method;
                    bool methodWasFound;
                    lock (m_rpcHandlers)
                        methodWasFound = m_rpcHandlers.TryGetValue (methodName, out method);

                    if (methodWasFound)
                    {
                        xmlRprcRequest.Params.Add (request.Url); // Param[2]

                        string xff = "X-Forwarded-For";
                        string xfflower = xff.ToLower ();
                        foreach (string s in request.Headers.AllKeys)
                        {
                            if (s != null && s.Equals (xfflower))
                            {
                                xff = xfflower;
                                break;
                            }
                        }

                        xmlRprcRequest.Params.Add (request.Headers.Get (xff)); // Param[3]

                        try {
                            xmlRpcResponse = method (xmlRprcRequest, request.RemoteIPEndPoint);
                        } catch (Exception e) {
                            string errorMessage
                                = string.Format (
                                    "Requested method [{0}] from {1} threw exception: {2} {3}",
                                    methodName, request.RemoteIPEndPoint.Address, e.Message, e.StackTrace);

                            MainConsole.Instance.ErrorFormat ("[Base HTTP server]: {0}", errorMessage);

                            // if the registered XmlRpc method threw an exception, we pass a fault-code along
                            xmlRpcResponse = new XmlRpcResponse ();

                            // Code probably set in accordance with http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
                            xmlRpcResponse.SetFault (-32603, errorMessage);
                        }
                    } else {
                        xmlRpcResponse = new XmlRpcResponse ();

                        // Code set in accordance with http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
                        xmlRpcResponse.SetFault (
                            XmlRpcErrorCodes.SERVER_ERROR_METHOD,
                            string.Format ("Requested method [{0}] not found", methodName));
                    }

                    response.ContentType = "text/xml";
                    responseString = XmlRpcResponseSerializer.Singleton.Serialize (xmlRpcResponse);
                } else {
                    response.ContentType = "text/plain";
                    response.StatusCode = 404;
                    response.StatusDescription = "Not Found";
                    responseString = "Not found";

                    MainConsole.Instance.ErrorFormat ("[Base HTTP server]: Handler not found for http request {0} {1}", request.HttpMethod, request.Url.PathAndQuery);
                }
            }

            byte [] buffer = Encoding.UTF8.GetBytes (responseString);

            response.SendChunked = false;
            response.ContentEncoding = Encoding.UTF8;

            return buffer;
        }