/// <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> /// 是否为http协议 /// </summary> /// <param name="buffer">收到的数据</param> /// <param name="headerLength">头数据长度,包括双换行</param> /// <returns></returns> public static bool IsHttp(IReceiveBuffer buffer, out int headerLength) { var methodLength = Protocol.GetMthodLength(buffer); var methodName = buffer.ReadString(methodLength, Encoding.ASCII); if (Protocol.MethodNames.Any(m => m.StartsWith(methodName, StringComparison.OrdinalIgnoreCase)) == false) { headerLength = 0; return false; } buffer.Position = 0; var headerIndex = buffer.IndexOf(Protocol.DoubleCrlf); if (headerIndex < 0) { headerLength = 0; return true; } headerLength = headerIndex + Protocol.DoubleCrlf.Length; return true; }
/// <summary> /// 是否为http协议 /// </summary> /// <param name="buffer">收到的数据</param> /// <param name="headerLength">头数据长度,包括双换行</param> /// <returns></returns> public static bool IsHttp(IReceiveBuffer buffer, out int headerLength) { var methodLength = Protocol.GetMthodLength(buffer); var methodName = buffer.ReadString(methodLength, Encoding.ASCII); if (Protocol.MethodNames.Any(m => m.StartsWith(methodName, StringComparison.OrdinalIgnoreCase)) == false) { headerLength = 0; return(false); } buffer.Position = 0; var headerIndex = buffer.IndexOf(Protocol.DoubleCrlf); if (headerIndex < 0) { headerLength = 0; return(true); } headerLength = headerIndex + Protocol.DoubleCrlf.Length; return(true); }
/// <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(); }