console: use an ETag for avatars

The Remote-User header is unlikely to be visible from the browser.
This commit is contained in:
Vincent Bernat
2022-06-01 11:58:00 +02:00
parent 0a2c801297
commit 78f628b6db
2 changed files with 19 additions and 2 deletions

View File

@@ -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)
} }

View File

@@ -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,
}, },
}) })
}) })