summaryrefslogtreecommitdiff
path: root/cautious_http_client.go
blob: 2f33ae053d06800c60ba3b8d35dc073689a546da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main

import (
	"encoding/json"
	"fmt"
	"net"
	"net/http"
	"net/url"
	"time"
)

type CautiousHTTPClient interface {
	Get(string) (*http.Response, error)
	GetJSON(string, interface{}) error
}

type cautiousHttpClient struct {
	client *http.Client
}

func NewCautiousHTTPClient() CautiousHTTPClient {
	// May Need: TLSClientConfig *tls.Config
	CautiousTransport := &http.Transport{
		Proxy: http.ProxyFromEnvironment,
		DialContext: (&net.Dialer{
			Timeout:   1 * time.Second,
			KeepAlive: 30 * time.Second,
			DualStack: true,
		}).DialContext,
		MaxIdleConns:           100,
		IdleConnTimeout:        90 * time.Second,
		TLSHandshakeTimeout:    1 * time.Second,
		ExpectContinueTimeout:  1 * time.Second,
		ResponseHeaderTimeout:  10 * time.Second,
		MaxResponseHeaderBytes: 500000, // .5 MB
	}

	return &cautiousHttpClient{
		client: &http.Client{
			Transport: CautiousTransport,
			Timeout:   30 * time.Second,
		},
	}
}

func (c *cautiousHttpClient) Get(gurl string) (*http.Response, error) {
	u, err := url.Parse(gurl)
	if err != nil {
		return nil, err
	}

	// TODO
	if u.Scheme != "https" && false {
		return nil, fmt.Errorf("URL for GET must be secure")
	}

	r, err := c.client.Get(u.String())
	if err != nil {
		return nil, err
	}
	r.Body = http.MaxBytesReader(nil, r.Body, 1000000)
	return r, err
}

func (c *cautiousHttpClient) GetJSON(url string, rv interface{}) error {
	r, err := c.Get(url)
	if err != nil {
		return err
	}
	defer r.Body.Close()

	d := json.NewDecoder(r.Body)
	err = d.Decode(rv)
	if err != nil {
		return err
	}

	return nil
}