static HttpServerBuffer CreateResponseForGetTime() { var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8); WriteCommonHeaders(formatter, @"HTTP/1.1 200 OK"); formatter.Append(HttpNewline); formatter.Append(@"<html><head><title>Time</title></head><body>"); formatter.Append(DateTime.UtcNow, 'O'); formatter.Append(@"</body></html>"); return(new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared)); }
static void RunLoop(bool log) { var loop = new UVLoop(); var listener = new TcpListener("127.0.0.1", 8080, loop); var formatter = new BufferFormatter(512, FormattingData.InvariantUtf8); listener.ConnectionAccepted += (Tcp connection) => { if (log) { Console.WriteLine("connection accepted"); } connection.ReadCompleted += (ByteSpan data) => { if (log) { unsafe { var requestString = new Utf8String(data.UnsafeBuffer, data.Length); Console.WriteLine("*REQUEST:\n {0}", requestString.ToString()); } } formatter.Clear(); formatter.Append("HTTP/1.1 200 OK"); formatter.Append("\r\n\r\n"); formatter.Append("Hello World!"); if (log) { formatter.Format(" @ {0:O}", DateTime.UtcNow); } var response = formatter.Buffer.Slice(0, formatter.CommitedByteCount); // formatter should have a property for written bytes GCHandle gcHandle; var byteSpan = response.Pin(out gcHandle); connection.TryWrite(byteSpan); connection.Dispose(); gcHandle.Free(); // TODO: formatter should format to ByteSpan, to avoid pinning }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
public void The_http_extension_methods_can_be_composed_to_generate_the_http_message() { _formatter.AppendHttpStatusLine(HttpVersion.V1_1, 200, new Utf8String("OK")); _formatter.Append(new Utf8String("Connection : open")); _formatter.AppendHttpNewLine(); _formatter.AppendHttpNewLine(); _formatter.Append(HttpBody); _formatter.AppendHttpNewLine(); _formatter.Append(HttpBody); var result = _formatter.Buffer; result.Should().ContainInOrder(_httpMessageInBytes); ArrayPool <byte> .Shared.Return(result); }
static void RunLoop(bool log) { var loop = new UVLoop(); var listener = new TcpListener(s_ipAddress, s_port, loop); var formatter = new BufferFormatter(512, FormattingData.InvariantUtf8); listener.ConnectionAccepted += (Tcp connection) => { if (log) { Console.WriteLine("connection accepted"); } connection.ReadCompleted += (Span <byte> data) => { if (log) { unsafe { var requestString = new Utf8String(data); Console.WriteLine("*REQUEST:\n {0}", requestString.ToString()); } } formatter.Clear(); formatter.Append("HTTP/1.1 200 OK"); formatter.Append("\r\n\r\n"); formatter.Append("Hello World!"); if (log) { formatter.Format(" @ {0:O}", DateTime.UtcNow); } var response = formatter.Buffer.Slice(0, formatter.CommitedByteCount); // formatter should have a property for written bytes connection.TryWrite(response); connection.Dispose(); }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
protected virtual HttpServerBuffer CreateResponseFor400(ByteSpan receivedBytes) // Bad Request { var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8); WriteCommonHeaders(formatter, "1.1", "400", "Bad Request", false); formatter.Append(HttpNewline); return(new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared)); }
protected virtual HttpServerBuffer CreateResponseFor400(Span <byte> receivedBytes) // Bad Request { BufferFormatter formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8); WriteCommonHeaders(formatter, @"HTTP/1.1 400 Bad Request"); formatter.Append(HttpNewline); return(new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared)); }
protected virtual HttpServerBuffer CreateResponseFor404(HttpRequestLine requestLine) // Not Found { Log.LogMessage(Log.Level.Warning, "Request {0}, Response: 404 Not Found", requestLine); var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8); WriteCommonHeaders(formatter, "1.1", "404", "Not Found", false); formatter.Append(HttpNewline); return(new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared)); }
private void EncodeStringToUtf8() { string text = "Hello World!"; int stringsToWrite = 2000; int size = stringsToWrite * text.Length + stringsToWrite; BufferFormatter formatter = new BufferFormatter(size, FormattingData.InvariantUtf8, pool); timer.Restart(); for (int itteration = 0; itteration < itterationsInvariant; itteration++) { formatter.Clear(); for (int i = 0; i < stringsToWrite; i++) { formatter.Append(text); formatter.Append(1); } Assert.Equal(size, formatter.CommitedByteCount); } PrintTime(); }
static void ProcessRequest(TcpClient socket) { HttpServer.Listen(socket, (request) => { if (request.RequestUri.Equals(new Utf8String("/plaintext"))) { var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8); HttpWriter.WriteCommonHeaders(formatter, "HTTP/1.1 200 OK"); formatter.Append("Hello, World!"); socket.Write(formatter); } }); }
private HttpServerBuffer CreateResponseForHelloWorld() { var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8); formatter.Append(@"HTTP/1.1 200 OK"); formatter.Append(HttpNewline); formatter.Append("Content-Length: 15"); formatter.Append(HttpNewline); formatter.Append("Content-Type: text/plain; charset=UTF-8"); formatter.Append(HttpNewline); formatter.Append("Server: .NET Core Sample Server"); formatter.Append(HttpNewline); formatter.Append("Date: "); formatter.Append(DateTime.UtcNow, 'R'); formatter.Append(HttpNewline); formatter.Append(HttpNewline); formatter.Append("Hello, World"); return(new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared)); }
protected virtual void WriteResponseFor400(BufferFormatter formatter, Span <byte> receivedBytes) // Bad Request { Log.LogMessage(Log.Level.Warning, "Request {0}, Response: 400 Bad Request", receivedBytes.Length); WriteCommonHeaders(formatter, "1.1", "400", "Bad Request", false); formatter.Append(HttpNewline); }
protected virtual void WriteResponseFor404(BufferFormatter formatter, HttpRequestLine requestLine) // Not Found { Log.LogMessage(Log.Level.Warning, "Request {0}, Response: 404 Not Found", requestLine); WriteCommonHeaders(formatter, "1.1", "404", "Not Found", false); formatter.Append(HttpNewline); }
public static void WriteCommonHeaders(BufferFormatter formatter, string responseLine) { var currentTime = DateTime.UtcNow; formatter.Append(responseLine); formatter.Append(HttpNewline); formatter.Append("Date: "); formatter.Append(currentTime, 'R'); formatter.Append(HttpNewline); formatter.Append("Server: .NET Core Sample Server"); formatter.Append(HttpNewline); formatter.Append("Last-Modified: "); formatter.Append(currentTime, 'R'); formatter.Append(HttpNewline); formatter.Append("Content-Type: text/html; charset=UTF-8"); formatter.Append(HttpNewline); formatter.Append("Connection: close"); formatter.Append(HttpNewline); formatter.Append(HttpNewline); }