/// <summary>
        /// Receive and parse the http response from server
        /// </summary>
        /// <returns></returns>
        internal async Task ReceiveResponse()
        {
            //return if this is already read
            if (Response.StatusCode != 0)
            {
                return;
            }

            string httpStatus = await ServerConnection.StreamReader.ReadLineAsync();

            if (httpStatus == null)
            {
                throw new IOException();
            }

            if (httpStatus == string.Empty)
            {
                //Empty content in first-line, try again
                httpStatus = await ServerConnection.StreamReader.ReadLineAsync();
            }

            Response.ParseResponseLine(httpStatus, out var version, out int statusCode, out string statusDescription);

            Response.HttpVersion       = version;
            Response.StatusCode        = statusCode;
            Response.StatusDescription = statusDescription;

            //For HTTP 1.1 comptibility server may send expect-continue even if not asked for it in request
            if (Response.StatusCode == (int)HttpStatusCode.Continue &&
                Response.StatusDescription.Equals("continue", StringComparison.CurrentCultureIgnoreCase))
            {
                //Read the next line after 100-continue
                Response.Is100Continue = true;
                Response.StatusCode    = 0;
                await ServerConnection.StreamReader.ReadLineAsync();

                //now receive response
                await ReceiveResponse();

                return;
            }

            if (Response.StatusCode == (int)HttpStatusCode.ExpectationFailed &&
                Response.StatusDescription.Equals("expectation failed", StringComparison.CurrentCultureIgnoreCase))
            {
                //read next line after expectation failed response
                Response.ExpectationFailed = true;
                Response.StatusCode        = 0;
                await ServerConnection.StreamReader.ReadLineAsync();

                //now receive response
                await ReceiveResponse();

                return;
            }

            //Read the response headers in to unique and non-unique header collections
            await HeaderParser.ReadHeaders(ServerConnection.StreamReader, Response.Headers);
        }
Example #2
0
        /// <summary>
        ///     Receive and parse the http response from server
        /// </summary>
        /// <returns></returns>
        internal async Task ReceiveResponse(CancellationToken cancellationToken)
        {
            // return if this is already read
            if (Response.StatusCode != 0)
            {
                return;
            }

            var httpStatus = await Connection.Stream.ReadResponseStatus(cancellationToken);

            Response.HttpVersion       = httpStatus.Version;
            Response.StatusCode        = httpStatus.StatusCode;
            Response.StatusDescription = httpStatus.Description;

            // Read the response headers in to unique and non-unique header collections
            await HeaderParser.ReadHeaders(Connection.Stream, Response.Headers, cancellationToken);
        }
        /// <summary>
        ///     Receive and parse the http response from server
        /// </summary>
        /// <returns></returns>
        internal async Task ReceiveResponse(CancellationToken cancellationToken)
        {
            // return if this is already read
            if (Response.StatusCode != 0)
            {
                return;
            }

            string httpStatus;

            try
            {
                httpStatus = await Connection.Stream.ReadLineAsync(cancellationToken);

                if (httpStatus == null)
                {
                    throw new ServerConnectionException("Server connection was closed.");
                }
            }
            catch (Exception e) when(!(e is ServerConnectionException))
            {
                throw new ServerConnectionException("Server connection was closed.");
            }

            if (httpStatus == string.Empty)
            {
                httpStatus = await Connection.Stream.ReadLineAsync(cancellationToken);
            }

            Response.ParseResponseLine(httpStatus, out var version, out int statusCode, out string statusDescription);

            Response.HttpVersion       = version;
            Response.StatusCode        = statusCode;
            Response.StatusDescription = statusDescription;

            // Read the response headers in to unique and non-unique header collections
            await HeaderParser.ReadHeaders(Connection.Stream, Response.Headers, cancellationToken);
        }
        /// <summary>
        ///     Receive and parse the http response from server
        /// </summary>
        /// <returns></returns>
        internal async Task ReceiveResponse(CancellationToken cancellationToken)
        {
            // return if this is already read
            if (Response.StatusCode != 0)
            {
                return;
            }

            string httpStatus;

            try
            {
                httpStatus = await ServerConnection.Stream.ReadLineAsync(cancellationToken);

                if (httpStatus == null)
                {
                    throw new ServerConnectionException("Server connection was closed.");
                }
            }
            catch (Exception e) when(!(e is ServerConnectionException))
            {
                throw new ServerConnectionException("Server connection was closed.");
            }

            if (httpStatus == string.Empty)
            {
                httpStatus = await ServerConnection.Stream.ReadLineAsync(cancellationToken);
            }

            Version version;
            int     statusCode;
            string  statusDescription;

            Response.ParseResponseLine(httpStatus, out version, out statusCode, out statusDescription);

            Response.HttpVersion       = version;
            Response.StatusCode        = statusCode;
            Response.StatusDescription = statusDescription;

            // For HTTP 1.1 comptibility server may send expect-continue even if not asked for it in request
            if (Response.StatusCode == (int)HttpStatusCode.Continue &&
                Response.StatusDescription.EqualsIgnoreCase("continue"))
            {
                // Read the next line after 100-continue
                Response.Is100Continue = true;
                Response.StatusCode    = 0;
                await ServerConnection.Stream.ReadLineAsync(cancellationToken);

                // now receive response
                await ReceiveResponse(cancellationToken);

                return;
            }

            if (Response.StatusCode == (int)HttpStatusCode.ExpectationFailed &&
                Response.StatusDescription.EqualsIgnoreCase("expectation failed"))
            {
                // read next line after expectation failed response
                Response.ExpectationFailed = true;
                Response.StatusCode        = 0;
                await ServerConnection.Stream.ReadLineAsync(cancellationToken);

                // now receive response
                await ReceiveResponse(cancellationToken);

                return;
            }

            // Read the response headers in to unique and non-unique header collections
            await HeaderParser.ReadHeaders(ServerConnection.Stream, Response.Headers, cancellationToken);
        }
