public async Task Invoke(HttpContext context)
        {
            bool useMinification = _options.IsMinificationEnabled() && _minificationManagers.Count > 0;
            bool useCompression  = _options.IsCompressionEnabled() && _compressionManager != null;

            if (!useMinification && !useCompression)
            {
                await _next.Invoke(context);

                return;
            }

            HttpRequest  request  = context.Request;
            HttpResponse response = context.Response;

            using (var cachedStream = new MemoryStream())
            {
                Stream originalStream = response.Body;
                response.Body = cachedStream;

                try
                {
                    await _next.Invoke(context);
                }
                catch (Exception)
                {
                    response.Body = originalStream;
                    cachedStream.Clear();

                    throw;
                }

                byte[] cachedBytes     = cachedStream.ToArray();
                int    cachedByteCount = cachedBytes.Length;
                bool   isProcessed     = false;

                response.Body = originalStream;
                cachedStream.Clear();

                if (cachedByteCount == 0)
                {
                    return;
                }

                if (response.StatusCode == 200)
                {
                    string   httpMethod  = request.Method;
                    string   contentType = response.ContentType;
                    string   mediaType   = null;
                    Encoding encoding    = null;

                    if (contentType != null)
                    {
                        MediaTypeHeaderValue mediaTypeHeader;

                        if (MediaTypeHeaderValue.TryParse(contentType, out mediaTypeHeader))
                        {
                            mediaType = mediaTypeHeader.MediaType
#if ASPNETCORE2
                                        .Value
#endif
                                        .ToLowerInvariant()
                            ;
                            encoding = mediaTypeHeader.Encoding;
                        }
                    }

                    encoding = encoding ?? Encoding.GetEncoding(0);

                    string      currentUrl  = request.Path.Value;
                    QueryString queryString = request.QueryString;
                    if (queryString.HasValue)
                    {
                        currentUrl += queryString.Value;
                    }

                    string            content          = encoding.GetString(cachedBytes);
                    string            processedContent = content;
                    IHeaderDictionary responseHeaders  = response.Headers;
                    bool isEncodedContent = responseHeaders.IsEncodedContent();
                    Action <string, string> appendHttpHeader = (key, value) =>
                    {
                        responseHeaders.Append(key, new StringValues(value));
                    };

                    if (useMinification && _options.IsAllowableResponseSize(cachedByteCount))
                    {
                        foreach (IMarkupMinificationManager minificationManager in _minificationManagers)
                        {
                            if (minificationManager.IsSupportedHttpMethod(httpMethod) &&
                                mediaType != null && minificationManager.IsSupportedMediaType(mediaType) &&
                                minificationManager.IsProcessablePage(currentUrl))
                            {
                                if (isEncodedContent)
                                {
                                    throw new InvalidOperationException(
                                              string.Format(
                                                  AspNetCommonStrings.MarkupMinificationIsNotApplicableToEncodedContent,
                                                  responseHeaders["Content-Encoding"]
                                                  )
                                              );
                                }

                                IMarkupMinifier minifier = minificationManager.CreateMinifier();

                                MarkupMinificationResult minificationResult = minifier.Minify(processedContent,
                                                                                              currentUrl, encoding, minificationManager.GenerateStatistics);
                                if (minificationResult.Errors.Count == 0)
                                {
                                    processedContent = minificationResult.MinifiedContent;
                                    if (_options.IsPoweredByHttpHeadersEnabled())
                                    {
                                        minificationManager.AppendPoweredByHttpHeader(appendHttpHeader);
                                    }

                                    isProcessed = true;
                                }
                            }

                            if (isProcessed)
                            {
                                break;
                            }
                        }
                    }

                    if (useCompression && !isEncodedContent &&
                        _compressionManager.IsSupportedHttpMethod(httpMethod) &&
                        _compressionManager.IsSupportedMediaType(mediaType) &&
                        _compressionManager.IsProcessablePage(currentUrl))
                    {
                        byte[] processedBytes = encoding.GetBytes(processedContent);

                        using (var inputStream = new MemoryStream(processedBytes))
                            using (var outputStream = new MemoryStream())
                            {
                                string      acceptEncoding = request.Headers["Accept-Encoding"];
                                ICompressor compressor     = _compressionManager.CreateCompressor(acceptEncoding);

                                using (Stream compressedStream = compressor.Compress(outputStream))
                                {
                                    await inputStream.CopyToAsync(compressedStream);
                                }

                                byte[] compressedBytes     = outputStream.ToArray();
                                int    compressedByteCount = compressedBytes.Length;

                                outputStream.Clear();
                                inputStream.Clear();

                                responseHeaders["Content-Length"] = compressedByteCount.ToString();
                                compressor.AppendHttpHeaders(appendHttpHeader);
                                await originalStream.WriteAsync(compressedBytes, 0, compressedByteCount);
                            }

                        isProcessed = true;
                    }
                    else
                    {
                        if (isProcessed)
                        {
                            byte[] processedBytes     = encoding.GetBytes(processedContent);
                            int    processedByteCount = processedBytes.Length;

                            responseHeaders["Content-Length"] = processedByteCount.ToString();
                            await originalStream.WriteAsync(processedBytes, 0, processedByteCount);
                        }
                    }
                }

                if (!isProcessed)
                {
                    await originalStream.WriteAsync(cachedBytes, 0, cachedByteCount);
                }
            }
        }
