Beispiel #1
0
        private bool Receive()
        {
            CurrentRequest.Response = HTTPProtocolFactory.Get(HTTPProtocolFactory.GetProtocolFromUri(CurrentRequest.CurrentUri), CurrentRequest, Stream, CurrentRequest.UseStreaming, false);

            if (!CurrentRequest.Response.Receive())
            {
                CurrentRequest.Response = null;
                return(false);
            }

            // We didn't check HTTPManager.IsCachingDisabled's value on purpose. (sending out a request with conditional get then change IsCachingDisabled to true may produce undefined behavior)
            if (CurrentRequest.Response.StatusCode == 304)
            {
                int bodyLength;
                using (var cacheStream = HTTPCacheService.GetBody(CurrentRequest.CurrentUri, out bodyLength))
                {
                    if (!CurrentRequest.Response.HasHeader("content-length"))
                    {
                        CurrentRequest.Response.Headers.Add("content-length", new List <string>(1)
                        {
                            bodyLength.ToString()
                        });
                    }
                    CurrentRequest.Response.IsFromCache = true;
                    CurrentRequest.Response.ReadRaw(cacheStream, bodyLength);
                }
            }

            return(true);
        }
 private bool Receive()
 {
     CurrentRequest.Response = HTTPProtocolFactory.Get(HTTPProtocolFactory.GetProtocolFromUri(CurrentRequest.CurrentUri), CurrentRequest, Stream, CurrentRequest.UseStreaming, false);
     if (!CurrentRequest.Response.Receive(-1))
     {
         CurrentRequest.Response = null;
         return(false);
     }
     if (CurrentRequest.Response.StatusCode == 304)
     {
         int length;
         using (Stream stream = HTTPCacheService.GetBody(CurrentRequest.CurrentUri, out length))
         {
             if (!CurrentRequest.Response.HasHeader("content-length"))
             {
                 CurrentRequest.Response.Headers.Add("content-length", new List <string>
                 {
                     length.ToString()
                 });
             }
             CurrentRequest.Response.ReadRaw(stream, length);
         }
     }
     return(true);
 }
        private bool Receive()
        {
            SupportedProtocols protocol = CurrentRequest.ProtocolHandler == SupportedProtocols.Unknown ? HTTPProtocolFactory.GetProtocolFromUri(CurrentRequest.CurrentUri) : CurrentRequest.ProtocolHandler;

            CurrentRequest.Response = HTTPProtocolFactory.Get(protocol, CurrentRequest, Stream, CurrentRequest.UseStreaming, false);

            if (!CurrentRequest.Response.Receive())
            {
                CurrentRequest.Response = null;
                return(false);
            }

            // We didn't check HTTPManager.IsCachingDisabled's value on purpose. (sending out a request with conditional get then change IsCachingDisabled to true may produce undefined behavior)
            if (CurrentRequest.Response.StatusCode == 304)
            {
#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
            }

            return(true);
        }
 private bool Receive()
 {
     this.CurrentRequest.Response = HTTPProtocolFactory.Get(HTTPProtocolFactory.GetProtocolFromUri(this.CurrentRequest.CurrentUri), this.CurrentRequest, this.Stream, this.CurrentRequest.UseStreaming, false);
     if (!this.CurrentRequest.Response.Receive(-1, true))
     {
         this.CurrentRequest.Response = null;
         return(false);
     }
     if (this.CurrentRequest.Response.StatusCode == 304)
     {
         int contentLength;
         using (Stream body = HTTPCacheService.GetBody(this.CurrentRequest.CurrentUri, out contentLength))
         {
             if (!this.CurrentRequest.Response.HasHeader("content-length"))
             {
                 this.CurrentRequest.Response.Headers.Add("content-length", new List <string>(1)
                 {
                     contentLength.ToString()
                 });
             }
             this.CurrentRequest.Response.IsFromCache = true;
             this.CurrentRequest.Response.ReadRaw(body, contentLength);
         }
     }
     return(true);
 }
Beispiel #5
0
        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
                && !CurrentRequest.DisableCache
