Esempio n. 1
0
        public static void RunSingleSegmentParser(int numberOfRequests, int concurrentConnections, byte[] requestPayload, Action <HttpRequestSingleSegment, WritableBuffer> writeResponse)
        {
            var factory  = new PipelineFactory();
            var listener = new FakeListener(factory, concurrentConnections);

            listener.OnConnection(async connection =>
            {
                while (true)
                {
                    // Wait for data
                    var result           = await connection.Input.ReadAsync();
                    ReadableBuffer input = result.Buffer;

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

                        var requestBytes = input.First.Span;

                        if (requestBytes.Length != 492)
                        {
                            continue;
                        }
                        // Parse the input http request
                        HttpRequestSingleSegment parsedRequest = HttpRequestSingleSegment.Parse(requestBytes);

                        // Writing directly to pooled buffers
                        var output = connection.Output.Alloc();
                        writeResponse(parsedRequest, output);
                        await output.FlushAsync();
                    }
                    catch (Exception e)
                    {
                        var istr = new Utf8String(input.First.Span).ToString();
                        Debug.WriteLine(e.Message);
                    }
                    finally
                    {
                        // Consume the input
                        connection.Input.Advance(input.End, input.End);
                    }
                }
            });

            var tasks = new Task[numberOfRequests];

            for (int i = 0; i < numberOfRequests; i++)
            {
                tasks[i] = listener.ExecuteRequestAsync(requestPayload);
            }

            Task.WaitAll(tasks);

            listener.Dispose();
            factory.Dispose();
        }
Esempio n. 2
0
        private static WritableBuffer WriteResponse(WriteResponseDelegate writeResponse, IPipeConnection connection, Buffer <byte> requestBuffer)
        {
            HttpRequestSingleSegment parsedRequest = HttpRequestSingleSegment.Parse(requestBuffer.Span);

            // Writing directly to pooled buffers
            var output = connection.Output.Alloc();

            writeResponse(parsedRequest, output);
            return(output);
        }
Esempio n. 3
0
        //[Fact(Skip = "System.TypeLoadException : The generic type 'System.Collections.Generic.KeyValuePair`2' was used with an invalid instantiation in assembly 'System.Private.CoreLib")]
        public void CanParseBodylessRequest()
        {
            var request = new Utf8String("GET / HTTP/1.1\r\nConnection: close\r\n\r\n").CopyBytes().AsSpan();
            var parsed  = HttpRequestSingleSegment.Parse(request);

            Assert.Equal(HttpMethod.Get, parsed.RequestLine.Method);
            Assert.Equal(HttpVersion.V1_1, parsed.RequestLine.Version);
            Assert.Equal("/", parsed.RequestLine.RequestUri.ToString(TextEncoder.Utf8));
            Assert.Equal(1, parsed.Headers.Count);
            Assert.Equal(0, parsed.Body.Length);
        }
        //[Fact(Skip = "System.TypeLoadException : The generic type 'System.Collections.Generic.KeyValuePair`2' was used with an invalid instantiation in assembly 'System.Private.CoreLib")]
        public void CanParseBodylessRequest()
        {
            var request = new Utf8Span("GET / HTTP/1.1\r\nConnection: close\r\n\r\n").Bytes;
            var parsed  = HttpRequestSingleSegment.Parse(request);

            Assert.Equal(HttpMethod.Get, parsed.RequestLine.Method);
            Assert.Equal(HttpVersion.V1_1, parsed.RequestLine.Version);
            Assert.Equal("/", parsed.RequestLine.RequestUri.ToString(SymbolTable.InvariantUtf8));
            Assert.Equal(1, parsed.Headers.Count);
            Assert.True(parsed.Body.IsEmpty);
        }