123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Threading;
- using BestHTTP.Extensions;
- using BestHTTP.Authentication;
- #if (!NETFX_CORE && !UNITY_WP8) || UNITY_EDITOR
- using System.Net.Security;
- #endif
- #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
- using BestHTTP.Caching;
- #endif
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using Org.BouncyCastle.Crypto.Tls;
- #endif
- #if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
- using BestHTTP.Cookies;
- #endif
- #if NETFX_CORE || BUILD_FOR_WP8
- using System.Threading.Tasks;
- using Windows.Networking.Sockets;
- using TcpClient = BestHTTP.PlatformSupport.TcpClient.WinRT.TcpClient;
-
- #pragma warning disable 4014
- #elif UNITY_WP8 && !UNITY_EDITOR
- using TcpClient = BestHTTP.PlatformSupport.TcpClient.WP8.TcpClient;
- #else
- using TcpClient = BestHTTP.PlatformSupport.TcpClient.General.TcpClient;
- #endif
- namespace BestHTTP
- {
-
-
-
-
- internal sealed class KeepAliveHeader
- {
-
-
-
- public TimeSpan TimeOut { get; private set; }
-
-
-
- public int MaxRequests { get; private set; }
- public void Parse(List<string> headerValues)
- {
- HeaderParser parser = new HeaderParser(headerValues[0]);
- HeaderValue value;
- if (parser.TryGet("timeout", out value) && value.HasValue)
- {
- int intValue = 0;
- if (int.TryParse(value.Value, out intValue))
- this.TimeOut = TimeSpan.FromSeconds(intValue);
- else
- this.TimeOut = TimeSpan.MaxValue;
- }
- if (parser.TryGet("max", out value) && value.HasValue)
- {
- int intValue = 0;
- if (int.TryParse("max", out intValue))
- this.MaxRequests = intValue;
- else
- this.MaxRequests = int.MaxValue;
- }
- }
- }
- internal enum RetryCauses
- {
-
-
-
- None,
-
-
-
- Reconnect,
-
-
-
- Authenticate,
- #if !BESTHTTP_DISABLE_PROXY
-
-
-
- ProxyAuthenticate,
- #endif
- }
-
-
-
- internal sealed class HTTPConnection : ConnectionBase
- {
- public override bool IsRemovable
- {
- get
- {
-
- if (base.IsRemovable)
- return true;
-
- if (IsFree && KeepAlive != null && (DateTime.UtcNow - base.LastProcessTime) >= KeepAlive.TimeOut)
- return true;
- return false;
- }
- }
- #region Private Properties
- private TcpClient Client;
- private Stream Stream;
- private KeepAliveHeader KeepAlive;
- #endregion
- internal HTTPConnection(string serverAddress)
- :base(serverAddress)
- {}
- #region Request Processing Implementation
- protected override
- #if NETFX_CORE
- async
- #endif
- 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)
-
- if (TryLoadAllFromCache())
- return;
- #endif
- if (Client != null && !Client.IsConnected())
- Close();
- do
- {
- if (cause == RetryCauses.Reconnect)
- {
- Close();
- #if NETFX_CORE
- await Task.Delay(100);
- #else
- Thread.Sleep(100);
- #endif
- }
- LastProcessedUri = CurrentRequest.CurrentUri;
- cause = RetryCauses.None;
-
- Connect();
- if (State == HTTPConnectionStates.AbortRequested)
- throw new Exception("AbortRequested");
- #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
-
- if (!CurrentRequest.DisableCache)
- HTTPCacheService.SetHeaders(CurrentRequest);
- #endif
-
-
- 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");
-
- if (!alreadyReconnected && !CurrentRequest.DisableRetry)
- {
- alreadyReconnected = true;
- cause = RetryCauses.Reconnect;
- }
- else
- throw ex;
- }
-
- 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)
-
- if (CurrentRequest.IsCookiesEnabled)
- CookieJar.Set(CurrentRequest.Response);
- #endif
- switch (CurrentRequest.Response.StatusCode)
- {
-
-
- 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
-
-
- 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
-
- case 301:
- case 302:
- case 307:
- case 308:
- {
- 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));
-
- if (!CurrentRequest.CallOnBeforeRedirection(redirectUri))
- {
- HTTPManager.Logger.Information("HTTPConnection", "OnBeforeRedirection returned False");
- goto default;
- }
-
- CurrentRequest.RemoveHeader("Host");
-
- CurrentRequest.SetHeader("Referer", CurrentRequest.CurrentUri.ToString());
-
- CurrentRequest.RedirectUri = redirectUri;
-
- 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 (CurrentRequest.Response == null || !CurrentRequest.Response.IsClosedManually) {
-
-
- bool closeByServer = CurrentRequest.Response == null || CurrentRequest.Response.HasHeaderWithValue("connection", "close");
- bool closeByClient = !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
-
- 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)
- {
-
-
-
- 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);
-
- 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
- }
- }
- }
- 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
- if (HTTPManager.Logger.Level == Logger.Loglevels.All)
- HTTPManager.Logger.Verbose("HTTPConnection", string.Format("'{0}' - Connecting to {1}:{2}", this.CurrentRequest.CurrentUri.ToString(), uri.Host, uri.Port.ToString()));
- #if !NETFX_CORE && (!UNITY_WEBGL || UNITY_EDITOR)
- Client.SendBufferSize = HTTPManager.SendBufferSize;
- Client.ReceiveBufferSize = HTTPManager.ReceiveBufferSize;
- if (HTTPManager.Logger.Level == Logger.Loglevels.All)
- HTTPManager.Logger.Verbose("HTTPConnection", string.Format("'{0}' - Buffer sizes - Send: {1} Receive: {2} Blocking: {3}", this.CurrentRequest.CurrentUri.ToString(), Client.SendBufferSize.ToString(), Client.ReceiveBufferSize.ToString(), Client.Client.Blocking.ToString()));
- #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
- StartTime = DateTime.UtcNow;
- if (Stream == null)
- {
- bool isSecure = HTTPProtocolFactory.IsSecureProtocol(CurrentRequest.CurrentUri);
- Stream = Client.GetStream();
-
- #if !BESTHTTP_DISABLE_PROXY
- #region Proxy Handling
- if (HasProxy && (!Proxy.IsTransparent || (isSecure && Proxy.NonTransparentForHTTPS)))
- {
- var outStream = new BinaryWriter(new WriteOnlyBufferedStream(Stream, HTTPRequest.UploadChunkSize));
- bool retry;
- do
- {
-
- retry = false;
- string connectStr = string.Format("CONNECT {0}:{1} HTTP/1.1", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port);
- HTTPManager.Logger.Information("HTTPConnection", "Sending " + connectStr);
- outStream.SendAsASCII(connectStr);
- 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);
-
- if (HasProxy && Proxy.Credentials != null)
- {
- switch (Proxy.Credentials.Type)
- {
- case AuthenticationTypes.Basic:
-
- 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, true);
- if (!string.IsNullOrEmpty(authentication))
- {
- string auth = string.Format("Proxy-Authorization: {0}", authentication);
- if (HTTPManager.Logger.Level <= Logger.Loglevels.Information)
- HTTPManager.Logger.Information("HTTPConnection", "Sending proxy authorization header: " + auth);
- outStream.Write(auth.GetASCIIBytes());
- outStream.Write(HTTPRequest.EOL);
- }
- }
- break;
- }
- }
- outStream.Write(HTTPRequest.EOL);
-
- outStream.Flush();
- CurrentRequest.ProxyResponse = new HTTPResponse(CurrentRequest, Stream, false, false);
-
- 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 + " Body: " + CurrentRequest.ProxyResponse.DataAsText);
- switch(CurrentRequest.ProxyResponse.StatusCode)
- {
-
-
- 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
-
- if (isSecure)
- {
-
-
- #if !NETFX_CORE && (!UNITY_WEBGL || UNITY_EDITOR) && NET_4_6
-
- #endif
- #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());
-
-
-
-
- List<string> hostNames = CurrentRequest.CustomTLSServerNameList;
-
- if ((hostNames == null || hostNames.Count == 0) && !CurrentRequest.CurrentUri.IsHostIsAnIPAddress())
- {
- 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
- }
- }
- }
- private bool Receive()
- {
- SupportedProtocols protocol = CurrentRequest.ProtocolHandler == SupportedProtocols.Unknown ? HTTPProtocolFactory.GetProtocolFromUri(CurrentRequest.CurrentUri) : CurrentRequest.ProtocolHandler;
- if (HTTPManager.Logger.Level == Logger.Loglevels.All)
- HTTPManager.Logger.Verbose("HTTPConnection", string.Format("{0} - Receive - protocol: {1}", this.CurrentRequest.CurrentUri.ToString(), protocol.ToString()));
- CurrentRequest.Response = HTTPProtocolFactory.Get(protocol, CurrentRequest, Stream, CurrentRequest.UseStreaming, false);
- if (!CurrentRequest.Response.Receive())
- {
- if (HTTPManager.Logger.Level == Logger.Loglevels.All)
- HTTPManager.Logger.Verbose("HTTPConnection", string.Format("{0} - Receive - Failed! Response will be null, returning with false.", this.CurrentRequest.CurrentUri.ToString()));
- CurrentRequest.Response = null;
- return false;
- }
- if (CurrentRequest.Response.StatusCode == 304
- #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
- && !CurrentRequest.DisableCache
- #endif
- )
- {
- #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
- if (CurrentRequest.IsRedirected)
- {
- if (!LoadFromCache(CurrentRequest.RedirectUri))
- LoadFromCache(CurrentRequest.Uri);
- }
- else
- LoadFromCache(CurrentRequest.Uri);
- #else
- return false;
- #endif
- }
- if (HTTPManager.Logger.Level == Logger.Loglevels.All)
- HTTPManager.Logger.Verbose("HTTPConnection", string.Format("{0} - Receive - Finished Successfully!", this.CurrentRequest.CurrentUri.ToString()));
- return true;
- }
- #endregion
- #region Helper Functions
- #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
- private bool LoadFromCache(Uri uri)
- {
- if (HTTPManager.Logger.Level == Logger.Loglevels.All)
- HTTPManager.Logger.Verbose("HTTPConnection", string.Format("{0} - LoadFromCache for Uri: {1}", this.CurrentRequest.CurrentUri.ToString(), uri.ToString()));
- var cacheEntity = HTTPCacheService.GetEntity(uri);
- if (cacheEntity == null)
- {
- HTTPManager.Logger.Warning("HTTPConnection", string.Format("{0} - LoadFromCache for Uri: {1} - Cached entity not found!", this.CurrentRequest.CurrentUri.ToString(), uri.ToString()));
- return false;
- }
- CurrentRequest.Response.CacheFileInfo = cacheEntity;
- int bodyLength;
- using (var cacheStream = cacheEntity.GetBodyStream(out bodyLength))
- {
- if (cacheStream == null)
- return false;
- if (!CurrentRequest.Response.HasHeader("content-length"))
- CurrentRequest.Response.Headers.Add("content-length", new List<string>(1) { bodyLength.ToString() });
- CurrentRequest.Response.IsFromCache = true;
- if (!CurrentRequest.CacheOnly)
- CurrentRequest.Response.ReadRaw(cacheStream, bodyLength);
- }
- return true;
- }
- private bool TryLoadAllFromCache()
- {
- if (CurrentRequest.DisableCache || !HTTPCacheService.IsSupported)
- return false;
-
- try
- {
-
-
-
- if (HTTPCacheService.IsCachedEntityExpiresInTheFuture(CurrentRequest))
- {
- if (HTTPManager.Logger.Level == Logger.Loglevels.All)
- HTTPManager.Logger.Verbose("HTTPConnection", string.Format("{0} - TryLoadAllFromCache - whole response loading from cache", this.CurrentRequest.CurrentUri.ToString()));
- CurrentRequest.Response = HTTPCacheService.GetFullResponse(CurrentRequest);
- if (CurrentRequest.Response != null)
- return true;
- }
- }
- catch
- {
- HTTPCacheService.DeleteEntity(CurrentRequest.CurrentUri);
- }
- return false;
- }
- #endif
- #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
- private void TryStoreInCache()
- {
-
- if (!CurrentRequest.UseStreaming &&
- !CurrentRequest.DisableCache &&
- CurrentRequest.Response != null &&
- HTTPCacheService.IsSupported &&
- HTTPCacheService.IsCacheble(CurrentRequest.CurrentUri, CurrentRequest.MethodType, CurrentRequest.Response))
- {
- if(CurrentRequest.IsRedirected)
- HTTPCacheService.Store(CurrentRequest.Uri, CurrentRequest.MethodType, CurrentRequest.Response);
- else
- HTTPCacheService.Store(CurrentRequest.CurrentUri, CurrentRequest.MethodType, CurrentRequest.Response);
- }
- }
- #endif
- private Uri GetRedirectUri(string location)
- {
- Uri result = null;
- try
- {
- result = new Uri(location);
- if (result.IsFile || result.AbsolutePath == location)
- result = null;
- }
- #if !NETFX_CORE
- catch (UriFormatException)
- #else
- catch
- #endif
- {
-
- result = null;
- }
- if (result == null)
- {
- var uri = CurrentRequest.Uri;
- var builder = new UriBuilder(uri.Scheme, uri.Host, uri.Port, location);
- result = builder.Uri;
- }
- return result;
- }
- internal override void Abort(HTTPConnectionStates newState)
- {
- State = newState;
- switch(State)
- {
- case HTTPConnectionStates.TimedOut: TimedOutStart = DateTime.UtcNow; break;
- }
- if (Stream != null)
- {
- try
- {
- Stream.Dispose();
- }
- catch
- { }
- }
- }
- private void Close()
- {
- KeepAlive = null;
- LastProcessedUri = null;
- if (Client != null)
- {
- try
- {
- Client.Close();
- }
- catch
- {
- }
- finally
- {
- Stream = null;
- Client = null;
- }
- }
- }
- #endregion
- protected override void Dispose(bool disposing)
- {
- Close();
- base.Dispose(disposing);
- }
- }
- }
|