-
Notifications
You must be signed in to change notification settings - Fork 25
/
rw.go
52 lines (45 loc) · 1.14 KB
/
rw.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
package requests
import (
"errors"
"io"
"net"
"net/url"
)
type readWriteCloser struct {
body io.ReadCloser
conn *connecotr
}
func (obj *readWriteCloser) Conn() net.Conn {
return obj.conn.rawConn.(net.Conn)
}
func (obj *readWriteCloser) Read(p []byte) (n int, err error) {
i, err := obj.body.Read(p)
if err == io.EOF {
obj.Close()
}
return i, err
}
func (obj *readWriteCloser) Proxys() []*url.URL {
if l := len(obj.conn.proxys); l > 0 {
proxys := make([]*url.URL, l)
for i, proxy := range obj.conn.proxys {
proxys[i] = cloneUrl(proxy)
}
}
return obj.conn.proxys
}
var errGospiderBodyClose = errors.New("gospider body close error")
func (obj *readWriteCloser) Close() (err error) {
obj.conn.bodyCnl(errGospiderBodyClose)
err = obj.body.Close() //reuse conn
return
}
// safe close conn
func (obj *readWriteCloser) CloseConn() {
obj.conn.bodyCnl(errors.New("readWriterCloser close conn"))
obj.conn.safeCnl(errors.New("readWriterCloser close conn"))
}
// force close conn
func (obj *readWriteCloser) ForceCloseConn() {
obj.conn.rawConn.CloseWithError(errConnectionForceClosed)
}