Beispiel #1
0
        static void WriteResponseForPostJson(HttpRequest request, ReadOnlySequence <byte> body, TcpConnectionFormatter response)
        {
            // read request json
            int requestedCount;

            // TODO: this should not convert to span
            var dom = JsonObject.Parse(body.First.Span);

            try
            {
                requestedCount = (int)dom["Count"];
            }
            finally
            {
                dom.Dispose();
            }

            WriteCommonHeaders(ref response, Http.Version.Http11, 200, "OK");
            response.Append("Content-Type : application/json; charset=UTF-8\r\n");
            response.AppendEoh();

            // write response JSON
            var jsonWriter = new JsonWriterUtf8(response, prettyPrint: false);

            jsonWriter.WriteObjectStart();
            jsonWriter.WriteArrayStart("values");
            for (int i = 0; i < requestedCount; i++)
            {
                jsonWriter.WriteValue("hello!");
            }
            jsonWriter.WriteArrayEnd();
            jsonWriter.WriteObjectEnd();
        }
Beispiel #2
0
        static void WriteResponseForGetJson(HttpRequest request, ReadOnlySequence <byte> body, TcpConnectionFormatter response)
        {
            WriteCommonHeaders(ref response, Http.Version.Http11, 200, "OK");

            response.Append("Content-Type : application/json; charset=UTF-8\r\n");
            response.AppendEoh();

            // write response JSON
            var jsonWriter = new JsonWriterUtf8(response, prettyPrint: false);

            jsonWriter.WriteObjectStart();
            jsonWriter.WriteArrayStart("values");
            for (int i = 0; i < 5; i++)
            {
                jsonWriter.WriteValue("hello!");
            }
            jsonWriter.WriteArrayEnd();
            jsonWriter.WriteObjectEnd();
        }
Beispiel #3
0
        static void WriteResponseForHelloWorld(HttpRequest request, ReadOnlySequence <byte> body, TcpConnectionFormatter response)
        {
            WriteCommonHeaders(ref response, Http.Version.Http11, 200, "OK");

            response.Append("Content-Type : text/plain; charset=UTF-8\r\n");
            response.AppendEoh();

            response.Append("Hello, World");
        }
Beispiel #4
0
        static void WriteResponseForGetTime(HttpRequest request, ReadOnlySequence <byte> body, TcpConnectionFormatter response)
        {
            WriteCommonHeaders(ref response, Http.Version.Http11, 200, "OK");

            response.Append("Content-Type : text/html; charset=UTF-8\r\n");
            response.AppendEoh();

            response.Format(@"<html><head><title>Time</title></head><body>{0:O}</body></html>", DateTime.UtcNow);
        }
Beispiel #5
0
        static void WriteResponseForPostJson(HttpRequest request, ReadOnlySequence <byte> body, TcpConnectionFormatter response)
        {
            // read request json
            int requestedCount;

            using (JsonDocument dom = JsonDocument.Parse(body.First))
            {
                requestedCount = dom.RootElement.GetProperty("Count").GetInt32();
            }

            WriteCommonHeaders(ref response, Http.Version.Http11, 200, "OK");
            response.Append("Content-Type : application/json; charset=UTF-8\r\n");
            response.AppendEoh();

            // write response JSON
            var jsonWriter = new Utf8JsonWriter(response);

            jsonWriter.WriteStartObject();
            jsonWriter.WriteStartArray("values");
            for (int i = 0; i < requestedCount; i++)
            {
                jsonWriter.WriteStringValue("hello!");
            }
            jsonWriter.WriteEndArray();
            jsonWriter.WriteEndObject();
            jsonWriter.Flush();
        }