Replace HTTP header strings with httpx constants
Signed-off-by: Philip Laine <philip.laine@gmail.com>
This commit is contained in:
parent
d5b5e9250f
commit
51f11bf30b
@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
- [#906](https://github.com/spegel-org/spegel/pull/906) Replace HTTP header strings with httpx constants.
|
||||||
|
|
||||||
### Deprecated
|
### Deprecated
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
|
@ -6,4 +6,9 @@ const (
|
|||||||
HeaderContentRange = "Content-Range"
|
HeaderContentRange = "Content-Range"
|
||||||
HeaderRange = "Range"
|
HeaderRange = "Range"
|
||||||
HeaderAcceptRanges = "Accept-Ranges"
|
HeaderAcceptRanges = "Accept-Ranges"
|
||||||
|
HeaderUserAgent = "User-Agent"
|
||||||
|
HeaderAccept = "Accept"
|
||||||
|
HeaderAuthorization = "Authorization"
|
||||||
|
HeaderWWWAuthenticate = "WWW-Authenticate"
|
||||||
|
HeaderXForwardedFor = "X-Forwarded-For"
|
||||||
)
|
)
|
||||||
|
@ -79,7 +79,7 @@ func (s *ServeMux) Handle(pattern string, handler HandlerFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetClientIP(req *http.Request) string {
|
func GetClientIP(req *http.Request) string {
|
||||||
forwardedFor := req.Header.Get("X-Forwarded-For")
|
forwardedFor := req.Header.Get(HeaderXForwardedFor)
|
||||||
if forwardedFor != "" {
|
if forwardedFor != "" {
|
||||||
comps := strings.Split(forwardedFor, ",")
|
comps := strings.Split(forwardedFor, ",")
|
||||||
if len(comps) > 1 {
|
if len(comps) > 1 {
|
||||||
|
@ -91,7 +91,7 @@ func TestGetClientIP(t *testing.T) {
|
|||||||
name: "x forwarded for single",
|
name: "x forwarded for single",
|
||||||
request: &http.Request{
|
request: &http.Request{
|
||||||
Header: http.Header{
|
Header: http.Header{
|
||||||
"X-Forwarded-For": []string{"localhost"},
|
HeaderXForwardedFor: []string{"localhost"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expected: "localhost",
|
expected: "localhost",
|
||||||
@ -100,7 +100,7 @@ func TestGetClientIP(t *testing.T) {
|
|||||||
name: "x forwarded for multiple",
|
name: "x forwarded for multiple",
|
||||||
request: &http.Request{
|
request: &http.Request{
|
||||||
Header: http.Header{
|
Header: http.Header{
|
||||||
"X-Forwarded-For": []string{"localhost,127.0.0.1"},
|
HeaderXForwardedFor: []string{"localhost,127.0.0.1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expected: "localhost",
|
expected: "localhost",
|
||||||
|
@ -174,18 +174,18 @@ func (c *Client) fetch(ctx context.Context, method string, dist DistributionPath
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ocispec.Descriptor{}, err
|
return nil, ocispec.Descriptor{}, err
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "spegel")
|
req.Header.Set(httpx.HeaderUserAgent, "spegel")
|
||||||
req.Header.Add("Accept", "application/vnd.oci.image.manifest.v1+json")
|
req.Header.Add(httpx.HeaderAccept, "application/vnd.oci.image.manifest.v1+json")
|
||||||
req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.v2+json")
|
req.Header.Add(httpx.HeaderAccept, "application/vnd.docker.distribution.manifest.v2+json")
|
||||||
req.Header.Add("Accept", "application/vnd.oci.image.index.v1+json")
|
req.Header.Add(httpx.HeaderAccept, "application/vnd.oci.image.index.v1+json")
|
||||||
req.Header.Add("Accept", "application/vnd.docker.distribution.manifest.list.v2+json")
|
req.Header.Add(httpx.HeaderAccept, "application/vnd.docker.distribution.manifest.list.v2+json")
|
||||||
if len(brr) > 0 {
|
if len(brr) > 0 {
|
||||||
req.Header.Add(httpx.HeaderRange, httpx.FormatMultipartRangeHeader(brr))
|
req.Header.Add(httpx.HeaderRange, httpx.FormatMultipartRangeHeader(brr))
|
||||||
}
|
}
|
||||||
token, ok := c.tc.Load(tcKey)
|
token, ok := c.tc.Load(tcKey)
|
||||||
if ok {
|
if ok {
|
||||||
//nolint: errcheck // We know it will be a string.
|
//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)
|
resp, err := c.hc.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -193,7 +193,7 @@ func (c *Client) fetch(ctx context.Context, method string, dist DistributionPath
|
|||||||
}
|
}
|
||||||
if resp.StatusCode == http.StatusUnauthorized {
|
if resp.StatusCode == http.StatusUnauthorized {
|
||||||
c.tc.Delete(tcKey)
|
c.tc.Delete(tcKey)
|
||||||
wwwAuth := resp.Header.Get("WWW-Authenticate")
|
wwwAuth := resp.Header.Get(httpx.HeaderWWWAuthenticate)
|
||||||
token, err = getBearerToken(ctx, wwwAuth, c.hc)
|
token, err = getBearerToken(ctx, wwwAuth, c.hc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ocispec.Descriptor{}, err
|
return nil, ocispec.Descriptor{}, err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user