#endif
                )
            {
#if !BESTHTTP_DISABLE_CACHING
                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);
        }
Beispiel #6
0
        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);
            }

            // We didn't check HTTPManager.IsCachingDisabled's value on purpose. (sending out a request with conditional get then change IsCachingDisabled to true may produce undefined behavior)
            if (CurrentRequest.Response.StatusCode == 304)
            {
#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);
        }
        private bool Receive()
        {
            SupportedProtocols protocol = (base.CurrentRequest.ProtocolHandler != SupportedProtocols.Unknown) ? base.CurrentRequest.ProtocolHandler : HTTPProtocolFactory.GetProtocolFromUri(base.CurrentRequest.CurrentUri);

            if (HTTPManager.Logger.Level == Loglevels.All)
            {
                HTTPManager.Logger.Verbose("HTTPConnection", $"{base.CurrentRequest.CurrentUri.ToString()} - Receive - protocol: {protocol.ToString()}");
            }
            base.CurrentRequest.Response = HTTPProtocolFactory.Get(protocol, base.CurrentRequest, this.Stream, base.CurrentRequest.UseStreaming, false);
            if (!base.CurrentRequest.Response.Receive(-1, true))
            {
                if (HTTPManager.Logger.Level == Loglevels.All)
                {
                    HTTPManager.Logger.Verbose("HTTPConnection", $"{base.CurrentRequest.CurrentUri.ToString()} - Receive - Failed! Response will be null, returning with false.");
                }
                base.CurrentRequest.Response = null;
                return(false);
            }
            if ((base.CurrentRequest.Response.StatusCode == 0x130) && !base.CurrentRequest.DisableCache)
            {
                if (base.CurrentRequest.IsRedirected)
                {
                    if (!this.LoadFromCache(base.CurrentRequest.RedirectUri))
                    {
                        this.LoadFromCache(base.CurrentRequest.Uri);
                    }
                }
                else
                {
                    this.LoadFromCache(base.CurrentRequest.Uri);
                }
            }
            if (HTTPManager.Logger.Level == Loglevels.All)
            {
                HTTPManager.Logger.Verbose("HTTPConnection", $"{base.CurrentRequest.CurrentUri.ToString()} - Receive - Finished Successfully!");
            }
            return(true);
        }
Beispiel #8
0
        private bool Receive()
        {
            SupportedProtocols protocol = CurrentRequest.ProtocolHandler == SupportedProtocols.Unknown ? HTTPProtocolFactory.GetProtocolFromUri(CurrentRequest.CurrentUri) : CurrentRequest.ProtocolHandler;

            CurrentRequest.Response = HTTPProtocolFactory.Get(protocol, CurrentRequest, Stream, CurrentRequest.UseStreaming, false);

            if (!CurrentRequest.Response.Receive())
            {
                CurrentRequest.Response = null;
                return(false);
            }

            // We didn't check HTTPManager.IsCachingDisabled's value on purpose. (sending out a request with conditional get then change IsCachingDisabled to true may produce undefined behavior)
            if (CurrentRequest.Response.StatusCode == 304)
            {
#if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
                int bodyLength;
                using (var cacheStream = HTTPCacheService.GetBody(CurrentRequest.CurrentUri, out bodyLength))
                {
                    if (!CurrentRequest.Response.HasHeader("content-length"))
                    {
                        CurrentRequest.Response.Headers.Add("content-length", new List <string>(1)
                        {
                            bodyLength.ToString()
                        });
                    }
                    CurrentRequest.Response.IsFromCache = true;
                    CurrentRequest.Response.ReadRaw(cacheStream, bodyLength);
                }
#else
                return(false);
#endif
            }

            return(true);
        }
