public static async Task <FormData> FromRequest(IOwinRequest 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"]; 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"]; 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); } }
public async Task HandleRequest(IOwinContext context, string rootpath, string relativePath) { var api = relativePath.ToLower(); var formdata = await FormData.FromRequest(context.Request); if (formdata == null) { context.Response.StatusCode = 500; context.Response.ContentType = "text/plain"; MyJson.JsonNode_Object json = new MyJson.JsonNode_Object(); json["msg"] = new MyJson.JsonNode_ValueString("formdata format error."); json["tag"] = new MyJson.JsonNode_ValueNumber(-1); await context.Response.WriteAsync(json.ToString()); return; } if (relativePath == "ver") { await ver(context, formdata); return; } else if (relativePath == "filemap_list") { await filemap_list(context, formdata); return; } else if (relativePath == "filemap_add") { await filemap_add(context, formdata); return; } else if (relativePath == "filemap_remove") { await filemap_remove(context, formdata); return; } else if (relativePath == "file_list") { await file_list(context, formdata); return; } else if (relativePath == "file_save") { await file_save(context, formdata); return; } else if (relativePath == "file_remove") { await file_remove(context, formdata); return; } else if (relativePath == "file_cmd") { await file_cmd(context, formdata); return; } else { MyJson.JsonNode_Object jsonr = new MyJson.JsonNode_Object(); jsonr["tag"] = new MyJson.JsonNode_ValueNumber(-1000); jsonr["msg"] = new MyJson.JsonNode_ValueString("unknown cmd."); await context.Response.WriteAsync(jsonr.ToString()); } return; }