private async Task SendResponse(ImageContext imageContext, string key, DateTimeOffset lastModified, byte[] buffer, int length) { imageContext.ComprehendRequestHeaders(lastModified, length); string contentType = FormatHelpers.GetContentType(this.options.Configuration, key); switch (imageContext.GetPreconditionState()) { case ImageContext.PreconditionState.Unspecified: case ImageContext.PreconditionState.ShouldProcess: if (imageContext.IsHeadRequest()) { await imageContext.SendStatusAsync(ResponseConstants.Status200Ok, contentType); } this.logger.LogImageServed(imageContext.GetDisplayUrl(), key); if (buffer == null) { // We're pulling the buffer from the cache. This should be pooled. CachedBuffer cachedBuffer = await this.cache.GetAsync(key); await imageContext.SendAsync(contentType, cachedBuffer.Buffer, cachedBuffer.Length); BufferDataPool.Return(cachedBuffer.Buffer); } else { await imageContext.SendAsync(contentType, buffer, length); } break; case ImageContext.PreconditionState.NotModified: this.logger.LogImageNotModified(imageContext.GetDisplayUrl()); await imageContext.SendStatusAsync(ResponseConstants.Status304NotModified, contentType); break; case ImageContext.PreconditionState.PreconditionFailed: this.logger.LogImagePreconditionFailed(imageContext.GetDisplayUrl()); await imageContext.SendStatusAsync(ResponseConstants.Status412PreconditionFailed, contentType); break; default: var exception = new NotImplementedException(imageContext.GetPreconditionState().ToString()); Debug.Fail(exception.ToString()); throw exception; } }
private async Task SendResponse(ImageContext imageContext, string key, DateTimeOffset lastModified, Stream stream) { imageContext.ComprehendRequestHeaders(lastModified, stream.Length); string contentType = FormatHelpers.GetContentType(this.options.Configuration, key); switch (imageContext.GetPreconditionState()) { case ImageContext.PreconditionState.Unspecified: case ImageContext.PreconditionState.ShouldProcess: if (imageContext.IsHeadRequest()) { await imageContext.SendStatusAsync(ResponseConstants.Status200Ok, contentType).ConfigureAwait(false); } this.logger.LogImageServed(imageContext.GetDisplayUrl(), key); await imageContext.SendAsync(contentType, stream).ConfigureAwait(false); break; case ImageContext.PreconditionState.NotModified: this.logger.LogImageNotModified(imageContext.GetDisplayUrl()); await imageContext.SendStatusAsync(ResponseConstants.Status304NotModified, contentType).ConfigureAwait(false); break; case ImageContext.PreconditionState.PreconditionFailed: this.logger.LogImagePreconditionFailed(imageContext.GetDisplayUrl()); await imageContext.SendStatusAsync(ResponseConstants.Status412PreconditionFailed, contentType).ConfigureAwait(false); break; default: var exception = new NotImplementedException(imageContext.GetPreconditionState().ToString()); Debug.Fail(exception.ToString()); throw exception; } }