Пример #1
0
        public void TechEmpowerHelloWorldNoIO(int numberOfRequests, int concurrentConnections)
        {
            RawInMemoryHttpServer.Run(numberOfRequests, concurrentConnections, s_genericRequest, (request, response) => {
                var formatter = new BufferWriterFormatter <PipeWriter>(response, SymbolTable.InvariantUtf8);
                formatter.Append("HTTP/1.1 200 OK");
                formatter.Append("\r\nContent-Length: 13");
                formatter.Append("\r\nContent-Type: text/plain");
                formatter.Format("\r\nDate: {0:R}", DateTime.UtcNow);
                formatter.Append("Server: System.IO.Pipelines");
                formatter.Append("\r\n\r\n");

                // write body
                formatter.Append("Hello, World!");
            });
        }
Пример #2
0
        public void TechEmpowerJsonNoIO(int numberOfRequests, int concurrentConnections)
        {
            RawInMemoryHttpServer.Run(numberOfRequests, concurrentConnections, s_genericRequest, (request, response) => {
                var formatter = new BufferWriterFormatter <PipeWriter>(response, SymbolTable.InvariantUtf8);
                formatter.Append("HTTP/1.1 200 OK");
                formatter.Append("\r\nContent-Length: 25");
                formatter.Append("\r\nContent-Type: application/json");
                formatter.Format("\r\nDate: {0:R}", DateTime.UtcNow);
                formatter.Append("Server: System.IO.Pipelines");
                formatter.Append("\r\n\r\n");

                // write body
                var writer = new JsonWriterUtf8(formatter);
                writer.WriteObjectStart();
                writer.WriteAttribute("message", "Hello, World!");
                writer.WriteObjectEnd();
            });
        }
Пример #3
0
        protected async Task ProcessConnection(IDuplexPipe connection)
        {
            var httpParser = new HttpRequestParser();

            while (true)
            {
                // Wait for data
                var result = await connection.Input.ReadAsync();

                var input    = result.Buffer;
                var consumed = input.Start;
                var examined = input.Start;

                try
                {
                    if (input.IsEmpty && result.IsCompleted)
                    {
                        // No more data
                        break;
                    }

                    // Parse the input http request
                    var parseResult = httpParser.ParseRequest(input, out consumed, out examined);

                    switch (parseResult)
                    {
                    case HttpRequestParser.ParseResult.Incomplete:
                        if (result.IsCompleted)
                        {
                            // Didn't get the whole request and the connection ended
                            throw new EndOfStreamException();
                        }
                        // Need more data
                        continue;

                    case HttpRequestParser.ParseResult.Complete:
                        break;

                    case HttpRequestParser.ParseResult.BadRequest:
                        throw new Exception();

                    default:
                        break;
                    }

                    // Writing directly to pooled buffers
                    var output    = connection.Output;
                    var formatter = new BufferWriterFormatter <PipeWriter>(output, SymbolTable.InvariantUtf8);
                    formatter.Append("HTTP/1.1 200 OK");
                    formatter.Append("\r\nContent-Length: 13");
                    formatter.Append("\r\nContent-Type: text/plain");
                    formatter.Append("\r\n\r\n");
                    formatter.Append("Hello, World!");
                    await output.FlushAsync();

                    httpParser.Reset();
                }
                finally
                {
                    // Consume the input
                    connection.Input.AdvanceTo(consumed, examined);
                }
            }
        }