Example #1
0
        private void Connect()
        {
            Uri uri =
#if !BESTHTTP_DISABLE_PROXY
                CurrentRequest.HasProxy ? CurrentRequest.Proxy.Address :
#endif
                CurrentRequest.CurrentUri;

            #region TCP Connection

            if (Client == null)
            {
                Client = new TcpClient();
            }

            if (!Client.Connected)
            {
                Client.ConnectTimeout = CurrentRequest.ConnectTimeout;

#if NETFX_CORE || (UNITY_WP8 && !UNITY_EDITOR)
                Client.UseHTTPSProtocol =
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
                    !CurrentRequest.UseAlternateSSL &&
#endif
                    HTTPProtocolFactory.IsSecureProtocol(uri);
#endif

                Client.Connect(uri.Host, uri.Port);

                if (HTTPManager.Logger.Level <= Logger.Loglevels.Information)
                {
                    HTTPManager.Logger.Information("HTTPConnection", "Connected to " + uri.Host + ":" + uri.Port.ToString());
                }
            }
            else if (HTTPManager.Logger.Level <= Logger.Loglevels.Information)
            {
                HTTPManager.Logger.Information("HTTPConnection", "Already connected to " + uri.Host + ":" + uri.Port.ToString());
            }

            #endregion

            lock (HTTPManager.Locker)
                StartTime = DateTime.UtcNow;

            if (Stream == null)
            {
                bool isSecure = HTTPProtocolFactory.IsSecureProtocol(CurrentRequest.CurrentUri);

#if !BESTHTTP_DISABLE_PROXY
                #region Proxy Handling

                if (HasProxy && (!Proxy.IsTransparent || (isSecure && Proxy.NonTransparentForHTTPS)))
                {
                    Stream = Client.GetStream();
                    var outStream = new BinaryWriter(Stream);

                    bool retry;
                    do
                    {
                        // If we have to becouse of a authentication request, we will switch it to true
                        retry = false;

                        outStream.SendAsASCII(string.Format("CONNECT {0}:{1} HTTP/1.1", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port));
                        outStream.Write(HTTPRequest.EOL);

                        outStream.SendAsASCII("Proxy-Connection: Keep-Alive");
                        outStream.Write(HTTPRequest.EOL);

                        outStream.SendAsASCII("Connection: Keep-Alive");
                        outStream.Write(HTTPRequest.EOL);

                        outStream.SendAsASCII(string.Format("Host: {0}:{1}", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port));
                        outStream.Write(HTTPRequest.EOL);

                        // Proxy Authentication
                        if (HasProxy && Proxy.Credentials != null)
                        {
                            switch (Proxy.Credentials.Type)
                            {
                            case AuthenticationTypes.Basic:
                                // With Basic authentication we don't want to wait for a challange, we will send the hash with the first request
                                outStream.Write(string.Format("Proxy-Authorization: {0}", string.Concat("Basic ", Convert.ToBase64String(Encoding.UTF8.GetBytes(Proxy.Credentials.UserName + ":" + Proxy.Credentials.Password)))).GetASCIIBytes());
                                outStream.Write(HTTPRequest.EOL);
                                break;

                            case AuthenticationTypes.Unknown:
                            case AuthenticationTypes.Digest:
                                var digest = DigestStore.Get(Proxy.Address);
                                if (digest != null)
                                {
                                    string authentication = digest.GenerateResponseHeader(CurrentRequest, Proxy.Credentials);
                                    if (!string.IsNullOrEmpty(authentication))
                                    {
                                        outStream.Write(string.Format("Proxy-Authorization: {0}", authentication).GetASCIIBytes());
                                        outStream.Write(HTTPRequest.EOL);
                                    }
                                }

                                break;
                            }
                        }

                        outStream.Write(HTTPRequest.EOL);

                        // Make sure to send all the wrote data to the wire
                        outStream.Flush();

                        CurrentRequest.ProxyResponse = new HTTPResponse(CurrentRequest, Stream, false, false);

                        // Read back the response of the proxy
                        if (!CurrentRequest.ProxyResponse.Receive(-1, true))
                        {
                            throw new Exception("Connection to the Proxy Server failed!");
                        }

                        if (HTTPManager.Logger.Level <= Logger.Loglevels.Information)
                        {
                            HTTPManager.Logger.Information("HTTPConnection", "Proxy returned - status code: " + CurrentRequest.ProxyResponse.StatusCode + " message: " + CurrentRequest.ProxyResponse.Message);
                        }

                        switch (CurrentRequest.ProxyResponse.StatusCode)
                        {
                        // Proxy authentication required
                        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8
                        case 407:
                        {
                            string authHeader = DigestStore.FindBest(CurrentRequest.ProxyResponse.GetHeaderValues("proxy-authenticate"));
                            if (!string.IsNullOrEmpty(authHeader))
                            {
                                var digest = DigestStore.GetOrCreate(Proxy.Address);
                                digest.ParseChallange(authHeader);

                                if (Proxy.Credentials != null && digest.IsUriProtected(Proxy.Address) && (!CurrentRequest.HasHeader("Proxy-Authorization") || digest.Stale))
                                {
                                    retry = true;
                                }
                            }
                            break;
                        }

                        default:
                            if (!CurrentRequest.ProxyResponse.IsSuccess)
                            {
                                throw new Exception(string.Format("Proxy returned Status Code: \"{0}\", Message: \"{1}\" and Response: {2}", CurrentRequest.ProxyResponse.StatusCode, CurrentRequest.ProxyResponse.Message, CurrentRequest.ProxyResponse.DataAsText));
                            }
                            break;
                        }
                    } while (retry);
                }
                #endregion
#endif // #if !BESTHTTP_DISABLE_PROXY

                // We have to use CurrentRequest.CurrentUri here, becouse uri can be a proxy uri with a different protocol
                if (isSecure)
                {
                    #region SSL Upgrade

#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
                    if (CurrentRequest.UseAlternateSSL)
                    {
                        var handler = new TlsClientProtocol(Client.GetStream(), new Org.BouncyCastle.Security.SecureRandom());

                        // http://tools.ietf.org/html/rfc3546#section-3.1
                        // It is RECOMMENDED that clients include an extension of type "server_name" in the client hello whenever they locate a server by a supported name type.
                        List <string> hostNames = new List <string>(1);
                        hostNames.Add(CurrentRequest.CurrentUri.Host);

                        handler.Connect(new LegacyTlsClient(CurrentRequest.CurrentUri,
                                                            CurrentRequest.CustomCertificateVerifyer == null ? new AlwaysValidVerifyer() : CurrentRequest.CustomCertificateVerifyer,
                                                            CurrentRequest.CustomClientCredentialsProvider,
                                                            hostNames));

                        Stream = handler.Stream;
                    }
                    else
#endif
                    {
#if !NETFX_CORE && !UNITY_WP8
                        SslStream sslStream = new SslStream(Client.GetStream(), false, (sender, cert, chain, errors) =>
                        {
                            return(CurrentRequest.CallCustomCertificationValidator(cert, chain));
                        });

                        if (!sslStream.IsAuthenticated)
                        {
                            sslStream.AuthenticateAsClient(CurrentRequest.CurrentUri.Host);
                        }
                        Stream = sslStream;
#else
                        Stream = Client.GetStream();
#endif
                    }

                    #endregion
                }
                else
                {
                    Stream = Client.GetStream();
                }
            }
        }
