mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
console: use an ETag for avatars
The Remote-User header is unlikely to be visible from the browser.
This commit is contained in:
@@ -35,6 +35,14 @@ func (c *Component) UserAvatarHandlerFunc(gc *gin.Context) {
|
|||||||
hash := fnv.New64()
|
hash := fnv.New64()
|
||||||
hash.Write([]byte(info.Login))
|
hash.Write([]byte(info.Login))
|
||||||
randSource := rand.New(rand.NewSource(int64(hash.Sum64())))
|
randSource := rand.New(rand.NewSource(int64(hash.Sum64())))
|
||||||
|
etag := fmt.Sprintf(`"%x"`, hash.Sum64())
|
||||||
|
|
||||||
|
// Do we have a If-None-Match header?
|
||||||
|
if header := gc.GetHeader("If-None-Match"); header == etag {
|
||||||
|
gc.Header("ETag", etag)
|
||||||
|
gc.Status(http.StatusNotModified)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Grab list of parts
|
// Grab list of parts
|
||||||
parts := []string{}
|
parts := []string{}
|
||||||
@@ -86,8 +94,7 @@ func (c *Component) UserAvatarHandlerFunc(gc *gin.Context) {
|
|||||||
|
|
||||||
// Serve the result
|
// Serve the result
|
||||||
gc.Header("Content-Type", "image/png")
|
gc.Header("Content-Type", "image/png")
|
||||||
gc.Header("Cache-Control", "max-age=86400")
|
gc.Header("ETag", etag)
|
||||||
gc.Header("Vary", "Remote-User")
|
|
||||||
gc.Status(http.StatusOK)
|
gc.Status(http.StatusOK)
|
||||||
png.Encode(gc.Writer, img)
|
png.Encode(gc.Writer, img)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,6 +87,16 @@ func TestUserHandler(t *testing.T) {
|
|||||||
}(),
|
}(),
|
||||||
ContentType: "image/png",
|
ContentType: "image/png",
|
||||||
StatusCode: 200,
|
StatusCode: 200,
|
||||||
|
}, {
|
||||||
|
Description: "avatar, simple user, etag",
|
||||||
|
URL: "/api/v0/console/user/avatar",
|
||||||
|
Header: func() netHTTP.Header {
|
||||||
|
headers := make(netHTTP.Header)
|
||||||
|
headers.Add("Remote-User", "alfred")
|
||||||
|
headers.Add("If-None-Match", `"b2e72a535032fa89"`)
|
||||||
|
return headers
|
||||||
|
}(),
|
||||||
|
StatusCode: 304,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user