Beispiel #9
0
        void OnResponse(int httpStatus, byte[] buffer)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    Stream = ms;

                    XHR_GetStatusLine(NativeId, OnBufferCallback);
                    XHR_GetResponseHeaders(NativeId, OnBufferCallback);

                    if (buffer != null && buffer.Length > 0)
                    {
                        ms.Write(buffer, 0, buffer.Length);
                    }

                    ms.Seek(0L, SeekOrigin.Begin);

                    SupportedProtocols protocol = CurrentRequest.ProtocolHandler == SupportedProtocols.Unknown ? HTTPProtocolFactory.GetProtocolFromUri(CurrentRequest.CurrentUri) : CurrentRequest.ProtocolHandler;
                    CurrentRequest.Response = HTTPProtocolFactory.Get(protocol, CurrentRequest, ms, CurrentRequest.UseStreaming, false);

                    CurrentRequest.Response.Receive(buffer != null && buffer.Length > 0 ? (int)buffer.Length : -1, true);
                }
            }
            catch (Exception e)
            {
                HTTPManager.Logger.Exception(this.NativeId + " WebGLConnection", "OnResponse", e);

                if (CurrentRequest != null)
                {
                    // 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;
                    }
                }
            }
            finally
            {
                Connections.Remove(NativeId);

                Stream = null;

                if (CurrentRequest != null)
                {
                    lock (HTTPManager.Locker)
                    {
                        State = HTTPConnectionStates.Closed;
                        if (CurrentRequest.State == HTTPRequestStates.Processing)
                        {
                            if (CurrentRequest.Response != null)
                            {
                                CurrentRequest.State = HTTPRequestStates.Finished;
                            }
                            else
                            {
                                CurrentRequest.State = HTTPRequestStates.Error;
                            }
                        }
                    }
                }

                LastProcessTime = DateTime.UtcNow;

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

                XHR_Release(NativeId);
            }
        }
Beispiel #10
0
        void OnResponse(int httpStatus, byte[] buffer, int bufferLength)
        {
            try
            {
                using (var ms = new BufferPoolMemoryStream())
                {
                    Stream = ms;

                    XHR_GetStatusLine(NativeId, OnBufferCallback);
                    XHR_GetResponseHeaders(NativeId, OnBufferCallback);

                    if (buffer != null && bufferLength > 0)
                    {
                        ms.Write(buffer, 0, bufferLength);
                    }

                    ms.Seek(0L, SeekOrigin.Begin);

                    var    internalBuffer = ms.GetBuffer();
                    string tmp            = System.Text.Encoding.UTF8.GetString(internalBuffer);
                    HTTPManager.Logger.Information(this.NativeId + " OnResponse - full response ", tmp);

                    SupportedProtocols protocol = CurrentRequest.ProtocolHandler == SupportedProtocols.Unknown ? HTTPProtocolFactory.GetProtocolFromUri(CurrentRequest.CurrentUri) : CurrentRequest.ProtocolHandler;
                    CurrentRequest.Response = HTTPProtocolFactory.Get(protocol, CurrentRequest, ms, CurrentRequest.UseStreaming, false);

                    CurrentRequest.Response.Receive(buffer != null && bufferLength > 0 ? (int)bufferLength : -1, true);

#if !BESTHTTP_DISABLE_COOKIES
                    if (CurrentRequest.IsCookiesEnabled)
                    {
                        BestHTTP.Cookies.CookieJar.Set(CurrentRequest.Response);
                    }
#endif
                }
            }
            catch (Exception e)
            {
                HTTPManager.Logger.Exception(this.NativeId + " WebGLConnection", "OnResponse", e);

                if (CurrentRequest != null)
                {
                    // 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;
                    }
                }
            }
            finally
            {
                Connections.Remove(NativeId);

                Stream = null;

                if (CurrentRequest != null)
                {
                    lock (HTTPManager.Locker)
                    {
                        State = HTTPConnectionStates.Closed;
                        if (CurrentRequest.State == HTTPRequestStates.Processing)
                        {
                            if (CurrentRequest.Response != null)
                            {
                                CurrentRequest.State = HTTPRequestStates.Finished;
                            }
                            else
                            {
                                CurrentRequest.State = HTTPRequestStates.Error;
                            }
                        }
                    }
                }

                LastProcessTime = DateTime.UtcNow;

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

                XHR_Release(NativeId);
            }
        }