<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Rotativa
{
    public abstract class AsPdfResultBase: ActionResult
    {
        private const string ContentType = "application/pdf";

        public string FileName { get; set; }
        public string WkhtmltopdfPath { get; set; }

        protected AsPdfResultBase()
        {
            this.WkhtmltopdfPath = string.Empty;
        }

        protected abstract string GetUrl(ControllerContext context);

        private string GetWkParams(ControllerContext context)
        {
            var switches = string.Empty;

            HttpCookie authenticationCookie = context.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authenticationCookie != null)
            {
                var authCookieValue = authenticationCookie.Value;
                switches += " --cookie .ASPXAUTH " + authCookieValue;
            }

            var url = GetUrl(context);
            switches += " " + url;
            return switches;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            var response = PrepareResponse(context.HttpContext.Response);

            var switches = GetWkParams(context);

            if (this.WkhtmltopdfPath == "") this.WkhtmltopdfPath = HttpContext.Current.Server.MapPath("~/Rotativa");
            var fileContent = Convert(this.WkhtmltopdfPath, switches);

            response.OutputStream.Write(fileContent, 0, fileContent.Length); 
        }
        
        private HttpResponseBase PrepareResponse(HttpResponseBase response)
        {
            response.ContentType = ContentType;

            if (!String.IsNullOrEmpty(this.FileName))
            {
                // TODO: strip non US_ANSI chars from the filename
                response.AddHeader("Content-Disposition", "attachment; filename=" + this.FileName);
            }
            response.AddHeader("Content-Type", ContentType);
            return response;
        }

        private static byte[] Convert(string wkhtmltopdfPath, string switches)
        {
            // adding the switch to ouput on stdout
            switches += " -"; 
            var proc = new Process()
            {
                StartInfo = new ProcessStartInfo()
                {
                    FileName = Path.Combine(wkhtmltopdfPath, "wkhtmltopdf.exe"),
                    Arguments = switches,<commit_msg>now you can set the cookie name<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Rotativa
{
    public abstract class AsPdfResultBase : ActionResult
    {
        private const string ContentType = "application/pdf";

        public string FileName { get; set; }
        public string WkhtmltopdfPath { get; set; }
        public string CookieName { get; set; }

        protected AsPdfResultBase()
        {
            this.WkhtmltopdfPath = string.Empty;
            this.CookieName = ".ASPXAUTH";
        }

        protected abstract string GetUrl(ControllerContext context);

        private string GetWkParams(ControllerContext context)
        {
            var switches = string.Empty;

            HttpCookie authenticationCookie = context.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authenticationCookie != null)
            {
                var authCookieValue = authenticationCookie.Value;
                switches += " --cookie " + CookieName + " " + authCookieValue;
            }

            var url = GetUrl(context);
            switches += " " + url;
            return switches;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            var response = PrepareResponse(context.HttpContext.Response);

            var switches = GetWkParams(context);

            if (this.WkhtmltopdfPath == "") this.WkhtmltopdfPath = HttpContext.Current.Server.MapPath("~/Rotativa");
            var fileContent = Convert(this.WkhtmltopdfPath, switches);

            response.OutputStream.Write(fileContent, 0, fileContent.Length);
        }

        private HttpResponseBase PrepareResponse(HttpResponseBase response)
        {
            response.ContentType = ContentType;

            if (!String.IsNullOrEmpty(this.FileName))
            {
                // TODO: strip non US_ANSI chars from the filename
                response.AddHeader("Content-Disposition", "attachment; filename=" + this.FileName);
            }
            response.AddHeader("Content-Type", ContentType);
            return response;
        }

        private static byte[] Convert(string wkhtmltopdfPath, string switches)
        {
            // adding the switch to ouput on stdout
            switches += " -";
            var proc = new Process()
            {
                StartInfo = new ProcessStartInfo()
                {
                    FileName = Path.Combine(wkhtmltopdfPath, "wkhtmltopdf.exe"),
                    Arguments = switches,
{'fraction_non_alphanumeric': 0.08517179023508137, 'fraction_numerical': 0.0003616636528028933, 'mean_word_length': 2.0473829201101927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6785282', 'n_tokens_mistral': 1406, 'n_tokens_neox': 1332, 'n_words': 326}