// 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) { 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 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; } } }