github.com/netdata/go.d.plugin@v0.58.1/pkg/web/request.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package web
     4  
     5  import (
     6  	"encoding/base64"
     7  	"io"
     8  	"net/http"
     9  	"strings"
    10  )
    11  
    12  // Request is the configuration of the HTTP request.
    13  // This structure is not intended to be used directly as part of a module's configuration.
    14  // Supported configuration file formats: YAML.
    15  type Request struct {
    16  	// URL specifies the URL to access.
    17  	URL string `yaml:"url"`
    18  
    19  	// Body specifies the HTTP request body to be sent by the client.
    20  	Body string `yaml:"body"`
    21  
    22  	// Method specifies the HTTP method (GET, POST, PUT, etc.). An empty string means GET.
    23  	Method string `yaml:"method"`
    24  
    25  	// Headers specifies the HTTP request header fields to be sent by the client.
    26  	Headers map[string]string `yaml:"headers"`
    27  
    28  	// Username specifies the username for basic HTTP authentication.
    29  	Username string `yaml:"username"`
    30  
    31  	// Password specifies the password for basic HTTP authentication.
    32  	Password string `yaml:"password"`
    33  
    34  	// ProxyUsername specifies the username for basic HTTP authentication.
    35  	// It is used to authenticate a user agent to a proxy server.
    36  	ProxyUsername string `yaml:"proxy_username"`
    37  
    38  	// ProxyPassword specifies the password for basic HTTP authentication.
    39  	// It is used to authenticate a user agent to a proxy server.
    40  	ProxyPassword string `yaml:"proxy_password"`
    41  }
    42  
    43  // Copy makes a full copy of the Request.
    44  func (r Request) Copy() Request {
    45  	headers := make(map[string]string, len(r.Headers))
    46  	for k, v := range r.Headers {
    47  		headers[k] = v
    48  	}
    49  	r.Headers = headers
    50  	return r
    51  }
    52  
    53  // NewHTTPRequest returns a new *http.Requests given a Request configuration and an error if any.
    54  func NewHTTPRequest(cfg Request) (*http.Request, error) {
    55  	var body io.Reader
    56  	if cfg.Body != "" {
    57  		body = strings.NewReader(cfg.Body)
    58  	}
    59  
    60  	req, err := http.NewRequest(cfg.Method, cfg.URL, body)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	if cfg.Username != "" || cfg.Password != "" {
    66  		req.SetBasicAuth(cfg.Username, cfg.Password)
    67  	}
    68  
    69  	if cfg.ProxyUsername != "" && cfg.ProxyPassword != "" {
    70  		basicAuth := base64.StdEncoding.EncodeToString([]byte(cfg.ProxyUsername + ":" + cfg.ProxyPassword))
    71  		req.Header.Set("Proxy-Authorization", "Basic "+basicAuth)
    72  	}
    73  
    74  	for k, v := range cfg.Headers {
    75  		switch k {
    76  		case "host", "Host":
    77  			req.Host = v
    78  		default:
    79  			req.Header.Set(k, v)
    80  		}
    81  	}
    82  
    83  	return req, nil
    84  }