public void TestContentLengthNotClosed() { string sampleContent = "<test><val>hello</val></test>"; int sampleContentLength = Encoding.UTF8.GetByteCount(sampleContent); string sampleRequest = "POST / HTTP/1.0\r\nHost: localhost\r\nContent-Length: " + sampleContentLength + "\r\n\r\n" + sampleContent; ChunkedBuffer buffer = new ChunkedBuffer(pool); buffer.Write(Encoding.ASCII.GetBytes(sampleRequest), 0, Encoding.ASCII.GetByteCount(sampleRequest)); HttpRequest request = new HttpRequest(pool); Assert.IsTrue(request.Parse(buffer.Stream, false)); Assert.AreEqual("POST", request.Action); Assert.AreEqual("/", request.Path); Assert.AreEqual("HTTP/1.0", request.Version); Assert.AreEqual("POST / HTTP/1.0", request.CommandLine); Assert.AreEqual("localhost", request.Header["Host"]); Assert.AreEqual(sampleContentLength, request.BodySize); Assert.AreEqual(sampleContentLength, request.Body.WritePosition); MemoryStream stream = new MemoryStream(); request.Write(stream, false); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Assert.AreEqual(sampleRequest, reader.ReadToEnd()); } }
public void TestStreamWriteAndReadAndClose() { ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[10]); }); using (ChunkedBuffer stream = new ChunkedBuffer(pool)) { Assert.AreEqual(0, stream.ReadPosition); Assert.AreEqual(0, stream.WritePosition); stream.Write(TestData, 0, TestData.Length); Assert.AreEqual(TestData.Length, stream.WritePosition); Assert.AreEqual(0, stream.ReadPosition); Assert.AreEqual(0, pool.ObjectsInPool); Assert.AreEqual(Math.Round((float)TestData.Length / 10f, MidpointRounding.AwayFromZero), pool.TotalNumberOfObjects); using (StreamReader reader = new StreamReader(stream.Stream)) { Assert.AreEqual(Encoding.UTF8.GetString(TestData, 0, TestData.Length), reader.ReadToEnd()); Assert.AreEqual(TestData.Length, stream.ReadPosition); Assert.AreEqual(TestData.Length, stream.WritePosition); } Assert.AreEqual(0, stream.ReadPosition); Assert.AreEqual(0, stream.WritePosition); } }
public void TestSimpleClosed() { string sampleRequest = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n"; using (ChunkedBuffer buffer = new ChunkedBuffer(pool)) { buffer.Write(Encoding.ASCII.GetBytes(sampleRequest), 0, Encoding.ASCII.GetByteCount(sampleRequest)); using (HttpRequest request = new HttpRequest(pool)) { Assert.IsTrue(request.Parse(buffer.Stream, true)); Assert.AreEqual("GET", request.Action); Assert.AreEqual("/", request.Path); Assert.AreEqual("HTTP/1.0", request.Version); Assert.AreEqual("GET / HTTP/1.0", request.CommandLine); Assert.AreEqual("localhost", request.Header["Host"]); Assert.AreEqual(0, request.BodySize); Assert.AreEqual(0, request.Body.WritePosition); MemoryStream stream = new MemoryStream(); request.Write(stream, false); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Assert.AreEqual(sampleRequest, reader.ReadToEnd()); } } } }
public void TestReadAndWritePoolUsage() { Random rand = new Random(this.GetHashCode() ^ DateTime.Now.Millisecond); byte[] randomUtf8StringBytes = new byte[rand.Next(2000, 5000)]; for (int i = 0; i < randomUtf8StringBytes.Length; i++) { randomUtf8StringBytes[i] = (byte)rand.Next(0x0020, 0x007F); } ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[10]); }); using (ChunkedBuffer buffer = new ChunkedBuffer(pool)) { buffer.Write(randomUtf8StringBytes, 0, randomUtf8StringBytes.Length); Assert.AreEqual((randomUtf8StringBytes.Length / 10) + (randomUtf8StringBytes.Length % 10 > 0 ? 1 : 0), pool.TotalNumberOfObjects); Assert.AreEqual(randomUtf8StringBytes.Length, buffer.AvailableBytesToRead); byte[] readBytes = new byte[randomUtf8StringBytes.Length]; buffer.Read(readBytes, 0, readBytes.Length); for (int i = 0; i < readBytes.Length; i++) { Assert.AreEqual(readBytes[i], randomUtf8StringBytes[i]); } } Assert.AreEqual(pool.ObjectsInPool, pool.TotalNumberOfObjects); }
public void TestSimpleClosed() { string sampleRequest = "HTTP/1.0 200 OK\r\nHost: localhost\r\n\r\n"; ChunkedBuffer buffer = new ChunkedBuffer(pool); buffer.Write(Encoding.ASCII.GetBytes(sampleRequest), 0, Encoding.ASCII.GetByteCount(sampleRequest)); HttpResponse response = new HttpResponse(pool); Assert.IsTrue(response.Parse(buffer.Stream, true)); Assert.AreEqual("HTTP/1.0", response.Version); Assert.AreEqual("200", response.Code); Assert.AreEqual("OK", response.Reason); Assert.AreEqual("HTTP/1.0 200 OK", response.CommandLine); Assert.AreEqual("localhost", response.Header["Host"]); Assert.AreEqual(0, response.BodySize); Assert.AreEqual(0, response.Body.WritePosition); MemoryStream stream = new MemoryStream(); response.Write(stream, false); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Assert.AreEqual(sampleRequest, reader.ReadToEnd()); } }
public void TestDrainToStreamSync() { using (ChunkedBuffer buffer = new ChunkedBuffer(new ObjectPool<byte[]>(() => { return new byte[10]; }))) { buffer.Write(TestData, 0, TestData.Length); MemoryStream stream = new MemoryStream(); buffer.DrainToStreamSync(stream); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Assert.AreEqual(TestDataString, reader.ReadToEnd()); } } }
public void TestWrite() { ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[10]); }); using (ChunkedBuffer stream = new ChunkedBuffer(pool)) { Assert.AreEqual(0, stream.ReadPosition); Assert.AreEqual(0, stream.WritePosition); stream.Write(TestData, 0, TestData.Length); Assert.AreEqual(TestData.Length, stream.WritePosition); Assert.AreEqual(0, stream.ReadPosition); Assert.AreEqual(0, pool.ObjectsInPool); Assert.AreEqual(Math.Round((float)TestData.Length / 10f, MidpointRounding.AwayFromZero), pool.TotalNumberOfObjects); } }
public void TestDrainToStream() { using (ChunkedBuffer buffer = new ChunkedBuffer(new ObjectPool <byte[]>(() => { return(new byte[10]); }))) { buffer.Write(TestData, 0, TestData.Length); MemoryStream stream = new MemoryStream(); buffer.DrainToStream(stream).WaitForValue(TimeSpan.FromSeconds(5)); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Assert.AreEqual(TestDataString, reader.ReadToEnd()); } } }
public void Start(bool isTls = false) { string sampleContent = "<test><val>hello</val></test>"; int sampleContentLength = Encoding.UTF8.GetByteCount(sampleContent); string chunk1Content = "<test><val>"; string chunk2Content = "hello</val>"; string chunk3Content = "</test>"; int chunk1ContentLength = Encoding.UTF8.GetByteCount(chunk1Content); int chunk2ContentLength = Encoding.UTF8.GetByteCount(chunk2Content); int chunk3ContentLength = Encoding.UTF8.GetByteCount(chunk3Content); string chunk1HttpContent = "HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n" + string.Format("{0:X}", chunk1ContentLength) + "\r\n" + chunk1Content + "\r\n"; string chunk2HttpContent = string.Format("{0:X}", chunk2ContentLength) + "\r\n" + chunk2Content + "\r\n"; string chunk3HttpContent = string.Format("{0:X}", chunk3ContentLength) + "\r\n" + chunk3Content + "\r\n"; string chunk4HttpContent = "0\r\n\r\n"; server = SockNetServer.Create(GetLocalIpAddress(), 0, ServerSockNetChannel.DefaultBacklog, pool); try { server.AddModule(new HttpSockNetChannelModule(HttpSockNetChannelModule.ParsingMode.Server)); server.Pipe.AddIncomingLast<HttpRequest>((ISockNetChannel channel, ref HttpRequest data) => { ChunkedBuffer buffer1 = new ChunkedBuffer(channel.BufferPool); buffer1.Write(Encoding.ASCII.GetBytes(chunk1HttpContent), 0, Encoding.ASCII.GetByteCount(chunk1HttpContent)); channel.Send(buffer1); ChunkedBuffer buffer2 = new ChunkedBuffer(channel.BufferPool); buffer2.Write(Encoding.ASCII.GetBytes(chunk2HttpContent), 0, Encoding.ASCII.GetByteCount(chunk2HttpContent)); channel.Send(buffer2); ChunkedBuffer buffer3 = new ChunkedBuffer(channel.BufferPool); buffer3.Write(Encoding.ASCII.GetBytes(chunk3HttpContent), 0, Encoding.ASCII.GetByteCount(chunk3HttpContent)); channel.Send(buffer3); ChunkedBuffer buffer4 = new ChunkedBuffer(channel.BufferPool); buffer4.Write(Encoding.ASCII.GetBytes(chunk4HttpContent), 0, Encoding.ASCII.GetByteCount(chunk4HttpContent)); channel.Send(buffer4); }); if (isTls) { byte[] rawCert = CertificateUtil.CreateSelfSignCertificatePfx("CN=\"test\"; C=\"USA\"", DateTime.Today.AddDays(-10), DateTime.Today.AddDays(+10)); server.BindWithTLS(new X509Certificate2(rawCert), (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return true; }).WaitForValue(TimeSpan.FromSeconds(5)); } else { server.Bind().WaitForValue(TimeSpan.FromSeconds(5)); } Assert.IsTrue(server.IsActive); } catch (Exception) { Stop(); } }
public void TestReadAndWritePoolUsage() { Random rand = new Random(this.GetHashCode() ^ DateTime.Now.Millisecond); byte[] randomUtf8StringBytes = new byte[rand.Next(2000, 5000)]; for (int i = 0; i < randomUtf8StringBytes.Length; i++) { randomUtf8StringBytes[i] = (byte)rand.Next(0x0020, 0x007F); } ObjectPool<byte[]> pool = new ObjectPool<byte[]>(() => { return new byte[10]; }); using (ChunkedBuffer buffer = new ChunkedBuffer(pool)) { buffer.Write(randomUtf8StringBytes, 0, randomUtf8StringBytes.Length); Assert.AreEqual((randomUtf8StringBytes.Length / 10) + (randomUtf8StringBytes.Length % 10 > 0 ? 1 : 0), pool.TotalNumberOfObjects); Assert.AreEqual(randomUtf8StringBytes.Length, buffer.AvailableBytesToRead); byte[] readBytes = new byte[randomUtf8StringBytes.Length]; buffer.Read(readBytes, 0, readBytes.Length); for (int i = 0; i < readBytes.Length; i++) { Assert.AreEqual(readBytes[i], randomUtf8StringBytes[i]); } } Assert.AreEqual(pool.ObjectsInPool, pool.TotalNumberOfObjects); }
public void TestWrite() { ObjectPool<byte[]> pool = new ObjectPool<byte[]>(() => { return new byte[10]; }); using (ChunkedBuffer stream = new ChunkedBuffer(pool)) { Assert.AreEqual(0, stream.ReadPosition); Assert.AreEqual(0, stream.WritePosition); stream.Write(TestData, 0, TestData.Length); Assert.AreEqual(TestData.Length, stream.WritePosition); Assert.AreEqual(0, stream.ReadPosition); Assert.AreEqual(0, pool.ObjectsInPool); Assert.AreEqual(Math.Round((float)TestData.Length / 10f, MidpointRounding.AwayFromZero), pool.TotalNumberOfObjects); } }
public void TestStreamWriteAndReadAndFlush() { ObjectPool<byte[]> pool = new ObjectPool<byte[]>(() => { return new byte[10]; }); using (ChunkedBuffer stream = new ChunkedBuffer(pool)) { Assert.AreEqual(0, stream.ReadPosition); Assert.AreEqual(0, stream.WritePosition); stream.Write(TestData, 0, TestData.Length); Assert.AreEqual(TestData.Length, stream.WritePosition); Assert.AreEqual(0, stream.ReadPosition); Assert.AreEqual(0, pool.ObjectsInPool); Assert.AreEqual(Math.Round((float)TestData.Length / 10f, MidpointRounding.AwayFromZero), pool.TotalNumberOfObjects); StreamReader reader = new StreamReader(stream.Stream); Assert.AreEqual(Encoding.UTF8.GetString(TestData, 0, TestData.Length), reader.ReadToEnd()); Assert.AreEqual(TestData.Length, stream.ReadPosition); Assert.AreEqual(TestData.Length, stream.WritePosition); stream.Flush(); Assert.AreEqual(0, stream.ReadPosition); Assert.AreEqual(0, stream.WritePosition); } }
public void TestChunked() { string sampleContent = "<test><val>hello</val></test>"; int sampleContentLength = Encoding.UTF8.GetByteCount(sampleContent); string chunk1Content = "<test><val>"; string chunk2Content = "hello</val>"; string chunk3Content = "</test>"; int chunk1ContentLength = Encoding.UTF8.GetByteCount(chunk1Content); int chunk2ContentLength = Encoding.UTF8.GetByteCount(chunk2Content); int chunk3ContentLength = Encoding.UTF8.GetByteCount(chunk3Content); string chunk1Request = "POST / HTTP/1.0\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\n" + string.Format("{0:X}", chunk1ContentLength) + "\r\n" + chunk1Content + "\r\n"; string chunk2Request = string.Format("{0:X}", chunk2ContentLength) + "\r\n" + chunk2Content + "\r\n"; string chunk3Request = string.Format("{0:X}", chunk3ContentLength) + "\r\n" + chunk3Content + "\r\n"; string chunk4Request = "0\r\n\r\n"; ChunkedBuffer buffer1 = new ChunkedBuffer(pool); buffer1.Write(Encoding.ASCII.GetBytes(chunk1Request), 0, Encoding.ASCII.GetByteCount(chunk1Request)); ChunkedBuffer buffer2 = new ChunkedBuffer(pool); buffer2.Write(Encoding.ASCII.GetBytes(chunk2Request), 0, Encoding.ASCII.GetByteCount(chunk2Request)); ChunkedBuffer buffer3 = new ChunkedBuffer(pool); buffer3.Write(Encoding.ASCII.GetBytes(chunk3Request), 0, Encoding.ASCII.GetByteCount(chunk3Request)); ChunkedBuffer buffer4 = new ChunkedBuffer(pool); buffer4.Write(Encoding.ASCII.GetBytes(chunk4Request), 0, Encoding.ASCII.GetByteCount(chunk4Request)); HttpRequest request = new HttpRequest(pool); Assert.IsFalse(request.IsChunked); Assert.IsFalse(request.Parse(buffer1.Stream, false)); Assert.IsTrue(request.IsChunked); Assert.IsFalse(request.Parse(buffer2.Stream, false)); Assert.IsTrue(request.IsChunked); Assert.IsFalse(request.Parse(buffer3.Stream, false)); Assert.IsTrue(request.IsChunked); Assert.IsTrue(request.Parse(buffer4.Stream, false)); Assert.IsTrue(request.IsChunked); Assert.AreEqual("POST", request.Action); Assert.AreEqual("/", request.Path); Assert.AreEqual("HTTP/1.0", request.Version); Assert.AreEqual("POST / HTTP/1.0", request.CommandLine); Assert.AreEqual("localhost", request.Header["Host"]); Assert.AreEqual(sampleContentLength, request.BodySize); Assert.AreEqual(sampleContentLength, request.Body.WritePosition); MemoryStream stream = new MemoryStream(); request.Write(stream, false); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Assert.AreEqual("POST / HTTP/1.0\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\n" + sampleContent, reader.ReadToEnd()); } buffer1.Dispose(); buffer2.Dispose(); buffer3.Dispose(); buffer4.Dispose(); request.Dispose(); }
/// <summary> /// Writes the given data into this stream. /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="length"></param> public override void Write(byte[] buffer, int offset, int count) { chunkedBuffer.Write(buffer, offset, count); }
public void TestContentLengthPartial() { string sampleContent = "<test><val>hello</val></test>"; int sampleContentLength = Encoding.UTF8.GetByteCount(sampleContent); string sampleRequest = "HTTP/1.0 200 OK\r\nHost: localhost\r\nContent-Length: " + sampleContentLength + "\r\n\r\n" + sampleContent; int partialSize = sampleRequest.Length / 3; string sampleRequest1 = sampleRequest.Substring(0, partialSize); string sampleRequest2 = sampleRequest.Substring(partialSize, partialSize); string sampleRequest3 = sampleRequest.Substring(partialSize * 2, sampleRequest.Length - (partialSize * 2)); ChunkedBuffer buffer = new ChunkedBuffer(pool); buffer.Write(Encoding.ASCII.GetBytes(sampleRequest1), 0, Encoding.ASCII.GetByteCount(sampleRequest1)); HttpResponse response = new HttpResponse(pool); Assert.IsFalse(response.Parse(buffer.Stream, false)); buffer.Write(Encoding.ASCII.GetBytes(sampleRequest2), 0, Encoding.ASCII.GetByteCount(sampleRequest2)); Assert.IsFalse(response.Parse(buffer.Stream, false)); buffer.Write(Encoding.ASCII.GetBytes(sampleRequest3), 0, Encoding.ASCII.GetByteCount(sampleRequest3)); Assert.IsTrue(response.Parse(buffer.Stream, false)); Assert.AreEqual("HTTP/1.0", response.Version); Assert.AreEqual("200", response.Code); Assert.AreEqual("OK", response.Reason); Assert.AreEqual("HTTP/1.0 200 OK", response.CommandLine); Assert.AreEqual("localhost", response.Header["Host"]); Assert.AreEqual(sampleContentLength, response.BodySize); Assert.AreEqual(sampleContentLength, response.Body.WritePosition); MemoryStream stream = new MemoryStream(); response.Write(stream, false); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Assert.AreEqual(sampleRequest, reader.ReadToEnd()); } }