Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

http client that has timeouts, ssl cert ignore, redirect policy and

returns conns
  • Loading branch information...
commit b1c0110042f8f89c3efabb68ec73e7ab8c782de8 0 parents
@mreiferson authored
Showing with 94 additions and 0 deletions.
  1. +17 −0 LICENSE
  2. +6 −0 README.md
  3. +71 −0 httpclient.go
17 LICENSE
@@ -0,0 +1,17 @@
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
6 README.md
@@ -0,0 +1,6 @@
+httpclient.go
+=============
+
+simple API around Go's built in HTTP package
+
+tested w/ Go 1
71 httpclient.go
@@ -0,0 +1,71 @@
+package httpclient
+
+import (
+ "crypto/tls"
+ "errors"
+ "net"
+ "net/http"
+ "time"
+)
+
+// HttpClient wraps Go's built in HTTP client providing an API to:
+// * set connect timeout
+// * set read/write timeout
+// * skip invalid SSL certificates
+// * return the connection object from Do()
+//
+// NOTE: this client is not goroutine safe (this is a result of the
+// inability of the built in API to provide access to the connection
+// and the resulting hack to work around that)
+type HttpClient struct {
+ client *http.Client
+ conn net.Conn
+ ConnectTimeout time.Duration
+ ReadWriteTimeout time.Duration
+ MaxRedirects int
+}
+
+func New(skipInvalidSSL bool) *HttpClient {
+ client := &http.Client{}
+ h := &HttpClient{client: client}
+
+ dialFunc := func(netw, addr string) (net.Conn, error) {
+ return h.dial(netw, addr)
+ }
+ redirFunc := func(r *http.Request, v []*http.Request) error {
+ return h.redirectPolicy(r, v)
+ }
+
+ transport := &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: skipInvalidSSL},
+ Dial: dialFunc,
+ }
+
+ client.CheckRedirect = redirFunc
+ client.Transport = transport
+
+ return h
+}
+
+func (h *HttpClient) redirectPolicy(req *http.Request, via []*http.Request) error {
+ if len(via) >= h.MaxRedirects {
+ return errors.New("stopped after 3 redirects")
+ }
+ return nil
+}
+
+func (h *HttpClient) dial(netw, addr string) (net.Conn, error) {
+ deadline := time.Now().Add(h.ReadWriteTimeout + h.ConnectTimeout)
+ c, err := net.DialTimeout(netw, addr, h.ConnectTimeout)
+ if err != nil {
+ return nil, err
+ }
+ c.SetDeadline(deadline)
+ h.conn = c
+ return c, nil
+}
+
+func (h *HttpClient) Do(req *http.Request) (*http.Response, net.Conn, error) {
+ resp, err := h.client.Do(req)
+ return resp, h.conn, err
+}
Please sign in to comment.
Something went wrong with that request. Please try again.