Replace HTTP header strings with httpx constants

Signed-off-by: Philip Laine <philip.laine@gmail.com>
This commit is contained in:
Philip Laine 2025-06-05 11:42:05 +02:00
parent d5b5e9250f
commit 51f11bf30b
No known key found for this signature in database
GPG Key ID: F6D0B743CA3EFF33
5 changed files with 22 additions and 15 deletions

View File

@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- [#906](https://github.com/spegel-org/spegel/pull/906) Replace HTTP header strings with httpx constants.
### Deprecated
### Removed

View File

@ -6,4 +6,9 @@ const (
HeaderContentRange = "Content-Range"
HeaderRange = "Range"
HeaderAcceptRanges = "Accept-Ranges"
HeaderUserAgent = "User-Agent"
HeaderAccept = "Accept"
HeaderAuthorization = "Authorization"
HeaderWWWAuthenticate = "WWW-Authenticate"
HeaderXForwardedFor = "X-Forwarded-For"
)

View File

@ -79,7 +79,7 @@ func (s *ServeMux) Handle(pattern string, handler HandlerFunc) {
}
func GetClientIP(req *http.Request) string {
forwardedFor := req.Header.Get("X-Forwarded-For")
forwardedFor := req.Header.Get(HeaderXForwardedFor)
if forwardedFor != "" {
comps := strings.Split(forwardedFor, ",")
if len(comps) > 1 {

View File

@ -91,7 +91,7 @@ func TestGetClientIP(t *testing.T) {
name: "x forwarded for single",
request: &http.Request{
Header: http.Header{
"X-Forwarded-For": []string{"localhost"},
HeaderXForwardedFor: []string{"localhost"},
},
},
expected: "localhost",
@ -100,7 +100,7 @@ func TestGetClientIP(t *testing.T) {
name: "x forwarded for multiple",
request: &http.Request{
Header: http.Header{
"X-Forwarded-For": []string{"localhost,127.0.0.1"},
HeaderXForwardedFor: []string{"localhost,127.0.0.1"},
},
},
expected: "localhost",

View File

@ -174,18 +174,18 @@ func (c *Client) fetch(ctx context.Context, method string, dist DistributionPath
if err != nil {
return nil, ocispec.Descriptor{}, err
}
req.Header.Set("User-Agent", "spegel")
req.Header.Add("Accept", "application/vnd.oci.image.manifest.v1+json")
req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.v2+json")
req.Header.Add("Accept", "application/vnd.oci.image.index.v1+json")
req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.list.v2+json")
req.Header.Set(httpx.HeaderUserAgent, "spegel")
req.Header.Add(httpx.HeaderAccept, "application/vnd.oci.image.manifest.v1+json")
req.Header.Add(httpx.HeaderAccept, "application/vnd.docker.distribution.manifest.v2+json")
req.Header.Add(httpx.HeaderAccept, "application/vnd.oci.image.index.v1+json")
req.Header.Add(httpx.HeaderAccept, "application/vnd.docker.distribution.manifest.list.v2+json")
if len(brr) > 0 {
req.Header.Add(httpx.HeaderRange, httpx.FormatMultipartRangeHeader(brr))
}
token, ok := c.tc.Load(tcKey)
if ok {
//nolint: errcheck // We know it will be a string.
req.Header.Set("Authorization", "Bearer "+token.(string))
req.Header.Set(httpx.HeaderAuthorization, "Bearer "+token.(string))
}
resp, err := c.hc.Do(req)
if err != nil {
@ -193,7 +193,7 @@ func (c *Client) fetch(ctx context.Context, method string, dist DistributionPath
}
if resp.StatusCode == http.StatusUnauthorized {
c.tc.Delete(tcKey)
wwwAuth := resp.Header.Get("WWW-Authenticate")
wwwAuth := resp.Header.Get(httpx.HeaderWWWAuthenticate)
token, err = getBearerToken(ctx, wwwAuth, c.hc)
if err != nil {
return nil, ocispec.Descriptor{}, err