コード例 #1
0
        public async Task ProcessAsync(HttpContext context)
        {
            JObject jsonParam = null;

            try
            {
                context.Response.ContentType = "application/json;charset=UTF-8";

                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")
                {
                    string jsonrpc = context.Request.Query["jsonrpc"];
                    string id      = context.Request.Query["id"];
                    string _method = context.Request.Query["method"];
                    string _params = context.Request.Query["params"];
                    if (jsonrpc == null)
                    {
                        throw new Exception("do not have element:jsonrpc");
                    }
                    if (id == null)
                    {
                        throw new Exception("do not have element:id");
                    }
                    if (_method == null)
                    {
                        throw new Exception("do not have element:method");
                    }
                    if (_params == null)
                    {
                        throw new Exception("do not have element:params");
                    }
                    jsonParam            = new JObject();
                    jsonParam["jsonrpc"] = jsonrpc;
                    jsonParam["id"]      = id;
                    jsonParam["method"]  = _method;
                    jsonParam["params"]  = JArray.Parse(_params);
                }
                else if (context.Request.Method == "POST")
                {
                    var ctype = context.Request.ContentType;
                    if (ctype == "application/x-www-form-urlencoded" || (ctype.IndexOf("multipart/form-data;") == 0))
                    {
                        var form = await FormData.FromRequest(context.Request);

                        var _jsonrpc   = form.mapParams["jsonrpc"];
                        var _id        = form.mapParams["id"];
                        var _method    = form.mapParams["method"];
                        var _strparams = form.mapParams["params"];
                        if (_jsonrpc == null)
                        {
                            throw new Exception("do not have element:jsonrpc");
                        }
                        if (_id == null)
                        {
                            throw new Exception("do not have element:id");
                        }
                        if (_method == null)
                        {
                            throw new Exception("do not have element:method");
                        }
                        if (_strparams == null)
                        {
                            throw new Exception("do not have element:params");
                        }
                        jsonParam            = new JObject();
                        jsonParam["jsonrpc"] = _jsonrpc;
                        jsonParam["id"]      = long.Parse(_id);
                        jsonParam["method"]  = _method;
                        jsonParam["params"]  = JArray.Parse(_strparams);
                    }
                    else
                    {
                        var text = await FormData.GetStringFromRequest(context.Request);

                        jsonParam = JObject.Parse(text);
                        if (jsonParam["jsonrpc"] == null)
                        {
                            throw new Exception("do not have element:jsonrpc");
                        }
                        if (jsonParam["id"] == null)
                        {
                            throw new Exception("do not have element:id");
                        }
                        if (jsonParam["method"] == null)
                        {
                            throw new Exception("do not have element:method");
                        }
                        if (jsonParam["params"] == null)
                        {
                            throw new Exception("do not have element:params");
                        }
                    }
                }
                else
                {
                    throw new Exception("not implement request method.");
                }

                if (mapAction.TryGetValue(jsonParam["method"].Value <string>(), out ActionRPC method))
                {
                    var json = await method(jsonParam);

                    JObject result = new JObject();
                    result["result"]  = json;
                    result["id"]      = jsonParam["id"].Value <int>();
                    result["jsonrpc"] = "2.0";
                    await context.Response.WriteAsync(result.ToString());
                }
                else
                {
                    throw new Exception("Do not have this method.");
                }
            }
            catch (Exception err)
            {
                try
                {
                    if (failAction != null)
                    {
                        var errorobj = await failAction(jsonParam, err.Message);

                        if (jsonParam == null)
                        {
                            jsonParam            = new JObject();
                            jsonParam["jsonrpc"] = "2.0";
                            jsonParam["id"]      = null;
                        }
                        jsonParam["error"] = errorobj.ToJObject();
                        await context.Response.WriteAsync(jsonParam.ToString());
                    }
                    else
                    {
                        var errorobj = new ErrorObject();
                        errorobj.data    = jsonParam;
                        errorobj.message = err.Message;
                        errorobj.code    = -32000;
                        if (jsonParam == null)
                        {
                            jsonParam            = new JObject();
                            jsonParam["jsonrpc"] = "2.0";
                            jsonParam["id"]      = null;
                        }
                        jsonParam["error"] = errorobj.ToJObject();
                        await context.Response.WriteAsync(jsonParam.ToString());
                    }
                }
                catch
                {
                }
            }
        }