Ejemplo n.º 1
0
        protected JObject ProcessRequest(HttpContext context, JObject request)
        {
            if (!request.ContainsKey("id"))
            {
                return(null);
            }
            if (!request.ContainsKey("method") || !request.ContainsKey("params") || !(request["params"] is JArray))
            {
                return(RpcMessage.CreateErrorResult(request["id"], RpcMessage.INVALID_REQUEST, "Invalid Request"));
            }

            JToken  result   = new JObject();
            JObject response = RpcMessage.CreateResponse(request["id"]);

            try
            {
                JToken id         = request["id"];
                string method     = request["method"].Value <string>();
                JArray parameters = (JArray)request["params"];

                if (Process(id, method, parameters, out result))
                {
                    response["result"] = result;
                }
                else
                {
                    response["error"] = result;
                }
            }
            catch (Exception e)
            {
                response["error"] = RpcMessage.CreateErrorResult(request["id"], e.HResult, e.Message);
            }

            return(response);
        }
Ejemplo n.º 2
0
        protected async Task ProcessAsync(HttpContext context)
        {
            context.Response.Headers["Access-Control-Allow-Origin"]  = "*";
            context.Response.Headers["Access-Control-Allow-Methods"] = "GET, POST";
            context.Response.Headers["Access-Control-Allow-Headers"] = "Content-Type";
            context.Response.Headers["Access-Control-Max-Age"]       = "31536000";
            if (context.Request.Method != "GET" && context.Request.Method != "POST")
            {
                return;
            }

            JObject request = null;

            if (context.Request.Method == "GET")
            {
                string jsonrpc    = context.Request.Query["jsonrpc"];
                string id         = context.Request.Query["id"];
                string method     = context.Request.Query["method"];
                string type       = context.Request.Query["type"];
                string parameters = context.Request.Query["params"];
                if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(method) && !string.IsNullOrEmpty(parameters))
                {
                    try
                    {
                        parameters = Encoding.UTF8.GetString(Convert.FromBase64String(parameters));
                    }
                    catch (FormatException) { }
                    request = new JObject();
                    if (!string.IsNullOrEmpty(jsonrpc))
                    {
                        request["jsonrpc"] = jsonrpc;
                    }
                    request["id"]     = double.Parse(id);
                    request["method"] = method;
                    request["type"]   = type;
                    request["params"] = JArray.Parse(parameters);
                }
            }
            else if (context.Request.Method == "POST")
            {
                using (StreamReader reader = new StreamReader(context.Request.Body))
                {
                    try
                    {
                        request = JObject.Parse(reader.ReadToEnd());
                    }
                    catch (FormatException) { }
                }
            }
            JObject response = RpcMessage.CreateResponse(request["id"]);

            if (request == null)
            {
                response["error"] = RpcMessage.CreateErrorResult(null, RpcMessage.PARSE_ERROR, "Parse error");
            }
            else
            {
                response = ProcessRequest(context, request);
            }
            if (response == null)
            {
                return;
            }
            context.Response.ContentType = "application/json-rpc";
            await context.Response.WriteAsync(response.ToString(), Encoding.UTF8);
        }