-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.go
More file actions
947 lines (838 loc) · 31.4 KB
/
http.go
File metadata and controls
947 lines (838 loc) · 31.4 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
/*
* Copyright (c) 2020. Aberic - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gnomon
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/xml"
"github.com/golang/protobuf/proto"
"github.com/vmihailenco/msgpack"
"gopkg.in/yaml.v3"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
)
// HTTPGet get 请求
func HTTPGet(url string) (resp *http.Response, err error) {
return HTTPGetTLS(url, &HTTPTLSConfig{})
}
// HTTPPostJSON post 请求
//
// content-type=application/json
func HTTPPostJSON(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPostJSONTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPutJSON put 请求
//
// content-type=application/json
func HTTPPutJSON(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPutJSONTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPatchJSON patch 请求
//
// content-type=application/json
func HTTPPatchJSON(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPatchJSONTLS(url, model, &HTTPTLSConfig{})
}
// HTTPDeleteJSON delete 请求
func HTTPDeleteJSON(url string, model interface{}) (resp *http.Response, err error) {
return HTTPDeleteJSONTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPostXML post 请求
//
// content-type=application/xml
func HTTPPostXML(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPostXMLTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPutXML put 请求
//
// content-type=application/xml
func HTTPPutXML(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPutXMLTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPatchXML patch 请求
//
// content-type=application/xml
func HTTPPatchXML(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPatchXMLTLS(url, model, &HTTPTLSConfig{})
}
// HTTPDeleteXML delete 请求
func HTTPDeleteXML(url string, model interface{}) (resp *http.Response, err error) {
return HTTPDeleteXMLTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPostYaml post 请求
//
// content-type=application/x-yaml
func HTTPPostYaml(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPostYamlTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPutYaml put 请求
//
// content-type=application/x-yaml
func HTTPPutYaml(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPutYamlTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPatchYaml patch 请求
//
// content-type=application/x-yaml
func HTTPPatchYaml(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPatchYamlTLS(url, model, &HTTPTLSConfig{})
}
// HTTPDeleteYaml delete 请求
func HTTPDeleteYaml(url string, model interface{}) (resp *http.Response, err error) {
return HTTPDeleteYamlTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPostMsgPack post 请求
//
// content-type=application/x-msgpack
func HTTPPostMsgPack(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPostMsgPackTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPutMsgPack put 请求
//
// content-type=application/x-msgpack
func HTTPPutMsgPack(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPutMsgPackTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPatchMsgPack patch 请求
//
// content-type=application/x-msgpack
func HTTPPatchMsgPack(url string, model interface{}) (resp *http.Response, err error) {
return HTTPPatchMsgPackTLS(url, model, &HTTPTLSConfig{})
}
// HTTPDeleteMsgPack delete 请求
func HTTPDeleteMsgPack(url string, model interface{}) (resp *http.Response, err error) {
return HTTPDeleteMsgPackTLS(url, model, &HTTPTLSConfig{})
}
// HTTPPostProtoBuf post 请求
//
// content-type=application/x-protobuf
func HTTPPostProtoBuf(url string, pm proto.Message) (resp *http.Response, err error) {
return HTTPPostProtoBufTLS(url, pm, &HTTPTLSConfig{})
}
// HTTPPutProtoBuf put 请求
//
// content-type=application/x-protobuf
func HTTPPutProtoBuf(url string, pm proto.Message) (resp *http.Response, err error) {
return HTTPPutProtoBufTLS(url, pm, &HTTPTLSConfig{})
}
// HTTPPatchProtoBuf patch 请求
//
// content-type=application/x-protobuf
func HTTPPatchProtoBuf(url string, pm proto.Message) (resp *http.Response, err error) {
return HTTPPatchProtoBufTLS(url, pm, &HTTPTLSConfig{})
}
// HTTPDeleteProtoBuf delete 请求
func HTTPDeleteProtoBuf(url string, pm proto.Message) (resp *http.Response, err error) {
return HTTPDeleteProtoBufTLS(url, pm, &HTTPTLSConfig{})
}
// HTTPDelete delete 请求
func HTTPDelete(url string) (resp *http.Response, err error) {
return HTTPDeleteTLS(url, &HTTPTLSConfig{})
}
// HTTPDo 自定义请求处理
func HTTPDo(req *http.Request) (resp *http.Response, err error) {
return HTTPDoTLS(req, &HTTPTLSConfig{})
}
// HTTPGetTLS get tls 请求
func HTTPGetTLS(url string, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPGetTLSBytes(url, tlsConfig.trans())
}
// HTTPGetHostTLS get tls 请求
//func HTTPGetHostTLS(url, host string, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
// return HTTPGetHostTLSBytes(url, host, tlsConfig.trans())
//}
//func HttpGetTLSProxy(expectURL, proxyURL string, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
// return httpRequestTLSProxy(expectURL, proxyURL, tlsConfig.trans())
//}
// HTTPPostJSONTLS post tls 请求
//
// content-type=application/json
func HTTPPostJSONTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPostJSONTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPutJSONTLS put tls 请求
//
// content-type=application/json
func HTTPPutJSONTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPutJSONTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPatchJSONTLS patch tls 请求
//
// content-type=application/json
func HTTPPatchJSONTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPatchJSONTLSBytes(url, model, tlsConfig.trans())
}
// HTTPDeleteJSONTLS delete tls 请求
func HTTPDeleteJSONTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPDeleteJSONTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPostXMLTLS post tls 请求
//
// content-type=application/xml
func HTTPPostXMLTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPostXMLTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPutXMLTLS put tls 请求
//
// content-type=application/xml
func HTTPPutXMLTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPutXMLTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPatchXMLTLS patch tls 请求
//
// content-type=application/xml
func HTTPPatchXMLTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPatchXMLTLSBytes(url, model, tlsConfig.trans())
}
// HTTPDeleteXMLTLS delete tls 请求
func HTTPDeleteXMLTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPDeleteXMLTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPostYamlTLS post tls 请求
//
// content-type=application/x-yaml
func HTTPPostYamlTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPostYamlTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPutYamlTLS put tls 请求
//
// content-type=application/x-yaml
func HTTPPutYamlTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPutYamlTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPatchYamlTLS patch tls 请求
//
// content-type=application/x-yaml
func HTTPPatchYamlTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPatchYamlTLSBytes(url, model, tlsConfig.trans())
}
// HTTPDeleteYamlTLS delete tls 请求
func HTTPDeleteYamlTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPDeleteYamlTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPostMsgPackTLS post tls 请求
//
// content-type=application/x-msgpack
func HTTPPostMsgPackTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPostMsgPackTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPutMsgPackTLS put tls 请求
//
// content-type=application/x-msgpack
func HTTPPutMsgPackTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPutMsgPackTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPatchMsgPackTLS patch tls 请求
//
// content-type=application/x-msgpack
func HTTPPatchMsgPackTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPatchMsgPackTLSBytes(url, model, tlsConfig.trans())
}
// HTTPDeleteMsgPackTLS delete tls 请求
func HTTPDeleteMsgPackTLS(url string, model interface{}, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPDeleteMsgPackTLSBytes(url, model, tlsConfig.trans())
}
// HTTPPostProtoBufTLS post tls 请求
//
// content-type=application/x-protobuf
func HTTPPostProtoBufTLS(url string, pm proto.Message, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPostProtoBufTLSBytes(url, pm, tlsConfig.trans())
}
// HTTPPutProtoBufTLS put tls 请求
//
// content-type=application/x-protobuf
func HTTPPutProtoBufTLS(url string, pm proto.Message, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPutProtoBufTLSBytes(url, pm, tlsConfig.trans())
}
// HTTPPatchProtoBufTLS patch tls 请求
//
// content-type=application/x-protobuf
func HTTPPatchProtoBufTLS(url string, pm proto.Message, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPatchProtoBufTLSBytes(url, pm, tlsConfig.trans())
}
// HTTPDeleteProtoBufTLS delete tls 请求
func HTTPDeleteProtoBufTLS(url string, pm proto.Message, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPDeleteProtoBufTLSBytes(url, pm, tlsConfig.trans())
}
// HTTPDeleteTLS delete tls 请求
func HTTPDeleteTLS(url string, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPDeleteTLSBytes(url, tlsConfig.trans())
}
// HTTPDoTLS 处理 tls 请求
func HTTPDoTLS(req *http.Request, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPDoTLSBytes(req, tlsConfig.trans())
}
// HTTPGetTLSBytes get tls 请求
func HTTPGetTLSBytes(url string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestTLSBytes(http.MethodGet, url, "", nil, tlsConfig)
}
// HTTPGetHostTLSBytes get tls 请求
func HTTPGetHostTLSBytes(url, host string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestTLSBytes(http.MethodGet, url, host, nil, tlsConfig)
}
//func HttpGetTLSBytesProxy(expectURL, proxyURL string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
// return httpRequestTLSProxy(expectURL, proxyURL, tlsConfig)
//}
// HTTPPostJSONTLSBytes post tls 请求
//
// content-type=application/json
func HTTPPostJSONTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestJSON(http.MethodPost, url, model, tlsConfig)
}
// HTTPPutJSONTLSBytes put tls 请求
//
// content-type=application/json
func HTTPPutJSONTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestJSON(http.MethodPut, url, model, tlsConfig)
}
// HTTPPatchJSONTLSBytes patch tls 请求
//
// content-type=application/json
func HTTPPatchJSONTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestJSON(http.MethodPatch, url, model, tlsConfig)
}
// HTTPDeleteJSONTLSBytes delete tls 请求
func HTTPDeleteJSONTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestJSON(http.MethodDelete, url, model, tlsConfig)
}
// HTTPPostXMLTLSBytes post tls 请求
//
// content-type=application/xml
func HTTPPostXMLTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestXML(http.MethodPost, url, model, tlsConfig)
}
// HTTPPutXMLTLSBytes put tls 请求
//
// content-type=application/xml
func HTTPPutXMLTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestXML(http.MethodPut, url, model, tlsConfig)
}
// HTTPPatchXMLTLSBytes patch tls 请求
//
// content-type=application/xml
func HTTPPatchXMLTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestXML(http.MethodPatch, url, model, tlsConfig)
}
// HTTPDeleteXMLTLSBytes delete tls 请求
func HTTPDeleteXMLTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestXML(http.MethodDelete, url, model, tlsConfig)
}
// HTTPPostYamlTLSBytes post tls 请求
//
// content-type=application/x-yaml
func HTTPPostYamlTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestYaml(http.MethodPost, url, model, tlsConfig)
}
// HTTPPutYamlTLSBytes put tls 请求
//
// content-type=application/x-yaml
func HTTPPutYamlTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestYaml(http.MethodPut, url, model, tlsConfig)
}
// HTTPPatchYamlTLSBytes patch tls 请求
//
// content-type=application/x-yaml
func HTTPPatchYamlTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestYaml(http.MethodPatch, url, model, tlsConfig)
}
// HTTPDeleteYamlTLSBytes delete tls 请求
func HTTPDeleteYamlTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestYaml(http.MethodDelete, url, model, tlsConfig)
}
// HTTPPostMsgPackTLSBytes post tls 请求
//
// content-type=application/x-msgpack
func HTTPPostMsgPackTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestMsgPack(http.MethodPost, url, model, tlsConfig)
}
// HTTPPutMsgPackTLSBytes put tls 请求
//
// content-type=application/x-msgpack
func HTTPPutMsgPackTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestMsgPack(http.MethodPut, url, model, tlsConfig)
}
// HTTPPatchMsgPackTLSBytes patch tls 请求
//
// content-type=application/x-msgpack
func HTTPPatchMsgPackTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestMsgPack(http.MethodPatch, url, model, tlsConfig)
}
// HTTPDeleteMsgPackTLSBytes delete tls 请求
func HTTPDeleteMsgPackTLSBytes(url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestMsgPack(http.MethodDelete, url, model, tlsConfig)
}
// HTTPPostProtoBufTLSBytes post tls 请求
//
// content-type=application/x-protobuf
func HTTPPostProtoBufTLSBytes(url string, pm proto.Message, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestProtoBuf(http.MethodPost, url, pm, tlsConfig)
}
// HTTPPutProtoBufTLSBytes put tls 请求
//
// content-type=application/x-protobuf
func HTTPPutProtoBufTLSBytes(url string, pm proto.Message, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestProtoBuf(http.MethodPut, url, pm, tlsConfig)
}
// HTTPPatchProtoBufTLSBytes patch tls 请求
//
// content-type=application/x-protobuf
func HTTPPatchProtoBufTLSBytes(url string, pm proto.Message, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestProtoBuf(http.MethodPatch, url, pm, tlsConfig)
}
// HTTPDeleteProtoBufTLSBytes delete tls 请求
//
// content-type=application/x-protobuf
func HTTPDeleteProtoBufTLSBytes(url string, pm proto.Message, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestProtoBuf(http.MethodDelete, url, pm, tlsConfig)
}
// HTTPDeleteTLSBytes delete tls 请求
func HTTPDeleteTLSBytes(url string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestTLSBytes(http.MethodDelete, url, "", nil, tlsConfig)
}
// HTTPDoTLSBytes 处理 tls 请求
func HTTPDoTLSBytes(req *http.Request, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestTLSBytesDo(req, tlsConfig)
}
// HTTPPostForm post 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPostForm(url string, paramMap map[string]string) (resp *http.Response, err error) {
return HTTPPostFormTLS(url, paramMap, &HTTPTLSConfig{})
}
// HTTPPutForm put 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPutForm(url string, paramMap map[string]string) (resp *http.Response, err error) {
return HTTPPutFormTLS(url, paramMap, &HTTPTLSConfig{})
}
// HTTPPatchForm patch 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPatchForm(url string, paramMap map[string]string) (resp *http.Response, err error) {
return HTTPPatchFormTLS(url, paramMap, &HTTPTLSConfig{})
}
// HTTPPostFormMultipart post 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPostFormMultipart(url string, paramMap map[string]string, fileMap map[string]string) (resp *http.Response, err error) {
return HTTPPostFormMultipartTLS(url, paramMap, fileMap, &HTTPTLSConfig{})
}
// HTTPPutFormMultipart put 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPutFormMultipart(url string, paramMap map[string]string, fileMap map[string]string) (resp *http.Response, err error) {
return HTTPPutFormMultipartTLS(url, paramMap, fileMap, &HTTPTLSConfig{})
}
// HTTPPatchFormMultipart patch 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPatchFormMultipart(url string, paramMap map[string]string, fileMap map[string]string) (resp *http.Response, err error) {
return HTTPPatchFormMultipartTLS(url, paramMap, fileMap, &HTTPTLSConfig{})
}
// HTTPPostFormTLS post tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPostFormTLS(url string, paramMap map[string]string, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPostFormTLSBytes(url, paramMap, tlsConfig.trans())
}
// HTTPPutFormTLS put tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPutFormTLS(url string, paramMap map[string]string, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPutFormTLSBytes(url, paramMap, tlsConfig.trans())
}
// HTTPPatchFormTLS patch tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPatchFormTLS(url string, paramMap map[string]string, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPatchFormTLSBytes(url, paramMap, tlsConfig.trans())
}
// HTTPPostFormMultipartTLS post tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPostFormMultipartTLS(url string, paramMap map[string]string, fileMap map[string]string, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPostFormMultipartTLSBytes(url, paramMap, fileMap, tlsConfig.trans())
}
// HTTPPutFormMultipartTLS put tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPutFormMultipartTLS(url string, paramMap map[string]string, fileMap map[string]string, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPutFormMultipartTLSBytes(url, paramMap, fileMap, tlsConfig.trans())
}
// HTTPPatchFormMultipartTLS patch tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPatchFormMultipartTLS(url string, paramMap map[string]string, fileMap map[string]string, tlsConfig *HTTPTLSConfig) (resp *http.Response, err error) {
return HTTPPatchFormMultipartTLSBytes(url, paramMap, fileMap, tlsConfig.trans())
}
// HTTPPostFormTLSBytes post tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPostFormTLSBytes(url string, paramMap map[string]string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestForm(http.MethodPost, url, paramMap, tlsConfig)
}
// HTTPPutFormTLSBytes put tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPutFormTLSBytes(url string, paramMap map[string]string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestForm(http.MethodPut, url, paramMap, tlsConfig)
}
// HTTPPatchFormTLSBytes patch tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPatchFormTLSBytes(url string, paramMap map[string]string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestForm(http.MethodPatch, url, paramMap, tlsConfig)
}
// HTTPPostFormMultipartTLSBytes post tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPostFormMultipartTLSBytes(url string, paramMap map[string]string, fileMap map[string]string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestFormMultipart(http.MethodPost, url, paramMap, fileMap, tlsConfig)
}
// HTTPPutFormMultipartTLSBytes put tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPutFormMultipartTLSBytes(url string, paramMap map[string]string, fileMap map[string]string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestFormMultipart(http.MethodPut, url, paramMap, fileMap, tlsConfig)
}
// HTTPPatchFormMultipartTLSBytes patch tls 请求
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func HTTPPatchFormMultipartTLSBytes(url string, paramMap map[string]string, fileMap map[string]string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
return httpRequestFormMultipart(http.MethodPatch, url, paramMap, fileMap, tlsConfig)
}
// httpRequestJSON json 请求
//
// model 结构体
func httpRequestJSON(method, url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
var (
data []byte
req *http.Request
)
if data, err = json.Marshal(model); err != nil {
return nil, err
}
if req, err = http.NewRequest(method, url, bytes.NewReader(data)); nil != err {
return
}
req.Header.Set("content-type", "application/json")
return httpRequestTLSBytesDo(req, tlsConfig)
}
// httpRequestXML xml 请求
//
// model 结构体
func httpRequestXML(method, url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
var (
data []byte
req *http.Request
)
if data, err = xml.Marshal(model); err != nil {
return nil, err
}
if req, err = http.NewRequest(method, url, bytes.NewReader(data)); nil != err {
return
}
req.Header.Set("content-type", "application/xml")
return httpRequestTLSBytesDo(req, tlsConfig)
}
// httpRequestYaml yaml 请求
//
// model 结构体
func httpRequestYaml(method, url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
var (
data []byte
req *http.Request
)
if data, err = yaml.Marshal(model); err != nil {
return nil, err
}
if req, err = http.NewRequest(method, url, bytes.NewReader(data)); nil != err {
return
}
req.Header.Set("content-type", "application/x-yaml")
return httpRequestTLSBytesDo(req, tlsConfig)
}
// httpRequestMsgPack msgpack 请求
//
// model 结构体
func httpRequestMsgPack(method, url string, model interface{}, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
var (
data []byte
req *http.Request
)
if data, err = msgpack.Marshal(model); err != nil {
return nil, err
}
if req, err = http.NewRequest(method, url, bytes.NewReader(data)); nil != err {
return
}
req.Header.Set("content-type", "application/x-msgpack")
return httpRequestTLSBytesDo(req, tlsConfig)
}
// httpRequestProtoBuf protobuf 请求
//
// model 结构体
func httpRequestProtoBuf(method, url string, pm proto.Message, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
var (
data []byte
req *http.Request
)
if data, err = proto.Marshal(pm); err != nil {
return nil, err
}
if req, err = http.NewRequest(method, url, bytes.NewReader(data)); nil != err {
return
}
req.Header.Set("content-type", "application/x-protobuf")
return httpRequestTLSBytesDo(req, tlsConfig)
}
// httpRequestForm
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func httpRequestForm(method, url string, paramMap map[string]string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
var (
req *http.Request
bodyBuffer = &bytes.Buffer{}
bodyWriter = multipart.NewWriter(bodyBuffer)
)
for key, value := range paramMap {
if err = bodyWriter.WriteField(key, value); nil != err {
return nil, err
}
}
if err = bodyWriter.Close(); nil != err {
return nil, err
}
if req, err = http.NewRequest(method, url, bodyBuffer); nil != err {
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return httpRequestTLSBytesDo(req, tlsConfig)
}
// httpRequestFormMultipart
//
// paramMap form普通参数
//
// fileMap form附件key及附件路径
func httpRequestFormMultipart(method, url string, paramMap map[string]string, fileMap map[string]string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
var (
req *http.Request
bodyBuffer = &bytes.Buffer{}
bodyWriter = multipart.NewWriter(bodyBuffer)
)
for key, value := range paramMap {
if err = bodyWriter.WriteField(key, value); nil != err {
return nil, err
}
}
for key, value := range fileMap {
var (
fileSplitArr []string
fileWriter io.Writer
file *os.File
)
fileSplitArr = strings.Split(value, string(filepath.Separator))
if fileWriter, err = bodyWriter.CreateFormFile(key, fileSplitArr[len(fileSplitArr)-1]); nil != err {
return nil, err
}
if file, err = os.Open(value); nil != err {
return nil, err
}
if _, err = io.Copy(fileWriter, file); nil != err {
return nil, err
}
func() { _ = file.Close() }()
}
if err = bodyWriter.Close(); nil != err {
return nil, err
}
if req, err = http.NewRequest(method, url, bodyBuffer); nil != err {
return
}
req.Header.Set("Content-Type", "multipart/form-data")
return httpRequestTLSBytesDo(req, tlsConfig)
}
func httpRequestTLSBytes(method, url, host string, body io.Reader, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
var req *http.Request
if req, err = http.NewRequest(method, url, body); nil != err {
return
}
if StringIsNotEmpty(host) {
req.Host = host
}
return httpRequestTLSBytesDo(req, tlsConfig)
}
func httpRequestTLSBytesDo(req *http.Request, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
var (
tlsClient *http.Client
tlsClientKey string
)
if nil == tlsConfig {
tlsClientKey = ""
} else {
bs := append(tlsConfig.RootCrtBytes, append(tlsConfig.KeyBytes, tlsConfig.CertBytes...)...)
tlsClientKey = HashMD516Bytes(bs)
}
if tlsClient, err = getTLSClient(tlsClientKey, tlsConfig); nil != err {
return
}
return tlsClient.Do(req)
}
var (
clients = map[string]*http.Client{}
clientLock sync.Mutex
)
func getTLSClient(tlsClientKey string, tlsConfig *HTTPTLSBytesConfig) (*http.Client, error) {
if tlsClient, exist := clients[tlsClientKey]; exist {
return tlsClient, nil
}
defer clientLock.Unlock()
clientLock.Lock()
if tlsClient, exist := clients[tlsClientKey]; exist {
return tlsClient, nil
}
var (
tlsClient = &http.Client{}
err error
)
if tlsClient.Transport, err = getTLSTransport(tlsConfig); nil != err {
return nil, err
}
clients[tlsClientKey] = tlsClient
return tlsClient, nil
}
func getTLSTransport(tlsConfig *HTTPTLSBytesConfig) (transport *http.Transport, err error) {
var (
pool = x509.NewCertPool()
cert tls.Certificate
)
transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: tlsConfig.InsecureSkipVerify,
},
}
if nil != tlsConfig.RootCrtBytes && pool.AppendCertsFromPEM(tlsConfig.RootCrtBytes) {
transport.TLSClientConfig.RootCAs = pool
}
if nil != tlsConfig.KeyBytes && nil != tlsConfig.CertBytes {
// 用于对方验证我方证书合法性
if cert, err = tls.X509KeyPair(tlsConfig.CertBytes, tlsConfig.KeyBytes); nil != err {
return
}
transport.TLSClientConfig.Certificates = []tls.Certificate{cert}
}
return
}
// expectURL https://localhost:8888
//
// proxyURL https://www.baidu.com:443
//func httpRequestTLSProxy(expectURL, proxyURL string, tlsConfig *HTTPTLSBytesConfig) (resp *http.Response, err error) {
// var (
// u *url.URL
// transport *http.Transport
// )
// if u, err = url.Parse(expectURL); err != nil {
// panic(err)
// }
// if transport, err = getTLSTransport(tlsConfig); nil != err {
// return nil, err
// }
// transport.Proxy = http.ProxyURL(u)
// // disabled HTTP/2
// //transport.TLSNextProto = make(map[string]func(authority string, c *tls.Conn) http.RoundTripper)
// //transport = &http.Transport{
// // Proxy: http.ProxyURL(u),
// // // disabled HTTP/2
// // TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
// //}
// client := &http.Client{Transport: transport}
// return client.Get(proxyURL)
//}
// HTTPTLSConfig http tls 请求配置
type HTTPTLSConfig struct {
RootCrtFilePath string // 服务端根证书,用于我方验证对方证书合法性
CertFilePath string // 服务端签发的子证书,用于对方验证我方证书合法性
KeyFilePath string // 客户端私钥,用于对方验证我方证书合法性
InsecureSkipVerify bool // 是否验证服务端证书,即双向认证
}
func (htc *HTTPTLSConfig) trans() *HTTPTLSBytesConfig {
var (
htbc = &HTTPTLSBytesConfig{InsecureSkipVerify: htc.InsecureSkipVerify}
rootCrtBytes, keyBytes, certBytes []byte
err error
)
if rootCrtBytes, err = ioutil.ReadFile(htc.RootCrtFilePath); nil == err {
htbc.RootCrtBytes = rootCrtBytes
}
if certBytes, err = ioutil.ReadFile(htc.CertFilePath); nil == err {
htbc.CertBytes = certBytes
}
if keyBytes, err = ioutil.ReadFile(htc.KeyFilePath); nil == err {
htbc.KeyBytes = keyBytes
}
return htbc
}
// HTTPTLSBytesConfig http tls 请求配置
type HTTPTLSBytesConfig struct {
RootCrtBytes []byte // 服务端根证书,用于我方验证对方证书合法性
CertBytes []byte // 服务端签发的子证书,用于对方验证我方证书合法性
KeyBytes []byte // 客户端私钥,用于对方验证我方证书合法性
InsecureSkipVerify bool // 是否验证服务端证书,即双向认证
}