public Task Complete() { InterceptMode interceptMode = Intercept(); Detach(); switch (interceptMode) { case InterceptMode.DoingNothing: return(TaskHelpers.Completed()); case InterceptMode.CompressingToStorage: _compressingStream.Close(); _compressedItem = _storage.Commit(_compressedItemBuilder); _response.ContentLength = _compressedItem.CompressedLength; _response.ETag = _compressedETag; _response.Headers.Set("Content-Encoding", _encoding.Name); if (_compressedItem.PhysicalPath != null && _originalSendFileAsyncDelegate != null) { return(_originalSendFileAsyncDelegate.Invoke(_compressedItem.PhysicalPath, 0, _compressedItem.CompressedLength, _request.CallCancelled)); } else { // TODO: stream copy operation } return(TaskHelpers.Completed()); case InterceptMode.SentFromStorage: _response.ETag = _compressedETag; _response.Headers.Set("Content-Encoding", _encoding.Name); if (_compressedItem != null) { _response.ContentLength = _compressedItem.CompressedLength; if (_compressedItem.PhysicalPath != null && _originalSendFileAsyncDelegate != null) { return(_originalSendFileAsyncDelegate.Invoke(_compressedItem.PhysicalPath, 0, _compressedItem.CompressedLength, _request.CallCancelled)); } else { // TODO: stream copy operation } } return(TaskHelpers.Completed()); } throw new NotImplementedException(); }
public InterceptMode InterceptOnce() { string etag = _response.ETag; if (string.IsNullOrEmpty(etag)) { return(InterceptMode.DoingNothing); } if (etag.StartsWith("\"", StringComparison.Ordinal) && etag.EndsWith("\"", StringComparison.Ordinal)) { _compressedETag = etag.Substring(0, etag.Length - 1) + "^" + _encoding.Name + "\""; } else { _compressedETag = "\"" + etag + "^" + _encoding.Name + "\""; } HttpStatusCode statusCode = (HttpStatusCode)_response.StatusCode; if (statusCode == HttpStatusCode.NotModified) { return(InterceptMode.SentFromStorage); } var key = new CompressedKey { ETag = _compressedETag, RequestPath = _request.Path.Value, RequestQueryString = _request.QueryString.Value, RequestMethod = _request.Method, }; _compressedItem = _storage.Open(key); if (_compressedItem != null) { return(InterceptMode.SentFromStorage); } _compressedItemBuilder = _storage.Create(key); _compressingStream = _encoding.CompressTo(_compressedItemBuilder.Stream); return(InterceptMode.CompressingToStorage); }
public Task Complete() { InterceptMode interceptMode = Intercept(); Detach(); switch (interceptMode) { case InterceptMode.DoingNothing: return TaskHelpers.Completed(); case InterceptMode.CompressingToStorage: _compressingStream.Close(); _compressedItem = _storage.Commit(_compressedItemBuilder); _response.SetHeader("Content-Length", _compressedItem.CompressedLength.ToString(CultureInfo.InvariantCulture)); _response.SetHeader("ETag", _compressedETag); _response.SetHeader("Content-Encoding", _encoding.Name); if (_compressedItem.PhysicalPath != null && _originalSendFileAsyncDelegate != null) { return _originalSendFileAsyncDelegate.Invoke(_compressedItem.PhysicalPath, 0, _compressedItem.CompressedLength, _request.CallCancelled); } else { // TODO: stream copy operation } return TaskHelpers.Completed(); case InterceptMode.SentFromStorage: _response.SetHeader("ETag", _compressedETag); _response.SetHeader("Content-Encoding", _encoding.Name); if (_compressedItem != null) { _response.SetHeader("Content-Length", _compressedItem.CompressedLength.ToString(CultureInfo.InvariantCulture)); if (_compressedItem.PhysicalPath != null && _originalSendFileAsyncDelegate != null) { return _originalSendFileAsyncDelegate.Invoke(_compressedItem.PhysicalPath, 0, _compressedItem.CompressedLength, _request.CallCancelled); } else { // TODO: stream copy operation } } return TaskHelpers.Completed(); } throw new NotImplementedException(); }
public InterceptMode InterceptOnce() { StringSegment etag = SingleSegment(_response, "ETag"); if (!etag.HasValue) { return InterceptMode.DoingNothing; } if (etag.StartsWith("\"", StringComparison.Ordinal) && etag.EndsWith("\"", StringComparison.Ordinal)) { _compressedETag = etag.Substring(0, etag.Count - 1) + "^" + _encoding.Name + "\""; } else { _compressedETag = "\"" + etag.Value + "^" + _encoding.Name + "\""; } HttpStatusCode statusCode = (HttpStatusCode)_response.StatusCode; if (statusCode == HttpStatusCode.NotModified) { return InterceptMode.SentFromStorage; } var key = new CompressedKey { ETag = _compressedETag, RequestPath = _request.Path, RequestQueryString = _request.QueryString, RequestMethod = _request.Method, }; _compressedItem = _storage.Open(key); if (_compressedItem != null) { return InterceptMode.SentFromStorage; } _compressedItemBuilder = _storage.Create(key); _compressingStream = _encoding.CompressTo(_compressedItemBuilder.Stream); return InterceptMode.CompressingToStorage; }