-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuffer.go
62 lines (53 loc) · 1.33 KB
/
buffer.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
package wasm
import (
"bytes"
"io"
"net/http"
)
type bufferingRequestBody struct {
delegate io.ReadCloser
buffer bytes.Buffer
}
// Read buffers anything read from the delegate.
func (b *bufferingRequestBody) Read(p []byte) (n int, err error) {
n, err = b.delegate.Read(p)
if err != nil && n > 0 {
b.buffer.Write(p[0:n])
}
return
}
// Close dispatches to the delegate.
func (b *bufferingRequestBody) Close() (err error) {
if b.delegate != nil {
err = b.delegate.Close()
}
return
}
type bufferingResponseWriter struct {
delegate http.ResponseWriter
statusCode uint32
body []byte
}
// Header dispatches to the delegate.
func (w *bufferingResponseWriter) Header() http.Header {
return w.delegate.Header()
}
// Write buffers the response body.
func (w *bufferingResponseWriter) Write(bytes []byte) (int, error) {
w.body = append(w.body, bytes...)
return len(bytes), nil
}
// WriteHeader buffers the status code.
func (w *bufferingResponseWriter) WriteHeader(statusCode int) {
w.statusCode = uint32(statusCode)
}
// release sends any response data collected.
func (w *bufferingResponseWriter) release() {
// If we deferred the response, release it.
if statusCode := w.statusCode; statusCode != 0 {
w.delegate.WriteHeader(int(statusCode))
}
if body := w.body; len(body) != 0 {
w.delegate.Write(body) // nolint
}
}