Exemplo n.º 1
0
        protected bool ReadPayload(int forceReadRawContentLength)
        {
            // Reading from an already unpacked stream (eq. From a file cache)
            if (forceReadRawContentLength != -1)
            {
#if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
                this.IsFromCache = true;
#endif
                ReadRaw(Stream, forceReadRawContentLength);

                return(true);
            }

            //  http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
            //  1.Any response message which "MUST NOT" include a message-body (such as the 1xx, 204, and 304 responses and any response to a HEAD request)
            //      is always terminated by the first empty line after the header fields, regardless of the entity-header fields present in the message.
            if ((StatusCode >= 100 && StatusCode < 200) || StatusCode == 204 || StatusCode == 304 || baseRequest.MethodType == HTTPMethods.Head)
            {
                return(true);
            }

#if (!UNITY_WEBGL || UNITY_EDITOR)
            if (HasHeaderWithValue("transfer-encoding", "chunked"))
            {
                ReadChunked(Stream);
            }
            else
#endif
            {
                //  http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
                //      Case 3 in the above link.
                List <string> contentLengthHeaders = GetHeaderValues("content-length");
                var           contentRangeHeaders  = GetHeaderValues("content-range");
                if (contentLengthHeaders != null && contentRangeHeaders == null)
                {
                    ReadRaw(Stream, int.Parse(contentLengthHeaders[0]));
                }
                else if (contentRangeHeaders != null)
                {
                    if (contentLengthHeaders != null)
                    {
                        ReadRaw(Stream, int.Parse(contentLengthHeaders[0]));
                    }
                    else
                    {
                        HTTPRange range = GetRange();
                        ReadRaw(Stream, (range.LastBytePos - range.FirstBytePos) + 1);
                    }
                }
                else
                {
                    ReadUnknownSize(Stream);
                }
            }

            return(true);
        }
 static int QPYX_GetRange_YXQP(IntPtr L_YXQP)
 {
     try
     {
         ToLua.CheckArgsCount(L_YXQP, 1);
         BestHTTP.HTTPResponse QPYX_obj_YXQP = (BestHTTP.HTTPResponse)ToLua.CheckObject <BestHTTP.HTTPResponse>(L_YXQP, 1);
         BestHTTP.HTTPRange    QPYX_o_YXQP   = QPYX_obj_YXQP.GetRange();
         ToLua.PushSealed(L_YXQP, QPYX_o_YXQP);
         return(1);
     }
     catch (Exception e_YXQP)                {
         return(LuaDLL.toluaL_exception(L_YXQP, e_YXQP));
     }
 }
 static int GetRange(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         BestHTTP.HTTPResponse obj = (BestHTTP.HTTPResponse)ToLua.CheckObject <BestHTTP.HTTPResponse>(L, 1);
         BestHTTP.HTTPRange    o   = obj.GetRange();
         ToLua.PushSealed(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Exemplo n.º 4
0
        internal virtual bool Receive(int forceReadRawContentLength = -1)
        {
            string versionStr = string.Empty;

            // On WP platform we aren't able to determined sure enough whether the tcp connection is closed or not.
            //  So if we get an exception here, we need to recreate the connection.
            try
            {
                // Read out 'HTTP/1.1' from the "HTTP/1.1 {StatusCode} {Message}"
                versionStr = ReadTo(Stream, (byte)' ');
            }
            catch (Exception e)
            {
                if (!baseRequest.DisableRetry)
                {
                    return(false);
                }

                throw e;
            }

            if (!baseRequest.DisableRetry && string.IsNullOrEmpty(versionStr))
            {
                return(false);
            }

            string[] versions = versionStr.Split(new char[] { '/', '.' });
            this.VersionMajor = int.Parse(versions[1]);
            this.VersionMinor = int.Parse(versions[2]);

            int    statusCode;
            string statusCodeStr = ReadTo(Stream, (byte)' ');

            if (baseRequest.DisableRetry)
            {
                statusCode = int.Parse(statusCodeStr);
            }
            else if (!int.TryParse(statusCodeStr, out statusCode))
            {
                return(false);
            }

            this.StatusCode = statusCode;

            this.Message = ReadTo(Stream, LF);

            //Read Headers
            ReadHeaders(Stream);

            IsUpgraded = StatusCode == 101 && HasHeaderWithValue("connection", "upgrade");

            // Reading from an already unpacked stream (eq. From a file cache)
            if (forceReadRawContentLength != -1)
            {
                this.IsFromCache = true;
                ReadRaw(Stream, forceReadRawContentLength);

                return(true);
            }

            //  http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
            //  1.Any response message which "MUST NOT" include a message-body (such as the 1xx, 204, and 304 responses and any response to a HEAD request)
            //      is always terminated by the first empty line after the header fields, regardless of the entity-header fields present in the message.
            if ((StatusCode >= 100 && StatusCode < 200) || StatusCode == 204 || StatusCode == 304 || baseRequest.MethodType == HTTPMethods.Head)
            {
                return(true);
            }

            if (HasHeaderWithValue("transfer-encoding", "chunked"))
            {
                ReadChunked(Stream);
            }
            else
            {
                //  http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
                //      Case 3 in the above link.
                List <string> contentLengthHeaders = GetHeaderValues("content-length");
                var           contentRangeHeaders  = GetHeaderValues("content-range");
                if (contentLengthHeaders != null && contentRangeHeaders == null)
                {
                    ReadRaw(Stream, int.Parse(contentLengthHeaders[0]));
                }
                else if (contentRangeHeaders != null)
                {
                    HTTPRange range = GetRange();
                    ReadRaw(Stream, (range.LastBytePos - range.FirstBytePos) + 1);
                }
                else
                {
                    ReadUnknownSize(Stream);
                }
            }

            return(true);
        }
Exemplo n.º 5
0
        internal virtual bool Receive(int forceReadRawContentLength = -1, bool readPayloadData = true)
        {
            string text = string.Empty;

            try
            {
                text = HTTPResponse.ReadTo(this.Stream, 32);
            }
            catch
            {
                if (!this.baseRequest.DisableRetry)
                {
                    bool result = false;
                    return(result);
                }
                throw;
            }
            if (!this.baseRequest.DisableRetry && string.IsNullOrEmpty(text))
            {
                return(false);
            }
            string[] array = text.Split(new char[]
            {
                '/',
                '.'
            });
            this.VersionMajor = int.Parse(array[1]);
            this.VersionMinor = int.Parse(array[2]);
            string text2 = HTTPResponse.NoTrimReadTo(this.Stream, 32, 10);
            int    statusCode;

            if (this.baseRequest.DisableRetry)
            {
                statusCode = int.Parse(text2);
            }
            else if (!int.TryParse(text2, out statusCode))
            {
                return(false);
            }
            this.StatusCode = statusCode;
            if (text2.Length > 0 && (byte)text2[text2.Length - 1] != 10 && (byte)text2[text2.Length - 1] != 13)
            {
                this.Message = HTTPResponse.ReadTo(this.Stream, 10);
            }
            else
            {
                this.Message = string.Empty;
            }
            this.ReadHeaders(this.Stream);
            this.IsUpgraded = (this.StatusCode == 101 && (this.HasHeaderWithValue("connection", "upgrade") || this.HasHeader("upgrade")));
            if (!readPayloadData)
            {
                return(true);
            }
            if (forceReadRawContentLength != -1)
            {
                this.IsFromCache = true;
                this.ReadRaw(this.Stream, forceReadRawContentLength);
                return(true);
            }
            if ((this.StatusCode >= 100 && this.StatusCode < 200) || this.StatusCode == 204 || this.StatusCode == 304 || this.baseRequest.MethodType == HTTPMethods.Head)
            {
                return(true);
            }
            if (this.HasHeaderWithValue("transfer-encoding", "chunked"))
            {
                this.ReadChunked(this.Stream);
            }
            else
            {
                List <string> headerValues  = this.GetHeaderValues("content-length");
                List <string> headerValues2 = this.GetHeaderValues("content-range");
                if (headerValues != null && headerValues2 == null)
                {
                    this.ReadRaw(this.Stream, int.Parse(headerValues[0]));
                }
                else if (headerValues2 != null)
                {
                    HTTPRange range = this.GetRange();
                    this.ReadRaw(this.Stream, range.LastBytePos - range.FirstBytePos + 1);
                }
                else
                {
                    this.ReadUnknownSize(this.Stream);
                }
            }
            return(true);
        }
Exemplo n.º 6
0
        internal virtual bool Receive(int forceReadRawContentLength = -1)
        {
            string empty = string.Empty;

            try
            {
                empty = ReadTo(Stream, 32);
            }
            catch (Exception ex)
            {
                if (baseRequest.DisableRetry)
                {
                    throw ex;
                }
                return(false);

                IL_0035 :;
            }
            if (!baseRequest.DisableRetry && string.IsNullOrEmpty(empty))
            {
                return(false);
            }
            string[] array = empty.Split('/', '.');
            VersionMajor = int.Parse(array[1]);
            VersionMinor = int.Parse(array[2]);
            string s = ReadTo(Stream, 32);
            int    result;

            if (baseRequest.DisableRetry)
            {
                result = int.Parse(s);
            }
            else if (!int.TryParse(s, out result))
            {
                return(false);
            }
            StatusCode = result;
            Message    = ReadTo(Stream, 10);
            ReadHeaders(Stream);
            IsUpgraded = (StatusCode == 101 && HasHeaderWithValue("connection", "upgrade"));
            if (forceReadRawContentLength != -1)
            {
                IsFromCache = true;
                ReadRaw(Stream, forceReadRawContentLength);
                return(true);
            }
            if ((StatusCode >= 100 && StatusCode < 200) || StatusCode == 204 || StatusCode == 304 || baseRequest.MethodType == HTTPMethods.Head)
            {
                return(true);
            }
            if (HasHeaderWithValue("transfer-encoding", "chunked"))
            {
                ReadChunked(Stream);
            }
            else
            {
                List <string> headerValues  = GetHeaderValues("content-length");
                List <string> headerValues2 = GetHeaderValues("content-range");
                if (headerValues != null && headerValues2 == null)
                {
                    ReadRaw(Stream, int.Parse(headerValues[0]));
                }
                else if (headerValues2 != null)
                {
                    HTTPRange range = GetRange();
                    ReadRaw(Stream, range.LastBytePos - range.FirstBytePos + 1);
                }
            }
            return(true);
        }