Beispiel #1
0
        public override void Close()
        {
            bool isEncodedContent = HttpHeadersHelpers.IsEncodedContent(_response.Headers);

            if (!isEncodedContent)
            {
                byte[] cachedBytes     = _cachedStream.ToArray();
                int    cachedByteCount = cachedBytes.Length;

                bool isMinified = false;

                if (_configuration.IsAllowableResponseSize(cachedByteCount))
                {
                    string          content  = _encoding.GetString(cachedBytes);
                    IMarkupMinifier minifier = _minificationManager.CreateMinifier();

                    MarkupMinificationResult minificationResult = minifier.Minify(content,
                                                                                  _currentUrl, _encoding, _minificationManager.GenerateStatistics);
                    if (minificationResult.Errors.Count == 0)
                    {
                        if (_configuration.IsPoweredByHttpHeadersEnabled())
                        {
                            _minificationManager.AppendPoweredByHttpHeader((key, value) =>
                            {
                                _response.Headers[key] = value;
                            });
                        }

                        using (var writer = new StreamWriter(_originalStream, _encoding))
                        {
                            writer.Write(minificationResult.MinifiedContent);
                        }

                        isMinified = true;
                    }
                }

                if (!isMinified)
                {
                    _cachedStream.Seek(0, SeekOrigin.Begin);
                    _cachedStream.CopyTo(_originalStream);
                }
            }

            _cachedStream.Clear();
            _originalStream.Close();

            if (isEncodedContent)
            {
                throw new InvalidOperationException(
                          string.Format(
                              AspNetCommonStrings.MarkupMinificationIsNotApplicableToEncodedContent,
                              _response.Headers["Content-Encoding"]
                              )
                          );
            }
        }
        public override void Close()
        {
            byte[] cacheBytes = _cacheStream.ToArray();
            int    cacheSize  = cacheBytes.Length;

            bool isMinified = false;

            if (_configuration.IsAllowableResponseSize(cacheSize))
            {
                string          content  = _encoding.GetString(cacheBytes);
                IMarkupMinifier minifier = _minificationManager.CreateMinifier();

                MarkupMinificationResult minificationResult = minifier.Minify(content,
                                                                              _currentUrl, _encoding, false);
                if (minificationResult.Errors.Count == 0)
                {
                    if (_configuration.IsPoweredByHttpHeadersEnabled())
                    {
                        _minificationManager.AppendPoweredByHttpHeader((key, value) =>
                        {
                            _response.Headers[key] = value;
                        });
                    }

                    using (var writer = new StreamWriter(_stream, _encoding))
                    {
                        writer.Write(minificationResult.MinifiedContent);
                    }

                    isMinified = true;
                }
            }

            if (!isMinified)
            {
                _cacheStream.Seek(0, SeekOrigin.Begin);
                _cacheStream.CopyTo(_stream);
            }

            _cacheStream.SetLength(0);
            _stream.Close();
        }