-
Notifications
You must be signed in to change notification settings - Fork 0
/
cURL.go
297 lines (224 loc) · 6.39 KB
/
cURL.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package PHPer4GoLang
import (
// "log"
// "strconv"
"errors"
"time"
"net/http"
"strings"
"io"
"io/ioutil"
"crypto/tls"
)
type cURLHandler struct {
url string ;
method string ;
returntransfer bool ;
inited int ;
postfields string ;
is_post bool ;
is_header bool ;
opt_header []string ;
http_Transport http.Transport ;
http_Client http.Client ;
http_Response *http.Response ;
TLS *tls.ConnectionState ;
base_uri string ;
} ;
type cURLInfo struct {
Url string ;
Content_type string ;
Http_code int ;
Header_size int ;
Request_size int ;
Filetime int ;
Ssl_verify_result int ;
Redirect_count int ;
Total_time int ;
Namelookup_time int ;
Connect_time int ;
Pretransfer_time int ;
Size_upload int ;
Size_download int ;
Speed_download int ;
Speed_upload int ;
Download_content_length int64 ;
Upload_content_length int ;
Starttransfer_time int ;
Redirect_time int ;
Certinfo []string ;
Redirect_url string ;
} ;
type GuzzleResponse struct {
ch *cURLHandler ;
method string ;
url string ;
payload string ;
stat int ;
} ;
type GuzzleBody struct {
res *GuzzleResponse ;
} ;
func GuzzleHttpClient(anys ... any) (cURLHandler,error){
err := errors.New("ASSERT") ;
var ch cURLHandler ;
ch.method = "GET" ;
if(len(anys) > 0){
for kk,vv := range anys[0].(map[string]any){
switch(kk){
case "base_uri":{
ch.base_uri = vv.(string) ;
}
}
}
}
err = nil ;
return ch,err ;
}
func (ch *cURLHandler) Request(anys ... any) (res GuzzleResponse,err error) {
err = Err(1,"HOHO[%v]",123) ;
res.ch = ch ;
if(len(anys) > 0){
res.method = anys[0].(string) ;
}
if(len(anys) > 1){
res.url = anys[1].(string) ;
}
if(len(anys) > 2){
for kk,vv := range anys[2].(map[string]any){
_ = vv ;
switch(kk){
case "http_errors":{
}
case "allow_redirects":{
}
case "headers":{
}
case "json":{
}
case "form_params":{
}
}
}
}
res.payload = "RESrES" ;
res.stat = 201 ;
return ;
}
func (res *GuzzleResponse) GetStatusCode (anys ... any) int {
return res.stat ;
}
func (res *GuzzleResponse) GetBody(anys ... any) GuzzleBody {
var body GuzzleBody ;
body.res = res ;
return body ;
}
func (body *GuzzleBody) GetContents(anys ... any) string {
return body.res.payload ;
}
func Curl_init() (cURLHandler,error){
err := errors.New("ASSERT") ;
var ch cURLHandler ;
ch.method = "GET" ;
err = nil ;
return ch,err ;
}
func Curl_setopt(v ...any){
var ch *cURLHandler = v[0].(*cURLHandler) ;
switch(v[1]){
case CURLOPT_URL:{
ch.url = Strval(v[2]) ;
Debugf("CURLOPT_URL[%s]",ch.url) ;
}
case CURLOPT_HTTPHEADER:{
ch.opt_header = v[2].([]string) ;
}
case CURLOPT_RETURNTRANSFER:{
ch.returntransfer = Boolval(v[2]) ;
}
case CURLOPT_HEADER:{
ch.is_header = Boolval(v[2]) ;
}
case CURLOPT_POST:{
ch.is_post = Boolval(v[2]) ;
}
case CURLOPT_POSTFIELDS:{
ch.postfields = Strval(v[2]) ;
}
}
}
func Curl_exec(ch *cURLHandler) (string,error){
payload := "" ;
err := errors.New("ASSERT") ;
if(ch.inited != 1){
ch.http_Transport.MaxIdleConns = 10 ;
ch.http_Transport.IdleConnTimeout = 30 * time.Second ;
ch.http_Transport.DisableCompression = true ;
ch.http_Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
} ;
ch.http_Client.Transport = &(ch.http_Transport) ;
ch.inited = 1 ;
}
method := ch.method ;
var io_Reader_Post io.Reader = nil ;
if(ch.is_post == true){
method = "POST" ;
io_Reader_Post = strings.NewReader(ch.postfields) ;
}
http_NewRequest , err := http.NewRequest(method,ch.url,io_Reader_Post) ;
if(err != nil){
Debugf("ERR[%v]",err) ;
}else{
if(false){
http_NewRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")
http_NewRequest.Header.Set("Content-Type", "application/json")
}
if(len(ch.opt_header) > 0){
for _,line := range ch.opt_header{
match,err := Preg_match(`^([^\:]+)\s*:\s*(.*)$`,line) ;
if(err == nil){
http_NewRequest.Header.Set(match[1],match[2]) ;
}
}
}
ch.http_Response , err = ch.http_Client.Do(http_NewRequest) ;
if(err != nil){
Debugf("ERR[%v]",err) ;
}else{
defer ch.http_Response.Body.Close() ;
body, err := ioutil.ReadAll(ch.http_Response.Body) ;
if(err != nil){
Debugf("ERR[%v]",err) ;
}else{
payload = string(body) ;
ch.TLS = ch.http_Response.TLS ;
for k,vs := range ch.http_Response.Header{
for idx,v := range vs{
_ = idx ;
_ = k ;
_ = v ;
// Debugf("[%v][%v][%v]",idx,k,v) ;
}
}
}
}
}
return payload , err ;
}
func Curl_close(ch *cURLHandler){
}
func Curl_getinfo(ch *cURLHandler) (cURLInfo){
var info cURLInfo ;
// Debugf("ServerName[%v]",ch.TLS.ServerName) ;
info.Http_code = ch.http_Response.StatusCode ;
info.Download_content_length = ch.http_Response.ContentLength ;
if(ch.TLS != nil){
if(ch.TLS.HandshakeComplete){
info.Ssl_verify_result = 1 ;
}else{
info.Ssl_verify_result = 0 ;
}
}
return info ;
}