From a0741d99b80d7da1c063853382756b3a9689f6a7 Mon Sep 17 00:00:00 2001 From: Gary Kim Date: Sun, 30 Aug 2020 07:49:26 -0400 Subject: Add TLSConfig to nctalk (#1195) Signed-off-by: Gary Kim --- vendor/github.com/monaco-io/request/.travis.yml | 16 ---- vendor/github.com/monaco-io/request/README.md | 26 ++++++- vendor/github.com/monaco-io/request/build.go | 98 +++++++++++++++++++++++++ vendor/github.com/monaco-io/request/const.go | 46 ++++++++++++ vendor/github.com/monaco-io/request/model.go | 23 +++--- vendor/github.com/monaco-io/request/request.go | 75 ++----------------- vendor/github.com/monaco-io/request/url.go | 4 +- 7 files changed, 186 insertions(+), 102 deletions(-) delete mode 100644 vendor/github.com/monaco-io/request/.travis.yml create mode 100644 vendor/github.com/monaco-io/request/build.go create mode 100644 vendor/github.com/monaco-io/request/const.go (limited to 'vendor/github.com/monaco-io/request') diff --git a/vendor/github.com/monaco-io/request/.travis.yml b/vendor/github.com/monaco-io/request/.travis.yml deleted file mode 100644 index abc79551..00000000 --- a/vendor/github.com/monaco-io/request/.travis.yml +++ /dev/null @@ -1,16 +0,0 @@ -language: go - -go: - - 1.14.x - - tip - -sudo: false - -before_install: - - go get -t -v ./... - -script: - - go test -race -coverprofile=coverage.txt -covermode=atomic - -after_success: - - bash <(curl -s https://codecov.io/bash) -t 6cf6f7ab-26e6-429c-ac44-f0ad85c1e586 diff --git a/vendor/github.com/monaco-io/request/README.md b/vendor/github.com/monaco-io/request/README.md index 7ac32be0..153dbeaf 100644 --- a/vendor/github.com/monaco-io/request/README.md +++ b/vendor/github.com/monaco-io/request/README.md @@ -2,7 +2,6 @@ -[![Build Status](https://travis-ci.org/monaco-io/request.svg?branch=master)](https://travis-ci.org/monaco-io/request) [![GoDoc](https://godoc.org/github.com/monaco-io/request?status.svg)](https://pkg.go.dev/github.com/monaco-io/request?tab=doc) [![codecov](https://codecov.io/gh/monaco-io/request/branch/master/graph/badge.svg)](https://codecov.io/gh/monaco-io/request) [![Release](https://img.shields.io/github/release/monaco-io/request.svg?style=flat-square)](https://github.com/monaco-io/request/releases) @@ -192,6 +191,31 @@ func main() { } ``` + +### TLS + +```go +package main + +import ( + "log" + "crypto/tls" + + "github.com/monaco-io/request" +) + +func main() { + client := request.Client{ + URL: "https://google.com", + TLSConfig: &tls.Config{InsecureSkipVerify: true}, + } + + resp, err := client.Do() + + log.Println(resp.Code, string(resp.Data), err) +} +``` + ## License [MIT](LICENSE) 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 + } +} diff --git a/vendor/github.com/monaco-io/request/const.go b/vendor/github.com/monaco-io/request/const.go new file mode 100644 index 00000000..d7b108fa --- /dev/null +++ b/vendor/github.com/monaco-io/request/const.go @@ -0,0 +1,46 @@ +package request + +const ( + // ApplicationJSON application/json + ApplicationJSON ContentType = "application/json" + + // ApplicationXWwwFormURLEncoded application/x-www-form-urlencoded + ApplicationXWwwFormURLEncoded ContentType = "application/x-www-form-urlencoded" + + // MultipartFormData multipart/form-data + MultipartFormData ContentType = "multipart/form-data" +) + +const ( + // OPTIONS http options + OPTIONS = "OPTIONS" + + // GET http get + GET = "GET" + + // HEAD http head + HEAD = "HEAD" + + // POST http post + POST = "POST" + + // PUT http put + PUT = "PUT" + + // DELETE http delete + DELETE = "DELETE" + + // TRACE http trace + TRACE = "TRACE" + + // CONNECT http connect + CONNECT = "CONNECT" + + // PATCH http patch + PATCH = "PATCH" +) + +const ( + emptyString = "" + contentType = "Content-Type" +) diff --git a/vendor/github.com/monaco-io/request/model.go b/vendor/github.com/monaco-io/request/model.go index 6dd58bca..56016477 100644 --- a/vendor/github.com/monaco-io/request/model.go +++ b/vendor/github.com/monaco-io/request/model.go @@ -1,24 +1,18 @@ package request import ( + "crypto/tls" "net/http" "time" ) -const ( - // ApplicationJSON application/json - ApplicationJSON ContentType = "application/json" - - // ApplicationXWwwFormURLEncoded application/x-www-form-urlencoded - ApplicationXWwwFormURLEncoded ContentType = "application/x-www-form-urlencoded" - - // MultipartFormData multipart/form-data - MultipartFormData ContentType = "multipart/form-data" -) - // ContentType Content-Type type ContentType string +// Method http method +// TODO: +type Method string + // Client Method /* Method = "OPTIONS" ; Section 9.2 @@ -44,10 +38,13 @@ type Client struct { ProxyURL string ContentType ContentType Cookies []*http.Cookie + TLSConfig *tls.Config // private - client *http.Client - req *http.Request + client *http.Client + requestURL requestURL + req *http.Request + transport *http.Transport } // BasicAuth Add Username:Password as Basic Auth diff --git a/vendor/github.com/monaco-io/request/request.go b/vendor/github.com/monaco-io/request/request.go index 0a4e7df4..6eb45acf 100644 --- a/vendor/github.com/monaco-io/request/request.go +++ b/vendor/github.com/monaco-io/request/request.go @@ -1,98 +1,33 @@ package request import ( - "bytes" - "crypto/tls" "io/ioutil" "net/http" - "net/http/cookiejar" - "net/url" - "time" ) // Do send http request func (c *Client) Do() (resp SugaredResp, err error) { defer resp.Close() - if err := c.buildRequest(); err != nil { - return resp, err + if err = c.buildRequest(); err != nil { + return } // send request and close on func call end if resp.resp, err = c.client.Do(c.req); err != nil { - return resp, err + return } // read response data form resp resp.Data, err = ioutil.ReadAll(resp.resp.Body) resp.Code = resp.resp.StatusCode - return resp, err -} - -func (c *Client) buildRequest() (err error) { - - // encode requestURL.httpURL like https://google.com?hello=world&package=request - ru := requestURL{ - urlString: c.URL, - parameters: c.Params, - } - if err := ru.EncodeURL(); err != nil { - return err - } - - // build request - c.req, err = http.NewRequest(c.Method, ru.string(), bytes.NewReader(c.Body)) - if err != nil { - return err - } - - // apply Header to request - if c.Method == "POST" { - if c.ContentType == "" { - c.ContentType = ApplicationJSON - } - c.req.Header.Set("Content-Type", string(c.ContentType)) - } - for k, v := range c.Header { - c.req.Header.Add(k, v) - } - - // apply basic Auth of request header - if c.BasicAuth.Username != "" && c.BasicAuth.Password != "" { - c.req.SetBasicAuth(c.BasicAuth.Username, c.BasicAuth.Password) - } - - c.client = &http.Client{} - - // apply timeout - if c.Timeout > 0 { - c.client.Timeout = c.Timeout * time.Second - } - - // apply cookies - if c.Cookies != nil { - jar, _ := cookiejar.New(nil) - jar.SetCookies(&url.URL{Scheme: ru.scheme(), Host: ru.host()}, c.Cookies) - c.client.Jar = jar - } - - // apply proxy - if c.ProxyURL != "" { - if proxy, err := url.Parse(c.ProxyURL); err == nil && proxy != nil { - c.client.Transport = &http.Transport{ - Proxy: http.ProxyURL(proxy), - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - } - } - - return err + return } // Resp do request and get original http response struct func (c *Client) Resp() (resp *http.Response, err error) { if err = c.buildRequest(); err != nil { - return resp, err + return } return c.client.Do(c.req) } diff --git a/vendor/github.com/monaco-io/request/url.go b/vendor/github.com/monaco-io/request/url.go index 98526149..b49b713e 100644 --- a/vendor/github.com/monaco-io/request/url.go +++ b/vendor/github.com/monaco-io/request/url.go @@ -12,14 +12,14 @@ type requestURL struct { func (ru *requestURL) EncodeURL() (err error) { ru.httpURL, err = url.Parse(ru.urlString) if err != nil { - return err + return } query := ru.httpURL.Query() for k := range ru.parameters { query.Set(k, ru.parameters[k]) } ru.httpURL.RawQuery = query.Encode() - return err + return } // String return example: https://www.google.com/search?a=1&b=2 -- cgit v1.2.3