-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
70 lines (67 loc) · 1.57 KB
/
sync.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
/*
@Author: Trial(Trialpro@gmail.com)
@Creat: 2022/4/16 13:30
@Notice: 同步请求
*/
package penguin
import (
"io/ioutil"
"net/http"
)
//Sync 同步请求
func (this *PenguinHttp) Sync(url string) (*PenguinResult, error) {
req, err := this.makeReq(url)
if err != nil {
return nil, err
}
//Cookies build
for _, cookie := range this.cookie {
req.AddCookie(cookie)
}
do, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
//res := &PenguinResult{
// Status: do.Status,
// StatusCode: do.StatusCode,
// Proto: do.Proto,
// ProtoMajor: do.ProtoMajor,
// ProtoMinor: do.ProtoMinor,
// Header: do.Header,
// ContentLength: do.ContentLength,
// TransferEncoding: do.TransferEncoding,
// Close: do.Close,
// Uncompressed: do.Uncompressed,
// Trailer: do.Trailer,
// TLS: do.TLS,
// Cookies: make(map[string]string),
//}
res := this.pool.Get().(*PenguinResult)
{
res.Status = do.Status
res.StatusCode = do.StatusCode
res.Proto = do.Proto
res.ProtoMajor = do.ProtoMajor
res.ProtoMinor = do.ProtoMinor
res.Header = do.Header
res.TransferEncoding = do.TransferEncoding
res.Close = do.Close
res.Uncompressed = do.Uncompressed
res.Trailer = do.Trailer
res.TLS = do.TLS
}
defer func() {
do.Body.Close()
res.Clear()
this.pool.Put(res)
}()
res.bin, _ = ioutil.ReadAll(do.Body)
cookies := do.Cookies()
if len(cookies) > 0 {
for _, cookie := range cookies {
res.Cookies[cookie.Name] = cookie.Value
}
}
return res, nil
}