-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_http.go
More file actions
183 lines (166 loc) · 4.56 KB
/
Copy pathrunner_http.go
File metadata and controls
183 lines (166 loc) · 4.56 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
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
package main
import (
"context"
"crypto/subtle"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"strings"
"time"
)
const defaultRunnerPort = 7821
type runnerHTTP struct {
mgr *watchManager
ln net.Listener
srv *http.Server
startedTime time.Time
}
func newRunnerHTTP(mgr *watchManager) (*runnerHTTP, error) {
port := mgr.port
if port == 0 {
port = defaultRunnerPort
}
return newRunnerHTTPOnPort(mgr, port)
}
// newRunnerHTTPOnPort binds on the requested port. Tests use this with
// port 0 (OS-assigned) to avoid clashing with a real daemon.
func newRunnerHTTPOnPort(mgr *watchManager, port int) (*runnerHTTP, error) {
addr := fmt.Sprintf("127.0.0.1:%d", port)
ln, err := net.Listen("tcp", addr)
if err != nil && port != 0 {
log.Printf("runner: %s busy, falling back to OS-assigned port", addr)
ln, err = net.Listen("tcp", "127.0.0.1:0")
}
if err != nil {
return nil, fmt.Errorf("listen %s: %w", addr, err)
}
if ta, ok := ln.Addr().(*net.TCPAddr); ok {
mgr.port = ta.Port
}
h := &runnerHTTP{
mgr: mgr,
ln: ln,
startedTime: time.Now(),
}
mux := http.NewServeMux()
mux.HandleFunc("/w/", h.handleWatch)
mux.HandleFunc("/healthz", h.handleHealthz)
h.srv = &http.Server{
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
}
return h, nil
}
func (h *runnerHTTP) serve() {
if err := h.srv.Serve(h.ln); err != nil && err != http.ErrServerClosed {
log.Printf("runnerhttp: serve: %v", err)
}
}
func (h *runnerHTTP) shutdown() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
_ = h.srv.Shutdown(shutdownCtx)
cancel()
}
func (h *runnerHTTP) handleHealthz(w http.ResponseWriter, r *http.Request) {
tok := r.URL.Query().Get("t")
if subtle.ConstantTimeCompare([]byte(tok), []byte(h.mgr.daemonToken)) != 1 {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
h.mgr.mu.Lock()
count := len(h.mgr.entries)
h.mgr.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"ok": true,
"version": Version,
"port": h.mgr.port,
"uptime_s": int64(time.Since(h.startedTime).Seconds()),
"watch_count": count,
})
}
func (h *runnerHTTP) handleWatch(w http.ResponseWriter, r *http.Request) {
id, sub, ok := splitWatchPath(r.URL.Path)
if !ok {
http.NotFound(w, r)
return
}
h.mgr.mu.Lock()
e, exists := h.mgr.entries[id]
h.mgr.mu.Unlock()
if !exists || e.state == nil {
http.NotFound(w, r)
return
}
tok := r.URL.Query().Get("t")
if subtle.ConstantTimeCompare([]byte(tok), []byte(e.info.Token)) != 1 {
log.Printf("runnerhttp: /w/%s bad token from %s", id, r.RemoteAddr)
http.Error(w, "forbidden", http.StatusForbidden)
return
}
switch sub {
case "":
serveWatchIndex(e.state, w, r)
case "events":
serveWatchEvents(e.state, w, r)
default:
http.NotFound(w, r)
}
}
// serveWatchIndex renders the preview HTML. Wraps watchState.handleIndex
// without its path != "/" check (which made sense in foreground mode where
// any non-root path was a 404, but the runner routes under /w/<id>).
func serveWatchIndex(s *watchState, w http.ResponseWriter, r *http.Request) {
html, _ := s.snapshot()
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
_, _ = w.Write(html)
}
// serveWatchEvents streams SSE updates. Wraps watchState.handleEvents to
// swap the connection-check path (the foreground handler checked "/" and
// 404'd otherwise; the runner's path /w/<id>/events is already validated
// by the mux).
func serveWatchEvents(s *watchState, w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("X-Accel-Buffering", "no")
_, content := s.snapshot()
writeSSE(w, flusher, "content", content)
ch := s.subscribe()
defer s.unsubscribe(ch)
ctx := r.Context()
for {
select {
case <-ctx.Done():
return
case data, open := <-ch:
if !open {
return
}
writeSSE(w, flusher, "content", data)
}
}
}
func splitWatchPath(p string) (id, sub string, ok bool) {
const prefix = "/w/"
if !strings.HasPrefix(p, prefix) {
return "", "", false
}
rest := strings.TrimPrefix(p, prefix)
parts := strings.SplitN(rest, "/", 2)
if len(parts) == 0 || parts[0] == "" {
return "", "", false
}
if len(parts) == 2 {
return parts[0], parts[1], true
}
return parts[0], "", true
}