Example #2
0
        void ThreadFunc(object param)
        {
            bool alreadyReconnected = false;
            bool redirected         = false;

            RetryCauses cause = RetryCauses.None;

#if LOCK_ON_FILE
            object uriLock = null;
#endif

#if UNITY_WEBPLAYER
            // Right now, no caching supported in the webplayer
            if (!CurrentRequest.DisableCache)
            {
                CurrentRequest.DisableCache = true;
            }
#endif

            try
            {
                if (!HasProxy && CurrentRequest.HasProxy)
                {
                    Proxy = CurrentRequest.Proxy;
                }

                // Lock only if we will use the cached entity.
#if LOCK_ON_FILE
                if (!CurrentRequest.DisableCache)
                {
                    Monitor.Enter(uriLock = HTTPCacheFileLock.Acquire(CurrentRequest.CurrentUri));
                }
#endif

                // Try load the full response from an already saved cache entity. If the response
                if (TryLoadAllFromCache())
                {
                    return;
                }

                if (Client != null && !Client.IsConnected())
                {
                    Close();
                }

                do // of while (reconnect)
                {
                    if (cause == RetryCauses.Reconnect)
                    {
                        Close();
#if NETFX_CORE
                        await Task.Delay(100);
#else
                        Thread.Sleep(100);
#endif
                    }

                    LastProcessedUri = CurrentRequest.CurrentUri;

                    cause = RetryCauses.None;

                    // Connect to the server
                    Connect();

                    if (State == HTTPConnectionStates.AbortRequested)
                    {
                        throw new Exception("AbortRequested");
                    }

                    // Setup cache control headers before we send out the request
                    if (!CurrentRequest.DisableCache)
                    {
                        HTTPCacheService.SetHeaders(CurrentRequest);
                    }

                    // Write the request to the stream
                    // sentRequest will be true if the request sent out successfully(no SocketException), so we can try read the response
                    bool sentRequest = CurrentRequest.SendOutTo(Stream);

                    // sentRequest only true if there are no exceptions during CurrentRequest.SendOutTo.
                    if (!sentRequest)
                    {
                        Close();

                        if (State == HTTPConnectionStates.TimedOut)
                        {
                            throw new Exception("AbortRequested");
                        }

                        // We will try again only once
                        if (!alreadyReconnected)
                        {
                            alreadyReconnected = true;
                            cause = RetryCauses.Reconnect;
                        }
                    }

                    // If sending out the request succeded, we will try read the response.
                    if (sentRequest)
                    {
                        bool received = Receive();

                        if (State == HTTPConnectionStates.TimedOut)
                        {
                            throw new Exception("AbortRequested");
                        }

                        if (!received && !alreadyReconnected)
                        {
                            alreadyReconnected = true;
                            cause = RetryCauses.Reconnect;
                        }

                        if (CurrentRequest.Response != null)
                        {
                            switch (CurrentRequest.Response.StatusCode)
                            {
                            // Not authorized
                            // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2
                            case 401:
                            {
                                string authHeader = CurrentRequest.Response.GetFirstHeaderValue("www-authenticate");
                                if (!string.IsNullOrEmpty(authHeader))
                                {
                                    var digest = DigestStore.GetOrCreate(CurrentRequest.CurrentUri);
                                    digest.ParseChallange(authHeader);

                                    if (CurrentRequest.Credentials != null && digest.IsUriProtected(CurrentRequest.CurrentUri) && (!CurrentRequest.HasHeader("Authorization") || digest.Stale))
                                    {
                                        cause = RetryCauses.Authenticate;
                                    }
                                }

                                goto default;
                            }

                            // Proxy authentication required
                            // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8
                            case 407:
                            {
                                if (CurrentRequest.HasProxy)
                                {
                                    string authHeader = CurrentRequest.Response.GetFirstHeaderValue("proxy-authenticate");
                                    if (!string.IsNullOrEmpty(authHeader))
                                    {
                                        var digest = DigestStore.GetOrCreate(CurrentRequest.Proxy.Address);
                                        digest.ParseChallange(authHeader);

                                        if (CurrentRequest.Proxy.Credentials != null && digest.IsUriProtected(CurrentRequest.Proxy.Address) && (!CurrentRequest.HasHeader("Proxy-Authorization") || digest.Stale))
                                        {
                                            cause = RetryCauses.ProxyAuthenticate;
                                        }
                                    }
                                }

                                goto default;
                            }

                            // Redirected
                            case 301:     // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2
                            case 302:     // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3
                            case 307:     // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8
                            case 308:     // http://tools.ietf.org/html/rfc7238
                            {
                                if (CurrentRequest.RedirectCount >= CurrentRequest.MaxRedirects)
                                {
                                    goto default;
                                }
                                CurrentRequest.RedirectCount++;

                                string location = CurrentRequest.Response.GetFirstHeaderValue("location");
                                if (!string.IsNullOrEmpty(location))
                                {
                                    Uri redirectUri = GetRedirectUri(location);

                                    // Let the user to take some control over the redirection
                                    if (!CurrentRequest.CallOnBeforeRedirection(redirectUri))
                                    {
                                        HTTPManager.Logger.Information("HTTPConnection", "OnBeforeRedirection returned False");
                                        goto default;
                                    }

                                    // Remove the previously set Host header.
                                    CurrentRequest.RemoveHeader("Host");

                                    // Set the Referer header to the last Uri.
                                    CurrentRequest.SetHeader("Referer", CurrentRequest.CurrentUri.ToString());

                                    // Set the new Uri, the CurrentUri will return this while the IsRedirected property is true
                                    CurrentRequest.RedirectUri = redirectUri;

                                    // Discard the redirect response, we don't need it any more
                                    CurrentRequest.Response = null;

                                    redirected = CurrentRequest.IsRedirected = true;
                                }
                                else
                                            #if !NETFX_CORE
                                { throw new MissingFieldException(string.Format("Got redirect status({0}) without 'location' header!", CurrentRequest.Response.StatusCode.ToString())); }
                                            #else
                                { throw new Exception(string.Format("Got redirect status({0}) without 'location' header!", CurrentRequest.Response.StatusCode.ToString())); }
                                            #endif

                                goto default;
                            }

                            default:
                                if (CurrentRequest.IsCookiesEnabled)
                                {
                                    CookieJar.Set(CurrentRequest.Response);
                                }

                                TryStoreInCache();
                                break;
                            }

                            // If we have a response and the server telling us that it closed the connection after the message sent to us, then
                            //  we will colse the connection too.
                            if (CurrentRequest.Response == null ||
                                CurrentRequest.Response.HasHeaderWithValue("connection", "close") ||
                                CurrentRequest.UseAlternateSSL)
                            {
                                Close();
                            }
                        }
                    }
                } while (cause != RetryCauses.None);
            }
            catch (TimeoutException e)
            {
                CurrentRequest.Response  = null;
                CurrentRequest.Exception = e;
                CurrentRequest.State     = HTTPRequestStates.ConnectionTimedOut;

                Close();
            }
            catch (Exception e)
            {
                if (CurrentRequest.UseStreaming)
                {
                    HTTPCacheService.DeleteEntity(CurrentRequest.CurrentUri);
                }

                // Something gone bad, Response must be null!
                CurrentRequest.Response = null;

                switch (State)
                {
                case HTTPConnectionStates.AbortRequested:
                    CurrentRequest.State = HTTPRequestStates.Aborted;
                    break;

                case HTTPConnectionStates.TimedOut:
                    CurrentRequest.State = HTTPRequestStates.TimedOut;
                    break;

                default:
                    CurrentRequest.Exception = e;
                    CurrentRequest.State     = HTTPRequestStates.Error;
                    break;
                }

                Close();
            }
            finally
            {
#if LOCK_ON_FILE
                if (!CurrentRequest.DisableCache && uriLock != null)
                {
                    Monitor.Exit(uriLock);
                }
#endif

                // Avoid state changes. While we are in this block changing the connection's State, on Unity's main thread
                //  the HTTPManager's OnUpdate will check the connections's State and call functions that can change the inner state of
                //  the object. (Like setting the CurrentRequest to null in function Recycle() causing a NullRef exception)
                lock (HTTPManager.Locker)
                {
                    if (CurrentRequest != null && CurrentRequest.Response != null && CurrentRequest.Response.IsUpgraded)
                    {
                        State = HTTPConnectionStates.Upgraded;
                    }
                    else
                    {
                        State = redirected ? HTTPConnectionStates.Redirected : (Client == null ? HTTPConnectionStates.Closed : HTTPConnectionStates.WaitForRecycle);
                    }

                    // Change the request's state only when the whole processing finished
                    if (CurrentRequest.State == HTTPRequestStates.Processing && (State == HTTPConnectionStates.Closed || State == HTTPConnectionStates.WaitForRecycle))
                    {
                        CurrentRequest.State = HTTPRequestStates.Finished;
                    }

                    if (CurrentRequest.State == HTTPRequestStates.ConnectionTimedOut)
                    {
                        State = HTTPConnectionStates.Closed;
                    }

                    LastProcessTime = DateTime.UtcNow;
                }

                HTTPCacheService.SaveLibrary();
                CookieJar.Persist();
            }
        }
