Esempio n. 1
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();
        }
Esempio n. 2
0
        static void WriteResponseForPostJson(HttpRequest request, ReadOnlyBytes 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 JsonWriter(response, prettyPrint: false);

            jsonWriter.WriteObjectStart();
            jsonWriter.WriteArrayStart("values");
            for (int i = 0; i < requestedCount; i++)
            {
                jsonWriter.WriteValue("hello!");
            }
            jsonWriter.WriteArrayEnd();
            jsonWriter.WriteObjectEnd();
        }
Esempio n. 3
0
        static void WriteResponseForPostJson(HttpRequest request, TcpConnectionFormatter response)
        {
            // read request json
            int requestedCount;

            // TODO: this should not convert to span
            using (var dom = JsonObject.Parse(request.Body.ToSpan())) {
                requestedCount = (int)dom["Count"];
            }

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

            // write response JSON
            var jsonWriter = new JsonWriter <TcpConnectionFormatter>(response, prettyPrint: false);

            jsonWriter.WriteObjectStart();
            jsonWriter.WriteArrayStart();
            for (int i = 0; i < requestedCount; i++)
            {
                jsonWriter.WriteString("hello!");
            }
            jsonWriter.WriteArrayEnd();
            jsonWriter.WriteObjectEnd();
        }
Esempio n. 4
0
        static void WriteResponseForGetTime(HttpRequest request, ReadOnlyBytes 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);
        }
Esempio n. 5
0
        static void WriteResponseForHelloWorld(HttpRequest request, ReadOnlyBytes 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");
        }
Esempio n. 6
0
        static void WriteResponseForGetJson(HttpRequest request, ReadOnlyBytes 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 JsonWriter(response, prettyPrint: false);

            jsonWriter.WriteObjectStart();
            jsonWriter.WriteArrayStart("values");
            for (int i = 0; i < 5; i++)
            {
                jsonWriter.WriteValue("hello!");
            }
            jsonWriter.WriteArrayEnd();
            jsonWriter.WriteObjectEnd();
        }
Esempio n. 7
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 Utf8JsonWriter(response);

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