MCPcopy
hub / github.com/MetaCubeX/mihomo / HttpRequest

Function HttpRequest

component/http/http.go:32–91  ·  view source on GitHub ↗
(ctx context.Context, url, method string, header map[string][]string, body io.Reader, options ...Option)

Source from the content-addressed store, hash-verified

30}
31
32func 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}

Callers

nothing calls this directly

Calls 12

GetTLSConfigFunction · 0.92
HandleTcpFunction · 0.92
GetTunnelFunction · 0.92
DialContextFunction · 0.92
UAFunction · 0.85
AddMethod · 0.80
StringMethod · 0.65
GetMethod · 0.65
SetMethod · 0.65
DialContextMethod · 0.65
ParseMethod · 0.45
DoMethod · 0.45

Tested by

no test coverage detected