-
Notifications
You must be signed in to change notification settings - Fork 0
/
res.go
73 lines (61 loc) · 1.7 KB
/
res.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
package delta
import (
"bytes"
"encoding/base64"
"errors"
"io"
"net/http"
)
var _ http.ResponseWriter = &ResponseWriter{}
var _ io.StringWriter = &ResponseWriter{}
var _ io.ReaderFrom = &ResponseWriter{}
// ResponseWriter implements http.ResponseWriter used for buffering response
type ResponseWriter struct {
header http.Header
buffer bytes.Buffer
status int
encode bool
}
// NewResponseWriter creates new empty ResponseWriter
func NewResponseWriter() *ResponseWriter {
res := &ResponseWriter{
header: make(http.Header),
status: 200,
}
return res
}
// Header returns http.Header. You can modify it to send response header
func (rw *ResponseWriter) Header() http.Header {
return rw.header
}
// Write appends chunk to response body
func (rw *ResponseWriter) Write(chunk []byte) (int, error) {
return rw.buffer.Write(chunk)
}
// WriteHeader set status code of current request
func (rw *ResponseWriter) WriteHeader(statusCode int) {
rw.status = statusCode
}
func (rw *ResponseWriter) bodyString() string {
if !rw.encode {
return rw.buffer.String()
}
return base64.RawStdEncoding.EncodeToString(rw.buffer.Bytes())
}
// ReadFrom implements
func (rw *ResponseWriter) ReadFrom(r io.Reader) (n int64, err error) {
return rw.buffer.ReadFrom(r)
}
// WriteString implements io.StringWriter
func (rw *ResponseWriter) WriteString(s string) (n int, err error) {
return rw.buffer.WriteString(s)
}
// SetBase64Encoding overrides base64 encoding for this response
// see Configuration.SetBase64Encoding
func SetBase64Encoding(w http.ResponseWriter, enabled bool) error {
if rw, ok := w.(*ResponseWriter); ok {
rw.encode = enabled
return nil
}
return errors.New("SetBase64Encoding: invalid ResponseWriter object")
}