<commit_before>/*
 * Phosphorus Five, copyright 2014 - 2017, <NAME>, <EMAIL>
 * 
 * This file is part of Phosphorus Five.
 *
 * Phosphorus Five is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as published by
 * the Free Software Foundation.
 *
 *
 * Phosphorus Five is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Phosphorus Five.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * If you cannot for some reasons use the GPL license, Phosphorus
 * Five is also commercially available under Quid Pro Quo terms. Check 
 * out our website at http://gaiasoul.com for more details.
 */

using System;
using System.IO;
using System.Linq;
using System.Text;

namespace p5.ajax.core.filters
{
    /// <summary>
    ///     An HTTP response filter used when rendering plain HTML back to client.
    ///     Notice, this class will significantly "clean up" the HTML, taking great pride in nicely formatting the output HTML.
    /// </summary>
    public class HtmlFilter : Filter
    {
        private bool _removeViewState;

        /// <summary>
        ///     Initializes a new instance of the HtmlFilter class.
        /// </summary>
        /// <param name="page">The AjaxPage this instance is rendering for</param>
        /// <param name="removeViewState">If true, all traces of ViewState will be removed from the page</param>
        public HtmlFilter (AjaxPage page, bool removeViewState)
            : base (page)
        {
            _removeViewState = removeViewState;
        }

        /// <summary>
        ///     Renders the response.
        /// </summary>
        /// <returns>The HTML response returned back to client</returns>
        protected override string RenderResponse ()
        {
            // Retrieving entire HTML from stream.
            TextReader reader = new StreamReader (this, ContentEncoding);
            var content = reader.ReadToEnd ();

            // Cleaning up HTML, which means removing __VIEWSTATE input and nicely formatting <head> section
            if (_removeViewState)
                content = RemoveViewState (content);

            content = CleanHead (content);

            // Including CSS files.
            content = IncludeCSSFiles (content);

            // Including JavaScript files and JavaScript inline content
            content = IncludeJavaScript (content);
            return content;
        }

        /*
         * Removes the __VIEWSTATE input element, in adddition to its "aspNetHidden" wrapper div, if it exists.
         */
        string RemoveViewState (string html)
        {
            // Setting up a stringbuilder to create our return value, being HTML without ViewState wrapper div.
            var buffer = new StringBuilder ();

            // First checking if we have a "wrapper div" around __VIEWSTATE input.
            int startOffset = html.IndexOf (@"<div class=""aspNetHidden"">", StringComparison.InvariantCulture);
            int endOffset = -1;
            if (startOffset != -1) {

                // Removing entire "__VIEWSTATE wrapper div".
                endOffset = html.IndexOf ("</div>", startOffset, StringComparison.InvariantCulture) + 6;

            } else {

                // Defaulting to only removing __VIEWSTATE input
                startOffset = html.IndexOf ("__VIEWSTATE", StringComparison.InvariantCulture);
                if (startOffset == -1)
                    return html;
                endOffset = html.IndexOf ('>', startOffset);
                while (html [startOffset] != '<')<commit_msg>Before attempting to massage HTML output, we need to check that this is not a redirect<commit_after>/*
 * Phosphorus Five, copyright 2014 - 2017, <NAME>, <EMAIL>
 * 
 * This file is part of Phosphorus Five.
 *
 * Phosphorus Five is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as published by
 * the Free Software Foundation.
 *
 *
 * Phosphorus Five is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Phosphorus Five.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * If you cannot for some reasons use the GPL license, Phosphorus
 * Five is also commercially available under Quid Pro Quo terms. Check 
 * out our website at http://gaiasoul.com for more details.
 */

using System;
using System.IO;
using System.Linq;
using System.Text;

namespace p5.ajax.core.filters
{
    /// <summary>
    ///     An HTTP response filter used when rendering plain HTML back to client.
    ///     Notice, this class will significantly "clean up" the HTML, taking great pride in nicely formatting the output HTML.
    /// </summary>
    public class HtmlFilter : Filter
    {
        private bool _removeViewState;

        /// <summary>
        ///     Initializes a new instance of the HtmlFilter class.
        /// </summary>
        /// <param name="page">The AjaxPage this instance is rendering for</param>
        /// <param name="removeViewState">If true, all traces of ViewState will be removed from the page</param>
        public HtmlFilter (AjaxPage page, bool removeViewState)
            : base (page)
        {
            _removeViewState = removeViewState;
        }

        /// <summary>
        ///     Renders the response.
        /// </summary>
        /// <returns>The HTML response returned back to client</returns>
        protected override string RenderResponse ()
        {
            // Retrieving entire HTML from stream.
            TextReader reader = new StreamReader (this, ContentEncoding);
            var content = reader.ReadToEnd ();

            // Checking if this is a redirect, at which point no further massage is necessary.
            if (content.IndexOf ("<form", StringComparison.InvariantCulture) == -1)
                return content;

            // Cleaning up HTML, which means removing __VIEWSTATE input and nicely formatting <head> section
            if (_removeViewState)
                content = RemoveViewState (content);

            content = CleanHead (content);

            // Including CSS files.
            content = IncludeCSSFiles (content);

            // Including JavaScript files and JavaScript inline content
            content = IncludeJavaScript (content);
            return content;
        }

        /*
         * Removes the __VIEWSTATE input element, in adddition to its "aspNetHidden" wrapper div, if it exists.
         */
        string RemoveViewState (string html)
        {
            // Setting up a stringbuilder to create our return value, being HTML without ViewState wrapper div.
            var buffer = new StringBuilder ();

            // First checking if we have a "wrapper div" around __VIEWSTATE input.
            int startOffset = html.IndexOf (@"<div class=""aspNetHidden"">", StringComparison.InvariantCulture);
            int endOffset = -1;
            if (startOffset != -1) {

                // Removing entire "__VIEWSTATE wrapper div".
                endOffset = html.IndexOf ("</div>", startOffset, StringComparison.InvariantCulture) + 6;

            } else {

                // Defaulting to only removing __VIEWSTATE input
                startOffset = html.IndexOf ("__VIEWSTATE", StringComparison.InvariantCulture);
                endOffset = html.IndexOf ('>', startOffset);
                while (html [startOffset] != '<')
{'fraction_non_alphanumeric': 0.08340671782614165, 'fraction_numerical': 0.0035224556547993457, 'mean_word_length': 2.217320922703359, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3817934', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1869, 'n_words': 805}