public void ReadAndPositionTest() { ReceiveBuffer target = new ReceiveBuffer(); // TODO: 初始化为适当的值 var bytes = new byte[]{ 1, 3, 0,1, 0,0,0,1, 0,0,0,0,0,0,0,5, 255, 255,255 }; target.Write(bytes, 0, bytes.Length); target.Position = 0; Assert.IsTrue(target.ReadBoolean() && target.Position == 1); Assert.IsTrue(target.ReadByte() == 3 && target.Position == 1 + 1); Assert.IsTrue(target.ReadInt16() == 1 && target.Position == 1 + 1 + 2); Assert.IsTrue(target.ReadInt32() == 1 && target.Position == 1 + 1 + 2 + 4); Assert.IsTrue(target.ReadInt64() == 5 && target.Position == 1 + 1 + 2 + 4 + 8); Assert.IsTrue(target.ReadArray(1)[0] == 255 && target.Position == 1 + 1 + 2 + 4 + 8 + 1); Assert.IsTrue(target.ReadArray().Length == 2 && target.Position == 1 + 1 + 2 + 4 + 8 + 1 + 2); }
/// <summary> /// 解析连接请求信息 /// </summary> /// <param name="buffer">接收到的原始数量</param> /// <returns></returns> public static HttpRequest From(ReceiveBuffer buffer) { buffer.Position = 0; var bytes = buffer.ReadArray(); var request = HttpRequest.From(bytes, "http"); if (request == null) { return null; } if (string.Equals(request.Method, "POST", StringComparison.OrdinalIgnoreCase)) { var contentLength = int.Parse(request["Content-Length"]); if (request.Body.Length < contentLength) { return null; } } buffer.Clear(); return request; }