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