Example #5
0
        /// <summary>
        /// Receive and parse the http response from server
        /// </summary>
        /// <returns></returns>
        internal async Task ReceiveResponse()
        {
            //return if this is already read
            if (Response.ResponseStatusCode != null)
            {
                return;
            }

            string line = await ServerConnection.StreamReader.ReadLineAsync();

            if (line == null)
            {
                throw new IOException();
            }

            var httpResult = line.Split(ProxyConstants.SpaceSplit, 3);

            if (string.IsNullOrEmpty(httpResult[0]))
            {
                //Empty content in first-line, try again
                httpResult = (await ServerConnection.StreamReader.ReadLineAsync()).Split(ProxyConstants.SpaceSplit, 3);
            }

            var httpVersion = httpResult[0];

            var version = HttpHeader.Version11;

            if (string.Equals(httpVersion, "HTTP/1.0", StringComparison.OrdinalIgnoreCase))
            {
                version = HttpHeader.Version10;
            }

            Response.HttpVersion               = version;
            Response.ResponseStatusCode        = httpResult[1].Trim();
            Response.ResponseStatusDescription = httpResult[2].Trim();

            //For HTTP 1.1 comptibility server may send expect-continue even if not asked for it in request
            if (Response.ResponseStatusCode.Equals("100") &&
                Response.ResponseStatusDescription.Equals("continue", StringComparison.CurrentCultureIgnoreCase))
            {
                //Read the next line after 100-continue
                Response.Is100Continue      = true;
                Response.ResponseStatusCode = null;
                await ServerConnection.StreamReader.ReadLineAsync();

                //now receive response
                await ReceiveResponse();

                return;
            }

            if (Response.ResponseStatusCode.Equals("417") &&
                Response.ResponseStatusDescription.Equals("expectation failed", StringComparison.CurrentCultureIgnoreCase))
            {
                //read next line after expectation failed response
                Response.ExpectationFailed  = true;
                Response.ResponseStatusCode = null;
                await ServerConnection.StreamReader.ReadLineAsync();

                //now receive response
                await ReceiveResponse();

                return;
            }

            //Read the response headers in to unique and non-unique header collections
            await HeaderParser.ReadHeaders(ServerConnection.StreamReader, Response.NonUniqueResponseHeaders, Response.ResponseHeaders);
        }