Exemplo n.º 1
0
 private static void WriteHeaders(HttpHeaders headers, ref WritableBuffer buffer)
 {
     foreach (var header in headers)
     {
         WritableBufferExtensions.WriteAsciiString(ref buffer, $"{header.Key}:{string.Join(",", header.Value)}\r\n");
     }
 }
Exemplo n.º 2
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var key  = request.RequestUri.GetComponents(UriComponents.HostAndPort, UriFormat.SafeUnescaped);
            var path = request.RequestUri.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped);

            var state      = _connectionPool.GetOrAdd(key, k => GetConnection(request));
            var connection = await state.ConnectionTask;

            var requestBuffer = connection.Output.Alloc();

            WritableBufferExtensions.WriteAsciiString(ref requestBuffer, $"{request.Method} {path} HTTP/1.1");
            WriteHeaders(request.Headers, ref requestBuffer);

            // End of the headers
            WritableBufferExtensions.WriteAsciiString(ref requestBuffer, "\r\n\r\n");

            if (request.Method != HttpMethod.Get && request.Method != HttpMethod.Head)
            {
                WriteHeaders(request.Content.Headers, ref requestBuffer);

                await requestBuffer.FlushAsync();

                // Copy the body to the input channel
                var body = await request.Content.ReadAsStreamAsync();

                await body.CopyToAsync(connection.Output);
            }
            else
            {
                await requestBuffer.FlushAsync();
            }

            var response = new HttpResponseMessage();

            response.Content = new ChannelHttpContent(connection.Input);

            await ProduceResponse(state, connection, response);

            // Get off the libuv thread
            await Task.Yield();

            return(response);
        }
Exemplo n.º 3
0
        private static async Task RunHttpClient(IPAddress ip, int port)
        {
            var client = new UvTcpClient(ip, port);

            var consoleOutput = client.ChannelFactory.MakeWriteableChannel(Console.OpenStandardOutput());

            var connection = await client.ConnectAsync();

            // This is on a libuv thread

            while (true)
            {
                var buffer = connection.Input.Alloc();

                WritableBufferExtensions.WriteAsciiString(ref buffer, "GET / HTTP/1.1");
                WritableBufferExtensions.WriteAsciiString(ref buffer, "\r\n\r\n");

                await connection.Input.WriteAsync(buffer);

                // Write the client output to the console
                await CopyCompletedAsync(connection.Output, consoleOutput);
            }
        }
Exemplo n.º 4
0
        private static async Task RunRawHttpClient(IPAddress ip, int port)
        {
            var thread = new UvThread();
            var client = new UvTcpClient(thread, new IPEndPoint(ip, port));

            var consoleOutput = thread.ChannelFactory.MakeWriteableChannel(Console.OpenStandardOutput());

            var connection = await client.ConnectAsync();

            while (true)
            {
                var buffer = connection.Input.Alloc();

                WritableBufferExtensions.WriteAsciiString(ref buffer, "GET / HTTP/1.1");
                WritableBufferExtensions.WriteAsciiString(ref buffer, "\r\n\r\n");

                await buffer.FlushAsync();

                // Write the client output to the console
                await CopyCompletedAsync(connection.Output, consoleOutput);

                await Task.Delay(1000);
            }
        }