/
header.go
53 lines (42 loc) · 984 Bytes
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package doc
import (
"net/http"
"text/template"
)
var (
headerTmpl *template.Template
headerFmt = `{{with .DisplayHeader}} + Headers
{{range $name, $vals := .}}
{{$name}}: {{$vals | commaJoin}}{{end}}{{end}}
`
)
func init() {
funcMap := template.FuncMap{
"commaJoin": commaJoin,
}
headerTmpl = template.Must(template.New("headers").Funcs(funcMap).Parse(headerFmt))
}
type Header struct {
DisplayHeader http.Header
ContentType string
}
func NewHeader(h http.Header) *Header {
hCopy := make(http.Header, len(h))
CopyHeader(hCopy, h)
// remove header fields we don't want in the doc
delete(hCopy, "Accept-Encoding")
delete(hCopy, "User-Agent")
delete(hCopy, "Content-Length")
if len(hCopy) == 0 {
return nil
}
contentType := hCopy.Get("Content-Type")
delete(hCopy, "Content-Type")
return &Header{
DisplayHeader: hCopy,
ContentType: contentType,
}
}
func (h Header) Render() string {
return render(headerTmpl, h)
}