internal static async Task SendRequestToProxyAsync()

in src/Microsoft.Azure.Relay/WebSockets/NetStandard20/WebSocketHandle.Managed.cs [306:353]


        internal static async Task<string> SendRequestToProxyAsync(Uri uri, ClientWebSocketOptions options, Stream stream, AuthenticationHeaderValue ahv, CancellationToken cancellationToken)
        {
            const string successStatusCode = "HTTP/1.1 200";
            byte[] headers = BuildProxyHeaders(uri, options, ahv);
            // Write the proxy headers.
            await stream.WriteAsync(headers, 0, headers.Length, cancellationToken).ConfigureAwait(false);
            // Read the response from proxy server.
            string statusLine = await ReadResponseHeaderLineAsync(stream, cancellationToken).ConfigureAwait(false);

            // Parse if success response.
            if (statusLine.StartsWith(successStatusCode))
            {
                if (statusLine.Length > successStatusCode.Length && !char.IsWhiteSpace(statusLine[successStatusCode.Length]))
                {
                    throw new WebSocketException(SR.net_webstatus_ConnectFailure);
                }

                // Read response headers.
                string line;
                int contentLength = 0;
                while (!string.IsNullOrEmpty((line = await ReadResponseHeaderLineAsync(stream, cancellationToken).ConfigureAwait(false))))
                {
                    if (line.StartsWith("Content-Length", StringComparison.OrdinalIgnoreCase))
                    {
                        int colon = line.IndexOf(':');
                        if (colon == -1 || colon == line.Length - 1)
                        {
                            throw new WebSocketException(SR.net_WebSockets_InvalidResponseHeader);
                        }

                        int.TryParse(line.Substring(colon + 1), out contentLength);
                    }
                    else if (line.StartsWith("Transfer-Encoding:", StringComparison.OrdinalIgnoreCase))
                    {
                        int nextValidIndex = line.IndexOf(':') + 1;
                        if (nextValidIndex < line.Length && line.SubstringTrim(nextValidIndex).Equals("chunked", StringComparison.OrdinalIgnoreCase))
                        {
                            throw new WebSocketException(SR.net_webstatus_ConnectFailure);
                        }
                    }
                }

                await ReadResponseContent(stream, contentLength, cancellationToken).ConfigureAwait(false);
                return successStatusCode;
            }

            return statusLine;
        }