Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:headers concurrent map writes and add dial remote addr #794

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update http_response.go
arch3754 authored Feb 6, 2024
commit 95677e2bcc5dddec433f5371f5778f19d90ee3c3
50 changes: 30 additions & 20 deletions inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package http_response

import (
"compress/gzip"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
@@ -39,13 +39,13 @@ type Instance struct {
Targets []string `toml:"targets"`
Interface string `toml:"interface"`
ResponseTimeout config.Duration `toml:"response_timeout"`
Headers []string `toml:"headers"`
Body string `toml:"body"`
HttpRemoteAddr string `toml:"http_remote_addr"`
HttpsRemoteAddr string `toml:"https_remote_addr"`
ExpectResponseSubstring string `toml:"expect_response_substring"`
ExpectResponseRegularExpression string `toml:"expect_response_regular_expression"`
ExpectResponseStatusCode *int `toml:"expect_response_status_code"`
ExpectResponseStatusCodes string `toml:"expect_response_status_codes"`
config.HTTPProxy

client httpClient
config.HTTPCommonConfig
@@ -105,22 +105,22 @@ func (ins *Instance) createHTTPClient() (*http.Client, error) {
}

dialer := &net.Dialer{}

if ins.Interface != "" {
dialer.LocalAddr, err = netx.LocalAddressByInterfaceName(ins.Interface)
if err != nil {
return nil, err
}
}

proxy, err := ins.Proxy()
if err != nil {
return nil, err
}
//proxy, err := ins.Proxy()
//if err != nil {
// return nil, err
//}

client := httpx.CreateHTTPClient(httpx.TlsConfig(tlsCfg),
httpx.NetDialer(dialer), httpx.Proxy(proxy),

httpx.NetDialer(dialer),
httpx.SetTransportRemoteAddr(ins.HttpRemoteAddr),
httpx.SetTransportTlsRemoteAddr(ins.HttpsRemoteAddr, tlsCfg),
httpx.DisableKeepAlives(*ins.DisableKeepAlives),
httpx.Timeout(time.Duration(ins.Timeout)),
httpx.FollowRedirects(*ins.FollowRedirects))
@@ -242,12 +242,7 @@ func (ins *Instance) httpGather(target string) (map[string]string, map[string]in
return nil, nil, err
}

// compatible with old config
for i := 0; i < len(ins.Headers); i += 2 {
ins.HTTPCommonConfig.Headers[ins.Headers[i]] = ins.Headers[i+1]
}
ins.SetHeaders(request)

// Start Timer
start := time.Now()
resp, err := ins.client.Do(request)
@@ -300,12 +295,27 @@ func (ins *Instance) httpGather(target string) (map[string]string, map[string]in
// metric: response_code
fields["response_code"] = resp.StatusCode

bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("E! failed to read response body:", err)
return tags, fields, nil
var bs []byte
if resp.Header.Get("content-encode") == "gzip" {
var r *gzip.Reader
r, err = gzip.NewReader(resp.Body)
if err != nil {
log.Println("E! failed to read gzip response body:", err)
return tags, fields, nil
}
defer r.Close()
bs, err = io.ReadAll(r)
if err != nil {
log.Println("E! failed to read response body:", err)
return tags, fields, nil
}
} else {
bs, err = io.ReadAll(resp.Body)
if err != nil {
log.Println("E! failed to read response body:", err)
return tags, fields, nil
}
}

if len(ins.ExpectResponseSubstring) > 0 && !strings.Contains(string(bs), ins.ExpectResponseSubstring) ||
ins.regularExpression != nil && !ins.regularExpression.Match(bs) {
log.Println("E! body mismatch, response body:", string(bs))