Ejemplo n.º 1
0
        private async Task HandleAssetAsync(HttpContext context, IAsset asset, WebOptimizerOptions options)
        {
            IAssetResponse response = await _assetBuilder.BuildAsync(asset, context, options);

            if (response == null)
            {
                await _next(context);

                return;
            }

            await WriteOutputAsync(context, asset, response, response.CacheKey, options);
        }
Ejemplo n.º 2
0
        private async Task WriteOutputAsync(HttpContext context, IAsset asset, IAssetResponse cachedResponse, string cacheKey, WebOptimizerOptions options)
        {
            context.Response.ContentType = asset.ContentType;

            foreach (string name in cachedResponse.Headers.Keys)
            {
                context.Response.Headers[name] = cachedResponse.Headers[name];
            }

            if (!string.IsNullOrEmpty(cacheKey))
            {
                if (options.EnableCaching == true)
                {
                    context.Response.Headers[HeaderNames.CacheControl] = $"max-age=31536000"; // 1 year
                }

                context.Response.Headers[HeaderNames.ETag] = $"\"{cacheKey}\"";

                if (IsConditionalGet(context, cacheKey))
                {
                    context.Response.StatusCode = 304;
                    return;
                }
            }

            if (cachedResponse?.Body?.Length > 0)
            {
                await context.Response.Body.WriteAsync(cachedResponse.Body, 0, cachedResponse.Body.Length);
            }
        }