-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.go
279 lines (235 loc) · 7.88 KB
/
http.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package modulir
//go:generate go run scripts/embed_js/main.go
import (
"errors"
"fmt"
"net/http"
"path"
"sync"
"text/template"
"time"
"github.com/gorilla/websocket"
"golang.org/x/xerrors"
)
//////////////////////////////////////////////////////////////////////////////
//
//
//
// Public
//
//
//
//////////////////////////////////////////////////////////////////////////////
// Starts serving the built site over HTTP on the configured port. A server
// instance is returned so that it can be shut down gracefully.
func startServingTargetDirHTTP(c *Context, buildComplete *sync.Cond) *http.Server {
c.Log.Infof("Serving '%s' to: http://localhost:%v/", path.Clean(c.TargetDir), c.Port)
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir(c.TargetDir)))
if c.Websocket {
mux.HandleFunc("/websocket.js", getWebsocketJSHandler(c))
mux.HandleFunc("/websocket", getWebsocketHandler(c, buildComplete))
}
server := &http.Server{
Addr: fmt.Sprintf(":%v", c.Port),
Handler: mux,
ReadHeaderTimeout: 5 * time.Second, // protect against Slowloris attack
}
go func() {
err := server.ListenAndServe()
// ListenAndServe always returns a non-nil error (but if started
// successfully, it'll block for a long time).
if !errors.Is(err, http.ErrServerClosed) {
exitWithError(xerrors.Errorf("error starting HTTP server: %w", err))
}
}()
return server
}
//////////////////////////////////////////////////////////////////////////////
//
//
//
// Private
//
//
//
//////////////////////////////////////////////////////////////////////////////
// A type representing the extremely basic messages that we'll be serializing
// and sending back over a websocket.
type websocketEvent struct {
Type string `json:"type"`
}
const (
// Maximum message size allowed from peer.
websocketMaxMessageSize = 512
// The frequency at which to send pings back to clients connected over a
// websocket. Must be less than websocketPongWait.
websocketPingPeriod = (websocketPongWait * 9) / 10
// Time allowed to read the next pong message from the peer.
websocketPongWait = 10 * time.Second
// Time allowed to write a message to the peer.
websocketWriteWait = 10 * time.Second
)
// A template that will render the websocket JavaScript code that connecting
// clients will load and run. The `websocketJS` source of this template comes
// from `js.go` which is generated from sources found in the `./js` directory
// with `go generate`.
var websocketJSTemplate = template.Must(template.New("websocket.js").Parse(websocketJS))
// Part of the Gorilla websocket infrastructure that upgrades HTTP connections
// to websocket connections when we see an incoming websocket request.
var websocketUpgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// Thought about doing localhost only, but it may cause trouble for
// something eventually. If end user can connect to the web page,
// assume they're also safe for websockets.
return true
},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func getWebsocketHandler(c *Context, buildComplete *sync.Cond) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
conn, err := websocketUpgrader.Upgrade(w, r, nil)
if err != nil {
c.Log.Errorf("Error upgrading websocket connection: %v", err)
return
}
connClosed := make(chan struct{}, 1)
go websocketReadPump(c, conn, connClosed)
go websocketWritePump(c, conn, connClosed, buildComplete)
c.Log.Infof(logPrefix(c, conn) + "Opened")
}
}
func getWebsocketJSHandler(c *Context) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/javascript")
err := websocketJSTemplate.Execute(w, map[string]interface{}{
"Port": c.Port,
})
if err != nil {
c.Log.Errorf("Error executing template/writing websocket.js: %v", err)
return
}
}
}
// Produces a log prefix like `<WebSocket [::1]:53555>` which is colored if
// appropriate.
func logPrefix(c *Context, conn *websocket.Conn) string {
return fmt.Sprintf(c.colorizer.Bold("<WebSocket %v> ").String(),
conn.RemoteAddr())
}
func websocketReadPump(c *Context, conn *websocket.Conn, connClosed chan struct{}) {
defer func() {
conn.Close()
connClosed <- struct{}{}
}()
conn.SetReadLimit(websocketMaxMessageSize)
if err := conn.SetReadDeadline(time.Now().Add(websocketPongWait)); err != nil {
c.Log.Errorf(logPrefix(c, conn)+"Couldn't set WebSocket read deadline: %v",
err)
return
}
conn.SetPongHandler(func(string) error {
c.Log.Debugf(logPrefix(c, conn) + "Received pong")
if err := conn.SetReadDeadline(time.Now().Add(websocketPongWait)); err != nil {
c.Log.Errorf(logPrefix(c, conn)+"Couldn't set WebSocket read deadline: %v",
err)
}
return nil
})
for {
_, _, err := conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err) {
c.Log.Infof(logPrefix(c, conn)+"Closed: %v", err)
} else {
c.Log.Errorf(logPrefix(c, conn)+"Error reading message: %v",
err)
}
break
}
// We don't expect clients to send anything right now, so just ignore
// incoming messages.
}
c.Log.Debugf(logPrefix(c, conn) + "Read pump ending")
}
func websocketWritePump(c *Context, conn *websocket.Conn,
connClosed chan struct{}, buildComplete *sync.Cond,
) {
ticker := time.NewTicker(websocketPingPeriod)
defer func() {
ticker.Stop()
conn.Close()
}()
var done bool
var writeErr error
sendComplete := make(chan struct{}, 1)
// This is a hack because of course there's no way to select on a
// conditional variable. Instead, we have a separate Goroutine wait on the
// conditional variable and signal the main select below through a channel.
buildCompleteChan := make(chan struct{}, 1)
go func() {
for {
buildComplete.L.Lock()
buildComplete.Wait()
buildComplete.L.Unlock()
buildCompleteChan <- struct{}{}
// Break out of the Goroutine when we can to prevent a Goroutine
// leak.
//
// Unfortunately this isn't perfect. If we were sending a
// build_complete, the Goroutine will die right away because the
// wait below will fall through after the message was fully
// received, and the client-side JavaScript will being the page
// reload and close the websocket before that occurs. That's good.
//
// What isn't so good is that for other exit conditions like a
// closed connection or a failed ping, the Goroutine will still be
// waiting on the conditional variable's Wait above, and not exit
// right away. The good news is that the next build event that
// triggers will cause it to fall through and end the Goroutine. So
// it will eventually be cleaned up, but that clean up may be
// delayed.
<-sendComplete
if done {
break
}
}
c.Log.Debugf(logPrefix(c, conn) + "Build complete feeder ending")
}()
for {
select {
case <-buildCompleteChan:
if err := conn.SetWriteDeadline(time.Now().Add(websocketWriteWait)); err != nil {
c.Log.Errorf(logPrefix(c, conn)+"Couldn't set WebSocket read deadline: %v",
err)
}
writeErr = conn.WriteJSON(websocketEvent{Type: "build_complete"})
// Send shouldn't strictly need to be non-blocking, but we do one
// anyway just to hedge against future or unexpected problems so as
// not to accidentally stall out this loop.
select {
case sendComplete <- struct{}{}:
default:
}
case <-connClosed:
done = true
case <-ticker.C:
c.Log.Debugf(logPrefix(c, conn) + "Sending ping")
if err := conn.SetWriteDeadline(time.Now().Add(websocketWriteWait)); err != nil {
c.Log.Errorf(logPrefix(c, conn)+"Couldn't set WebSocket read deadline: %v",
err)
}
writeErr = conn.WriteMessage(websocket.PingMessage, nil)
}
if writeErr != nil {
c.Log.Errorf(logPrefix(c, conn)+"Error writing: %v",
writeErr)
done = true
}
if done {
break
}
}
c.Log.Debugf(logPrefix(c, conn) + "Write pump ending")
}