Inheritance: System.Net.Cache.BaseWrapperStream, ICloseEx
        //
        private void CheckRetrieveOnResponse(Stream responseStream) {

            GlobalLog.Assert(_ProtocolStatus == CacheValidationStatus.Continue || _ProtocolStatus == CacheValidationStatus.RetryResponseFromServer, "CheckRetrieveOnResponse()|Unexpected _ProtocolStatus = ", _ProtocolStatus);
            // if something goes wrong we release cache stream if any exists
            bool closeCacheStream = true;

            try {
                // This will inspect the live response on the correctness matter
                switch (_ProtocolStatus = ValidateResponse()) {

                case CacheValidationStatus.Continue:
                        closeCacheStream = false;
                        // The response looks good
                        break;

                case CacheValidationStatus.RetryResponseFromServer:
                        // The response is broken will need to retry or give up
                        closeCacheStream = false;
                        break;

                case CacheValidationStatus.Fail:
                        _ProtocolStatus = CacheValidationStatus.Fail;
                        _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_fail, "ValidateResponse"));
                        break;

                case CacheValidationStatus.DoNotUseCache:
                        break;

                default:
                    _ProtocolStatus = CacheValidationStatus.Fail;
                    _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_result, "ValidateResponse", _Validator.ValidationStatus.ToString()));
                    if(Logging.On) Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_unexpected_status, "ValidateResponse()", _Validator.ValidationStatus.ToString()));
                    break;
                }

            }
            catch (Exception e) {
                closeCacheStream = true;
                _ProtocolException = e;
                _ProtocolStatus = CacheValidationStatus.Fail;
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                    throw;
                }
                if(Logging.On) Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_object_and_exception, "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (e is WebException? e.Message: e.ToString())));
            }
            finally {
                // This is to release cache entry in case we are not interested in it
                if (closeCacheStream && _ResponseStream != null)
                {
                    _ResponseStream.Close();
                    _ResponseStream = null;
                    _Validator.CacheStream = Stream.Null;
                }
            }

            if (_ProtocolStatus != CacheValidationStatus.Continue) {
                return;
            }

            //
            // only CacheValidationStatus.Continue goes here with closeCacheStream == false
            //

            try {
                // This will tell us what to do next
                // Note this is a second time question to the caching protocol about a cached entry
                // Except that we now have live response to consider
                //
                // The validator can at any time replace the  cache stream and update cache Metadata (aka headers).
                //
                switch (_ProtocolStatus = RevalidateCache()) {

                case CacheValidationStatus.DoNotUseCache:
                case CacheValidationStatus.RemoveFromCache:
                case CacheValidationStatus.DoNotTakeFromCache:
                        closeCacheStream = true;
                        break;

                case CacheValidationStatus.ReturnCachedResponse:
                        if (_Validator.CacheStream == null || _Validator.CacheStream == Stream.Null)
                        {
                            _ProtocolStatus = CacheValidationStatus.Fail;
                            _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_no_stream, _Validator.CacheKey));
                            if(Logging.On) Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_null_cached_stream, "RevalidateCache()"));
                            break;
                        }

                        Stream stream = _Validator.CacheStream;

                        if (_Validator.CacheStreamOffset != 0L || _Validator.CacheStreamLength != _Validator.CacheEntry.StreamSize)
                        {
                            stream =  new RangeStream(stream, _Validator.CacheStreamOffset, _Validator.CacheStreamLength);
                            if(Logging.On) Logging.PrintInfo(Logging.RequestCache, SR.GetString(SR.net_log_cache_returned_range_cache, "RevalidateCache()", _Validator.CacheStreamOffset, _Validator.CacheStreamLength));
                        }
                        _ResponseStream = stream;
                        _ResponseStreamLength = _Validator.CacheStreamLength;
                        break;

                case CacheValidationStatus.CombineCachedAndServerResponse:

                        if (_Validator.CacheStream == null || _Validator.CacheStream == Stream.Null)
                        {
                            _ProtocolStatus = CacheValidationStatus.Fail;
                            _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_no_stream, _Validator.CacheKey));
                            if(Logging.On) Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_requested_combined_but_null_cached_stream, "RevalidateCache()"));
                            break;
                        }
                        //
                        // FTP cannot give the tail of the combined stream at that point
                        // Consider: Revisit the design of the CacheProtocol class
                        if (responseStream != null)
                        {
                            stream = new CombinedReadStream(_Validator.CacheStream, responseStream);
                        }
                        else
                        {
                            // So Abort can close the cache stream
                            stream = _Validator.CacheStream;
                        }
                        _ResponseStream = stream;
                        _ResponseStreamLength = _Validator.CacheStreamLength;
                        break;


                case CacheValidationStatus.Fail:
                        closeCacheStream = true;
                        _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_fail, "RevalidateCache"));
                        break;

                default:
                        closeCacheStream = true;
                        _ProtocolStatus = CacheValidationStatus.Fail;
                        _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_result, "RevalidateCache", _Validator.ValidationStatus.ToString()));
                        if(Logging.On) Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_unexpected_status, "RevalidateCache()", _Validator.ValidationStatus.ToString()));
                        break;
                }
            }
            catch (Exception e) {
                closeCacheStream = true;
                _ProtocolException = e;
                _ProtocolStatus = CacheValidationStatus.Fail;
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                    throw;
                }
                if(Logging.On) Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_object_and_exception, "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (e is WebException? e.Message: e.ToString())));
            }
            finally {
                // This is to release cache entry in case we are not interested in it
                if (closeCacheStream && _ResponseStream != null)
                {
                    _ResponseStream.Close();
                    _ResponseStream = null;
                    _Validator.CacheStream = Stream.Null;
                }
            }
        }
        //
        private void CheckRetrieveOnResponse(Stream responseStream)
        {
            GlobalLog.Assert(_ProtocolStatus == CacheValidationStatus.Continue || _ProtocolStatus == CacheValidationStatus.RetryResponseFromServer, "CheckRetrieveOnResponse()|Unexpected _ProtocolStatus = ", _ProtocolStatus);
            bool closeCacheStream = true;

            try {
                // This will inspect the live response on the correctness matter
                switch (_ProtocolStatus = ValidateResponse())
                {
                case CacheValidationStatus.Continue:
                    closeCacheStream = false;
                    // The response looks good
                    break;

                case CacheValidationStatus.RetryResponseFromServer:
                    closeCacheStream = false;
                    break;

                case CacheValidationStatus.Fail:
                    _ProtocolStatus    = CacheValidationStatus.Fail;
                    _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_fail, "ValidateResponse"));
                    break;

                case CacheValidationStatus.DoNotUseCache:
                    break;

                default:
                    _ProtocolStatus    = CacheValidationStatus.Fail;
                    _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_result, "ValidateResponse", _Validator.ValidationStatus.ToString()));
                    if (Logging.On)
                    {
                        Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_unexpected_status, "ValidateResponse()", _Validator.ValidationStatus.ToString()));
                    }
                    break;
                }
            }
            catch (Exception e) {
                closeCacheStream   = true;
                _ProtocolException = e;
                _ProtocolStatus    = CacheValidationStatus.Fail;
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                {
                    throw;
                }
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_object_and_exception, "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (e is WebException? e.Message: e.ToString())));
                }
            }
            finally {
                // This is to release cache entry in case we are not interested in it
                if (closeCacheStream && _ResponseStream != null)
                {
                    _ResponseStream.Close();
                    _ResponseStream        = null;
                    _Validator.CacheStream = Stream.Null;
                }
            }

            if (_ProtocolStatus != CacheValidationStatus.Continue)
            {
                return;
            }

            //
            // only CacheValidationStatus.Continue goes here with closeCacheStream == false
            //

            try {
                //
                switch (_ProtocolStatus = RevalidateCache())
                {
                case CacheValidationStatus.DoNotUseCache:
                case CacheValidationStatus.RemoveFromCache:
                case CacheValidationStatus.DoNotTakeFromCache:
                    closeCacheStream = true;
                    break;

                case CacheValidationStatus.ReturnCachedResponse:
                    if (_Validator.CacheStream == null || _Validator.CacheStream == Stream.Null)
                    {
                        _ProtocolStatus    = CacheValidationStatus.Fail;
                        _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_no_stream, _Validator.CacheKey));
                        if (Logging.On)
                        {
                            Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_null_cached_stream, "RevalidateCache()"));
                        }
                        break;
                    }

                    Stream stream = _Validator.CacheStream;

                    if (_Validator.CacheStreamOffset != 0L || _Validator.CacheStreamLength != _Validator.CacheEntry.StreamSize)
                    {
                        stream = new RangeStream(stream, _Validator.CacheStreamOffset, _Validator.CacheStreamLength);
                        if (Logging.On)
                        {
                            Logging.PrintInfo(Logging.RequestCache, SR.GetString(SR.net_log_cache_returned_range_cache, "RevalidateCache()", _Validator.CacheStreamOffset, _Validator.CacheStreamLength));
                        }
                    }
                    _ResponseStream       = stream;
                    _ResponseStreamLength = _Validator.CacheStreamLength;
                    break;

                case CacheValidationStatus.CombineCachedAndServerResponse:

                    if (_Validator.CacheStream == null || _Validator.CacheStream == Stream.Null)
                    {
                        _ProtocolStatus    = CacheValidationStatus.Fail;
                        _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_no_stream, _Validator.CacheKey));
                        if (Logging.On)
                        {
                            Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_requested_combined_but_null_cached_stream, "RevalidateCache()"));
                        }
                        break;
                    }
                    if (responseStream != null)
                    {
                        stream = new CombinedReadStream(_Validator.CacheStream, responseStream);
                    }
                    else
                    {
                        // So Abort can close the cache stream
                        stream = _Validator.CacheStream;
                    }
                    _ResponseStream       = stream;
                    _ResponseStreamLength = _Validator.CacheStreamLength;
                    break;


                case CacheValidationStatus.Fail:
                    closeCacheStream   = true;
                    _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_fail, "RevalidateCache"));
                    break;

                default:
                    closeCacheStream   = true;
                    _ProtocolStatus    = CacheValidationStatus.Fail;
                    _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_result, "RevalidateCache", _Validator.ValidationStatus.ToString()));
                    if (Logging.On)
                    {
                        Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_unexpected_status, "RevalidateCache()", _Validator.ValidationStatus.ToString()));
                    }
                    break;
                }
            }
            catch (Exception e) {
                closeCacheStream   = true;
                _ProtocolException = e;
                _ProtocolStatus    = CacheValidationStatus.Fail;
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                {
                    throw;
                }
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_object_and_exception, "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (e is WebException? e.Message: e.ToString())));
                }
            }
            finally {
                // This is to release cache entry in case we are not interested in it
                if (closeCacheStream && _ResponseStream != null)
                {
                    _ResponseStream.Close();
                    _ResponseStream        = null;
                    _Validator.CacheStream = Stream.Null;
                }
            }
        }
        //
        // Private methods
        //

        //
        // This method may be invoked as part of the request submission but before issuing a live request
        //
        private void CheckRetrieveBeforeSubmit() {

            GlobalLog.Assert(_ProtocolStatus == CacheValidationStatus.Continue, "CheckRetrieveBeforeSubmit()|Unexpected _ProtocolStatus = {0}", _ProtocolStatus);

            try {

                while (true)
                {
                    RequestCacheEntry cacheEntry;

                    if (_Validator.CacheStream != null && _Validator.CacheStream != Stream.Null)
                    {
                        // Reset to Initial state
                        _Validator.CacheStream.Close();
                        _Validator.CacheStream = Stream.Null;
                    }

                    if (_Validator.StrictCacheErrors)
                    {
                        _Validator.CacheStream = _RequestCache.Retrieve(_Validator.CacheKey, out cacheEntry);
                    }
                    else
                    {
                        Stream stream;
                        _RequestCache.TryRetrieve(_Validator.CacheKey, out cacheEntry, out stream);
                        _Validator.CacheStream = stream;
                    }

                    if (cacheEntry == null)
                    {
                        cacheEntry = new RequestCacheEntry();
                        cacheEntry.IsPrivateEntry = _RequestCache.IsPrivateCache;
                        _Validator.FetchCacheEntry(cacheEntry);
                    }

                    if (_Validator.CacheStream == null)
                    {
                        // If entry does not have a stream an empty stream wrapper must be returned.
                        // A null or Stream.Null value stands for non existent cache entry.
                        _Validator.CacheStream = Stream.Null;
                    }

                    ValidateFreshness(cacheEntry);

                    _ProtocolStatus = ValidateCache();

                    // This will tell us what to do next
                    switch (_ProtocolStatus) {

                    case CacheValidationStatus.ReturnCachedResponse:
                            if (_Validator.CacheStream == null || _Validator.CacheStream == Stream.Null)
                            {
                                if(Logging.On) Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_no_cache_entry, "ValidateCache()"));
                                _ProtocolStatus = CacheValidationStatus.Fail;
                                _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_no_stream, _Validator.CacheKey));
                                break;
                            }

                            // Final decision is made, check on a range response from cache
                            Stream stream = _Validator.CacheStream;
                            // The entry can now be replaced as we are not going for cache entry metadata-only  update
                            _RequestCache.UnlockEntry(_Validator.CacheStream);

                            if (_Validator.CacheStreamOffset != 0L || _Validator.CacheStreamLength != _Validator.CacheEntry.StreamSize)
                            {
                                stream =  new RangeStream(stream, _Validator.CacheStreamOffset, _Validator.CacheStreamLength);
                                if(Logging.On) Logging.PrintInfo(Logging.RequestCache, SR.GetString(SR.net_log_cache_returned_range_cache, "ValidateCache()", _Validator.CacheStreamOffset, _Validator.CacheStreamLength));
                            }
                            _ResponseStream = stream;
                            _ResponseStreamLength = _Validator.CacheStreamLength;
                            break;

                    case CacheValidationStatus.Continue:
                            // copy a cache stream ref
                            _ResponseStream = _Validator.CacheStream;
                            break;

                    case CacheValidationStatus.RetryResponseFromCache:
                            // loop thought cache retrieve
                            continue;

                    case CacheValidationStatus.DoNotTakeFromCache:
                    case CacheValidationStatus.DoNotUseCache:
                            break;

                    case CacheValidationStatus.Fail:
                            _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_fail, "ValidateCache"));
                            break;

                    default:
                        _ProtocolStatus = CacheValidationStatus.Fail;
                        _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_result, "ValidateCache", _Validator.ValidationStatus.ToString()));
                            if(Logging.On) Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_unexpected_status, "ValidateCache()", _Validator.ValidationStatus.ToString()));
                            break;
                    }
                    break;
                }
            }
            catch (Exception e) {
                _ProtocolStatus = CacheValidationStatus.Fail;
                _ProtocolException = e;
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                    throw;
                }
                if(Logging.On) Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_object_and_exception, "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (e is WebException? e.Message: e.ToString())));
            }
            finally {
                // This is to release cache entry on error
                if (_ResponseStream == null && _Validator.CacheStream != null && _Validator.CacheStream != Stream.Null)
                {
                    _Validator.CacheStream.Close();
                    _Validator.CacheStream = Stream.Null;
                }
            }
        }
        //
        // Private methods
        //

        //
        // This method may be invoked as part of the request submission but before issuing a live request
        //
        private void CheckRetrieveBeforeSubmit()
        {
            GlobalLog.Assert(_ProtocolStatus == CacheValidationStatus.Continue, "CheckRetrieveBeforeSubmit()|Unexpected _ProtocolStatus = {0}", _ProtocolStatus);

            try {
                while (true)
                {
                    RequestCacheEntry cacheEntry;

                    if (_Validator.CacheStream != null && _Validator.CacheStream != Stream.Null)
                    {
                        // Reset to Initial state
                        _Validator.CacheStream.Close();
                        _Validator.CacheStream = Stream.Null;
                    }

                    if (_Validator.StrictCacheErrors)
                    {
                        _Validator.CacheStream = _RequestCache.Retrieve(_Validator.CacheKey, out cacheEntry);
                    }
                    else
                    {
                        Stream stream;
                        _RequestCache.TryRetrieve(_Validator.CacheKey, out cacheEntry, out stream);
                        _Validator.CacheStream = stream;
                    }

                    if (cacheEntry == null)
                    {
                        cacheEntry = new RequestCacheEntry();
                        cacheEntry.IsPrivateEntry = _RequestCache.IsPrivateCache;
                        _Validator.FetchCacheEntry(cacheEntry);
                    }

                    if (_Validator.CacheStream == null)
                    {
                        // If entry does not have a stream an empty stream wrapper must be returned.
                        // A null or Stream.Null value stands for non existent cache entry.
                        _Validator.CacheStream = Stream.Null;
                    }

                    ValidateFreshness(cacheEntry);

                    _ProtocolStatus = ValidateCache();

                    // This will tell us what to do next
                    switch (_ProtocolStatus)
                    {
                    case CacheValidationStatus.ReturnCachedResponse:
                        if (_Validator.CacheStream == null || _Validator.CacheStream == Stream.Null)
                        {
                            if (Logging.On)
                            {
                                Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_no_cache_entry, "ValidateCache()"));
                            }
                            _ProtocolStatus    = CacheValidationStatus.Fail;
                            _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_no_stream, _Validator.CacheKey));
                            break;
                        }

                        // Final decision is made, check on a range response from cache
                        Stream stream = _Validator.CacheStream;
                        // The entry can now be replaced as we are not going for cache entry metadata-only  update
                        _RequestCache.UnlockEntry(_Validator.CacheStream);

                        if (_Validator.CacheStreamOffset != 0L || _Validator.CacheStreamLength != _Validator.CacheEntry.StreamSize)
                        {
                            stream = new RangeStream(stream, _Validator.CacheStreamOffset, _Validator.CacheStreamLength);
                            if (Logging.On)
                            {
                                Logging.PrintInfo(Logging.RequestCache, SR.GetString(SR.net_log_cache_returned_range_cache, "ValidateCache()", _Validator.CacheStreamOffset, _Validator.CacheStreamLength));
                            }
                        }
                        _ResponseStream       = stream;
                        _ResponseStreamLength = _Validator.CacheStreamLength;
                        break;

                    case CacheValidationStatus.Continue:
                        // copy a cache stream ref
                        _ResponseStream = _Validator.CacheStream;
                        break;

                    case CacheValidationStatus.RetryResponseFromCache:
                        // loop thought cache retrieve
                        continue;

                    case CacheValidationStatus.DoNotTakeFromCache:
                    case CacheValidationStatus.DoNotUseCache:
                        break;

                    case CacheValidationStatus.Fail:
                        _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_fail, "ValidateCache"));
                        break;

                    default:
                        _ProtocolStatus    = CacheValidationStatus.Fail;
                        _ProtocolException = new InvalidOperationException(SR.GetString(SR.net_cache_validator_result, "ValidateCache", _Validator.ValidationStatus.ToString()));
                        if (Logging.On)
                        {
                            Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_unexpected_status, "ValidateCache()", _Validator.ValidationStatus.ToString()));
                        }
                        break;
                    }
                    break;
                }
            }
            catch (Exception e) {
                _ProtocolStatus    = CacheValidationStatus.Fail;
                _ProtocolException = e;
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                {
                    throw;
                }
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString(SR.net_log_cache_object_and_exception, "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (e is WebException? e.Message: e.ToString())));
                }
            }
            finally {
                // This is to release cache entry on error
                if (_ResponseStream == null && _Validator.CacheStream != null && _Validator.CacheStream != Stream.Null)
                {
                    _Validator.CacheStream.Close();
                    _Validator.CacheStream = Stream.Null;
                }
            }
        }
        private void CheckRetrieveBeforeSubmit()
        {
            try
            {
                RequestCacheEntry entry;
            Label_0000:
                if ((this._Validator.CacheStream != null) && (this._Validator.CacheStream != Stream.Null))
                {
                    this._Validator.CacheStream.Close();
                    this._Validator.CacheStream = Stream.Null;
                }
                if (this._Validator.StrictCacheErrors)
                {
                    this._Validator.CacheStream = this._RequestCache.Retrieve(this._Validator.CacheKey, out entry);
                }
                else
                {
                    Stream stream;
                    this._RequestCache.TryRetrieve(this._Validator.CacheKey, out entry, out stream);
                    this._Validator.CacheStream = stream;
                }
                if (entry == null)
                {
                    entry = new RequestCacheEntry {
                        IsPrivateEntry = this._RequestCache.IsPrivateCache
                    };
                    this._Validator.FetchCacheEntry(entry);
                }
                if (this._Validator.CacheStream == null)
                {
                    this._Validator.CacheStream = Stream.Null;
                }
                this.ValidateFreshness(entry);
                this._ProtocolStatus = this.ValidateCache();
                switch (this._ProtocolStatus)
                {
                    case CacheValidationStatus.DoNotUseCache:
                    case CacheValidationStatus.DoNotTakeFromCache:
                        return;

                    case CacheValidationStatus.Fail:
                        this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_fail", new object[] { "ValidateCache" }));
                        return;

                    case CacheValidationStatus.RetryResponseFromCache:
                        goto Label_0000;

                    case CacheValidationStatus.ReturnCachedResponse:
                        if ((this._Validator.CacheStream != null) && (this._Validator.CacheStream != Stream.Null))
                        {
                            break;
                        }
                        if (Logging.On)
                        {
                            Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_no_cache_entry", new object[] { "ValidateCache()" }));
                        }
                        this._ProtocolStatus = CacheValidationStatus.Fail;
                        this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_no_stream", new object[] { this._Validator.CacheKey }));
                        return;

                    case CacheValidationStatus.Continue:
                        this._ResponseStream = this._Validator.CacheStream;
                        return;

                    default:
                        goto Label_02CB;
                }
                Stream cacheStream = this._Validator.CacheStream;
                this._RequestCache.UnlockEntry(this._Validator.CacheStream);
                if ((this._Validator.CacheStreamOffset != 0L) || (this._Validator.CacheStreamLength != this._Validator.CacheEntry.StreamSize))
                {
                    cacheStream = new RangeStream(cacheStream, this._Validator.CacheStreamOffset, this._Validator.CacheStreamLength);
                    if (Logging.On)
                    {
                        Logging.PrintInfo(Logging.RequestCache, SR.GetString("net_log_cache_returned_range_cache", new object[] { "ValidateCache()", this._Validator.CacheStreamOffset, this._Validator.CacheStreamLength }));
                    }
                }
                this._ResponseStream = cacheStream;
                this._ResponseStreamLength = this._Validator.CacheStreamLength;
                return;
            Label_02CB:
                this._ProtocolStatus = CacheValidationStatus.Fail;
                this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_result", new object[] { "ValidateCache", this._Validator.ValidationStatus.ToString() }));
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_unexpected_status", new object[] { "ValidateCache()", this._Validator.ValidationStatus.ToString() }));
                }
            }
            catch (Exception exception)
            {
                this._ProtocolStatus = CacheValidationStatus.Fail;
                this._ProtocolException = exception;
                if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
                {
                    throw;
                }
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_object_and_exception", new object[] { "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (exception is WebException) ? exception.Message : exception.ToString() }));
                }
            }
            finally
            {
                if (((this._ResponseStream == null) && (this._Validator.CacheStream != null)) && (this._Validator.CacheStream != Stream.Null))
                {
                    this._Validator.CacheStream.Close();
                    this._Validator.CacheStream = Stream.Null;
                }
            }
        }
        private void CheckRetrieveOnResponse(Stream responseStream)
        {
            bool flag = true;
            try
            {
                switch ((this._ProtocolStatus = this.ValidateResponse()))
                {
                    case CacheValidationStatus.DoNotUseCache:
                        goto Label_01CA;

                    case CacheValidationStatus.Fail:
                        this._ProtocolStatus = CacheValidationStatus.Fail;
                        this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_fail", new object[] { "ValidateResponse" }));
                        goto Label_01CA;

                    case CacheValidationStatus.RetryResponseFromServer:
                        flag = false;
                        goto Label_01CA;

                    case CacheValidationStatus.Continue:
                        flag = false;
                        goto Label_01CA;
                }
                this._ProtocolStatus = CacheValidationStatus.Fail;
                this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_result", new object[] { "ValidateResponse", this._Validator.ValidationStatus.ToString() }));
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_unexpected_status", new object[] { "ValidateResponse()", this._Validator.ValidationStatus.ToString() }));
                }
            }
            catch (Exception exception)
            {
                flag = true;
                this._ProtocolException = exception;
                this._ProtocolStatus = CacheValidationStatus.Fail;
                if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
                {
                    throw;
                }
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_object_and_exception", new object[] { "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (exception is WebException) ? exception.Message : exception.ToString() }));
                }
            }
            finally
            {
                if (flag && (this._ResponseStream != null))
                {
                    this._ResponseStream.Close();
                    this._ResponseStream = null;
                    this._Validator.CacheStream = Stream.Null;
                }
            }
        Label_01CA:
            if (this._ProtocolStatus != CacheValidationStatus.Continue)
            {
                return;
            }
            try
            {
                switch ((this._ProtocolStatus = this.RevalidateCache()))
                {
                    case CacheValidationStatus.DoNotUseCache:
                    case CacheValidationStatus.DoNotTakeFromCache:
                    case CacheValidationStatus.RemoveFromCache:
                        flag = true;
                        return;

                    case CacheValidationStatus.Fail:
                        flag = true;
                        this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_fail", new object[] { "RevalidateCache" }));
                        return;

                    case CacheValidationStatus.ReturnCachedResponse:
                        if ((this._Validator.CacheStream != null) && (this._Validator.CacheStream != Stream.Null))
                        {
                            break;
                        }
                        this._ProtocolStatus = CacheValidationStatus.Fail;
                        this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_no_stream", new object[] { this._Validator.CacheKey }));
                        if (Logging.On)
                        {
                            Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_null_cached_stream", new object[] { "RevalidateCache()" }));
                        }
                        return;

                    case CacheValidationStatus.CombineCachedAndServerResponse:
                        if ((this._Validator.CacheStream != null) && (this._Validator.CacheStream != Stream.Null))
                        {
                            goto Label_03FF;
                        }
                        this._ProtocolStatus = CacheValidationStatus.Fail;
                        this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_no_stream", new object[] { this._Validator.CacheKey }));
                        if (Logging.On)
                        {
                            Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_requested_combined_but_null_cached_stream", new object[] { "RevalidateCache()" }));
                        }
                        return;

                    default:
                        goto Label_046E;
                }
                Stream cacheStream = this._Validator.CacheStream;
                if ((this._Validator.CacheStreamOffset != 0L) || (this._Validator.CacheStreamLength != this._Validator.CacheEntry.StreamSize))
                {
                    cacheStream = new RangeStream(cacheStream, this._Validator.CacheStreamOffset, this._Validator.CacheStreamLength);
                    if (Logging.On)
                    {
                        Logging.PrintInfo(Logging.RequestCache, SR.GetString("net_log_cache_returned_range_cache", new object[] { "RevalidateCache()", this._Validator.CacheStreamOffset, this._Validator.CacheStreamLength }));
                    }
                }
                this._ResponseStream = cacheStream;
                this._ResponseStreamLength = this._Validator.CacheStreamLength;
                return;
            Label_03FF:
                if (responseStream != null)
                {
                    cacheStream = new CombinedReadStream(this._Validator.CacheStream, responseStream);
                }
                else
                {
                    cacheStream = this._Validator.CacheStream;
                }
                this._ResponseStream = cacheStream;
                this._ResponseStreamLength = this._Validator.CacheStreamLength;
                return;
            Label_046E:
                flag = true;
                this._ProtocolStatus = CacheValidationStatus.Fail;
                this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_result", new object[] { "RevalidateCache", this._Validator.ValidationStatus.ToString() }));
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_unexpected_status", new object[] { "RevalidateCache()", this._Validator.ValidationStatus.ToString() }));
                }
            }
            catch (Exception exception2)
            {
                flag = true;
                this._ProtocolException = exception2;
                this._ProtocolStatus = CacheValidationStatus.Fail;
                if (((exception2 is ThreadAbortException) || (exception2 is StackOverflowException)) || (exception2 is OutOfMemoryException))
                {
                    throw;
                }
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_object_and_exception", new object[] { "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (exception2 is WebException) ? exception2.Message : exception2.ToString() }));
                }
            }
            finally
            {
                if (flag && (this._ResponseStream != null))
                {
                    this._ResponseStream.Close();
                    this._ResponseStream = null;
                    this._Validator.CacheStream = Stream.Null;
                }
            }
        }
        private void CheckRetrieveBeforeSubmit()
        {
            try
            {
                RequestCacheEntry entry;
Label_0000:
                if ((this._Validator.CacheStream != null) && (this._Validator.CacheStream != Stream.Null))
                {
                    this._Validator.CacheStream.Close();
                    this._Validator.CacheStream = Stream.Null;
                }
                if (this._Validator.StrictCacheErrors)
                {
                    this._Validator.CacheStream = this._RequestCache.Retrieve(this._Validator.CacheKey, out entry);
                }
                else
                {
                    Stream stream;
                    this._RequestCache.TryRetrieve(this._Validator.CacheKey, out entry, out stream);
                    this._Validator.CacheStream = stream;
                }
                if (entry == null)
                {
                    entry = new RequestCacheEntry {
                        IsPrivateEntry = this._RequestCache.IsPrivateCache
                    };
                    this._Validator.FetchCacheEntry(entry);
                }
                if (this._Validator.CacheStream == null)
                {
                    this._Validator.CacheStream = Stream.Null;
                }
                this.ValidateFreshness(entry);
                this._ProtocolStatus = this.ValidateCache();
                switch (this._ProtocolStatus)
                {
                case CacheValidationStatus.DoNotUseCache:
                case CacheValidationStatus.DoNotTakeFromCache:
                    return;

                case CacheValidationStatus.Fail:
                    this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_fail", new object[] { "ValidateCache" }));
                    return;

                case CacheValidationStatus.RetryResponseFromCache:
                    goto Label_0000;

                case CacheValidationStatus.ReturnCachedResponse:
                    if ((this._Validator.CacheStream != null) && (this._Validator.CacheStream != Stream.Null))
                    {
                        break;
                    }
                    if (Logging.On)
                    {
                        Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_no_cache_entry", new object[] { "ValidateCache()" }));
                    }
                    this._ProtocolStatus    = CacheValidationStatus.Fail;
                    this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_no_stream", new object[] { this._Validator.CacheKey }));
                    return;

                case CacheValidationStatus.Continue:
                    this._ResponseStream = this._Validator.CacheStream;
                    return;

                default:
                    goto Label_02CB;
                }
                Stream cacheStream = this._Validator.CacheStream;
                this._RequestCache.UnlockEntry(this._Validator.CacheStream);
                if ((this._Validator.CacheStreamOffset != 0L) || (this._Validator.CacheStreamLength != this._Validator.CacheEntry.StreamSize))
                {
                    cacheStream = new RangeStream(cacheStream, this._Validator.CacheStreamOffset, this._Validator.CacheStreamLength);
                    if (Logging.On)
                    {
                        Logging.PrintInfo(Logging.RequestCache, SR.GetString("net_log_cache_returned_range_cache", new object[] { "ValidateCache()", this._Validator.CacheStreamOffset, this._Validator.CacheStreamLength }));
                    }
                }
                this._ResponseStream       = cacheStream;
                this._ResponseStreamLength = this._Validator.CacheStreamLength;
                return;

Label_02CB:
                this._ProtocolStatus    = CacheValidationStatus.Fail;
                this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_result", new object[] { "ValidateCache", this._Validator.ValidationStatus.ToString() }));
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_unexpected_status", new object[] { "ValidateCache()", this._Validator.ValidationStatus.ToString() }));
                }
            }
            catch (Exception exception)
            {
                this._ProtocolStatus    = CacheValidationStatus.Fail;
                this._ProtocolException = exception;
                if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
                {
                    throw;
                }
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_object_and_exception", new object[] { "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (exception is WebException) ? exception.Message : exception.ToString() }));
                }
            }
            finally
            {
                if (((this._ResponseStream == null) && (this._Validator.CacheStream != null)) && (this._Validator.CacheStream != Stream.Null))
                {
                    this._Validator.CacheStream.Close();
                    this._Validator.CacheStream = Stream.Null;
                }
            }
        }
        private void CheckRetrieveOnResponse(Stream responseStream)
        {
            bool flag = true;

            try
            {
                switch ((this._ProtocolStatus = this.ValidateResponse()))
                {
                case CacheValidationStatus.DoNotUseCache:
                    goto Label_01CA;

                case CacheValidationStatus.Fail:
                    this._ProtocolStatus    = CacheValidationStatus.Fail;
                    this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_fail", new object[] { "ValidateResponse" }));
                    goto Label_01CA;

                case CacheValidationStatus.RetryResponseFromServer:
                    flag = false;
                    goto Label_01CA;

                case CacheValidationStatus.Continue:
                    flag = false;
                    goto Label_01CA;
                }
                this._ProtocolStatus    = CacheValidationStatus.Fail;
                this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_result", new object[] { "ValidateResponse", this._Validator.ValidationStatus.ToString() }));
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_unexpected_status", new object[] { "ValidateResponse()", this._Validator.ValidationStatus.ToString() }));
                }
            }
            catch (Exception exception)
            {
                flag = true;
                this._ProtocolException = exception;
                this._ProtocolStatus    = CacheValidationStatus.Fail;
                if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
                {
                    throw;
                }
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_object_and_exception", new object[] { "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (exception is WebException) ? exception.Message : exception.ToString() }));
                }
            }
            finally
            {
                if (flag && (this._ResponseStream != null))
                {
                    this._ResponseStream.Close();
                    this._ResponseStream        = null;
                    this._Validator.CacheStream = Stream.Null;
                }
            }