Exemple #2
0
        public async Task Finish()
        {
            if (_minificationEnabled)
            {
                byte[] cachedBytes     = _cachedStream.ToArray();
                int    cachedByteCount = cachedBytes.Length;

                bool isMinified = false;

                if (cachedByteCount > 0 && _options.IsAllowableResponseSize(cachedByteCount))
                {
                    Encoding encoding = _encoding ?? Encoding.GetEncoding(0);
                    string   content  = encoding.GetString(cachedBytes);

                    IMarkupMinifier          minifier           = _currentMinificationManager.CreateMinifier();
                    MarkupMinificationResult minificationResult = minifier.Minify(content, _currentUrl,
                                                                                  _encoding, _currentMinificationManager.GenerateStatistics);

                    if (minificationResult.Errors.Count == 0)
                    {
                        IHeaderDictionary       responseHeaders  = _context.Response.Headers;
                        Action <string, string> appendHttpHeader = (key, value) =>
                        {
                            responseHeaders.Append(key, new StringValues(value));
                        };

                        if (_options.IsPoweredByHttpHeadersEnabled())
                        {
                            _currentMinificationManager.AppendPoweredByHttpHeader(appendHttpHeader);
                        }
                        responseHeaders.Remove(HeaderNames.ContentMD5);

                        string processedContent   = minificationResult.MinifiedContent;
                        byte[] processedBytes     = encoding.GetBytes(processedContent);
                        int    processedByteCount = processedBytes.Length;

                        if (_compressionEnabled)
                        {
                            _currentCompressor.AppendHttpHeaders(appendHttpHeader);
                            responseHeaders.Remove(HeaderNames.ContentLength);
                            await _compressionStream.WriteAsync(processedBytes, 0, processedByteCount);
                        }
                        else
                        {
                            responseHeaders[HeaderNames.ContentLength] = processedByteCount.ToString();
                            await _originalStream.WriteAsync(processedBytes, 0, processedByteCount);
                        }

                        isMinified = true;
                    }

                    if (!isMinified)
                    {
                        Stream outputStream = _compressionEnabled ? _compressionStream : _originalStream;

                        _cachedStream.Seek(0, SeekOrigin.Begin);
                        await _cachedStream.CopyToAsync(outputStream);
                    }
                }

                _cachedStream.Clear();
            }
        }
        protected async Task InternalFinishAsync()
        {
            if (_minificationEnabled)
            {
                bool isMinified      = false;
                int  cachedByteCount = (int)_cachedStream.Length;
                IHeaderDictionary       responseHeaders  = _context.Response.Headers;
                Action <string, string> appendHttpHeader = (key, value) =>
                {
                    responseHeaders.Append(key, new StringValues(value));
                };

                if (cachedByteCount > 0 && _options.IsAllowableResponseSize(cachedByteCount))
                {
                    Encoding encoding = _encoding ?? _options.DefaultEncoding;
#if NETSTANDARD1_3
                    byte[] cachedBytes = _cachedStream.ToArray();
                    string content     = encoding.GetString(cachedBytes);
#else
                    byte[] cachedBytes = _cachedStream.GetBuffer();
                    string content     = encoding.GetString(cachedBytes, 0, cachedByteCount);
#endif

                    IMarkupMinifier          minifier           = _currentMinificationManager.CreateMinifier();
                    MarkupMinificationResult minificationResult = minifier.Minify(content, _currentUrl,
                                                                                  _encoding, _currentMinificationManager.GenerateStatistics);

                    if (minificationResult.Errors.Count == 0)
                    {
                        if (_options.IsPoweredByHttpHeadersEnabled())
                        {
                            _currentMinificationManager.AppendPoweredByHttpHeader(appendHttpHeader);
                        }
                        responseHeaders.Remove(HeaderNames.ContentMD5);

                        string processedContent   = minificationResult.MinifiedContent;
                        var    byteArrayPool      = ArrayPool <byte> .Shared;
                        int    processedByteCount = encoding.GetByteCount(processedContent);
                        byte[] processedBytes     = byteArrayPool.Rent(processedByteCount);

                        try
                        {
                            encoding.GetBytes(processedContent, 0, processedContent.Length, processedBytes, 0);

                            if (_compressionEnabled)
                            {
                                _currentCompressor.AppendHttpHeaders(appendHttpHeader);
                                responseHeaders.Remove(HeaderNames.ContentLength);
                                await _compressionStream.WriteAsync(processedBytes, 0, processedByteCount);
                            }
                            else
                            {
                                responseHeaders[HeaderNames.ContentLength] = processedByteCount.ToString();
                                await _originalStream.WriteAsync(processedBytes, 0, processedByteCount);
                            }
                        }
                        finally
                        {
                            byteArrayPool.Return(processedBytes);
                        }

                        isMinified = true;
                    }
                }

                if (!isMinified)
                {
                    Stream outputStream;

                    if (_compressionEnabled)
                    {
                        outputStream = _compressionStream;
                        _currentCompressor.AppendHttpHeaders(appendHttpHeader);
                    }
                    else
                    {
                        outputStream = _originalStream;
                    }

                    _cachedStream.Seek(0, SeekOrigin.Begin);
                    await _cachedStream.CopyToAsync(outputStream);
                }

                _cachedStream.Clear();
            }
        }