Example #3
0
        void ThreadFunc(object param)
        {
            bool alreadyReconnected = false;
            bool redirected         = false;

            RetryCauses cause = RetryCauses.None;

            try
            {
#if !BESTHTTP_DISABLE_PROXY
                if (!HasProxy && CurrentRequest.HasProxy)
                {
                    Proxy = CurrentRequest.Proxy;
                }
#endif

#if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
                // Try load the full response from an already saved cache entity. If the response
                if (TryLoadAllFromCache())
                {
                    return;
                }
#endif

                if (Client != null && !Client.IsConnected())
                {
                    Close();
                }

                do // of while (reconnect)
                {
                    if (cause == RetryCauses.Reconnect)
                    {
                        Close();
#if NETFX_CORE
                        await Task.Delay(100);
#else
                        Thread.Sleep(100);
#endif
                    }

                    LastProcessedUri = CurrentRequest.CurrentUri;

                    cause = RetryCauses.None;

                    // Connect to the server
                    Connect();

                    if (State == HTTPConnectionStates.AbortRequested)
                    {
                        throw new Exception("AbortRequested");
                    }

                    #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
                    // Setup cache control headers before we send out the request
                    if (!CurrentRequest.DisableCache)
                    {
                        HTTPCacheService.SetHeaders(CurrentRequest);
                    }
                    #endif

                    // Write the request to the stream
                    // sentRequest will be true if the request sent out successfully(no SocketException), so we can try read the response
                    bool sentRequest = false;
                    try
                    {
#if !NETFX_CORE
                        Client.NoDelay = CurrentRequest.TryToMinimizeTCPLatency;
#endif
                        CurrentRequest.SendOutTo(Stream);

                        sentRequest = true;
                    }
                    catch (Exception ex)
                    {
                        Close();

                        if (State == HTTPConnectionStates.TimedOut ||
                            State == HTTPConnectionStates.AbortRequested)
                        {
                            throw new Exception("AbortRequested");
                        }

                        // We will try again only once
                        if (!alreadyReconnected && !CurrentRequest.DisableRetry)
                        {
                            alreadyReconnected = true;
                            cause = RetryCauses.Reconnect;
                        }
                        else // rethrow exception
                        {
                            throw ex;
                        }
                    }

                    // If sending out the request succeeded, we will try read the response.
                    if (sentRequest)
                    {
                        bool received = Receive();

                        if (State == HTTPConnectionStates.TimedOut ||
                            State == HTTPConnectionStates.AbortRequested)
                        {
                            throw new Exception("AbortRequested");
                        }

                        if (!received && !alreadyReconnected && !CurrentRequest.DisableRetry)
                        {
                            alreadyReconnected = true;
                            cause = RetryCauses.Reconnect;
                        }

                        if (CurrentRequest.Response != null)
                        {
#if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
                            // Try to store cookies before we do anything else, as we may remove the response deleting the cookies as well.
                            if (CurrentRequest.IsCookiesEnabled)
                            {
                                CookieJar.Set(CurrentRequest.Response);
                            }
#endif

                            switch (CurrentRequest.Response.StatusCode)
                            {
                            // Not authorized
                            // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2
                            case 401:
                            {
                                string authHeader = DigestStore.FindBest(CurrentRequest.Response.GetHeaderValues("www-authenticate"));
                                if (!string.IsNullOrEmpty(authHeader))
                                {
                                    var digest = DigestStore.GetOrCreate(CurrentRequest.CurrentUri);
                                    digest.ParseChallange(authHeader);

                                    if (CurrentRequest.Credentials != null && digest.IsUriProtected(CurrentRequest.CurrentUri) && (!CurrentRequest.HasHeader("Authorization") || digest.Stale))
                                    {
                                        cause = RetryCauses.Authenticate;
                                    }
                                }

                                goto default;
                            }

#if !BESTHTTP_DISABLE_PROXY
                            // Proxy authentication required
                            // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8
                            case 407:
                            {
                                if (CurrentRequest.HasProxy)
                                {
                                    string authHeader = DigestStore.FindBest(CurrentRequest.Response.GetHeaderValues("proxy-authenticate"));
                                    if (!string.IsNullOrEmpty(authHeader))
                                    {
                                        var digest = DigestStore.GetOrCreate(CurrentRequest.Proxy.Address);
                                        digest.ParseChallange(authHeader);

                                        if (CurrentRequest.Proxy.Credentials != null && digest.IsUriProtected(CurrentRequest.Proxy.Address) && (!CurrentRequest.HasHeader("Proxy-Authorization") || digest.Stale))
                                        {
                                            cause = RetryCauses.ProxyAuthenticate;
                                        }
                                    }
                                }

                                goto default;
                            }
#endif

                            // Redirected
                            case 301:     // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2
                            case 302:     // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3
                            case 307:     // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8
                            case 308:     // http://tools.ietf.org/html/rfc7238
                            {
                                if (CurrentRequest.RedirectCount >= CurrentRequest.MaxRedirects)
                                {
                                    goto default;
                                }
                                CurrentRequest.RedirectCount++;

                                string location = CurrentRequest.Response.GetFirstHeaderValue("location");
                                if (!string.IsNullOrEmpty(location))
                                {
                                    Uri redirectUri = GetRedirectUri(location);

                                    if (HTTPManager.Logger.Level == Logger.Loglevels.All)
                                    {
                                        HTTPManager.Logger.Verbose("HTTPConnection", string.Format("{0} - Redirected to Location: '{1}' redirectUri: '{1}'", this.CurrentRequest.CurrentUri.ToString(), location, redirectUri));
                                    }

                                    // Let the user to take some control over the redirection
                                    if (!CurrentRequest.CallOnBeforeRedirection(redirectUri))
                                    {
                                        HTTPManager.Logger.Information("HTTPConnection", "OnBeforeRedirection returned False");
                                        goto default;
                                    }

                                    // Remove the previously set Host header.
                                    CurrentRequest.RemoveHeader("Host");

                                    // Set the Referer header to the last Uri.
                                    CurrentRequest.SetHeader("Referer", CurrentRequest.CurrentUri.ToString());

                                    // Set the new Uri, the CurrentUri will return this while the IsRedirected property is true
                                    CurrentRequest.RedirectUri = redirectUri;

                                    // Discard the redirect response, we don't need it any more
                                    CurrentRequest.Response = null;

                                    redirected = CurrentRequest.IsRedirected = true;
                                }
                                else
#if !NETFX_CORE
                                { throw new MissingFieldException(string.Format("Got redirect status({0}) without 'location' header!", CurrentRequest.Response.StatusCode.ToString())); }
#else
                                { throw new Exception(string.Format("Got redirect status({0}) without 'location' header!", CurrentRequest.Response.StatusCode.ToString())); }
#endif

                                goto default;
                            }


                            default:
#if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
                                TryStoreInCache();
#endif
                                break;
                            }

                            // If we have a response and the server telling us that it closed the connection after the message sent to us, then
                            //  we will close the connection too.
                            bool closeByServer = CurrentRequest.Response == null || CurrentRequest.Response.HasHeaderWithValue("connection", "close");
                            bool closeByClient = !CurrentRequest.Response.IsClosedManually && !CurrentRequest.IsKeepAlive;
                            if (closeByServer || closeByClient)
                            {
                                Close();
                            }
                            else if (CurrentRequest.Response != null)
                            {
                                var keepAliveheaderValues = CurrentRequest.Response.GetHeaderValues("keep-alive");
                                if (keepAliveheaderValues != null && keepAliveheaderValues.Count > 0)
                                {
                                    if (KeepAlive == null)
                                    {
                                        KeepAlive = new KeepAliveHeader();
                                    }
                                    KeepAlive.Parse(keepAliveheaderValues);
                                }
                            }
                        }
                    }
                } while (cause != RetryCauses.None);
            }
            catch (TimeoutException e)
            {
                CurrentRequest.Response  = null;
                CurrentRequest.Exception = e;
                CurrentRequest.State     = HTTPRequestStates.ConnectionTimedOut;

                Close();
            }
            catch (Exception e)
            {
                if (CurrentRequest != null)
                {
#if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
                    if (CurrentRequest.UseStreaming)
                    {
                        HTTPCacheService.DeleteEntity(CurrentRequest.CurrentUri);
                    }
#endif

                    // Something gone bad, Response must be null!
                    CurrentRequest.Response = null;

                    switch (State)
                    {
                    case HTTPConnectionStates.Closed:
                    case HTTPConnectionStates.AbortRequested:
                        CurrentRequest.State = HTTPRequestStates.Aborted;
                        break;

                    case HTTPConnectionStates.TimedOut:
                        CurrentRequest.State = HTTPRequestStates.TimedOut;
                        break;

                    default:
                        CurrentRequest.Exception = e;
                        CurrentRequest.State     = HTTPRequestStates.Error;
                        break;
                    }
                }

                Close();
            }
            finally
            {
                if (CurrentRequest != null)
                {
                    // Avoid state changes. While we are in this block changing the connection's State, on Unity's main thread
                    //  the HTTPManager's OnUpdate will check the connections's State and call functions that can change the inner state of
                    //  the object. (Like setting the CurrentRequest to null in function Recycle() causing a NullRef exception)
                    lock (HTTPManager.Locker)
                    {
                        if (CurrentRequest != null && CurrentRequest.Response != null && CurrentRequest.Response.IsUpgraded)
                        {
                            State = HTTPConnectionStates.Upgraded;
                        }
                        else
                        {
                            State = redirected ? HTTPConnectionStates.Redirected : (Client == null ? HTTPConnectionStates.Closed : HTTPConnectionStates.WaitForRecycle);
                        }

                        // Change the request's state only when the whole processing finished
                        if (CurrentRequest.State == HTTPRequestStates.Processing && (State == HTTPConnectionStates.Closed || State == HTTPConnectionStates.WaitForRecycle))
                        {
                            if (CurrentRequest.Response != null)
                            {
                                CurrentRequest.State = HTTPRequestStates.Finished;
                            }
                            else
                            {
                                CurrentRequest.Exception = new Exception(string.Format("Remote server closed the connection before sending response header! Previous request state: {0}. Connection state: {1}",
                                                                                       CurrentRequest.State.ToString(),
                                                                                       State.ToString()));
                                CurrentRequest.State = HTTPRequestStates.Error;
                            }
                        }

                        if (CurrentRequest.State == HTTPRequestStates.ConnectionTimedOut)
                        {
                            State = HTTPConnectionStates.Closed;
                        }

                        LastProcessTime = DateTime.UtcNow;

                        if (OnConnectionRecycled != null)
                        {
                            RecycleNow();
                        }
                    }

#if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
                    HTTPCacheService.SaveLibrary();
#endif

#if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
                    CookieJar.Persist();
#endif
                }
            }
        }