aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/owner.go
blob: 02288f75827e4fb74a44e9b7c4837fc59e5b018a (plain)
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
package admin

import (
	"bytes"
	"errors"
	"io"
	"net/http"
)

const maxAdminRequestBytes = 8 << 20

// ownerMiddleware is the single execution boundary for ALL editor API routes,
// including reads (which must not observe partial edits) and undo/redo. Planning,
// file writes, runtime callbacks, reloads and history updates stay in one task.
// Register new API routes behind this boundary; handlers must never redispatch.
// Network I/O happens outside the task so slow browsers cannot block gameplay.
func (s *AdminServer) ownerMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, maxAdminRequestBytes))
		r.Body.Close()
		if err != nil {
			status := http.StatusBadRequest
			var tooLarge *http.MaxBytesError
			if errors.As(err, &tooLarge) {
				status = http.StatusRequestEntityTooLarge
			}
			writeJSONError(w, "cannot read request body: "+err.Error(), status)
			return
		}
		if r.Context().Err() != nil {
			return
		}
		r = r.Clone(r.Context())
		r.Body = io.NopCloser(bytes.NewReader(body))
		response := &bufferedResponse{header: make(http.Header)}
		var panicValue any
		if !s.runOnOwner(func() {
			// Preserve net/http's panic recovery on the HTTP goroutine;
			// a handler panic must not kill the game's owner loop.
			defer func() { panicValue = recover() }()
			if r.Context().Err() == nil {
				next.ServeHTTP(response, r)
			}
		}) {
			writeJSONError(w, "game is shutting down", http.StatusServiceUnavailable)
			return
		}
		if panicValue != nil {
			panic(panicValue)
		}
		for key, values := range response.header {
			w.Header()[key] = values
		}
		if response.status != 0 {
			w.WriteHeader(response.status)
		}
		_, _ = w.Write(response.body.Bytes())
	})
}

// bufferedResponse deliberately provides no streaming/hijacking interfaces:
// editor API handlers produce finite responses, never network streams.
type bufferedResponse struct {
	header http.Header
	status int
	body   bytes.Buffer
}

func (w *bufferedResponse) Header() http.Header { return w.header }

func (w *bufferedResponse) WriteHeader(status int) {
	if w.status == 0 {
		w.status = status
	}
}

func (w *bufferedResponse) Write(p []byte) (int, error) {
	if w.status == 0 {
		w.WriteHeader(http.StatusOK)
	}
	return w.body.Write(p)
}