forked from d1nfinite/zhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzhttp.go
138 lines (110 loc) · 3.72 KB
/
zhttp.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package zhttp
import (
"github.com/greyh4t/dnscache"
"golang.org/x/net/publicsuffix"
"net/http"
"net/http/cookiejar"
"runtime"
)
type Zhttp struct {
options *HttpOptions
dnsCache *dnscache.Resolver
transport *http.Transport
}
// New generate an *Zhttp client to send request
func New(options *HttpOptions) *Zhttp {
z := &Zhttp{options: options}
if z.options == nil {
z.options = &HttpOptions{}
}
if z.options.DNSCacheExpire > 0 {
if z.options.DNSServer != "" {
z.dnsCache = dnscache.NewCustomServer(z.options.DNSCacheExpire, z.options.DNSServer)
} else {
z.dnsCache = dnscache.New(z.options.DNSCacheExpire)
}
ensureDNSCacheFinalized(z.dnsCache)
}
z.transport = z.createTransport(z.options)
ensureTransporterFinalized(z.transport)
return z
}
// NewWithDNSCache generate an *Zhttp client that uses an external DNSCache
// This function will ignore HttpOptions.DNSCacheExpire and HttpOptions.DNSServer
func NewWithDNSCache(options *HttpOptions, cache *dnscache.Resolver) *Zhttp {
z := &Zhttp{options: options}
if z.options == nil {
z.options = &HttpOptions{}
}
if cache != nil {
z.dnsCache = cache
}
z.transport = z.createTransport(z.options)
ensureTransporterFinalized(z.transport)
return z
}
func ensureDNSCacheFinalized(resolver *dnscache.Resolver) {
runtime.SetFinalizer(&resolver, func(resolver **dnscache.Resolver) {
(*resolver).Close()
})
}
func ensureTransporterFinalized(httpTransport *http.Transport) {
runtime.SetFinalizer(&httpTransport, func(transportInt **http.Transport) {
(*transportInt).CloseIdleConnections()
})
}
// NewSession generate an client that will handle session for all request
func (z *Zhttp) NewSession() *Session {
s := &Session{Zhttp: z}
s.CookieJar, _ = cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
return s
}
func (z *Zhttp) Request(method, url string, options *ReqOptions) (*Response, error) {
return z.doRequest(method, url, options, nil)
}
func (z *Zhttp) Get(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("GET", url, options, nil)
}
func (z *Zhttp) Delete(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("DELETE", url, options, nil)
}
func (z *Zhttp) Head(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("HEAD", url, options, nil)
}
func (z *Zhttp) Patch(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("PATCH", url, options, nil)
}
func (z *Zhttp) Post(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("POST", url, options, nil)
}
func (z *Zhttp) Put(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("PUT", url, options, nil)
}
func (z *Zhttp) Options(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("OPTIONS", url, options, nil)
}
var z = New(nil)
func Request(method, url string, options *ReqOptions) (*Response, error) {
return z.doRequest(method, url, options, nil)
}
func Get(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("GET", url, options, nil)
}
func Delete(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("DELETE", url, options, nil)
}
func Head(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("HEAD", url, options, nil)
}
func Patch(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("PATCH", url, options, nil)
}
func Post(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("POST", url, options, nil)
}
func Put(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("PUT", url, options, nil)
}
func Options(url string, options *ReqOptions) (*Response, error) {
return z.doRequest("OPTIONS", url, options, nil)
}