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

add a default user-agent header #892

Merged
merged 12 commits into from
May 16, 2024
4 changes: 1 addition & 3 deletions cmd/goss/goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/urfave/cli"
)

var version string

// converts a cli context into a goss Config
func newRuntimeConfigFromCLI(c *cli.Context) *util.Config {
cfg := &util.Config{
Expand Down Expand Up @@ -69,7 +67,7 @@ func timeoutFlag(value time.Duration) cli.DurationFlag {
func main() {
app := cli.NewApp()
app.EnableBashCompletion = true
app.Version = version
app.Version = util.Version
app.Name = "goss"
app.Usage = "Quick and Easy server validation"
app.Flags = []cli.Flag{
Expand Down
4 changes: 3 additions & 1 deletion integration-tests/goss/goss-shared.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ http:
request-headers:
- "Foo: bar"
headers: ["Content-Type: application/json"]
body: ['"Foo": "bar"']
body:
- '"Foo": "bar"'
- '"User-Agent": "goss/0.0.0"'
http://httpbin/headers?host:
status: 200
timeout: 60000
Expand Down
2 changes: 1 addition & 1 deletion release-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fi
output="${output_dir}/${output_fname}"

GOOS="${os}" GOARCH="${arch}" CGO_ENABLED=0 go build \
-ldflags "-X main.version=${version_stamp} -s -w" \
-ldflags "-X github.com/goss-org/goss/util.Version=${version_stamp} -s -w" \
-o "${output}" \
github.com/goss-org/goss/cmd/goss

Expand Down
17 changes: 17 additions & 0 deletions system/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"github.com/goss-org/goss/util"
)

const USER_AGENT_HEADER_PREFIX = "user-agent:"
const DEFAULT_USER_AGENT_PREFIX = "goss/"

type HTTP interface {
HTTP() string
Status() (int, error)
Expand Down Expand Up @@ -47,6 +50,11 @@ type DefHTTP struct {

func NewDefHTTP(_ context.Context, httpStr string, system *System, config util.Config) HTTP {
headers := http.Header{}

if !hasUserAgentHeader(config.RequestHeader) {
config.RequestHeader = append(config.RequestHeader, fmt.Sprintf("%s %s%s", USER_AGENT_HEADER_PREFIX, DEFAULT_USER_AGENT_PREFIX, util.Version))
}

for _, r := range config.RequestHeader {
str := strings.SplitN(r, ": ", 2)
headers.Add(str[0], str[1])
Expand Down Expand Up @@ -209,3 +217,12 @@ func (u *DefHTTP) Body() (io.Reader, error) {

return u.resp.Body, nil
}

func hasUserAgentHeader(headers []string) bool {
for _, header := range headers {
if strings.HasPrefix(strings.ToLower(header), USER_AGENT_HEADER_PREFIX) {
return true
}
}
return false
}
3 changes: 3 additions & 0 deletions util/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package util

var Version string