예제 #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        = long.Parse(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"]      = _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
                {
                }
            }
        }
예제 #2
0
        public static async Task <FormData> FromRequest(Microsoft.AspNetCore.Http.HttpRequest request)
        {
            try
            {
                FormData data = new FormData();
                foreach (var kv in request.Query)
                {
                    data.mapParams[kv.Key] = kv.Value[0];
                }
                if (request.Method.ToUpper() == "POST")
                {
                    if (request.ContentType == null)
                    {
                        return(data);
                    }
                    else if (request.ContentType == "application/x-www-form-urlencoded")
                    {
                        byte[] allfile = null;
                        int    seek    = 0;
                        var    _clen   = request.Headers["Content-Length"];
                        string clen    = null;
                        if (_clen.Count > 0)
                        {
                            clen = _clen[0];
                        }
                        if (clen != null)
                        {
                            int leng = int.Parse(clen);
                            allfile = new byte[leng];

                            while (request.Body.CanRead)
                            {
                                int read = await request.Body.ReadAsync(allfile, seek, leng - seek);

                                seek += read;
                                if (read == 0)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            allfile = new byte[4 * 1024 * 1024];

                            while (request.Body.CanRead)
                            {
                                int read = await request.Body.ReadAsync(allfile, seek, 1024);

                                seek += read;
                                if (read == 0)
                                {
                                    break;
                                }
                            }
                        }


                        string text  = System.Text.Encoding.UTF8.GetString(allfile, 0, seek);
                        var    infos = text.Split(new char[] { '=', '&' });
                        for (var i = 0; i < infos.Length / 2; i++)
                        {
                            data.mapParams[infos[i * 2]] = Uri.UnescapeDataString(infos[i * 2 + 1]);
                        }
                    }
                    else if (request.ContentType.IndexOf("multipart/form-data;") == 0)
                    {
                        byte[] allfile = null;
                        int    seek    = 0;
                        var    _clen   = request.Headers["Content-Length"];
                        string clen    = null;
                        if (_clen.Count > 0)
                        {
                            clen = _clen[0];
                        }
                        if (clen != null)
                        {
                            int leng = int.Parse(clen);
                            allfile = new byte[leng];

                            while (request.Body.CanRead)
                            {
                                int read = await request.Body.ReadAsync(allfile, seek, leng - seek);

                                seek += read;
                                if (read == 0)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            allfile = new byte[4 * 1024 * 1024];

                            while (request.Body.CanRead)
                            {
                                int read = await request.Body.ReadAsync(allfile, seek, 1024);

                                seek += read;
                                if (read == 0)
                                {
                                    break;
                                }
                            }
                        }


                        var iSplitTag = request.ContentType.IndexOf("=") + 1;
                        var sSplitTag = "--" + request.ContentType.Substring(iSplitTag);
                        var bSplitTag = System.Text.Encoding.ASCII.GetBytes(sSplitTag);

                        int iTag = ByteIndexOf(allfile, seek, bSplitTag, 0);
                        if (iTag < 0)
                        {
                            string s = System.Text.Encoding.ASCII.GetString(allfile, 0, seek);
                        }
                        else
                        {
                            while (iTag >= 0)
                            {
                                int iTagNext = ByteIndexOf(allfile, seek, bSplitTag, iTag + 1);
                                if (iTagNext < 0)
                                {
                                    break;
                                }
                                var           bs         = System.Text.Encoding.ASCII.GetBytes("\r\n\r\n");
                                int           iStart     = iTag + bSplitTag.Length + 2;
                                int           iDataStart = ByteIndexOf(allfile, seek, bs, iStart) + 4;
                                string        s          = System.Text.Encoding.ASCII.GetString(allfile, iStart, iDataStart - iStart);
                                List <string> infos      = new List <string>(s.Split(new string[] { "; ", ": ", "\r\n", "=" }, StringSplitOptions.None));
                                var           i          = infos.IndexOf("name");
                                var           name       = infos[i + 1].Substring(1);
                                name = name.Substring(0, name.Length - 1);

                                byte[] ddata = new byte[iTagNext - iDataStart - 2];
                                Array.Copy(allfile, iDataStart, ddata, 0, ddata.Length);
                                if (infos.Contains("application/octet-stream"))
                                {
                                    data.mapFiles[name] = ddata;
                                }
                                else
                                {
                                    string txtData = System.Text.Encoding.UTF8.GetString(ddata);

                                    data.mapParams[name] = Uri.UnescapeDataString(txtData);
                                }
                                iTag = iTagNext;
                            }
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
                return(data);
            }
            catch
            {
                return(null);
            }
        }