diff options
author | Gary Kim <gary@garykim.dev> | 2020-08-30 07:49:26 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-30 13:49:26 +0200 |
commit | a0741d99b80d7da1c063853382756b3a9689f6a7 (patch) | |
tree | 86b6954d34558fe3a753284dea52928a031edc7a /vendor/github.com/monaco-io/request/build.go | |
parent | c63f08c8113598149f312bffd3fbdf1970660639 (diff) | |
download | matterbridge-msglm-a0741d99b80d7da1c063853382756b3a9689f6a7.tar.gz matterbridge-msglm-a0741d99b80d7da1c063853382756b3a9689f6a7.tar.bz2 matterbridge-msglm-a0741d99b80d7da1c063853382756b3a9689f6a7.zip |
Add TLSConfig to nctalk (#1195)
Signed-off-by: Gary Kim <gary@garykim.dev>
Diffstat (limited to 'vendor/github.com/monaco-io/request/build.go')
-rw-r--r-- | vendor/github.com/monaco-io/request/build.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/vendor/github.com/monaco-io/request/build.go b/vendor/github.com/monaco-io/request/build.go new file mode 100644 index 00000000..c2b1287b --- /dev/null +++ b/vendor/github.com/monaco-io/request/build.go @@ -0,0 +1,98 @@ +package request + +import ( + "bytes" + "net/http" + "net/http/cookiejar" + "net/url" + "time" +) + +// TODO: func unit test coverage +func (c *Client) buildRequest() (err error) { + if err = c.applyRequest(); err != nil { + return + } + + c.transport = &http.Transport{} + + c.applyHTTPHeader() + c.applyBasicAuth() + c.applyClient() + c.applyTimeout() + c.applyCookies() + c.applyTLSConfig() + err = c.applyProxy() + + c.client.Transport = c.transport + return +} + +func (c *Client) applyRequest() (err error) { + // encode requestURL.httpURL like https://google.com?hello=world&package=request + c.requestURL = requestURL{ + urlString: c.URL, + parameters: c.Params, + } + if err = c.requestURL.EncodeURL(); err != nil { + return + } + c.req, err = http.NewRequest(c.Method, c.requestURL.string(), bytes.NewReader(c.Body)) + return +} + +func (c *Client) applyHTTPHeader() { + if c.Method == POST { + if c.ContentType == emptyString { + c.ContentType = ApplicationJSON + } + c.req.Header.Set(contentType, string(c.ContentType)) + } + for k, v := range c.Header { + c.req.Header.Add(k, v) + } +} + +func (c *Client) applyBasicAuth() { + if c.BasicAuth.Username != emptyString && c.BasicAuth.Password != emptyString { + c.req.SetBasicAuth(c.BasicAuth.Username, c.BasicAuth.Password) + } +} + +func (c *Client) applyClient() { + c.client = &http.Client{} +} + +func (c *Client) applyTimeout() { + if c.Timeout > 0 { + c.client.Timeout = c.Timeout * time.Second + } +} + +func (c *Client) applyCookies() { + if c.Cookies != nil { + jar, _ := cookiejar.New(nil) + jar.SetCookies(&url.URL{Scheme: c.requestURL.scheme(), Host: c.requestURL.host()}, c.Cookies) + c.client.Jar = jar + } +} + +// TODO: test case +func (c *Client) applyProxy() (err error) { + if c.ProxyURL != emptyString { + var proxy *url.URL + if proxy, err = url.Parse(c.ProxyURL); err != nil { + return + } else if proxy != nil { + c.transport.Proxy = http.ProxyURL(proxy) + } + } + return +} + +func (c *Client) applyTLSConfig() { + // &tls.Config{InsecureSkipVerify: true} + if c.TLSConfig != nil { + c.transport.TLSClientConfig = c.TLSConfig + } +} |