opentofu/internal/httpclient/useragent.go
namgyalangmo cb2e9119aa
Update copyright notice (#1232)
Signed-off-by: namgyalangmo <75657887+namgyalangmo@users.noreply.github.com>
2024-02-08 09:48:59 +00:00

52 lines
1.2 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package httpclient
import (
"fmt"
"log"
"net/http"
"os"
"strings"
)
const (
appendUaEnvVar = "TF_APPEND_USER_AGENT"
customUaEnvVar = "OPENTOFU_USER_AGENT"
DefaultApplicationName = "OpenTofu"
)
type userAgentRoundTripper struct {
inner http.RoundTripper
userAgent string
}
func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if _, ok := req.Header["User-Agent"]; !ok {
req.Header.Set("User-Agent", rt.userAgent)
}
log.Printf("[TRACE] HTTP client %s request to %s", req.Method, req.URL.String())
return rt.inner.RoundTrip(req)
}
func OpenTofuUserAgent(version string) string {
ua := fmt.Sprintf("%s/%s", DefaultApplicationName, version)
if customUa := os.Getenv(customUaEnvVar); customUa != "" {
ua = customUa
log.Printf("[DEBUG] Using Custom User-Agent: %s", ua)
}
if add := os.Getenv(appendUaEnvVar); add != "" {
add = strings.TrimSpace(add)
if len(add) > 0 {
ua += " " + add
log.Printf("[DEBUG] Using modified User-Agent: %s", ua)
}
}
return ua
}