1 Star 0 Fork 0

jinyule/cow

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
error.go 1.86 KB
一键复制 编辑 原始数据 按行查看 历史
Chen Yufei 提交于 2013-10-31 13:42 . Show host name in error page.
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)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jinyule/cow.git
git@gitee.com:jinyule/cow.git
jinyule
cow
cow
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385