github.com/gogf/gf/v2@v2.7.4/net/gclient/gclient_config.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gclient 8 9 import ( 10 "context" 11 "crypto/tls" 12 "net" 13 "net/http" 14 "net/http/cookiejar" 15 "net/url" 16 "strings" 17 "time" 18 19 "golang.org/x/net/proxy" 20 21 "github.com/gogf/gf/v2/errors/gerror" 22 "github.com/gogf/gf/v2/internal/intlog" 23 "github.com/gogf/gf/v2/net/gsel" 24 "github.com/gogf/gf/v2/net/gsvc" 25 "github.com/gogf/gf/v2/text/gregex" 26 "github.com/gogf/gf/v2/text/gstr" 27 ) 28 29 // SetBrowserMode enables browser mode of the client. 30 // When browser mode is enabled, it automatically saves and sends cookie content 31 // from and to server. 32 func (c *Client) SetBrowserMode(enabled bool) *Client { 33 if enabled { 34 jar, _ := cookiejar.New(nil) 35 c.Jar = jar 36 } 37 return c 38 } 39 40 // SetHeader sets a custom HTTP header pair for the client. 41 func (c *Client) SetHeader(key, value string) *Client { 42 c.header[key] = value 43 return c 44 } 45 46 // SetHeaderMap sets custom HTTP headers with map. 47 func (c *Client) SetHeaderMap(m map[string]string) *Client { 48 for k, v := range m { 49 c.header[k] = v 50 } 51 return c 52 } 53 54 // SetAgent sets the User-Agent header for client. 55 func (c *Client) SetAgent(agent string) *Client { 56 c.header[httpHeaderUserAgent] = agent 57 return c 58 } 59 60 // SetContentType sets HTTP content type for the client. 61 func (c *Client) SetContentType(contentType string) *Client { 62 c.header[httpHeaderContentType] = contentType 63 return c 64 } 65 66 // SetHeaderRaw sets custom HTTP header using raw string. 67 func (c *Client) SetHeaderRaw(headers string) *Client { 68 for _, line := range gstr.SplitAndTrim(headers, "\n") { 69 array, _ := gregex.MatchString(httpRegexHeaderRaw, line) 70 if len(array) >= 3 { 71 c.header[array[1]] = array[2] 72 } 73 } 74 return c 75 } 76 77 // SetCookie sets a cookie pair for the client. 78 func (c *Client) SetCookie(key, value string) *Client { 79 c.cookies[key] = value 80 return c 81 } 82 83 // SetCookieMap sets cookie items with map. 84 func (c *Client) SetCookieMap(m map[string]string) *Client { 85 for k, v := range m { 86 c.cookies[k] = v 87 } 88 return c 89 } 90 91 // SetPrefix sets the request server URL prefix. 92 func (c *Client) SetPrefix(prefix string) *Client { 93 c.prefix = prefix 94 return c 95 } 96 97 // SetTimeout sets the request timeout for the client. 98 func (c *Client) SetTimeout(t time.Duration) *Client { 99 c.Client.Timeout = t 100 return c 101 } 102 103 // SetBasicAuth sets HTTP basic authentication information for the client. 104 func (c *Client) SetBasicAuth(user, pass string) *Client { 105 c.authUser = user 106 c.authPass = pass 107 return c 108 } 109 110 // SetRetry sets retry count and interval. 111 // TODO removed. 112 func (c *Client) SetRetry(retryCount int, retryInterval time.Duration) *Client { 113 c.retryCount = retryCount 114 c.retryInterval = retryInterval 115 return c 116 } 117 118 // SetRedirectLimit limits the number of jumps. 119 func (c *Client) SetRedirectLimit(redirectLimit int) *Client { 120 c.CheckRedirect = func(req *http.Request, via []*http.Request) error { 121 if len(via) >= redirectLimit { 122 return http.ErrUseLastResponse 123 } 124 return nil 125 } 126 return c 127 } 128 129 // SetNoUrlEncode sets the mark that do not encode the parameters before sending request. 130 func (c *Client) SetNoUrlEncode(noUrlEncode bool) *Client { 131 c.noUrlEncode = noUrlEncode 132 return c 133 } 134 135 // SetProxy set proxy for the client. 136 // This func will do nothing when the parameter `proxyURL` is empty or in wrong pattern. 137 // The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`. 138 // Only `http` and `socks5` proxies are supported currently. 139 func (c *Client) SetProxy(proxyURL string) { 140 if strings.TrimSpace(proxyURL) == "" { 141 return 142 } 143 _proxy, err := url.Parse(proxyURL) 144 if err != nil { 145 intlog.Errorf(context.TODO(), `%+v`, err) 146 return 147 } 148 if _proxy.Scheme == httpProtocolName { 149 if v, ok := c.Transport.(*http.Transport); ok { 150 v.Proxy = http.ProxyURL(_proxy) 151 } 152 } else { 153 auth := &proxy.Auth{} 154 user := _proxy.User.Username() 155 if user != "" { 156 auth.User = user 157 password, hasPassword := _proxy.User.Password() 158 if hasPassword && password != "" { 159 auth.Password = password 160 } 161 } else { 162 auth = nil 163 } 164 // refer to the source code, error is always nil 165 dialer, err := proxy.SOCKS5( 166 "tcp", 167 _proxy.Host, 168 auth, 169 &net.Dialer{ 170 Timeout: c.Client.Timeout, 171 KeepAlive: c.Client.Timeout, 172 }, 173 ) 174 if err != nil { 175 intlog.Errorf(context.TODO(), `%+v`, err) 176 return 177 } 178 if v, ok := c.Transport.(*http.Transport); ok { 179 v.DialContext = func(ctx context.Context, network, addr string) (conn net.Conn, e error) { 180 return dialer.Dial(network, addr) 181 } 182 } 183 // c.SetTimeout(10*time.Second) 184 } 185 } 186 187 // SetTLSKeyCrt sets the certificate and key file for TLS configuration of client. 188 func (c *Client) SetTLSKeyCrt(crtFile, keyFile string) error { 189 tlsConfig, err := LoadKeyCrt(crtFile, keyFile) 190 if err != nil { 191 return gerror.Wrap(err, "LoadKeyCrt failed") 192 } 193 if v, ok := c.Transport.(*http.Transport); ok { 194 tlsConfig.InsecureSkipVerify = true 195 v.TLSClientConfig = tlsConfig 196 return nil 197 } 198 return gerror.New(`cannot set TLSClientConfig for custom Transport of the client`) 199 } 200 201 // SetTLSConfig sets the TLS configuration of client. 202 func (c *Client) SetTLSConfig(tlsConfig *tls.Config) error { 203 if v, ok := c.Transport.(*http.Transport); ok { 204 v.TLSClientConfig = tlsConfig 205 return nil 206 } 207 return gerror.New(`cannot set TLSClientConfig for custom Transport of the client`) 208 } 209 210 // SetBuilder sets the load balance builder for client. 211 func (c *Client) SetBuilder(builder gsel.Builder) { 212 c.builder = builder 213 } 214 215 // SetDiscovery sets the load balance builder for client. 216 func (c *Client) SetDiscovery(discovery gsvc.Discovery) { 217 c.discovery = discovery 218 }