(ctx context.Context, url, method string, header map[string][]string, body io.Reader, options ...Option)
| 30 | } |
| 31 | |
| 32 | func HttpRequest(ctx context.Context, url, method string, header map[string][]string, body io.Reader, options ...Option) (*http.Response, error) { |
| 33 | opt := option{} |
| 34 | for _, o := range options { |
| 35 | o(&opt) |
| 36 | } |
| 37 | method = strings.ToUpper(method) |
| 38 | urlRes, err := URL.Parse(url) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | |
| 43 | req, err := http.NewRequest(method, urlRes.String(), body) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | for k, v := range header { |
| 49 | for _, v := range v { |
| 50 | req.Header.Add(k, v) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if req.Header.Get("User-Agent") == "" { |
| 55 | req.Header.Set("User-Agent", UA()) |
| 56 | } |
| 57 | |
| 58 | if user := urlRes.User; user != nil { |
| 59 | password, _ := user.Password() |
| 60 | req.SetBasicAuth(user.Username(), password) |
| 61 | } |
| 62 | |
| 63 | req = req.WithContext(ctx) |
| 64 | |
| 65 | tlsConfig, err := ca.GetTLSConfig(opt.caOption) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | |
| 70 | transport := &http.Transport{ |
| 71 | // from http.DefaultTransport |
| 72 | DisableKeepAlives: runtime.GOOS == "android", |
| 73 | MaxIdleConns: 100, |
| 74 | IdleConnTimeout: 30 * time.Second, |
| 75 | TLSHandshakeTimeout: 10 * time.Second, |
| 76 | ExpectContinueTimeout: 1 * time.Second, |
| 77 | DialContext: func(ctx context.Context, network, address string) (net.Conn, error) { |
| 78 | if opt.dialer != nil { |
| 79 | return opt.dialer.DialContext(ctx, network, address) |
| 80 | } |
| 81 | if conn, err := inner.HandleTcp(inner.GetTunnel(), address, opt.specialProxy); err == nil { |
| 82 | return conn, nil |
| 83 | } |
| 84 | return dialer.DialContext(ctx, network, address) |
| 85 | }, |
| 86 | TLSClientConfig: tlsConfig, |
| 87 | } |
| 88 | |
| 89 | client := http.Client{Transport: transport} |
nothing calls this directly
no test coverage detected