<commit_before>﻿using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;

/// <summary>
/// Taken from https://devnet.kentico.com/marketplace/utilities/kentico-cdn-injector
/// and http://stackoverflow.com/questions/6061286/looking-for-a-httphandler-to-modify-pages-on-the-fly-to-point-to-a-cdn
/// and http://stackoverflow.com/questions/4464983/how-do-i-implement-an-http-response-filter-to-operate-on-the-entire-content-at-o
/// </summary>
public class CdnFilter : Stream
{
    public string CdnSmallDomain = ConfigurationManager.AppSettings["CdnSmallDomain"];
    public string CdnLargeDomain = ConfigurationManager.AppSettings["CdnLargeDomain"];
    public string ForceSsl = ConfigurationManager.AppSettings["CdnForceSsl"];
    public string SmallFileExtensionsToMatch = ConfigurationManager.AppSettings["CdnSmallFileExtensionsToMatch"];
    public string LargeFileExtensionsToMatch = ConfigurationManager.AppSettings["CdnLargeFileExtensionsToMatch"];
    public string FilePathsToMatch = ConfigurationManager.AppSettings["CdnFilePathsToMatch"];
    public string FilePathsToIgnore = ConfigurationManager.AppSettings["CdnFilePathsToIgnore"];
    private readonly MemoryStream _cachedStream = new MemoryStream(1024);
    private readonly Stream _responseStream;
    private readonly CdnParser _cdnParser;
    private readonly bool _useSsl;
    private readonly bool _doCdnRewrite;
    private readonly string[] _smallFileExtensions;
    private readonly string[] _largeFileExtensions;
    private readonly string[] _matchFilePaths;
    private readonly string[] _excludeFilePaths;
    private readonly bool _doFilePathMatching;
    private bool _isClosing;
    private bool _isClosed;

    public override bool CanRead
    {
        get
        {
            return true;
        }
    }

    public override bool CanSeek
    {
        get
        {
            return true;
        }
    }

    public override bool CanWrite
    {
        get
        {
            return true;
        }
    }

    public override long Length
    {
        get
        {
            return 0L;
        }
    }

    public override long Position { get; set; }

    public CdnFilter(Stream inputStream, Uri currentUrl)
    {
        _responseStream = inputStream;
        var smallDomainName = string.IsNullOrWhiteSpace(CdnSmallDomain) ? string.Empty : CdnSmallDomain;
        var largeDomainName = string.IsNullOrWhiteSpace(CdnLargeDomain) ? string.Empty : CdnLargeDomain;
        if (!string.IsNullOrEmpty(smallDomainName) || !string.IsNullOrEmpty(largeDomainName))
            _doCdnRewrite = true;
        if (!string.IsNullOrWhiteSpace(FilePathsToMatch))
        {
            _matchFilePaths = FilePathsToMatch.Split(',');
            _doFilePathMatching = _matchFilePaths.Length > 0;
        }
        else
            _doCdnRewrite = false;
        if (!string.IsNullOrWhiteSpace(SmallFileExtensionsToMatch))
        {
            _smallFileExtensions = SmallFileExtensionsToMatch.Split(',');
        }
        if (!string.IsNullOrWhiteSpace(LargeFileExtensionsToMatch))
        {
            _largeFileExtensions = LargeFileExtensionsToMatch.Split(',');
        }
        if (!_smallFileExtensions.Any() && !_largeFileExtensions.Any())
            _doCdnRewrite = false;
        bool forceSsl;
        if (!string.IsNullOrWhiteSpace(ForceSsl) && bool.TryParse(ForceSsl, out forceSsl))
            _useSsl = forceSsl;
        if (!string.IsNullOrWhiteSpace(FilePathsToIgnore))
         { 
            var chArray = new[] { ',' };
            _excludeFilePaths = FilePathsToIgnore.Split(chArray);
            foreach (var str in _excludeFilePaths)
            {
                if (HttpContext.Current.Request.RawUrl.Contains(str))
                    _doCdnRewrite = false;
            }
        }
        _cdnParser = new CdnParser(_useSsl, smallDomainName, largeDomainName, _smallFileExtensions, _largeFileExtensions, _doFilePathMatching, _matchFilePaths, _excludeFilePaths, currentUrl);
    }

