forked from lalluviamola/web-blog
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil_http.go
More file actions
137 lines (127 loc) · 3.7 KB
/
util_http.go
File metadata and controls
137 lines (127 loc) · 3.7 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bytes"
"compress/gzip"
"encoding/json"
"encoding/xml"
"html/template"
"io/ioutil"
"net/http"
"path/filepath"
"strconv"
"strings"
)
func serveFileMust(w http.ResponseWriter, r *http.Request, path string) {
if r == nil {
d := readFileMust(path)
_, err := w.Write(d)
must(err)
return
}
http.ServeFile(w, r, path)
}
func acceptsGzip(r *http.Request) bool {
// TODO: would be safer to split by ", "
return r != nil && strings.Contains(r.Header.Get("Accept-Encoding"), "gzip")
}
func serveMaybeGzippedFile(w http.ResponseWriter, r *http.Request, path string) {
logf(ctx(), "path: '%s'\n", path)
if !pathExists(path) {
http.NotFound(w, r)
return
}
contentType := mimeTypeFromFileName(path)
usesGzip := acceptsGzip(r)
if usesGzip {
if pathExists(path + ".gz") {
path = path + ".gz"
} else {
usesGzip = false
}
}
if len(contentType) > 0 {
w.Header().Set("Content-Type", contentType)
}
// https://www.maxcdn.com/blog/accept-encoding-its-vary-important/
// prevent caching non-gzipped version
w.Header().Add("Vary", "Accept-Encoding")
if usesGzip {
w.Header().Set("Content-Encoding", "gzip")
}
d, err := ioutil.ReadFile(path)
if err != nil {
logerrf(ctx(), "ioutil.ReadFile('%s') failed with '%s'\n", path, err)
http.NotFound(w, r)
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(d)))
w.WriteHeader(200)
w.Write(d)
}
func serveRelativeFile(w http.ResponseWriter, r *http.Request, relativePath string) {
path := filepath.Join("www", relativePath)
serveMaybeGzippedFile(w, r, path)
}
func serveTemplate(w http.ResponseWriter, r *http.Request, relativePath string, data interface{}) {
path := filepath.Join("www", relativePath)
if !pathExists(path) {
path = filepath.Join("www", "tools", relativePath)
}
if !pathExists(path) {
http.NotFound(w, r)
return
}
files := []string{path}
tmpl := template.Must(template.ParseFiles(files...))
name := filepath.Base(relativePath)
var buf bytes.Buffer
err := tmpl.ExecuteTemplate(&buf, name, data)
panicIfErr(err)
httpOkWithHTML(w, r, buf.Bytes())
}
func httpOkBytesWithContentType(w http.ResponseWriter, r *http.Request, contentType string, content []byte) {
w.Header().Set("Content-Type", contentType)
// https://www.maxcdn.com/blog/accept-encoding-its-vary-important/
// prevent caching non-gzipped version
w.Header().Add("Vary", "Accept-Encoding")
if acceptsGzip(r) {
w.Header().Set("Content-Encoding", "gzip")
// Maybe: if len(content) above certain size, write as we go (on the other
// hand, if we keep uncompressed data in memory...)
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
gz.Write(content)
gz.Close()
content = buf.Bytes()
}
w.Header().Set("Content-Length", strconv.Itoa(len(content)))
w.Write(content)
}
func httpOkWithHTML(w http.ResponseWriter, r *http.Request, d []byte) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(d)
}
func httpOkWithJSON(w http.ResponseWriter, r *http.Request, v interface{}) {
b, err := json.MarshalIndent(v, "", "\t")
if err != nil {
// should never happen
logerrf(ctx(), "json.MarshalIndent() failed with %q\n", err)
}
httpOkBytesWithContentType(w, r, "application/json", b)
}
func httpOkWithJSONCompact(w http.ResponseWriter, r *http.Request, v interface{}) {
b, err := json.Marshal(v)
if err != nil {
// should never happen
logerrf(ctx(), "json.MarshalIndent() failed with %q\n", err)
}
httpOkBytesWithContentType(w, r, "application/json", b)
}
func httpOkWithXML(w http.ResponseWriter, r *http.Request, v interface{}) {
b, err := xml.MarshalIndent(v, "", "\t")
if err != nil {
// should never happen
logerrf(ctx(), "xml.MarshalIndent() failed with %q\n", err)
}
httpOkBytesWithContentType(w, r, "text/xml", b)
}