gateway: an image's decode is counted wherever its header sits

3eee19b08 batches a request's images by what each holds once decoded,
read from its header. fitHolds read only the first 4 KB of base64 for
the header, so a JPEG whose frame header sits behind a large EXIF
segment (a phone photo's often does, up to 64 KB) counted as holding
nothing, and was made beside three others each holding up to
codexImageMaxBytes. fitHolds now reads the header from the base64 as a
stream, as far as the format needs, and counts the bytes the image came
in too, as codexImageRefit does.

Run:
- go test -run TestCodex -count=1 ./internal/gateway/: ok.
- TestCodexAHeaderBehindExifIsCounted on 3eee19b08's codex_images.go:
  red, "a JPEG with its header behind 63 KB of EXIF holds 0 MB, want
  29 MB"; green with this change.
This commit is contained in:
yetone
2026-10-20 23:14:02 +08:00
parent 21c2ab6fa5
commit a9304efe01
2 changed files with 36 additions and 15 deletions
+9 -15
View File
@@ -321,28 +321,22 @@ func imageHolds(cfg image.Config, format string) (int, bool) {
return int(pixels) * bytesPerPixel, true
}
// fitsHead is how much of an image's base64 fitHolds reads to tell its
// header: enough for a PNG's chunks and a JPEG's segments, and little
// enough that asking costs nothing.
const fitsHead = 4 << 10
// fitHolds is what making an image of url will hold, from its header
// alone, so that nothing is decoded to find out. An image it can't read
// holds nothing and isn't made.
// alone, so that nothing is decoded to find out: its pixels and the bytes
// they came in, as codexImageRefit counts them. The header is read from
// the base64 as a stream, as far as the format needs, so a JPEG whose
// frame header sits behind a large EXIF segment is still counted; an
// image whose header can't be read at all isn't made either.
func fitHolds(url string) int {
comma := strings.IndexByte(url, ',')
if comma < 0 || !strings.HasSuffix(url[:comma], ";base64") {
return 0
}
b64 := url[comma+1:]
if len(b64) > fitsHead {
b64 = b64[:fitsHead&^3] // whole groups of four, as base64 reads them
if len(b64) > codexImageMaxBytes {
return 0 // codexImageRefit turns it away before decoding
}
head, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return 0
}
cfg, format, err := image.DecodeConfig(bytes.NewReader(head))
cfg, format, err := image.DecodeConfig(base64.NewDecoder(base64.StdEncoding, strings.NewReader(b64)))
if err != nil {
return 0
}
@@ -350,7 +344,7 @@ func fitHolds(url string) int {
if !ok {
return 0
}
return holds
return min(base64.StdEncoding.DecodedLen(len(b64))+holds, holdsMost)
}
// fitEntry is what an image is sent as: data in mime, or, with no data,
+27
View File
@@ -610,3 +610,30 @@ func TestCodexFittedImagesLetGoWhenIdle(t *testing.T) {
t.Errorf("%d bytes held, want %d", held, want)
}
}
// A JPEG's frame header can sit behind an EXIF segment of up to 64 KB, and
// a phone photo's often does. fitHolds read only the first 4 KB of base64
// for it, counted such an image as holding nothing, and put it in a batch
// with three others each holding up to codexImageMaxBytes. It is counted
// now for what it holds, wherever its header is.
func TestCodexAHeaderBehindExifIsCounted(t *testing.T) {
var shot bytes.Buffer
if err := jpeg.Encode(&shot, shotImage(3456, 2234), &jpeg.Options{Quality: 85}); err != nil {
t.Fatal(err)
}
raw := shot.Bytes()
// SOI, then an APP1 of the most a segment holds, then the rest
exif := make([]byte, 0xffff-2)
copy(exif, "Exif\x00\x00")
withExif := append([]byte{0xff, 0xd8, 0xff, 0xe1, 0xff, 0xff}, exif...)
withExif = append(withExif, raw[2:]...)
url := "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(withExif)
if _, _, err := image.DecodeConfig(bytes.NewReader(withExif)); err != nil {
t.Fatalf("the photo isn't a JPEG: %v", err)
}
cfg := image.Config{ColorModel: color.YCbCrModel, Width: 3456, Height: 2234}
pixels, _ := imageHolds(cfg, "jpeg")
if got, want := fitHolds(url), len(withExif)+pixels; got != want {
t.Errorf("a JPEG with its header behind %d KB of EXIF holds %d MB, want %d MB", len(exif)>>10, got>>20, want>>20)
}
}