    public override void Flush()
    {
        if (_isClosing && !_isClosed)
        {
            var encoding = HttpContext.Current.Response.ContentEncoding;
            if (_cachedStream != null)
            {
                var cachedContent = encoding.GetString(_cachedStream.ToArray());<commit_msg>updated SSL flag<commit_after>﻿using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using CMS.EventLog;
using CMS.SiteProvider;
using CMS.Helpers;

/// <summary>
/// Taken from https://devnet.kentico.com/marketplace/utilities/kentico-cdn-injector
/// and http://stackoverflow.com/questions/6061286/looking-for-a-httphandler-to-modify-pages-on-the-fly-to-point-to-a-cdn
/// and http://stackoverflow.com/questions/4464983/how-do-i-implement-an-http-response-filter-to-operate-on-the-entire-content-at-o
/// </summary>
public class CdnFilter : Stream
{
    public string CdnSmallDomain = ConfigurationManager.AppSettings["CdnSmallDomain"];
    public string CdnLargeDomain = ConfigurationManager.AppSettings["CdnLargeDomain"];
    public string ForceSsl = ConfigurationManager.AppSettings["CdnForceSsl"];
    public string SmallFileExtensionsToMatch = ConfigurationManager.AppSettings["CdnSmallFileExtensionsToMatch"];
    public string LargeFileExtensionsToMatch = ConfigurationManager.AppSettings["CdnLargeFileExtensionsToMatch"];
    public string FilePathsToMatch = ConfigurationManager.AppSettings["CdnFilePathsToMatch"];
    public string FilePathsToIgnore = ConfigurationManager.AppSettings["CdnFilePathsToIgnore"];
    private readonly MemoryStream _cachedStream = new MemoryStream(1024);
    private readonly Stream _responseStream;
    private readonly CdnParser _cdnParser;
    private readonly bool _useSsl;
    private readonly bool _doCdnRewrite;
    private readonly string[] _smallFileExtensions;
    private readonly string[] _largeFileExtensions;
    private readonly string[] _matchFilePaths;
    private readonly string[] _excludeFilePaths;
    private readonly bool _doFilePathMatching;
    private bool _isClosing;
    private bool _isClosed;

    public override bool CanRead
    {
        get
        {
            return true;
        }
    }

    public override bool CanSeek
    {
        get
        {
            return true;
        }
    }

    public override bool CanWrite
    {
        get
        {
            return true;
        }
    }

    public override long Length
    {
        get
        {
            return 0L;
        }
    }

    public override long Position { get; set; }

    public CdnFilter(Stream inputStream, Uri currentUrl)
    {
        _responseStream = inputStream;
        var smallDomainName = string.IsNullOrWhiteSpace(CdnSmallDomain) ? string.Empty : CdnSmallDomain;
        var largeDomainName = string.IsNullOrWhiteSpace(CdnLargeDomain) ? string.Empty : CdnLargeDomain;
        if (!string.IsNullOrEmpty(smallDomainName) || !string.IsNullOrEmpty(largeDomainName))
            _doCdnRewrite = true;
        if (!string.IsNullOrWhiteSpace(FilePathsToMatch))
        {
            _matchFilePaths = FilePathsToMatch.Split(',');
            _doFilePathMatching = _matchFilePaths.Length > 0;
        }
        else
            _doCdnRewrite = false;
        if (!string.IsNullOrWhiteSpace(SmallFileExtensionsToMatch))
        {
            _smallFileExtensions = SmallFileExtensionsToMatch.Split(',');
        }
        if (!string.IsNullOrWhiteSpace(LargeFileExtensionsToMatch))
        {
            _largeFileExtensions = LargeFileExtensionsToMatch.Split(',');
        }
        if (!_smallFileExtensions.Any() && !_largeFileExtensions.Any())
            _doCdnRewrite = false;
        bool forceSsl;
        if (!string.IsNullOrWhiteSpace(ForceSsl) && bool.TryParse(ForceSsl, out forceSsl))
            _useSsl = forceSsl;
        // Loads the request headers as a collection
        var headers = HttpContext.Current.Request.Headers;
        // Gets the value from the X-Forwarded-Ssl header
        var forwardedSsl = headers.Get("X-Forwarded-Ssl");
        var protoSsl = headers.Get("X-Forwarded-Proto");
        // Checks if the original request used HTTPS
        if ((!string.IsNullOrEmpty(forwardedSsl) && forwardedSsl == "On") || (!string.IsNullOrEmpty(protoSsl) && protoSsl == "https"))
        {
            _useSsl = true;
        }
        if (!string.IsNullOrWhiteSpace(FilePathsToIgnore))
        {
            var chArray = new[] { ',' };
            _excludeFilePaths = FilePathsToIgnore.Split(chArray);
            foreach (var str in _excludeFilePaths)
            {
                if (HttpContext.Current.Request.RawUrl.Contains(str))
                    _doCdnRewrite = false;
            }
        }
        _cdnParser = new CdnParser(_useSsl, smallDomainName, largeDomainName, _smallFileExtensions, _largeFileExtensions, _doFilePathMatching, _matchFilePaths, _excludeFilePaths, currentUrl);
    }

    public override void Flush()
    {
        if (_isClosing && !_isClosed)
        {
            var encoding = HttpContext.Current.Response.ContentEncoding;
            if (_cachedStream != null)
            {
                var cachedContent = encoding.GetString(_cachedStream.ToArray());
{'fraction_non_alphanumeric': 0.08934745030250649, 'fraction_numerical': 0.00432152117545376, 'mean_word_length': 2.7783673469387753, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '7524687', 'n_tokens_mistral': 2582, 'n_tokens_neox': 2508, 'n_words': 506}