1package mox
2
3import (
4 "bytes"
5 "compress/gzip"
6 "context"
7 "errors"
8 "fmt"
9 "io"
10 "log/slog"
11 "net/http"
12 "os"
13 "runtime"
14 "strings"
15 "sync"
16 "time"
17
18 "github.com/mjl-/mox/mlog"
19 "github.com/mjl-/mox/moxvar"
20)
21
22// WebappFile serves a merged HTML and JS webapp as a single compressed, cacheable
23// file. It merges the JS into the HTML at first load, caches a gzipped version
24// that is generated on first need, and responds with a Last-Modified header.
25type WebappFile struct {
26 HTML, JS []byte // Embedded html/js data.
27 HTMLPath, JSPath string // Paths to load html/js from during development.
28
29 sync.Mutex
30 combined []byte
31 combinedGzip []byte
32 mtime time.Time // For Last-Modified and conditional request.
33}
34
35// FallbackMtime returns a time to use for the Last-Modified header in case we
36// cannot find a file, e.g. when used in production.
37func FallbackMtime(log mlog.Log) time.Time {
38 p, err := os.Executable()
39 log.Check(err, "finding executable for mtime")
40 if err == nil {
41 st, err := os.Stat(p)
42 log.Check(err, "stat on executable for mtime")
43 if err == nil {
44 return st.ModTime()
45 }
46 }
47 log.Info("cannot find executable for webappfile mtime, using current time")
48 return time.Now()
49}
50
51func (a *WebappFile) serverError(log mlog.Log, w http.ResponseWriter, err error, action string) {
52 log.Errorx("serve webappfile", err, slog.String("msg", action))
53 http.Error(w, "500 - internal server error", http.StatusInternalServerError)
54}
55
56// Serve serves a combined file, with headers for caching and possibly gzipped.
57func (a *WebappFile) Serve(ctx context.Context, log mlog.Log, w http.ResponseWriter, r *http.Request) {
58 // We typically return the embedded file, but during development it's handy
59 // to load from disk.
60 fhtml, _ := os.Open(a.HTMLPath)
61 if fhtml != nil {
62 defer fhtml.Close()
63 }
64 fjs, _ := os.Open(a.JSPath)
65 if fjs != nil {
66 defer fjs.Close()
67 }
68
69 html := a.HTML
70 js := a.JS
71
72 var diskmtime time.Time
73 var refreshdisk bool
74 if fhtml != nil && fjs != nil {
75 sth, err := fhtml.Stat()
76 if err != nil {
77 a.serverError(log, w, err, "stat html")
78 return
79 }
80 stj, err := fjs.Stat()
81 if err != nil {
82 a.serverError(log, w, err, "stat js")
83 return
84 }
85
86 maxmtime := sth.ModTime()
87 if stj.ModTime().After(maxmtime) {
88 maxmtime = stj.ModTime()
89 }
90
91 a.Lock()
92 refreshdisk = maxmtime.After(a.mtime) || a.combined == nil
93 a.Unlock()
94
95 if refreshdisk {
96 html, err = io.ReadAll(fhtml)
97 if err != nil {
98 a.serverError(log, w, err, "reading html")
99 return
100 }
101 js, err = io.ReadAll(fjs)
102 if err != nil {
103 a.serverError(log, w, err, "reading js")
104 return
105 }
106 diskmtime = maxmtime
107 }
108 }
109
110 gz := AcceptsGzip(r)
111 var out []byte
112 var mtime time.Time
113 var origSize int64
114
115 func() {
116 a.Lock()
117 defer a.Unlock()
118
119 if refreshdisk || a.combined == nil {
120 script := []byte(`<script>/* placeholder */</script>`)
121 index := bytes.Index(html, script)
122 if index < 0 {
123 a.serverError(log, w, errors.New("script not found"), "generating combined html")
124 return
125 }
126 var b bytes.Buffer
127 b.Write(html[:index])
128 fmt.Fprintf(&b, "<script>\n// Javascript is generated from typescript, don't modify the javascript because changes will be lost.\nconst moxversion = \"%s\";\nconst moxgoversion = \"%s\";\nconst moxgoos = \"%s\";\nconst moxgoarch = \"%s\";\n", moxvar.Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
129 b.Write(js)
130 b.WriteString("\t\t</script>")
131 b.Write(html[index+len(script):])
132 out = b.Bytes()
133 a.combined = out
134 if refreshdisk {
135 a.mtime = diskmtime
136 } else {
137 a.mtime = FallbackMtime(log)
138 }
139 a.combinedGzip = nil
140 } else {
141 out = a.combined
142 }
143 if gz {
144 if a.combinedGzip == nil {
145 var b bytes.Buffer
146 gzw, err := gzip.NewWriterLevel(&b, gzip.BestCompression)
147 if err == nil {
148 _, err = gzw.Write(out)
149 }
150 if err == nil {
151 err = gzw.Close()
152 }
153 if err != nil {
154 a.serverError(log, w, err, "gzipping combined html")
155 return
156 }
157 a.combinedGzip = b.Bytes()
158 }
159 origSize = int64(len(out))
160 out = a.combinedGzip
161 }
162 mtime = a.mtime
163 }()
164
165 w.Header().Set("Content-Type", "text/html; charset=utf-8")
166 http.ServeContent(gzipInjector{w, gz, origSize}, r, "", mtime, bytes.NewReader(out))
167}
168
169// gzipInjector is a http.ResponseWriter that optionally injects a
170// Content-Encoding: gzip header, only in case of status 200 OK. Used with
171// http.ServeContent to serve gzipped content if the client supports it. We cannot
172// just unconditionally add the content-encoding header, because we don't know
173// enough if we will be sending data: http.ServeContent may be sending a "not
174// modified" response, and possibly others.
175type gzipInjector struct {
176 http.ResponseWriter // Keep most methods.
177 gz bool
178 origSize int64
179}
180
181// WriteHeader adds a Content-Encoding: gzip header before actually writing the
182// headers and status.
183func (w gzipInjector) WriteHeader(statusCode int) {
184 if w.gz && statusCode == http.StatusOK {
185 w.ResponseWriter.Header().Set("Content-Encoding", "gzip")
186 if lw, ok := w.ResponseWriter.(interface{ SetUncompressedSize(int64) }); ok {
187 lw.SetUncompressedSize(w.origSize)
188 }
189 }
190 w.ResponseWriter.WriteHeader(statusCode)
191}
192
193// AcceptsGzip returns whether the client accepts gzipped responses.
194func AcceptsGzip(r *http.Request) bool {
195 s := r.Header.Get("Accept-Encoding")
196 t := strings.Split(s, ",")
197 for _, e := range t {
198 e = strings.TrimSpace(e)
199 tt := strings.Split(e, ";")
200 if len(tt) > 1 && t[1] == "q=0" {
201 continue
202 }
203 if tt[0] == "gzip" {
204 return true
205 }
206 }
207 return false
208}
209