mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Move and refactor files and some more changes :3
This commit is contained in:
parent
781e520591
commit
303689ecbb
29 changed files with 1305 additions and 1160 deletions
51
utils/http.go
Normal file
51
utils/http.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type headerRoundTripper struct {
|
||||
http.RoundTripper
|
||||
header map[string]string
|
||||
}
|
||||
|
||||
func (rht headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
resp, err := rht.RoundTripper.RoundTrip(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for k, v := range rht.header {
|
||||
resp.Header.Set(k, v)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func CreateOrDefaultClient(proxy, useragent string) (*http.Client, error) {
|
||||
if proxy == "" {
|
||||
return http.DefaultClient, nil
|
||||
} else {
|
||||
proxyURL, err := url.Parse(proxy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var rt http.RoundTripper = &http.Transport{
|
||||
DisableCompression: true,
|
||||
Proxy: http.ProxyURL(proxyURL),
|
||||
}
|
||||
if useragent != "" {
|
||||
rt = headerRoundTripper{
|
||||
RoundTripper: rt,
|
||||
header: map[string]string{"User-Agent": useragent},
|
||||
}
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Transport: rt,
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue