mirror of
https://gitea.com/gitea/tea.git
synced 2026-02-21 22:03:32 +01:00
Move versions/filelocker into dedicated subpackages, and consistent headers in http requests (#888)
- move filelocker logic into dedicated subpackage - consistent useragent in requests Reviewed-on: https://gitea.com/gitea/tea/pulls/888 Co-authored-by: techknowlogick <techknowlogick@gitea.com> Co-committed-by: techknowlogick <techknowlogick@gitea.com>
This commit is contained in:
committed by
techknowlogick
parent
982adb4d02
commit
49a9032d8a
38
modules/httputil/httputil.go
Normal file
38
modules/httputil/httputil.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package httputil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"runtime"
|
||||
|
||||
"code.gitea.io/tea/modules/version"
|
||||
)
|
||||
|
||||
// UserAgent returns the standard User-Agent string for tea.
|
||||
func UserAgent() string {
|
||||
ua := fmt.Sprintf("tea/%s (%s/%s)", version.Version, runtime.GOOS, runtime.GOARCH)
|
||||
if version.SDK != "" {
|
||||
ua += fmt.Sprintf(" go-sdk/%s", version.SDK)
|
||||
}
|
||||
return ua
|
||||
}
|
||||
|
||||
// WrapTransport wraps an http.RoundTripper to add the User-Agent header.
|
||||
func WrapTransport(base http.RoundTripper) http.RoundTripper {
|
||||
if base == nil {
|
||||
base = http.DefaultTransport
|
||||
}
|
||||
return &userAgentTransport{base: base}
|
||||
}
|
||||
|
||||
type userAgentTransport struct {
|
||||
base http.RoundTripper
|
||||
}
|
||||
|
||||
func (t *userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Set("User-Agent", UserAgent())
|
||||
return t.base.RoundTrip(req)
|
||||
}
|
||||
Reference in New Issue
Block a user