// This method is a bit of a mess. We need to fix many Http and Json APIs void WriteResponseForPostJson(BufferFormatter formatter, HttpRequestLine requestLine, ReadOnlySpan<byte> body) { Console.WriteLine(new Utf8String(body)); uint requestedCount = ReadCountUsingReader(body).GetValueOrDefault(1); //uint requestedCount = ReadCountUsingNonAllocatingDom(body).GetValueOrDefault(1); // TODO: this needs to be written directly to the buffer after content length reservation is implemented. var buffer = ArrayPool<byte>.Shared.Rent(2048); var spanFormatter = new SpanFormatter(buffer.Slice(), FormattingData.InvariantUtf8); var json = new JsonWriter<SpanFormatter>(spanFormatter, prettyPrint: true); json.WriteObjectStart(); json.WriteArrayStart(); for (int i = 0; i < requestedCount; i++) { json.WriteString(DateTime.UtcNow.ToString()); // TODO: this needs to not allocate. } json.WriteArrayEnd(); ; json.WriteObjectEnd(); var responseBodyText = new Utf8String(buffer, 0, spanFormatter.CommitedByteCount); formatter.AppendHttpStatusLine(HttpVersion.V1_1, 200, new Utf8String("OK")); formatter.Append(new Utf8String("Content-Length : ")); formatter.Append(responseBodyText.Length); formatter.AppendHttpNewLine(); formatter.Append("Content-Type : text/plain; charset=UTF-8"); formatter.AppendHttpNewLine(); formatter.Append("Server : .NET Core Sample Serve"); formatter.AppendHttpNewLine(); formatter.Append(new Utf8String("Date : ")); formatter.Append(DateTime.UtcNow.ToString("R")); formatter.AppendHttpNewLine(); formatter.AppendHttpNewLine(); formatter.Append(responseBodyText); ArrayPool<byte>.Shared.Return(buffer); }
static void WriteResponseForHelloWorld(BufferFormatter formatter) { var responseBodyText = new Utf8String("Hello, World"); formatter.AppendHttpStatusLine(HttpVersion.V1_1, 200, new Utf8String("OK")); formatter.Append(new Utf8String("Content-Length : ")); formatter.Append(responseBodyText.Length); formatter.AppendHttpNewLine(); formatter.Append("Content-Type : text/plain; charset=UTF-8"); formatter.AppendHttpNewLine(); formatter.Append("Server : .NET Core Sample Serve"); formatter.AppendHttpNewLine(); formatter.Append(new Utf8String("Date : ")); formatter.Append(DateTime.UtcNow.ToString("R")); formatter.AppendHttpNewLine(); formatter.AppendHttpNewLine(); formatter.Append(responseBodyText); }
// TODO: this should not be here. Also, this should not allocate protected static void WriteCommonHeaders( BufferFormatter formatter, HttpVersion version, int statuCode, string reasonCode, bool keepAlive) { var currentTime = DateTime.UtcNow; formatter.AppendHttpStatusLine(version, statuCode, new Utf8String(reasonCode)); formatter.Append(new Utf8String("Date : ")); formatter.Append(currentTime, 'R'); formatter.AppendHttpNewLine(); formatter.Append("Server : .NET Core Sample Serve"); formatter.AppendHttpNewLine(); formatter.Append("Content-Type : text/html; charset=UTF-8"); formatter.AppendHttpNewLine(); if (!keepAlive) { formatter.Append("Connection : close"); } }