Skip to content

Commit

Permalink
Add ability to override proxy from environment in http response (#3626)
Browse files Browse the repository at this point in the history
  • Loading branch information
winem authored and danielnelson committed Mar 6, 2018
1 parent 8fac10e commit 1a03db7
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion plugins/inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
// HTTPResponse struct
type HTTPResponse struct {
Address string
Proxy string
Body string
Method string
ResponseTimeout internal.Duration
Expand Down Expand Up @@ -49,6 +50,9 @@ var sampleConfig = `
## Server address (default http://localhost)
# address = "http://localhost"
## Set http_proxy (telegraf uses the system wide proxy settings if it's is not set)
# http_proxy = "http://localhost:8888"
## Set response_timeout (default 5 seconds)
# response_timeout = "5s"
Expand Down Expand Up @@ -88,6 +92,22 @@ func (h *HTTPResponse) SampleConfig() string {
// ErrRedirectAttempted indicates that a redirect occurred
var ErrRedirectAttempted = errors.New("redirect")

// Set the proxy. A configured proxy overwrites the system wide proxy.
func getProxyFunc(http_proxy string) func(*http.Request) (*url.URL, error) {
if http_proxy == "" {
return http.ProxyFromEnvironment
}
proxyURL, err := url.Parse(http_proxy)
if err != nil {
return func(_ *http.Request) (*url.URL, error) {
return nil, errors.New("bad proxy: " + err.Error())
}
}
return func(r *http.Request) (*url.URL, error) {
return proxyURL, nil
}
}

// CreateHttpClient creates an http client which will timeout at the specified
// timeout period and can follow redirects if specified
func (h *HTTPResponse) createHttpClient() (*http.Client, error) {
Expand All @@ -98,7 +118,7 @@ func (h *HTTPResponse) createHttpClient() (*http.Client, error) {
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Proxy: getProxyFunc(h.Proxy),
DisableKeepAlives: true,
TLSClientConfig: tlsCfg,
},
Expand Down

0 comments on commit 1a03db7

Please sign in to comment.