Пример #1
0
        /// <summary>
        ///     Prepare and send the http(s) request
        /// </summary>
        /// <returns></returns>
        internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTransparent, CancellationToken cancellationToken)
        {
            var upstreamProxy = Connection.UpStreamProxy;

            bool useUpstreamProxy = upstreamProxy != null && Connection.IsHttps == false;

            var serverStream = Connection.Stream;

            string url;

            if (!useUpstreamProxy || isTransparent)
            {
                url = Request.RequestUriString;
            }
            else
            {
                url = Request.RequestUri.ToString();
            }

            string?upstreamProxyUserName = null;
            string?upstreamProxyPassword = null;

            // Send Authentication to Upstream proxy if needed
            if (!isTransparent && upstreamProxy != null &&
                Connection.IsHttps == false &&
                !string.IsNullOrEmpty(upstreamProxy.UserName) &&
                upstreamProxy.Password != null)
            {
                upstreamProxyUserName = upstreamProxy.UserName;
                upstreamProxyPassword = upstreamProxy.Password;
            }

            // prepare the request & headers
            var headerBuilder = new HeaderBuilder();

            headerBuilder.WriteRequestLine(Request.Method, url, Request.HttpVersion);
            headerBuilder.WriteHeaders(Request.Headers, !isTransparent, upstreamProxyUserName, upstreamProxyPassword);

            // write request headers
            await serverStream.WriteHeadersAsync(headerBuilder, cancellationToken);

            if (enable100ContinueBehaviour && Request.ExpectContinue)
            {
                // wait for expectation response from server
                await ReceiveResponse(cancellationToken);

                if (Response.StatusCode == (int)HttpStatusCode.Continue)
                {
                    Request.ExpectationSucceeded = true;
                }
                else
                {
                    Request.ExpectationFailed = true;
                }
            }
        }
Пример #2
0
        /// <summary>
        ///     Prepare and send the http(s) request
        /// </summary>
        /// <returns></returns>
        internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTransparent,
                                        CancellationToken cancellationToken)
        {
            var upstreamProxy = Connection.UpStreamProxy;

            bool useUpstreamProxy = upstreamProxy != null && Connection.IsHttps == false;

            var writer = Connection.StreamWriter;

            string url;

            if (useUpstreamProxy || isTransparent)
            {
                url = Request.RequestUriString;
            }
            else
            {
                url = Request.RequestUri.GetOriginalPathAndQuery();
            }

            var headerBuilder = new HeaderBuilder();

            // prepare the request & headers
            headerBuilder.WriteRequestLine(Request.Method, url, Request.HttpVersion);

            // Send Authentication to Upstream proxy if needed
            if (!isTransparent && upstreamProxy != null &&
                Connection.IsHttps == false &&
                !string.IsNullOrEmpty(upstreamProxy.UserName) &&
                upstreamProxy.Password != null)
            {
                headerBuilder.WriteHeader(HttpHeader.ProxyConnectionKeepAlive);
                headerBuilder.WriteHeader(HttpHeader.GetProxyAuthorizationHeader(upstreamProxy.UserName, upstreamProxy.Password));
            }

            // write request headers
            foreach (var header in Request.Headers)
            {
                if (isTransparent || header.Name != KnownHeaders.ProxyAuthorization)
                {
                    headerBuilder.WriteHeader(header);
                }
            }

            headerBuilder.WriteLine();

            await writer.WriteHeadersAsync(headerBuilder, cancellationToken);

            if (enable100ContinueBehaviour && Request.ExpectContinue)
            {
                // wait for expectation response from server
                await ReceiveResponse(cancellationToken);

                if (Response.StatusCode == (int)HttpStatusCode.Continue)
                {
                    Request.ExpectationSucceeded = true;
                }
                else
                {
                    Request.ExpectationFailed = true;
                }
            }
        }