Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ff5caad6f |
No files matched your search
@@ -175,14 +175,12 @@ path under `/v1/` answers 200, in maintenance mode too.
|
|||||||
- `GET` or `HEAD` `/v1/image/<host>/<path>/<size>.<format>` — an image, fetched,
|
- `GET` or `HEAD` `/v1/image/<host>/<path>/<size>.<format>` — an image, fetched,
|
||||||
resized and converted (below). Needs: a signature, unless the host is
|
resized and converted (below). Needs: a signature, unless the host is
|
||||||
allowlisted (see Source Hosts). Answers: 200; 304 when `If-None-Match` matches
|
allowlisted (see Source Hosts). Answers: 200; 304 when `If-None-Match` matches
|
||||||
the image's `ETag`; 400 for a URL or parameter that is not valid, or for the
|
the image's `ETag`; 400 for a URL or parameter that is not valid; 401 for a
|
||||||
format `auto` an `Accept` header that is not valid; 406 for the format `auto`
|
missing or wrong signature, a missing `exp` or an `exp` in the past; 403 when
|
||||||
when `Accept` allows none of the formats it chooses from; 401 for a missing or
|
the request's `Referer` names a host in `referer_blocklist`, checked before
|
||||||
wrong signature, a missing `exp` or an `exp` in the past; 403 when the
|
the signature, the cache and the upstream fetch; 403 when the upstream host,
|
||||||
request's `Referer` names a host in `referer_blocklist`, checked before the
|
or a host it redirects to, is `localhost`, ends in `.localhost` or `.local`,
|
||||||
signature, the cache and the upstream fetch; 403 when the upstream host, or a
|
or has an address in a blocked network (see `blocked_networks`); 502 when the
|
||||||
host it redirects to, is `localhost`, ends in `.localhost` or `.local`, or has
|
|
||||||
an address in a blocked network (see `blocked_networks`); 502 when the
|
|
||||||
upstream answered with an error status, and for 5 minutes after that for the
|
upstream answered with an error status, and for 5 minutes after that for the
|
||||||
same source URL; 503 when pixa is busy or in maintenance mode; 500 for any
|
same source URL; 503 when pixa is busy or in maintenance mode; 500 for any
|
||||||
other failure.
|
other failure.
|
||||||
@@ -192,8 +190,7 @@ path under `/v1/` answers 200, in maintenance mode too.
|
|||||||
decrypt, or that asks for a size or fit that is not valid; 410 once it has
|
decrypt, or that asks for a size or fit that is not valid; 410 once it has
|
||||||
expired; 504 when the upstream has not sent its response headers within
|
expired; 504 when the upstream has not sent its response headers within
|
||||||
`upstream_fetch_timeout`, but 500 when that time runs out while the image
|
`upstream_fetch_timeout`, but 500 when that time runs out while the image
|
||||||
itself is still arriving; 400 for an `Accept` header that is not valid, and
|
itself is still arriving; 403, 502, 503 and 500 as for `/v1/image/`.
|
||||||
406, 403, 502, 503 and 500, as for `/v1/image/`.
|
|
||||||
- `GET /robots.txt` — asks every crawler to stay away (`Disallow: /`). Needs:
|
- `GET /robots.txt` — asks every crawler to stay away (`Disallow: /`). Needs:
|
||||||
nothing. Answers: 200.
|
nothing. Answers: 200.
|
||||||
- `GET /.well-known/healthcheck.json` — JSON with `status` (`ok`), `now`,
|
- `GET /.well-known/healthcheck.json` — JSON with `status` (`ok`), `now`,
|
||||||
@@ -242,7 +239,7 @@ A request whose query string cannot be decoded, or gives any parameter more than
|
|||||||
once, is refused with 400.
|
once, is refused with 400.
|
||||||
|
|
||||||
- `<format>`: one of `orig` (or `original`), `jpeg` (or `jpg`), `png`, `webp`,
|
- `<format>`: one of `orig` (or `original`), `jpeg` (or `jpg`), `png`, `webp`,
|
||||||
`avif`, `gif`, or `auto` (below)
|
`avif`, `gif`
|
||||||
- `<size>`: `orig` or `<width>x<height>` (e.g. `800x600`)
|
- `<size>`: `orig` or `<width>x<height>` (e.g. `800x600`)
|
||||||
- `sig` and `exp`: the signature and its expiry, needed unless the host is
|
- `sig` and `exp`: the signature and its expiry, needed unless the host is
|
||||||
allowlisted (see Signature Specification)
|
allowlisted (see Signature Specification)
|
||||||
@@ -250,24 +247,6 @@ once, is refused with 400.
|
|||||||
both optional (values under Signature Specification). Both are part of what is
|
both optional (values under Signature Specification). Both are part of what is
|
||||||
cached, so each value of either is a separate cached image.
|
cached, so each value of either is a separate cached image.
|
||||||
|
|
||||||
With the format `auto`, pixa chooses the format for each request from its
|
|
||||||
`Accept` header, in this order:
|
|
||||||
|
|
||||||
1. AVIF, when the header names `image/avif`;
|
|
||||||
2. WebP, when it names `image/webp`;
|
|
||||||
3. JPEG, when the first of `image/jpeg`, `image/*` and `*/*` that it names
|
|
||||||
allows it, or when there is no `Accept` header or it is empty.
|
|
||||||
|
|
||||||
An entry with `q=0` refuses its format; other `q` values do not change the
|
|
||||||
order. AVIF and WebP must be named, as clients that cannot show them also send
|
|
||||||
`image/*` and `*/*`. pixa never sends a format the client refused: when the
|
|
||||||
header allows none of the three, the answer is 406, and a header that does not
|
|
||||||
parse, or has a `q` that is not a number from 0 to 1, is refused with 400. The
|
|
||||||
signature, or the token of an encrypted URL, covers `auto` itself, so one URL
|
|
||||||
serves every client. Each format chosen is cached as a separate image, and every
|
|
||||||
answer that depends on `Accept` (the image, a 304, and the 400 and 406 above)
|
|
||||||
carries `Vary: Accept`, so a shared cache keeps the formats apart too.
|
|
||||||
|
|
||||||
An image is served with `Cache-Control: public, max-age=<seconds>, immutable`.
|
An image is served with `Cache-Control: public, max-age=<seconds>, immutable`.
|
||||||
When the URL has an expiry (an `exp`, or the TTL of an encrypted URL), `max-age`
|
When the URL has an expiry (an `exp`, or the TTL of an encrypted URL), `max-age`
|
||||||
is the whole seconds left until then, at most one year, so no browser or proxy
|
is the whole seconds left until then, at most one year, so no browser or proxy
|
||||||
@@ -318,7 +297,7 @@ nor change what it asks for.
|
|||||||
3. The page shows the URL, `https://<host>/v1/e/<token>/img.<format>`, and when
|
3. The page shows the URL, `https://<host>/v1/e/<token>/img.<format>`, and when
|
||||||
it expires. `<host>` is the host the page was opened on, and the URL starts
|
it expires. `<host>` is the host the page was opened on, and the URL starts
|
||||||
with `http` instead while `debug` is on. The name after the token is ignored
|
with `http` instead while `debug` is on. The name after the token is ignored
|
||||||
and only gives the URL a file extension, `jpg` for `orig` and `auto`.
|
and only gives the URL a file extension, `jpg` for `orig`.
|
||||||
|
|
||||||
The token holds the source's host, path and query and the size, format, quality,
|
The token holds the source's host, path and query and the size, format, quality,
|
||||||
fit and expiry, encrypted with a key derived from `signing_key`. The source
|
fit and expiry, encrypted with a key derived from `signing_key`. The source
|
||||||
@@ -374,8 +353,7 @@ Where:
|
|||||||
- `width` — requested width in pixels, `0` for original
|
- `width` — requested width in pixels, `0` for original
|
||||||
- `height` — requested height in pixels, `0` for original
|
- `height` — requested height in pixels, `0` for original
|
||||||
- `format` — output format, one of those listed under Routes, with `original`
|
- `format` — output format, one of those listed under Routes, with `original`
|
||||||
signed as `orig` and `jpg` as `jpeg`; `auto` is signed as `auto`, not as the
|
signed as `orig` and `jpg` as `jpeg`
|
||||||
format chosen for the request
|
|
||||||
- `expiration` — the URL's `exp` query parameter, the Unix timestamp when the
|
- `expiration` — the URL's `exp` query parameter, the Unix timestamp when the
|
||||||
signature expires; a request whose `exp` is not a whole number, an empty
|
signature expires; a request whose `exp` is not a whole number, an empty
|
||||||
`exp=` included, is refused with 400
|
`exp=` included, is refused with 400
|
||||||
|
|||||||
@@ -30,17 +30,6 @@ P2: security: per-IP rate limiting on the image routes
|
|||||||
|
|
||||||
# Completed Steps
|
# Completed Steps
|
||||||
|
|
||||||
- 2026-10-05 the format `auto` (closes #88): a format in the `/v1/image/` path,
|
|
||||||
an encrypted URL's token and the generator page's format choice, chosen for
|
|
||||||
each request from `Accept` once the signature or token is checked: AVIF when
|
|
||||||
the header names `image/avif`, else WebP when it names `image/webp`, else JPEG
|
|
||||||
when the first of `image/jpeg`, `image/*` and `*/*` that it names allows it,
|
|
||||||
or when it names nothing; `q=0` refuses a format. AVIF and WebP must be named,
|
|
||||||
as clients that cannot show them send the wildcards too. A header that allows
|
|
||||||
none of the three answers 406, one that does not parse 400. The signature and
|
|
||||||
the token cover `auto` itself; the cache key and `ETag` use the format chosen.
|
|
||||||
Answers from the point the format is chosen carry `Vary: Accept`, next to the
|
|
||||||
CORS `Vary: Origin`; fixed-format answers do not.
|
|
||||||
- 2026-10-05 the markdown is formatted with prettier (closes #100): `script/fmt`
|
- 2026-10-05 the markdown is formatted with prettier (closes #100): `script/fmt`
|
||||||
and `script/fmt-check` run prettier 3.8.1, pinned in `package.json` and
|
and `script/fmt-check` run prettier 3.8.1, pinned in `package.json` and
|
||||||
`yarn.lock`, on `**/*.md` after `gofmt`, with four-space tabs and
|
`yarn.lock`, on `**/*.md` after `gofmt`, with four-space tabs and
|
||||||
@@ -672,6 +661,8 @@ P2: security: per-IP rate limiting on the image routes
|
|||||||
- per-origin rate limiting
|
- per-origin rate limiting
|
||||||
- P2: HTTP response handling
|
- P2: HTTP response handling
|
||||||
- Last-Modified headers
|
- Last-Modified headers
|
||||||
|
- Vary header for content negotiation
|
||||||
|
- P2: auto format selection (format=auto based on Accept header)
|
||||||
- P2: configuration
|
- P2: configuration
|
||||||
- YAML config file support
|
- YAML config file support
|
||||||
- P2: operational
|
- P2: operational
|
||||||
|
|||||||
@@ -371,7 +371,7 @@ func (s *Handlers) buildGeneratedURL(r *http.Request, token, format string) stri
|
|||||||
|
|
||||||
// Determine file extension for the trailing filename
|
// Determine file extension for the trailing filename
|
||||||
ext := format
|
ext := format
|
||||||
if ext == "" || ext == "orig" || ext == "auto" {
|
if ext == "" || ext == "orig" {
|
||||||
ext = "jpg" // Default extension
|
ext = "jpg" // Default extension
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,131 +0,0 @@
|
|||||||
package handlers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"mime"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"sneak.berlin/go/pixa/internal/imgcache"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Errors for an Accept header that an auto URL cannot be served for.
|
|
||||||
var (
|
|
||||||
errInvalidAccept = errors.New("invalid Accept header")
|
|
||||||
errNotAcceptable = errors.New(
|
|
||||||
"not acceptable: auto serves image/avif, image/webp or image/jpeg")
|
|
||||||
)
|
|
||||||
|
|
||||||
// chooseAutoFormat replaces the format auto in req with the format
|
|
||||||
// formatForAccept chooses from r's Accept header, and adds Vary: Accept to the
|
|
||||||
// response, which then depends on that header. It answers 400 for an Accept
|
|
||||||
// header that is not valid and 406 for one that allows none of the formats,
|
|
||||||
// and reports whether req can be served. Any other format is left as it is.
|
|
||||||
func (s *Handlers) chooseAutoFormat(
|
|
||||||
w http.ResponseWriter, r *http.Request, req *imgcache.ImageRequest,
|
|
||||||
) bool {
|
|
||||||
if req.Format != imgcache.FormatAuto {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Add("Vary", "Accept")
|
|
||||||
|
|
||||||
format, err := formatForAccept(strings.Join(r.Header.Values("Accept"), ","))
|
|
||||||
if errors.Is(err, errNotAcceptable) {
|
|
||||||
s.respondError(w, err.Error(), http.StatusNotAcceptable)
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
s.respondError(w, err.Error(), http.StatusBadRequest)
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Format = format
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// formatForAccept returns the format an auto URL is served in for the Accept
|
|
||||||
// header accept: AVIF when it names image/avif, else WebP when it names
|
|
||||||
// image/webp, else JPEG when its most specific entry of image/jpeg, image/*
|
|
||||||
// and */* allows it, or when it names nothing. A q of 0 refuses a format.
|
|
||||||
// AVIF and WebP must be named, as clients that cannot show them send image/*
|
|
||||||
// and */* too.
|
|
||||||
func formatForAccept(accept string) (imgcache.ImageFormat, error) {
|
|
||||||
qualities, err := parseAccept(accept)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(qualities) == 0 {
|
|
||||||
return imgcache.FormatJPEG, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if qualities["image/avif"] > 0 {
|
|
||||||
return imgcache.FormatAVIF, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if qualities["image/webp"] > 0 {
|
|
||||||
return imgcache.FormatWebP, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// For JPEG, the most specific entry the header has decides
|
|
||||||
quality, named := qualities["image/jpeg"]
|
|
||||||
if !named {
|
|
||||||
quality, named = qualities["image/*"]
|
|
||||||
}
|
|
||||||
|
|
||||||
if !named {
|
|
||||||
quality = qualities["*/*"]
|
|
||||||
}
|
|
||||||
|
|
||||||
if quality > 0 {
|
|
||||||
return imgcache.FormatJPEG, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", errNotAcceptable
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseAccept returns the q of each media range the Accept header accept
|
|
||||||
// names, 1 where it gives none. A media range named more than once keeps its
|
|
||||||
// lowest q, so a refusal is never overridden. A media range that does not
|
|
||||||
// parse, or a q that is not a number from 0 to 1, is an error.
|
|
||||||
func parseAccept(accept string) (map[string]float64, error) {
|
|
||||||
qualities := make(map[string]float64)
|
|
||||||
|
|
||||||
for entry := range strings.SplitSeq(accept, ",") {
|
|
||||||
// A header field list may hold empty entries
|
|
||||||
if strings.TrimSpace(entry) == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
mediaRange, params, err := mime.ParseMediaType(entry)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%w: %q: %w", errInvalidAccept, entry, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
quality := 1.0
|
|
||||||
|
|
||||||
if qParam, given := params["q"]; given {
|
|
||||||
quality, err = strconv.ParseFloat(qParam, 64)
|
|
||||||
inRange := quality >= 0 && quality <= 1
|
|
||||||
|
|
||||||
if err != nil || !inRange {
|
|
||||||
return nil, fmt.Errorf("%w: %q: q is not a number from 0 to 1",
|
|
||||||
errInvalidAccept, entry)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
previous, named := qualities[mediaRange]
|
|
||||||
if !named || quality < previous {
|
|
||||||
qualities[mediaRange] = quality
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return qualities, nil
|
|
||||||
}
|
|
||||||
@@ -3,6 +3,7 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"maps"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"slices"
|
"slices"
|
||||||
@@ -16,14 +17,6 @@ import (
|
|||||||
"sneak.berlin/go/pixa/internal/imgcache"
|
"sneak.berlin/go/pixa/internal/imgcache"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The content types the tests below expect.
|
|
||||||
const (
|
|
||||||
avifType = "image/avif"
|
|
||||||
webpType = "image/webp"
|
|
||||||
jpegType = "image/jpeg"
|
|
||||||
jsonType = "application/json"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestFormatForAccept verifies the format chosen for the format auto from each
|
// TestFormatForAccept verifies the format chosen for the format auto from each
|
||||||
// Accept header below, and the error for one that allows none of AVIF, WebP
|
// Accept header below, and the error for one that allows none of AVIF, WebP
|
||||||
// and JPEG or is not valid.
|
// and JPEG or is not valid.
|
||||||
@@ -42,7 +35,7 @@ func TestFormatForAccept(t *testing.T) {
|
|||||||
{"WebP-capable browser",
|
{"WebP-capable browser",
|
||||||
"image/webp,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5",
|
"image/webp,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5",
|
||||||
imgcache.FormatWebP, nil},
|
imgcache.FormatWebP, nil},
|
||||||
{"WebP only", webpType, imgcache.FormatWebP, nil},
|
{"WebP only", "image/webp", imgcache.FormatWebP, nil},
|
||||||
{"neither", "image/png,image/*;q=0.8,*/*;q=0.5", imgcache.FormatJPEG, nil},
|
{"neither", "image/png,image/*;q=0.8,*/*;q=0.5", imgcache.FormatJPEG, nil},
|
||||||
{"wildcard only", "*/*", imgcache.FormatJPEG, nil},
|
{"wildcard only", "*/*", imgcache.FormatJPEG, nil},
|
||||||
{"image wildcard only", "image/*", imgcache.FormatJPEG, nil},
|
{"image wildcard only", "image/*", imgcache.FormatJPEG, nil},
|
||||||
@@ -74,10 +67,10 @@ func TestFormatForAccept(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// autoPhotoURLs returns a signed /v1/image/ URL and an encrypted /v1/e/ URL,
|
// autoPhotoURLs returns, by route, a signed /v1/image/ URL and an encrypted
|
||||||
// both valid for a minute, for the JPEG at photoPath on signedHost at 50x50 in
|
// /v1/e/ URL, both valid for a minute, for the JPEG at photoPath on signedHost
|
||||||
// the format auto, made with h's image service and generator.
|
// at 50x50 in the format auto, made with h's image service and generator.
|
||||||
func autoPhotoURLs(t *testing.T, h *Handlers) (string, string) {
|
func autoPhotoURLs(t *testing.T, h *Handlers) map[string]string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
signedURL, err := h.imgSvc.GenerateSignedURL("", &imgcache.ImageRequest{
|
signedURL, err := h.imgSvc.GenerateSignedURL("", &imgcache.ImageRequest{
|
||||||
@@ -102,26 +95,26 @@ func autoPhotoURLs(t *testing.T, h *Handlers) (string, string) {
|
|||||||
t.Fatalf("Generate() error = %v", err)
|
t.Fatalf("Generate() error = %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return signedURL, "/v1/e/" + token + "/img.jpg"
|
return map[string]string{
|
||||||
|
"/v1/image/": signedURL,
|
||||||
|
"/v1/e/": "/v1/e/" + token + "/img.jpg",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// requestImage sends method for target to srv with an Accept header line for
|
// requestImage sends method for target to srv with header and returns the
|
||||||
// each of accept, and returns the response.
|
// response.
|
||||||
func requestImage(
|
func requestImage(
|
||||||
t *testing.T, srv http.Handler, method, target string, accept ...string,
|
t *testing.T, srv http.Handler, method, target string, header http.Header,
|
||||||
) *httptest.ResponseRecorder {
|
) *httptest.ResponseRecorder {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
req := httptest.NewRequestWithContext(t.Context(), method, target, nil)
|
req := httptest.NewRequestWithContext(t.Context(), method, target, nil)
|
||||||
|
maps.Copy(req.Header, header)
|
||||||
for _, value := range accept {
|
|
||||||
req.Header.Add("Accept", value)
|
|
||||||
}
|
|
||||||
|
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
srv.ServeHTTP(rec, req)
|
srv.ServeHTTP(rec, req)
|
||||||
t.Logf("%s %s with Accept %q: %d, Content-Type %s, Vary %v, X-Pixa-Cache %s",
|
t.Logf("%s %s with %v: %d, Content-Type %s, Vary %v, X-Pixa-Cache %s",
|
||||||
method, target, accept, rec.Code, rec.Header().Get("Content-Type"),
|
method, target, header, rec.Code, rec.Header().Get("Content-Type"),
|
||||||
rec.Header().Values("Vary"), rec.Header().Get("X-Pixa-Cache"))
|
rec.Header().Values("Vary"), rec.Header().Get("X-Pixa-Cache"))
|
||||||
|
|
||||||
return rec
|
return rec
|
||||||
@@ -135,7 +128,6 @@ func TestFormatAuto_ChosenFromAccept(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
h, srv := newSignedHostServer(t, slog.New(slog.DiscardHandler))
|
h, srv := newSignedHostServer(t, slog.New(slog.DiscardHandler))
|
||||||
signedURL, encryptedURL := autoPhotoURLs(t, h)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -144,36 +136,43 @@ func TestFormatAuto_ChosenFromAccept(t *testing.T) {
|
|||||||
wantType string
|
wantType string
|
||||||
}{
|
}{
|
||||||
{"AVIF accepted", []string{"image/avif,image/webp,*/*;q=0.8"},
|
{"AVIF accepted", []string{"image/avif,image/webp,*/*;q=0.8"},
|
||||||
http.StatusOK, avifType},
|
http.StatusOK, "image/avif"},
|
||||||
{"WebP accepted", []string{"image/webp,*/*;q=0.8"}, http.StatusOK, webpType},
|
{"WebP accepted", []string{"image/webp,*/*;q=0.8"},
|
||||||
{"no Accept", nil, http.StatusOK, jpegType},
|
http.StatusOK, "image/webp"},
|
||||||
{"two Accept lines", []string{"image/png", webpType}, http.StatusOK, webpType},
|
{"no Accept", nil, http.StatusOK, "image/jpeg"},
|
||||||
{"none of the three", []string{"image/gif"},
|
{"two Accept lines", []string{"image/png", "image/webp"},
|
||||||
http.StatusNotAcceptable, jsonType},
|
http.StatusOK, "image/webp"},
|
||||||
{"not valid", []string{"image/avif;q=high"}, http.StatusBadRequest, jsonType},
|
{"none of the three", []string{"image/png"},
|
||||||
|
http.StatusNotAcceptable, "application/json"},
|
||||||
|
{"not valid", []string{"image/avif;q=high"},
|
||||||
|
http.StatusBadRequest, "application/json"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for route, target := range autoPhotoURLs(t, h) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(route+" "+tt.name, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
for _, target := range []string{signedURL, encryptedURL} {
|
rec := requestImage(t, srv, http.MethodGet, target,
|
||||||
rec := requestImage(t, srv, http.MethodGet, target, tt.accept...)
|
http.Header{"Accept": tt.accept})
|
||||||
gotType := rec.Header().Get("Content-Type")
|
|
||||||
|
|
||||||
if rec.Code != tt.wantStatus || gotType != tt.wantType {
|
if rec.Code != tt.wantStatus {
|
||||||
t.Errorf("%s: %d %s, want %d %s; body %s", target,
|
t.Errorf("status = %d, want %d; body %s",
|
||||||
rec.Code, gotType, tt.wantStatus, tt.wantType, rec.Body)
|
rec.Code, tt.wantStatus, rec.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got := rec.Header().Get("Content-Type"); got != tt.wantType {
|
||||||
|
t.Errorf("Content-Type = %q, want %q", got, tt.wantType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !slices.Contains(rec.Header().Values("Vary"), "Accept") {
|
if !slices.Contains(rec.Header().Values("Vary"), "Accept") {
|
||||||
t.Errorf("%s: Vary = %v, want Accept in it",
|
t.Errorf("Vary = %v, want Accept in it",
|
||||||
target, rec.Header().Values("Vary"))
|
rec.Header().Values("Vary"))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestFormatAuto_SignatureCoversAuto verifies that a /v1/image/ URL with the
|
// TestFormatAuto_SignatureCoversAuto verifies that a /v1/image/ URL with the
|
||||||
// format auto is checked against a signature for auto, not for the format
|
// format auto is checked against a signature for auto, not for the format
|
||||||
@@ -199,43 +198,53 @@ func TestFormatAuto_SignatureCoversAuto(t *testing.T) {
|
|||||||
t.Fatalf("no /50x50.avif? in %s", signedForAVIF)
|
t.Fatalf("no /50x50.avif? in %s", signedForAVIF)
|
||||||
}
|
}
|
||||||
|
|
||||||
rec := requestImage(t, srv, http.MethodGet, target, avifType)
|
rec := requestImage(t, srv, http.MethodGet, target,
|
||||||
|
http.Header{"Accept": {"image/avif"}})
|
||||||
|
|
||||||
if rec.Code != http.StatusUnauthorized {
|
if rec.Code != http.StatusUnauthorized {
|
||||||
t.Errorf("status = %d, want %d", rec.Code, http.StatusUnauthorized)
|
t.Errorf("status = %d, want %d", rec.Code, http.StatusUnauthorized)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestFormatAuto_CachesEachFormatApart requests an auto URL for AVIF, then
|
// TestFormatAuto_CachesEachFormatApart requests an auto URL on each image
|
||||||
// JPEG, then both again. Each format is processed once and then served from
|
// route for AVIF, then JPEG, then both again. Each format is processed once
|
||||||
// the cache, with an ETag of its own, so a client never gets the other format
|
// and then served from the cache, with an ETag of its own, so a client never
|
||||||
// from the cache.
|
// gets the other format from the cache.
|
||||||
func TestFormatAuto_CachesEachFormatApart(t *testing.T) {
|
func TestFormatAuto_CachesEachFormatApart(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
for _, route := range []string{"/v1/image/", "/v1/e/"} {
|
||||||
|
t.Run(route, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
h, srv := newSignedHostServer(t, slog.New(slog.DiscardHandler))
|
h, srv := newSignedHostServer(t, slog.New(slog.DiscardHandler))
|
||||||
signedURL, _ := autoPhotoURLs(t, h)
|
target := autoPhotoURLs(t, h)[route]
|
||||||
|
|
||||||
steps := []struct {
|
steps := []struct {
|
||||||
|
accept string
|
||||||
wantType string
|
wantType string
|
||||||
wantCache string
|
wantCache string
|
||||||
}{
|
}{
|
||||||
{avifType, "MISS"},
|
{"image/avif", "image/avif", "MISS"},
|
||||||
{jpegType, "MISS"},
|
{"image/jpeg", "image/jpeg", "MISS"},
|
||||||
{avifType, "HIT"},
|
{"image/avif", "image/avif", "HIT"},
|
||||||
{jpegType, "HIT"},
|
{"image/jpeg", "image/jpeg", "HIT"},
|
||||||
}
|
}
|
||||||
|
|
||||||
etags := make(map[string]string)
|
etags := make(map[string]string)
|
||||||
|
|
||||||
for _, step := range steps {
|
for _, step := range steps {
|
||||||
rec := requestImage(t, srv, http.MethodGet, signedURL, step.wantType)
|
rec := requestImage(t, srv, http.MethodGet, target,
|
||||||
|
http.Header{"Accept": {step.accept}})
|
||||||
|
|
||||||
gotType := rec.Header().Get("Content-Type")
|
gotType := rec.Header().Get("Content-Type")
|
||||||
gotCache := rec.Header().Get("X-Pixa-Cache")
|
gotCache := rec.Header().Get("X-Pixa-Cache")
|
||||||
|
|
||||||
if rec.Code != http.StatusOK || gotType != step.wantType ||
|
if rec.Code != http.StatusOK || gotType != step.wantType ||
|
||||||
gotCache != step.wantCache {
|
gotCache != step.wantCache {
|
||||||
t.Fatalf("Accept %s: %d %s %s, want 200 %s %s", step.wantType,
|
t.Fatalf("Accept %s: %d %s %s, want 200 %s %s",
|
||||||
rec.Code, gotType, gotCache, step.wantType, step.wantCache)
|
step.accept, rec.Code, gotType, gotCache,
|
||||||
|
step.wantType, step.wantCache)
|
||||||
}
|
}
|
||||||
|
|
||||||
etag := rec.Header().Get("ETag")
|
etag := rec.Header().Get("ETag")
|
||||||
@@ -246,8 +255,10 @@ func TestFormatAuto_CachesEachFormatApart(t *testing.T) {
|
|||||||
etags[gotType] = etag
|
etags[gotType] = etag
|
||||||
}
|
}
|
||||||
|
|
||||||
if etags[avifType] == etags[jpegType] {
|
if etags["image/avif"] == etags["image/jpeg"] {
|
||||||
t.Errorf("AVIF and JPEG have the same ETag %s", etags[avifType])
|
t.Errorf("AVIF and JPEG have the same ETag %s", etags["image/avif"])
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -265,28 +276,28 @@ func TestFormatAuto_Vary(t *testing.T) {
|
|||||||
srv.Get("/v1/e/{token}/*", h.HandleImageEnc())
|
srv.Get("/v1/e/{token}/*", h.HandleImageEnc())
|
||||||
srv.Head("/v1/e/{token}/*", h.HandleImageEnc())
|
srv.Head("/v1/e/{token}/*", h.HandleImageEnc())
|
||||||
|
|
||||||
signedURL, encryptedURL := autoPhotoURLs(t, h)
|
autoURLs := autoPhotoURLs(t, h)
|
||||||
|
fixedURLs := map[string]string{
|
||||||
|
"/v1/image/": signedPhotoURL(t, h),
|
||||||
|
"/v1/e/": encPhotoURL(t, h),
|
||||||
|
}
|
||||||
|
|
||||||
for _, urls := range [][2]string{
|
for route, target := range autoURLs {
|
||||||
{signedURL, signedPhotoURL(t, h)},
|
t.Run(route, func(t *testing.T) {
|
||||||
{encryptedURL, encPhotoURL(t, h)},
|
t.Parallel()
|
||||||
} {
|
|
||||||
autoURL, fixedURL := urls[0], urls[1]
|
|
||||||
|
|
||||||
head := requestImage(t, srv, http.MethodHead, autoURL, webpType)
|
header := http.Header{"Accept": {"image/webp"}}
|
||||||
|
|
||||||
|
head := requestImage(t, srv, http.MethodHead, target, header)
|
||||||
checkVaryAccept(t, head, http.StatusOK, true)
|
checkVaryAccept(t, head, http.StatusOK, true)
|
||||||
|
|
||||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet,
|
header.Set("If-None-Match", head.Header().Get("ETag"))
|
||||||
autoURL, nil)
|
notModified := requestImage(t, srv, http.MethodGet, target, header)
|
||||||
req.Header.Set("Accept", webpType)
|
|
||||||
req.Header.Set("If-None-Match", head.Header().Get("ETag"))
|
|
||||||
|
|
||||||
notModified := httptest.NewRecorder()
|
|
||||||
srv.ServeHTTP(notModified, req)
|
|
||||||
checkVaryAccept(t, notModified, http.StatusNotModified, true)
|
checkVaryAccept(t, notModified, http.StatusNotModified, true)
|
||||||
|
|
||||||
fixed := requestImage(t, srv, http.MethodGet, fixedURL)
|
fixed := requestImage(t, srv, http.MethodGet, fixedURLs[route], nil)
|
||||||
checkVaryAccept(t, fixed, http.StatusOK, false)
|
checkVaryAccept(t, fixed, http.StatusOK, false)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,13 +308,11 @@ func checkVaryAccept(
|
|||||||
) {
|
) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
vary := rec.Header().Values("Vary")
|
|
||||||
t.Logf("status %d, Vary %v", rec.Code, vary)
|
|
||||||
|
|
||||||
if rec.Code != wantStatus {
|
if rec.Code != wantStatus {
|
||||||
t.Errorf("status = %d, want %d", rec.Code, wantStatus)
|
t.Errorf("status = %d, want %d", rec.Code, wantStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vary := rec.Header().Values("Vary")
|
||||||
if slices.Contains(vary, "Accept") != wantVary {
|
if slices.Contains(vary, "Accept") != wantVary {
|
||||||
t.Errorf("Vary = %v, want Accept in it: %v", vary, wantVary)
|
t.Errorf("Vary = %v, want Accept in it: %v", vary, wantVary)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,11 +43,6 @@ func (s *Handlers) HandleImage() http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// The signature covers the format auto, not the format chosen
|
|
||||||
if !s.chooseAutoFormat(w, r, req) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get cache key for logging
|
// Get cache key for logging
|
||||||
cacheKey := imgcache.CacheKey(req)
|
cacheKey := imgcache.CacheKey(req)
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func (s *Handlers) HandleImageEnc() http.HandlerFunc {
|
|||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
req, ok := s.parseImageEncRequest(w, r)
|
req, ok := s.parseImageEncRequest(w, r)
|
||||||
if !ok || !s.chooseAutoFormat(w, r, req) {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,11 +22,6 @@ const (
|
|||||||
FormatWebP ImageFormat = "webp"
|
FormatWebP ImageFormat = "webp"
|
||||||
FormatAVIF ImageFormat = "avif"
|
FormatAVIF ImageFormat = "avif"
|
||||||
FormatGIF ImageFormat = "gif"
|
FormatGIF ImageFormat = "gif"
|
||||||
|
|
||||||
// FormatAuto stands for AVIF, WebP or JPEG, chosen for each request
|
|
||||||
// from its Accept header once the URL's signature or token has been
|
|
||||||
// checked; it is never processed or cached as itself.
|
|
||||||
FormatAuto ImageFormat = "auto"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Size represents requested image dimensions
|
// Size represents requested image dimensions
|
||||||
|
|||||||
@@ -277,8 +277,6 @@ func parseFormat(s string) (ImageFormat, error) {
|
|||||||
return FormatAVIF, nil
|
return FormatAVIF, nil
|
||||||
case "gif":
|
case "gif":
|
||||||
return FormatGIF, nil
|
return FormatGIF, nil
|
||||||
case "auto":
|
|
||||||
return FormatAuto, nil
|
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("%w: %s", ErrInvalidFormat, s)
|
return "", fmt.Errorf("%w: %s", ErrInvalidFormat, s)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,6 +95,14 @@ func TestParseImageURL(t *testing.T) {
|
|||||||
Size: Size{Width: 200, Height: 200}, Format: FormatGIF,
|
Size: Size{Width: 200, Height: 200}, Format: FormatGIF,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "auto format",
|
||||||
|
input: "/v1/image/example.com/photo.jpg/200x200.auto",
|
||||||
|
want: &ParsedURL{
|
||||||
|
Host: testHostExample, Path: "/photo.jpg",
|
||||||
|
Size: Size{Width: 200, Height: 200}, Format: FormatAuto,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@@ -111,21 +119,6 @@ func TestParseImageURL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestParseImageURL_AutoFormat verifies that the format auto in an image URL
|
|
||||||
// parses as FormatAuto.
|
|
||||||
func TestParseImageURL_AutoFormat(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
got, err := ParseImageURL("/v1/image/example.com/photo.jpg/200x200.auto")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("ParseImageURL() error = %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if got.Format != FormatAuto {
|
|
||||||
t.Errorf("Format = %q, want %q", got.Format, FormatAuto)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseImageURL_Errors(t *testing.T) {
|
func TestParseImageURL_Errors(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
@@ -95,7 +95,6 @@
|
|||||||
</label>
|
</label>
|
||||||
<select id="format" name="format">
|
<select id="format" name="format">
|
||||||
<option value="orig" {{if eq .FormFormat "orig"}}selected{{end}}>Original</option>
|
<option value="orig" {{if eq .FormFormat "orig"}}selected{{end}}>Original</option>
|
||||||
<option value="auto" {{if eq .FormFormat "auto"}}selected{{end}}>Auto (AVIF, WebP or JPEG)</option>
|
|
||||||
<option value="jpeg" {{if eq .FormFormat "jpeg"}}selected{{end}}>JPEG</option>
|
<option value="jpeg" {{if eq .FormFormat "jpeg"}}selected{{end}}>JPEG</option>
|
||||||
<option value="png" {{if eq .FormFormat "png"}}selected{{end}}>PNG</option>
|
<option value="png" {{if eq .FormFormat "png"}}selected{{end}}>PNG</option>
|
||||||
<option value="webp" {{if eq .FormFormat "webp"}}selected{{end}}>WebP</option>
|
<option value="webp" {{if eq .FormFormat "webp"}}selected{{end}}>WebP</option>
|
||||||
|
|||||||
Reference in new issue
Block a user