-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
58 lines (53 loc) · 2.13 KB
/
http.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
package httputil
import (
"crypto/tls"
"crypto/x509"
"errors"
"net/http"
"net/url"
"os"
)
const (
// HeaderContentType the Content-Type http header
HeaderContentType = "Content-Type"
)
var (
// errAppendCertsFromPemFailed attempts to parse a series of PEM encoded certificates failed
errAppendCertsFromPemFailed = errors.New("append certs from pem failed")
errEmptyUrl = errors.New("url is empty")
)
// HttpClient an HTTP client component that supports chunked file uploads using the POST method
type HttpClient interface {
// HttpGet get http resource
HttpGet(url string) (resp *http.Response, err error)
// HttpGetWithCookie get http resource with cookies
HttpGetWithCookie(url string, header http.Header, cookies ...*http.Cookie) (resp *http.Response, err error)
// HttpPost send a post request with form data
HttpPost(url string, data url.Values) (resp *http.Response, err error)
// HttpPostWithCookie send a post request with form data and cookies
HttpPostWithCookie(url string, data url.Values, cookies ...*http.Cookie) (resp *http.Response, err error)
// HttpPostFileChunkWithCookie send a post request with form data, a file chunk and cookies
HttpPostFileChunkWithCookie(url string, fieldName string, fileName string, data url.Values, chunk []byte, cookies ...*http.Cookie) (resp *http.Response, err error)
// HttpPostWithoutRedirect send a post request with form data and not auto redirect
HttpPostWithoutRedirect(url string, data url.Values) (resp *http.Response, err error)
// Download if the local file does not exist and the alwaysDownload is false, downloads the remote file to local path
Download(path, url string, alwaysDownload bool) error
}
// NewTLSConfig create a tls config
func NewTLSConfig(insecureSkipVerify bool, certFile string) (*tls.Config, error) {
tlsConfig := &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
}
if !insecureSkipVerify {
roots := x509.NewCertPool()
pemCerts, err := os.ReadFile(certFile)
if err != nil {
return nil, err
}
if !roots.AppendCertsFromPEM(pemCerts) {
return nil, errAppendCertsFromPemFailed
}
tlsConfig.RootCAs = roots
}
return tlsConfig, nil
}