代码拉取完成,页面将自动刷新
package main
import (
"bytes"
"io"
"os"
"text/template"
"time"
)
// 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 *template.Template
func init() {
hostName, err := os.Hostname()
if err != nil {
hostName = "unknown host"
}
errPageRawTmpl := `<!DOCTYPE html>
<html>
<head> <title>COW Proxy</title> </head>
<body>
<h1>{{.H1}}</h1>
{{.Msg}}
<hr />
Generated by <i>COW ` + version + `</i> <br />
Host <i>` + hostName + `</i> <br />
{{.T}}
</body>
</html>
`
if headTmpl, err = template.New("errorHead").Parse(headRawTmpl); err != nil {
Fatal("Internal error on generating error head template")
}
if errPageTmpl, err = template.New("errorPage").Parse(errPageRawTmpl); err != nil {
Fatalf("Internal error on generating error page template")
}
}
func genErrorPage(h1, msg string) (string, error) {
var err error
data := struct {
H1 string
Msg string
T string
}{
h1,
msg,
time.Now().Format(time.ANSIC),
}
buf := new(bytes.Buffer)
err = errPageTmpl.Execute(buf, data)
return buf.String(), err
}
func sendPageGeneric(w io.Writer, codeReason, h1, msg string) {
page, err := genErrorPage(h1, msg)
if err != nil {
errl.Println("Error generating error page:", err)
return
}
data := struct {
CodeReason string
Length int
}{
codeReason,
len(page),
}
buf := new(bytes.Buffer)
if err := headTmpl.Execute(buf, data); err != nil {
errl.Println("Error generating error page header:", err)
return
}
buf.WriteString("\r\n")
buf.WriteString(page)
w.Write(buf.Bytes())
}
func sendErrorPage(w io.Writer, codeReason, h1, msg string) {
sendPageGeneric(w, codeReason, "[Error] "+h1, msg)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。