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()); } } } }
/// <summary> /// Invoked on channel connect. /// </summary> /// <param name="channel"></param> public void OnConnected(ISockNetChannel channel) { SockNetLogger.Log(SockNetLogger.LogLevel.INFO, this, "Sending WebSocket upgrade request."); channel.Pipe.AddIncomingLast<HttpResponse>(HandleHandshake); HttpRequest request = new HttpRequest(channel.BufferPool) { Action = "GET", Path = path, Version = "HTTP/1.1" }; request.Header["Host"] = hostname; request.Header["Upgrade"] = "websocket"; request.Header["Connection"] = "Upgrade"; request.Header[WebSocketUtil.WebSocketKeyHeader] = secKey; request.Header[WebSocketUtil.WebSocketVersionHeader] = "13"; channel.Send(request); }
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 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> /// Handles the WebSocket handshake. /// </summary> /// <param name="channel"></param> /// <param name="request"></param> private void HandleHandshake(ISockNetChannel channel, ref HttpRequest request) { string connection = request.Header["Connection"]; string upgrade = request.Header["Upgrade"]; string securityKey = request.Header[WebSocketUtil.WebSocketKeyHeader]; if (connection != null && upgrade != null && securityKey != null && "websocket".Equals(upgrade.Trim().ToLower()) && "upgrade".Equals(connection.Trim().ToLower())) { string[] requestProtocols = request.Headers[WebSocketUtil.WebSocketProtocolHeader]; List<string> handledProtocols = new List<string>(); if (requestProtocols != null && protocolDelegate != null) { for (int i = 0; i < requestProtocols.Length; i++) { if (protocolDelegate(channel, requestProtocols[i])) { handledProtocols.Add(requestProtocols[i]); } } } HttpResponse response = new HttpResponse(channel.BufferPool) { Version = "HTTP/1.1", Code = "101", Reason = "Switching Protocols" }; response.Header["Upgrade"] = "websocket"; response.Header["Connection"] = "Upgrade"; response.Header[WebSocketUtil.WebSocketAcceptHeader] = WebSocketUtil.GenerateAccept(securityKey); response.Header[WebSocketUtil.WebSocketProtocolHeader] = string.Join(",", handledProtocols.ToArray()); channel.Send(response); channel.RemoveModule(httpModule); channel.Pipe.AddIncomingFirst<object>(HandleIncomingFrames); channel.Pipe.AddOutgoingLast<object>(HandleOutgoingFrames); } else { SockNetLogger.Log(SockNetLogger.LogLevel.ERROR, this, "Expecting upgrade request."); channel.Close(); } }