Files
magpie/internal/gateway/capture.go
T
yetone 3c04877cfc archive: a request the archive kept is opened and downloaded from the Usage page, with its bodies whole (jorben, #447: 请求存档 reachable from usage history, full bodies, download) — the archive's name was only on the Gateway page's recent calls, gone once a call left the list or the gateway restarted, and the archive kept the first 256 KB of each body, the recent list's cut. The usage ledger's row now names the call's archive (archive in usage.jsonl, kept through the Requests page's packed rows), and its details on Usage → Requests show the Request archive panel, Fetch from archive and Download; with the archive on, a request it has no copy of says "Not archived". The bodies are kept whole up to requestArchiveMaxMB in settings.json (32 MB unless set, at most 1024): the response is teed to a temp file as it is sent, the request body written to one when the call ends, so the upload queue holds file handles, not bodies, and one upload at a time reads them; a body past the limit is kept to it and marked cut, with its whole size. What goes upstream is unchanged, the archive stays off unless turned on, headers and bodies are scrubbed as before, and the temp files are removed after the upload or when the queue is full. The page is sent a body to show only when it is 256 KB or less (a longer one is said by its size), an archive over 80 MB is only said to be; Download streams the object whole, to Downloads in the app (archive/export, -2, -3… for a name taken) and as the browser's download in magpie web (archive/file). TestArchiveWholeBodies, TestArchiveLimit, TestRowKeepsArchive and TestArchivePreviewAndDownload fail without the change; TestArchiveOffUsage, TestArchiveTooLargeToPreview, TestArchiveOldPreview keep the rest. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; request-archive-usage (new), request-archive, click-scroll, old-webkit, usage-ledger, usage-ledger-detail, usage-ledger-content, web-page, sse-body pass.
2026-10-02 00:57:51 +08:00

117 lines
2.5 KiB
Go

package gateway
import (
"bytes"
"io"
"net/http"
"os"
)
// Recent-call bodies are diagnostics, not an unbounded traffic log. Keeping the
// first 256 KiB is enough to inspect ordinary prompts and responses while
// preventing images or long streams from multiplying into tens of MiB across
// 40 calls.
const callBodyLimit = 256 << 10
type capturedBody struct {
buf bytes.Buffer
truncated bool
}
func (c *capturedBody) add(p []byte) {
if c.buf.Len() >= callBodyLimit {
c.truncated = c.truncated || len(p) > 0
return
}
n := min(len(p), callBodyLimit-c.buf.Len())
_, _ = c.buf.Write(p[:n])
if n < len(p) {
c.truncated = true
}
}
func (c *capturedBody) text() string { return c.buf.String() }
func captureRequestBody(p []byte) (string, bool) {
var c capturedBody
c.add(p)
return c.text(), c.truncated
}
type captureResponseWriter struct {
http.ResponseWriter
body capturedBody
// full: the whole body, for the request archive, when it is on
full *spool
}
func (w *captureResponseWriter) Write(p []byte) (int, error) {
w.body.add(p)
if w.full != nil {
w.full.add(p)
}
return w.ResponseWriter.Write(p)
}
// spool keeps a body whole for the request archive, beyond the first 256
// KiB Recent calls holds: in a temporary file, not in memory, up to limit
// bytes; past that only its size is counted. A file that can't be written
// leaves what was kept so far, cut.
type spool struct {
f *os.File
limit, kept int64
size int64 // every byte that came, kept or not
failed bool
}
func (s *spool) add(p []byte) {
s.size += int64(len(p))
if s.failed || s.kept >= s.limit || len(p) == 0 {
return
}
if s.f == nil {
f, err := os.CreateTemp("", "magpie-archive-*")
if err != nil {
s.failed = true
return
}
s.f = f
}
n := min(int64(len(p)), s.limit-s.kept)
if _, err := s.f.Write(p[:n]); err != nil {
s.failed = true
return
}
s.kept += n
}
// cut is whether less than the whole body was kept.
func (s *spool) cut() bool { return s.kept < s.size }
// read is what was kept, the file gone after.
func (s *spool) read() ([]byte, error) {
if s.f == nil {
return nil, nil
}
defer s.discard()
if _, err := s.f.Seek(0, io.SeekStart); err != nil {
return nil, err
}
return io.ReadAll(io.LimitReader(s.f, s.kept))
}
// discard removes the file: nothing is kept.
func (s *spool) discard() {
if s.f != nil {
s.f.Close()
os.Remove(s.f.Name())
s.f = nil
}
}
func (w *captureResponseWriter) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}