Exemplo n.º 1
0
    static HttpServerBuffer CreateResponseForGetTime(HttpRequestLine request)
    {
        var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8);
        WriteCommonHeaders(formatter, @"HTTP/1.1 200 OK", request.IsKeepAlive());
        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);
    }
Exemplo n.º 2
0
    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 += (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();
    }
Exemplo n.º 3
0
    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);
                socket.Dispose();
            }
        });
    }
Exemplo n.º 4
0
    // 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);
    }
Exemplo n.º 5
0
 private HttpServerBuffer CreateResponseForHelloWorld()
 {
     var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8);
     formatter.Append(@"HTTP/1.1 200 OK");
     formatter.Append(HttpNewline);
     formatter.Append("Content-Length: 12");
     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);
 }
Exemplo n.º 6
0
        // Not Found
        protected virtual HttpServerBuffer CreateResponseFor404(HttpRequestLine requestLine, ByteSpan headersAndBody)
        {
            Log.LogMessage(Log.Level.Warning, "Request {0}, Response: 404 Not Found", requestLine);

            BufferFormatter formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8);
            WriteCommonHeaders(formatter, @"HTTP/1.1 404 Not Found");
            formatter.Append(HttpNewline);
            return new HttpServerBuffer(formatter.Buffer, formatter.CommitedByteCount, BufferPool.Shared);
        }
Exemplo n.º 7
0
 // Bad Request
 protected virtual HttpServerBuffer CreateResponseFor400(ByteSpan receivedBytes)
 {
     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);
 }
Exemplo n.º 8
0
 protected 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);
 }
        private void EncodeStringToUtf8()
        {
            string text = "Hello World!";
            int stringsToWrite = 2000;
            int size = stringsToWrite * text.Length + stringsToWrite;
            BufferFormatter formatter = new BufferFormatter(size, FormattingData.InvariantUtf8);

            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();
        }
Exemplo n.º 10
0
    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);
    }
Exemplo n.º 11
0
 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, HttpVersion.V1_1, 200, "OK", keepAlive: false);
     formatter.Append(new Utf8String("Content-Length : "));
     formatter.Append(body.Length);
     formatter.AppendHttpNewLine();
     formatter.AppendHttpNewLine();
     formatter.Append(body);
 }
Exemplo n.º 12
0
 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);
 }
Exemplo n.º 13
0
 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);
 }
Exemplo n.º 14
0
        // 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");
            }
        }