Label_01CA:
            if (this._ProtocolStatus != CacheValidationStatus.Continue)
            {
                return;
            }
            try
            {
                switch ((this._ProtocolStatus = this.RevalidateCache()))
                {
                case CacheValidationStatus.DoNotUseCache:
                case CacheValidationStatus.DoNotTakeFromCache:
                case CacheValidationStatus.RemoveFromCache:
                    flag = true;
                    return;

                case CacheValidationStatus.Fail:
                    flag = true;
                    this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_fail", new object[] { "RevalidateCache" }));
                    return;

                case CacheValidationStatus.ReturnCachedResponse:
                    if ((this._Validator.CacheStream != null) && (this._Validator.CacheStream != Stream.Null))
                    {
                        break;
                    }
                    this._ProtocolStatus    = CacheValidationStatus.Fail;
                    this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_no_stream", new object[] { this._Validator.CacheKey }));
                    if (Logging.On)
                    {
                        Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_null_cached_stream", new object[] { "RevalidateCache()" }));
                    }
                    return;

                case CacheValidationStatus.CombineCachedAndServerResponse:
                    if ((this._Validator.CacheStream != null) && (this._Validator.CacheStream != Stream.Null))
                    {
                        goto Label_03FF;
                    }
                    this._ProtocolStatus    = CacheValidationStatus.Fail;
                    this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_no_stream", new object[] { this._Validator.CacheKey }));
                    if (Logging.On)
                    {
                        Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_requested_combined_but_null_cached_stream", new object[] { "RevalidateCache()" }));
                    }
                    return;

                default:
                    goto Label_046E;
                }
                Stream cacheStream = this._Validator.CacheStream;
                if ((this._Validator.CacheStreamOffset != 0L) || (this._Validator.CacheStreamLength != this._Validator.CacheEntry.StreamSize))
                {
                    cacheStream = new RangeStream(cacheStream, this._Validator.CacheStreamOffset, this._Validator.CacheStreamLength);
                    if (Logging.On)
                    {
                        Logging.PrintInfo(Logging.RequestCache, SR.GetString("net_log_cache_returned_range_cache", new object[] { "RevalidateCache()", this._Validator.CacheStreamOffset, this._Validator.CacheStreamLength }));
                    }
                }
                this._ResponseStream       = cacheStream;
                this._ResponseStreamLength = this._Validator.CacheStreamLength;
                return;

Label_03FF:
                if (responseStream != null)
                {
                    cacheStream = new CombinedReadStream(this._Validator.CacheStream, responseStream);
                }
                else
                {
                    cacheStream = this._Validator.CacheStream;
                }
                this._ResponseStream       = cacheStream;
                this._ResponseStreamLength = this._Validator.CacheStreamLength;
                return;

Label_046E:
                flag = true;
                this._ProtocolStatus    = CacheValidationStatus.Fail;
                this._ProtocolException = new InvalidOperationException(SR.GetString("net_cache_validator_result", new object[] { "RevalidateCache", this._Validator.ValidationStatus.ToString() }));
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_unexpected_status", new object[] { "RevalidateCache()", this._Validator.ValidationStatus.ToString() }));
                }
            }
            catch (Exception exception2)
            {
                flag = true;
                this._ProtocolException = exception2;
                this._ProtocolStatus    = CacheValidationStatus.Fail;
                if (((exception2 is ThreadAbortException) || (exception2 is StackOverflowException)) || (exception2 is OutOfMemoryException))
                {
                    throw;
                }
                if (Logging.On)
                {
                    Logging.PrintError(Logging.RequestCache, SR.GetString("net_log_cache_object_and_exception", new object[] { "CacheProtocol#" + this.GetHashCode().ToString(NumberFormatInfo.InvariantInfo), (exception2 is WebException) ? exception2.Message : exception2.ToString() }));
                }
            }
            finally
            {
                if (flag && (this._ResponseStream != null))
                {
                    this._ResponseStream.Close();
                    this._ResponseStream        = null;
                    this._Validator.CacheStream = Stream.Null;
                }
            }
        }