コード例 #1
0
ファイル: JavascriptManipulator.cs プロジェクト: J0hnLiu/vrs
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="environment"></param>
        private void ManipulateResponseStream(IDictionary <string, object> environment)
        {
            var context             = OwinContext.Create(environment);
            var isJavaScriptContent = context.ResponseHeadersDictionary.ContentTypeValue.MediaTypeParsed == MediaType.JavaScript;

            if (isJavaScriptContent)
            {
                var stream = context.ResponseBody;
                stream.Position = 0;
                var textContent = TextContent.Load(stream, leaveOpen: true);

                foreach (var manipulator in _Config.GetTextResponseManipulators())
                {
                    manipulator.ManipulateTextResponse(environment, textContent);
                }

                var suppressMinification = ((bool?)context.Environment[VrsEnvironmentKey.SuppressJavascriptMinification]) == true;
                var newContent           = !suppressMinification?_Minifier.MinifyJavaScript(textContent.Content) : textContent.Content;

                if (newContent.Length < textContent.Content.Length)
                {
                    textContent.Content = newContent;
                }

                if (textContent.IsDirty)
                {
                    stream.Position = 0;
                    stream.SetLength(0);
                    var bytes = textContent.GetBytes(includePreamble: true);
                    stream.Write(bytes, 0, bytes.Length);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="environment"></param>
        public void ManipulateResponseStream(IDictionary <string, object> environment)
        {
            var context = PipelineContext.GetOrCreate(environment);

            if (context.Response.IsJavascriptContentType)
            {
                var stream = context.Response.Body;
                stream.Position = 0;
                var textContent = TextContent.Load(stream, leaveOpen: true);

                foreach (var manipulator in _Config.GetTextResponseManipulators())
                {
                    manipulator.ManipulateTextResponse(environment, textContent);
                }

                var suppressMinification = context.Get <bool>(EnvironmentKey.SuppressJavascriptMinification);
                var newContent           = !suppressMinification?_Minifier.MinifyJavaScript(textContent.Content) : textContent.Content;

                if (newContent.Length < textContent.Content.Length)
                {
                    textContent.Content = newContent;
                }

                if (textContent.IsDirty)
                {
                    stream.Position = 0;
                    stream.SetLength(0);
                    var bytes = textContent.GetBytes(includePreamble: true);
                    stream.Write(bytes, 0, bytes.Length);
                }
            }
        }
コード例 #3
0
        public void JavascriptManipulatorConfiguration_AddTextResponseManipulator_Adds_Manipulator()
        {
            _Config.AddTextResponseManipulator(_Manipulator);

            var manipulators = _Config.GetTextResponseManipulators().ToArray();

            Assert.AreEqual(1, manipulators.Length);
            Assert.AreSame(_Manipulator, manipulators[0]);
        }