-
Notifications
You must be signed in to change notification settings - Fork 0
/
body.go
48 lines (40 loc) · 892 Bytes
/
body.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
package http
import (
"io"
"net/http"
)
type NoCloseBody struct {
s string
i int64 // current reading index
prevRune int // index of previous rune; or < 0
}
func (r *NoCloseBody) Len() int {
if r.i >= int64(len(r.s)) {
return 0
}
return int(int64(len(r.s)) - r.i)
}
func (r *NoCloseBody) Size() int64 { return int64(len(r.s)) }
func (r *NoCloseBody) Read(b []byte) (n int, err error) {
if r.i >= int64(len(r.s)) {
return 0, io.EOF
}
r.prevRune = -1
n = copy(b, r.s[r.i:])
r.i += int64(n)
return
}
func (r *NoCloseBody) Close() error {
r.i = 0
return nil
}
// 适用于轮询
func NewNoCloseBody(s string) *NoCloseBody { return &NoCloseBody{s, 0, -1} }
func NewNoCloseRequest(req *http.Request, s string) {
v := NewNoCloseBody(s)
req.ContentLength = int64(v.Len())
req.Body = v
req.GetBody = func() (io.ReadCloser, error) {
return v, nil
}
}