-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
116 lines (100 loc) · 2.09 KB
/
pipeline.go
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
package slot
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
func newPipeline(rw http.ResponseWriter) *pipeline {
r, wc := newPipe()
return &pipeline{
rw: rw,
state: &state{
Named: map[string]string{},
},
headers: http.Header{},
reader: bytes.NewBuffer(nil),
writer: wc,
next: r,
}
}
type pipeline struct {
rw http.ResponseWriter
state *state
headers http.Header
reader io.Reader
writer io.WriteCloser
next io.Reader
}
var _ http.ResponseWriter = (*pipeline)(nil)
func (pl *pipeline) Header() http.Header {
return pl.headers
}
func (pl *pipeline) WriteHeader(statusCode int) {
pl.rw.WriteHeader(statusCode)
}
func (pl *pipeline) Write(p []byte) (n int, err error) {
pl.state.Data += string(p)
return len(p), nil
}
func (pl *pipeline) WriteTo(slot string, p []byte) (n int, err error) {
pl.state.Named[slot] += string(p)
return len(p), nil
}
func (pl *pipeline) Close() error {
enc := json.NewEncoder(pl.writer)
enc.SetEscapeHTML(false)
if err := enc.Encode(pl.state); err != nil {
return err
}
// Flush headers as we close the writer
rh := pl.rw.Header()
for name := range pl.headers {
value := pl.headers.Get(name)
// Prefer headers set earlier in the pipeline vs later, they're more
// specific, e.g. view vs. layout.
if rh.Get(name) == "" {
rh.Set(name, value)
}
}
return pl.writer.Close()
}
func (pl *pipeline) Read(p []byte) (n int, err error) {
return pl.reader.Read(p)
}
func (pl *pipeline) Next() *pipeline {
r, wc := newPipe()
return &pipeline{
rw: pl.rw,
state: &state{
Named: pl.state.Named,
},
// Fresh set of headers to support concurrency
headers: http.Header{},
reader: pl.next,
writer: wc,
next: r,
}
}
func newPipe() (io.Reader, io.WriteCloser) {
pipe := &pipe{
b: new(bytes.Buffer),
c: make(chan struct{}),
}
return pipe, pipe
}
type pipe struct {
b *bytes.Buffer
c chan struct{}
}
func (p *pipe) Write(b []byte) (n int, err error) {
return p.b.Write(b)
}
func (p *pipe) Close() error {
close(p.c)
return nil
}
func (p *pipe) Read(b []byte) (n int, err error) {
<-p.c
return p.b.Read(b)
}