-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttpbuf.go
More file actions
46 lines (41 loc) · 1003 Bytes
/
httpbuf.go
File metadata and controls
46 lines (41 loc) · 1003 Bytes
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
package httpbuf
import (
"bytes"
"net/http"
"sync"
)
//Buffer is a type that implements http.ResponseWriter but buffers all the data
//and headers.
type Buffer struct {
bytes.Buffer
resp int
headers http.Header
once sync.Once
}
//Header implements the header method of http.ResponseWriter
func (b *Buffer) Header() http.Header {
b.once.Do(func() {
b.headers = make(http.Header)
})
return b.headers
}
//WriteHeader implements the WriteHeader method of http.ResponseWriter
func (b *Buffer) WriteHeader(resp int) {
b.resp = resp
}
//Apply takes an http.ResponseWriter and calls the required methods on it to
//output the buffered headers, response code, and data. It returns the number
//of bytes written and any errors flushing.
func (b *Buffer) Apply(w http.ResponseWriter) (n int, err error) {
if len(b.headers) > 0 {
h := w.Header()
for key, val := range b.headers {
h[key] = val
}
}
if b.resp > 0 {
w.WriteHeader(b.resp)
}
n, err = w.Write(b.Bytes())
return
}