package server import ( "net/http" "strings" "time" ) // This gate is outside authorization; it never grants access. DB mode=ro and // query_only enforce a second boundary even if a GET handler attempts a write. // The data directory should additionally be mounted read-only on standby. func (s *Server) withStandbyReadOnly(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/health/read" || r.URL.Path == "/health/write" { w.Header().Set("Cache-Control", "no-store") if r.Method != "GET" && r.Method != "HEAD" { w.WriteHeader(http.StatusMethodNotAllowed) return } ready := !s.store.ReadOnly || (r.URL.Path == "/health/read" && time.Now().Before(s.store.StandbyReadUntil)) if ready && s.store.DB != nil { ready = s.store.DB.PingContext(r.Context()) == nil } if !ready { http.Error(w, "not ready", http.StatusServiceUnavailable) return } w.WriteHeader(http.StatusOK) return } if !s.store.ReadOnly { next.ServeHTTP(w, r) return } w = &standbyResponseWriter{ResponseWriter: w} w.Header().Set("X-PagesWiki-DR-Role", "standby") w.Header().Set("Cache-Control", "private, no-store") fail := func(message string) { w.Header().Set("Retry-After", "60") http.Error(w, message, http.StatusServiceUnavailable) } // Health remains a liveness endpoint, NOT proof that this node can write. if r.URL.Path == "/health" && (r.Method == "GET" || r.Method == "HEAD") { next.ServeHTTP(w, r) return } if s.store.StandbyReadUntil.IsZero() || !time.Now().Before(s.store.StandbyReadUntil) { fail("DR snapshot expired; routing or offline refresh is required") return } if r.Method != "GET" && r.Method != "HEAD" && r.Method != "OPTIONS" && r.Method != "PROPFIND" && r.Method != "REPORT" { fail("DR standby is read-only; write request was not executed") return } // Authentication issuance, edit launch, federation and external renderers // can have side effects even on GET. Do not invoke them on a standby. for _, prefix := range []string{"/_/auth/", "/-/sys/", "/e/", "/o/e/", "/n/", "/:/", "/new", "/v/upload", "/_/adapter-gateway", "/_/adaptor/", "/_/api/v1/federation", "/_/api/v1/adapter", "/v/open", "/v/import", "/v/compile", "/_/api/v1/import"} { if strings.HasPrefix(r.URL.Path, prefix) { fail("operation unavailable on DR standby") return } } // V1 standby does not serve organizer or DAV endpoints: their current // read paths repair file-backed indexes and update credential metadata. // They become available after promotion; no false successful sync responses. for _, prefix := range []string{"/v/plan", "/v/calendar", "/v/event", "/v/tasks", "/_/api/v1/calendar", "/_/api/v1/caldav", "/_/api/v1/carddav", "/_/api/v1/task", "/_/api/v1/extension/", "/.well-known/caldav", "/.well-known/carddav"} { if strings.HasPrefix(r.URL.Path, prefix) { fail("organizer/DAV read service requires DR promotion in this version") return } } next.ServeHTTP(w, r) }) } // Immutable/raw handlers normally set long cache lifetimes. A standby must not // let those headers outlive the snapshot's authorization freshness deadline. type standbyResponseWriter struct { http.ResponseWriter wrote bool } func (w *standbyResponseWriter) WriteHeader(status int) { if w.wrote { return } w.wrote = true w.Header().Set("Cache-Control", "private, no-store") w.Header().Set("X-PagesWiki-DR-Role", "standby") w.ResponseWriter.WriteHeader(status) } func (w *standbyResponseWriter) Write(b []byte) (int, error) { if !w.wrote { w.WriteHeader(http.StatusOK) } return w.ResponseWriter.Write(b) } func (w *standbyResponseWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter }