private static void WriteResponseForHelloWorld(BufferFormatter formatter) { formatter.WriteHttpStatusLine(new Utf8String("1.1"), new Utf8String("200"), new Utf8String("Ok")); formatter.WriteHttpHeader(new Utf8String("Content-Length"), new Utf8String("12")); formatter.WriteHttpHeader(new Utf8String("Content-Type"), new Utf8String("text/plain; charset=UTF-8")); formatter.WriteHttpHeader(new Utf8String("Server"), new Utf8String(".NET Core Sample Serve")); // TODO: this needs to not allocate formatter.WriteHttpHeader(new Utf8String("Date"), new Utf8String(DateTime.UtcNow.ToString("R"))); formatter.EndHttpHeaderSection(); formatter.WriteHttpBody(new Utf8String("Hello, World")); }
private static HttpServerBuffer CreateResponseForHelloWorld() { var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8); formatter.WriteHttpStatusLine(new Utf8String("1.1"), new Utf8String("200"), new Utf8String("Ok")); formatter.WriteHttpHeader(new Utf8String("Content-Length"), new Utf8String("12")); formatter.WriteHttpHeader(new Utf8String("Content-Type"), new Utf8String("text/plain; charset=UTF-8")); formatter.WriteHttpHeader(new Utf8String("Server"), new Utf8String(".NET Core Sample Serve")); formatter.WriteHttpHeader(new Utf8String("Date"), new Utf8String(DateTime.UtcNow.ToString("R"))); formatter.EndHttpHeaderSection(); formatter.WriteHttpBody(new Utf8String("Hello, World")); return(new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared)); }
static void WriteResponseForGetTime(BufferFormatter formatter, HttpRequestLine request) { // TODO: this needs to not allocate var body = string.Format(@"<html><head><title>Time</title></head><body>{0}</body></html>", DateTime.UtcNow.ToString("O")); WriteCommonHeaders(formatter, "1.1", "200", "Ok", keepAlive: false); // TOOD: this needs to not allocate formatter.WriteHttpHeader(new Utf8String("Content-Length"), new Utf8String(body.Length.ToString())); formatter.EndHttpHeaderSection(); formatter.WriteHttpBody(new Utf8String(body)); }
public void It_has_an_extension_method_to_write_headers() { _formatter.WriteHttpHeader(GetUtf8EncodedString("Connection"), GetUtf8EncodedString("close")); var result = _formatter.Buffer; result.Should().ContainInOrder(_headerInBytes); ManagedBufferPool <byte> .SharedByteBufferPool.ReturnBuffer(ref result); }
public void WriteHttpHeader_resizes_the_buffer_taking_into_consideration_the_reserver() { _formatter = new BufferFormatter(32, FormattingData.InvariantUtf8, ManagedBufferPool <byte> .SharedByteBufferPool); var httpHeaderBuffer = _formatter.WriteHttpHeader(GetUtf8EncodedString("Connection"), GetUtf8EncodedString("close")); httpHeaderBuffer.UpdateValue("18446744073709551615"); var result = _formatter.Buffer; result.Should().ContainInOrder(Utf8Encoding.GetBytes("Connection : 18446744073709551615\r\n")); }
static HttpServerBuffer CreateResponseForGetTime(HttpRequestLine request) { var body = string.Format(@"<html><head><title>Time</title></head><body>{0}</body></html>", DateTime.UtcNow.ToString("O")); var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8); WriteCommonHeaders(formatter, "1.1", "200", "Ok", request.IsKeepAlive()); formatter.WriteHttpHeader(new Utf8String("Content-Length"), new Utf8String(body.Length.ToString())); formatter.EndHttpHeaderSection(); formatter.WriteHttpBody(new Utf8String(body)); return(new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared)); }
// TODO: this should not be here. Also, this should not allocate protected static void WriteCommonHeaders( BufferFormatter formatter, string version, string statuCode, string reasonCode, bool keepAlive) { var currentTime = DateTime.UtcNow; formatter.WriteHttpStatusLine( new Utf8String(version), new Utf8String(statuCode), new Utf8String(reasonCode)); formatter.WriteHttpHeader(new Utf8String("Date"), new Utf8String(currentTime.ToString("R"))); formatter.WriteHttpHeader(new Utf8String("Server"), new Utf8String(".NET Core Sample Serve")); formatter.WriteHttpHeader(new Utf8String("Last-Modified"), new Utf8String(currentTime.ToString("R"))); formatter.WriteHttpHeader(new Utf8String("Content-Type"), new Utf8String("text/html; charset=UTF-8")); if (!keepAlive) { formatter.WriteHttpHeader(new Utf8String("Connection"), new Utf8String("close")); } }