<commit_before>package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"text/template"
	"time"
)

var errPageRawTmpl = `<!DOCTYPE html>
<html>
	<head> <title>Proxy error</title> </head>
	<body>
		<h1>[Error] {{.H1}}</h1>
		{{.Msg}}
		{{.Form}}
		<hr />
		Generated by <i>cow-proxy</i> at {{.T}}
	</body>
</html>
`

// Use GET to simplify form processing. Strictly speaking, this has side
// effects and should use POST.
var blockedFormRawTmpl = `<p></p>
		<form action="http://{{.ListenAddr}}/blocked" method="get">
		<input type="hidden" name="uri" value={{.RequestURI}}>
		<b>Refresh to retry</b> or
		<input type="submit" name="add" value="Add {{.Domain}} to blocked sites">
		</form>
`

// Do not end with "\r\n" so we can add more header later
var headRawTmpl = "HTTP/1.1 {{.CodeReason}}\r\n" +
	"Connection: keep-alive\r\n" +
	"Cache-Control: no-cache\r\n" +
	"Pragma: no-cache\r\n" +
	"Content-Type: text/html\r\n" +
	"Content-Length: {{.Length}}\r\n"

var errPageTmpl, headTmpl, blockedFormTmpl *template.Template

func init() {
	var err error
	if headTmpl, err = template.New("errorHead").Parse(headRawTmpl); err != nil {
		fmt.Println("Internal error on generating error head template")
		os.Exit(1)
	}
	if errPageTmpl, err = template.New("errorPage").Parse(errPageRawTmpl); err != nil {
		fmt.Println("Internal error on generating error page template")
		os.Exit(1)
	}
	if blockedFormTmpl, err = template.New("blockedForm").Parse(blockedFormRawTmpl); err != nil {
		fmt.Println("Internal error on generating blocked form template")
		os.Exit(1)
	}
}

func genErrorPage(errMsg, detailedMsg, form string) (string, error) {
	var err error
	data := struct {
		H1   string
		Msg  string
		Form string
		T    string
	}{
		errMsg,
		detailedMsg,
		form,
		time.Now().Format(time.ANSIC),
	}

	buf := new(bytes.Buffer)
	err = errPageTmpl.Execute(buf, data)
	return buf.String(), err
}

func sendErrorPageGeneric(w *bufio.Writer, errCodeReason, errMsg, detailedMsg, form string) {
	page, err := genErrorPage(errMsg, detailedMsg, form)
	if err != nil {
		errl.Println("Error generating error page:", err)
		return
	}

	data := struct {
		CodeReason string
		Length     int
	}{
		errCodeReason,
		len(page),
	}
	buf := new(bytes.Buffer)
	if err := headTmpl.Execute(buf, data); err != nil {
		errl.Println("Error generating error page header:", err)
		return
	}

	w.WriteString(buf.String())
	w.WriteString("\r\n")
	w.WriteString(page)
	w.Flush()
}

func sendErrorPage(w *bufio.Writer, errCodeReason, errMsg, detailedMsg string) {
	sendErrorPageGeneric(w, errCodeReason, errMsg, detailedMsg, "")
}

func sendBlockedErrorPage(w *bufio.Writer, errCodeReason, errMsg, detailedMsg string, r *Request) {
	data := struct {<commit_msg>Variable renaming in error.go<commit_after>package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"text/template"
	"time"
)

var errPageRawTmpl = `<!DOCTYPE html>
<html>
	<head> <title>Proxy error</title> </head>
	<body>
		<h1>[Error] {{.H1}}</h1>
		{{.Msg}}
		{{.Form}}
		<hr />
		Generated by <i>cow-proxy</i> at {{.T}}
	</body>
</html>
`

// Use GET to simplify form processing. Strictly speaking, this has side
// effects and should use POST.
var blockedFormRawTmpl = `<p></p>
		<form action="http://{{.ListenAddr}}/blocked" method="get">
		<input type="hidden" name="uri" value={{.RequestURI}}>
		<b>Refresh to retry</b> or
		<input type="submit" name="add" value="Add {{.Domain}} to blocked sites">
		</form>
`

// Do not end with "\r\n" so we can add more header later
var headRawTmpl = "HTTP/1.1 {{.CodeReason}}\r\n" +
	"Connection: keep-alive\r\n" +
	"Cache-Control: no-cache\r\n" +
	"Pragma: no-cache\r\n" +
	"Content-Type: text/html\r\n" +
	"Content-Length: {{.Length}}\r\n"

var errPageTmpl, headTmpl, blockedFormTmpl *template.Template

func init() {
	var err error
	if headTmpl, err = template.New("errorHead").Parse(headRawTmpl); err != nil {
		fmt.Println("Internal error on generating error head template")
		os.Exit(1)
	}
	if errPageTmpl, err = template.New("errorPage").Parse(errPageRawTmpl); err != nil {
		fmt.Println("Internal error on generating error page template")
		os.Exit(1)
	}
	if blockedFormTmpl, err = template.New("blockedForm").Parse(blockedFormRawTmpl); err != nil {
		fmt.Println("Internal error on generating blocked form template")
		os.Exit(1)
	}
}

func genErrorPage(h1, msg, form string) (string, error) {
	var err error
	data := struct {
		H1   string
		Msg  string
		Form string
		T    string
	}{
		h1,
		msg,
		form,
		time.Now().Format(time.ANSIC),
	}

	buf := new(bytes.Buffer)
	err = errPageTmpl.Execute(buf, data)
	return buf.String(), err
}

func sendErrorPageGeneric(w *bufio.Writer, errCodeReason, h1, msg, form string) {
	page, err := genErrorPage(h1, msg, form)
	if err != nil {
		errl.Println("Error generating error page:", err)
		return
	}

	data := struct {
		CodeReason string
		Length     int
	}{
		errCodeReason,
		len(page),
	}
	buf := new(bytes.Buffer)
	if err := headTmpl.Execute(buf, data); err != nil {
		errl.Println("Error generating error page header:", err)
		return
	}

	w.WriteString(buf.String())
	w.WriteString("\r\n")
	w.WriteString(page)
	w.Flush()
}

func sendErrorPage(w *bufio.Writer, errCodeReason, errMsg, detailedMsg string) {
	sendErrorPageGeneric(w, errCodeReason, errMsg, detailedMsg, "")
}

func sendBlockedErrorPage(w *bufio.Writer, errCodeReason, errMsg, detailedMsg string, r *Request) {
	data := struct {
{'fraction_non_alphanumeric': 0.16149870801033592, 'fraction_numerical': 0.004060538944259874, 'mean_word_length': 4.117091595845137, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, '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': '7668111', 'n_tokens_mistral': 2110, 'n_tokens_neox': 1997, 'n_words': 505}