Esempio n. 1
0
        static async Task HandleHttpsRequests(ClientInfo clientInfo, int receiveBufferSize, CustomHttpsClient httpsClient)
        {
            while (true)
            {
                var cts = new CancellationTokenSource(105 * 1000);
                // receive http request from client
                var requestBytes = await ReceiveHttpRequestBytes(clientInfo.Stream, receiveBufferSize, cts.Token);

                var requestText = Encoding.UTF8.GetString(requestBytes);

                var line = new string(requestText.Replace("\r\n\r\n", "").Replace("\r\n", " ").Take(95).ToArray());
                //var line = requestText.Replace("\r\n\r\n", "").Replace("\r\n", " ");
                WriteLine($"client '{clientInfo.Id}' req: '{line}'");

                // send request to remote
                var rawResponse = await httpsClient.HandleSend(requestText);

                var responseText = Encoding.UTF8.GetString(rawResponse);

                // forward response to client
                await clientInfo.Stream.WriteAsync(rawResponse, cts.Token);

                if (responseText.Contains("Connection: close"))
                {
                    throw new IOException("closing connection");
                }
            }
        }