Repository navigation
Expand file tree
/
Copy pathwriter.go
More file actions
89 lines (81 loc) · 2.74 KB
/
Copy pathwriter.go
File metadata and controls
89 lines (81 loc) · 2.74 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
package backend
import (
"errors"
"fmt"
"io"
"log"
"net/http"
"sync/atomic"
)
const defaultChunkSize = 4096
// flushResponseWriter wraps http.ResponseWriter to chunk output and
// flush after each write. Implements io.ReaderFrom and io.Closer.
type flushResponseWriter struct {
http.ResponseWriter
log *log.Logger
chunkSize int
// started records whether the response status line has been committed
// (first WriteHeader, or first Write — which implicitly commits a 200).
// Once true, an error surfacing afterwards can only be logged:
// renderStatusError would both race the concurrent writer and be unable to
// change the already-sent status. While still false the caller may safely
// render a real error status instead of an implicit 200.
//
// It is atomic because Write/WriteHeader may run on a different goroutine
// from the handler that reads it: the server wraps this writer in a
// context writer (utils/ioutil) that drives the underlying Write from a
// spawned goroutine. That goroutine is joined before Serve returns, so the
// read is already ordered after the write — atomic keeps it sound without
// relying on that non-local happens-before.
started atomic.Bool
}
// WriteHeader records that the response has started, then delegates.
func (f *flushResponseWriter) WriteHeader(code int) {
f.started.Store(true)
f.ResponseWriter.WriteHeader(code)
}
// Write records that the response has started (the first Write commits a 200
// status if WriteHeader was not called), then delegates.
func (f *flushResponseWriter) Write(p []byte) (int, error) {
f.started.Store(true)
return f.ResponseWriter.Write(p)
}
// ReadFrom implements io.ReaderFrom.
func (f *flushResponseWriter) ReadFrom(r io.Reader) (int64, error) {
flusher := http.NewResponseController(f.ResponseWriter)
var n int64
p := make([]byte, f.chunkSize)
for {
nr, rerr := r.Read(p)
// An io.Reader may return data together with io.EOF, so write what was
// read before acting on the error — breaking on EOF first would drop
// the final chunk and truncate the response.
if nr > 0 {
nw, werr := f.Write(p[:nr])
n += int64(nw)
if werr != nil {
// Body partially written; renderStatusError would race the writer.
f.log.Printf("error writing response: %v", werr)
return n, werr
}
if nw != nr {
return n, io.ErrShortWrite
}
if ferr := flusher.Flush(); ferr != nil {
f.log.Printf("error flushing response: %v", ferr)
return n, fmt.Errorf("flush response: %w", ferr)
}
}
if rerr != nil {
if errors.Is(rerr, io.EOF) {
return n, nil
}
f.log.Printf("error reading source: %v", rerr)
return n, rerr
}
}
}
// Close implements io.Closer. It is a no-op.
func (f *flushResponseWriter) Close() error {
return nil
}