/// <summary> /// 从参数字符串生成键或索引集合 /// 如果参数未解码过,则将被解码再处理 /// </summary> /// <param name="parameters">http请求参数</param> /// <param name="decoded">参数是否已解码过</param> /// <returns></returns> public static HttpNameValueCollection Parse(string parameters, bool decoded) { var collection = new HttpNameValueCollection(); if (string.IsNullOrWhiteSpace(parameters) == true) { return(collection); } if (decoded == false) { parameters = HttpUtility.UrlDecode(parameters, Encoding.UTF8); } var keyValues = parameters.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in keyValues) { var kv = item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); if (kv.Length == 2) { collection.Add(kv[0], kv[1]); } } return(collection); }
/// <summary> /// 生成表单和文件 /// </summary> /// <param name="request">请求</param> /// <param name="streamReader">数据读取器</param> /// <param name="boundary">边界</param> private static void GenerateMultipartFormAndFiles(HttpRequest request, ISessionStreamReader streamReader, string boundary) { var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary); var maxPosition = streamReader.Length - Encoding.ASCII.GetBytes("--\r\n").Length; var files = new List <HttpFile>(); var form = new HttpNameValueCollection(); streamReader.Position = streamReader.Position + boundaryBytes.Length; while (streamReader.Position < maxPosition) { var headLength = streamReader.IndexOf(DoubleCRLF) + DoubleCRLF.Length; if (headLength < DoubleCRLF.Length) { break; } var head = streamReader.ReadString(Encoding.UTF8, headLength); var bodyLength = streamReader.IndexOf(boundaryBytes); if (bodyLength < 0) { break; } string fileName = null; var partItem = new MultipartItem(head); if (partItem.TryGetFileName(out fileName) == true) { var bytes = streamReader.ReadArray(bodyLength); var file = new HttpFile(partItem.Name, fileName, bytes); files.Add(file); } else { var byes = streamReader.ReadArray(bodyLength); var value = HttpUtility.UrlDecode(byes, Encoding.UTF8); form.Add(partItem.Name, value); } streamReader.Position = streamReader.Position + boundaryBytes.Length; } request.Form = form; request.Files = files.ToArray(); }
/// <summary> /// 生成表单和文件 /// </summary> /// <param name="request"></param> /// <param name="buffer"></param> /// <param name="boundary">边界</param> private static void GenerateMultipartFormAndFiles(HttpRequest request, IReceiveBuffer buffer, string boundary) { var doubleCrlf = Encoding.ASCII.GetBytes("\r\n\r\n"); var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary); var maxPosition = buffer.Length - Encoding.ASCII.GetBytes("--\r\n").Length; var files = new List <HttpFile>(); var form = new HttpNameValueCollection(); buffer.Position = buffer.Position + boundaryBytes.Length; while (buffer.Position < maxPosition) { var headLength = buffer.IndexOf(doubleCrlf) + doubleCrlf.Length; if (headLength < doubleCrlf.Length) { break; } var head = buffer.ReadString(headLength, Encoding.UTF8); var bodyLength = buffer.IndexOf(boundaryBytes); if (bodyLength < 0) { break; } var mHead = new MultipartHead(head); if (mHead.IsFile == true) { var stream = buffer.ReadArray(bodyLength); var file = new HttpFile(mHead, stream); files.Add(file); } else { var byes = buffer.ReadArray(bodyLength); var value = HttpUtility.UrlDecode(byes, Encoding.UTF8); form.Add(mHead.Name, value); } buffer.Position = buffer.Position + boundaryBytes.Length; } request.Form = form; request.Files = files.ToArray(); }
/// <summary> /// 生成表单和文件 /// </summary> /// <param name="request"></param> /// <param name="stream"></param> /// <param name="boundary">边界</param> private static void GenerateMultipartFormAndFiles(HttpRequest request, IStreamReader stream, string boundary) { var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary); var maxPosition = stream.Length - Encoding.ASCII.GetBytes("--\r\n").Length; var files = new List <HttpFile>(); var form = new HttpNameValueCollection(); stream.Position = stream.Position + boundaryBytes.Length; while (stream.Position < maxPosition) { var headLength = stream.IndexOf(HttpRequestParser.DoubleCrlf) + HttpRequestParser.DoubleCrlf.Length; if (headLength < HttpRequestParser.DoubleCrlf.Length) { break; } var head = stream.ReadString(Encoding.UTF8, headLength); var bodyLength = stream.IndexOf(boundaryBytes); if (bodyLength < 0) { break; } var mHead = new MultipartHead(head); if (mHead.IsFile == true) { var bytes = stream.ReadArray(bodyLength); var file = new HttpFile(mHead, bytes); files.Add(file); } else { var byes = stream.ReadArray(bodyLength); var value = HttpUtility.UrlDecode(byes, Encoding.UTF8); form.Add(mHead.Name, value); } stream.Position = stream.Position + boundaryBytes.Length; } request.Form = form; request.Files = files.ToArray(); }
/// <summary> /// 从参数字符串生成键或索引集合 /// </summary> /// <param name="parameters">http请求原始参数</param> /// <returns></returns> public static HttpNameValueCollection Parse(string parameters) { var collection = new HttpNameValueCollection(); if (string.IsNullOrWhiteSpace(parameters) == true) { return(collection); } var keyValues = parameters.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in keyValues) { var kv = item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); var key = HttpUtility.UrlDecode(kv[0], Encoding.UTF8); var value = kv.Length == 2 ? HttpUtility.UrlDecode(kv[1], Encoding.UTF8) : null; collection.Add(key, value); } return(collection); }
/// <summary> /// 从参数字符串生成键或索引集合 /// 如果参数未解码过,则将被解码再处理 /// </summary> /// <param name="parameters">http请求参数</param> /// <param name="decoded">参数是否已解码过</param> /// <returns></returns> public static HttpNameValueCollection Parse(string parameters, bool decoded) { var collection = new HttpNameValueCollection(); if (string.IsNullOrWhiteSpace(parameters) == true) { return collection; } if (decoded == false) { parameters = HttpUtility.UrlDecode(parameters, Encoding.UTF8); } var keyValues = parameters.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in keyValues) { var kv = item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); if (kv.Length == 2) { collection.Add(kv[0], kv[1]); } } return collection; }
/// <summary> /// 生成表单和文件 /// </summary> /// <param name="request"></param> /// <param name="buffer"></param> /// <param name="boundary">边界</param> private static void GenerateMultipartFormAndFiles(HttpRequest request, IReceiveBuffer buffer, string boundary) { var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary); var maxPosition = buffer.Length - Encoding.ASCII.GetBytes("--\r\n").Length; var files = new List<HttpFile>(); var form = new HttpNameValueCollection(); buffer.Position = buffer.Position + boundaryBytes.Length; while (buffer.Position < maxPosition) { var headLength = buffer.IndexOf(Protocol.DoubleCrlf) + Protocol.DoubleCrlf.Length; if (headLength < Protocol.DoubleCrlf.Length) { break; } var head = buffer.ReadString(headLength, Encoding.UTF8); var bodyLength = buffer.IndexOf(boundaryBytes); if (bodyLength < 0) { break; } var mHead = new MultipartHead(head); if (mHead.IsFile == true) { var stream = buffer.ReadArray(bodyLength); var file = new HttpFile(mHead, stream); files.Add(file); } else { var byes = buffer.ReadArray(bodyLength); var value = HttpUtility.UrlDecode(byes, Encoding.UTF8); form.Add(mHead.Name, value); } buffer.Position = buffer.Position + boundaryBytes.Length; } request.Form = form; request.Files = files.ToArray(); }