예제 #1
0
        public static async Task <FormData> FromRequest(HttpRequest request)
        {
            try

            {
                FormData data = new FormData();
                data.remoteIP = request.HttpContext.Connection.RemoteIpAddress.ToString();
                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"];
                        if (StringValues.IsNullOrEmpty(clen))
                        {
                            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"];
                        if (StringValues.IsNullOrEmpty(clen))
                        {
                            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);
            }
        }