-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwap.go
63 lines (51 loc) · 1.02 KB
/
rwap.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
package rwap
import (
"net/http"
"strconv"
)
// Rwap ...
type Rwap struct {
http.ResponseWriter
status int
contentLength int64
}
// New ...
func New(w http.ResponseWriter) *Rwap {
return &Rwap{ResponseWriter: w}
}
func (w *Rwap) Write(p []byte) (int, error) {
cl, err := w.ResponseWriter.Write(p)
w.contentLength += int64(cl)
return cl, err
}
// WriteHeader ...
func (w *Rwap) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}
// Status ...
func (w *Rwap) Status() int {
return w.status
}
// ContentLength ...
func (w *Rwap) ContentLength() int64 {
if w.contentLength > 0 {
return w.contentLength
}
cl, err := strconv.ParseInt(w.Header().Get("Content-Length"), 10, 64)
if err != nil {
return 0
}
return cl
}
// Wrap ...
func Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(New(w), r)
})
}
// AsRwap ...
func AsRwap(w http.ResponseWriter) (*Rwap, bool) {
rw, ok := w.(*Rwap)
return rw, ok
}