avatar

Local Gravatar-proxy handler backed by crate::store::avatar_cache.

Browsers fetch avatars from /avatars/{hash} instead of gravatar.com directly. The handler consults the shared AvatarCache on the AppState which reads a fresh on-disk copy when available and only reaches out to Gravatar on a cache miss.

const CACHE_CONTROL_MAX_AGE

source

const CACHE_CONTROL_MAX_AGE: &str = "public, max-age=604800";

Browser cache lifetime advertised for successful avatar responses.

Seven days matches the server-side crate::store::avatar_cache::DEFAULT_TTL and keeps first-party caching consistent with the upstream behaviour the proxy is replacing.

const MAX_HASH_LEN

source

const MAX_HASH_LEN: usize = 64;

Maximum length of an avatar hash accepted by the handler.

Real Gravatar identifiers are either MD5 (32 chars) or SHA-256 (64 chars); anything longer is almost certainly an attempt to abuse the route.

fn avatar_get

source

pub async fn avatar_get(
    State(state): State<AppState>,
    Path(hash): Path<String>,
) -> Result<Response, web::Error>

GET /avatars/:hash — serve a cached avatar, fetching it on a miss.

The handler delegates to AvatarCache::get_or_fetch which validates the hash (rejecting anything outside [0-9a-fA-F]+) before hitting the filesystem or upstream Gravatar. The response body is the raw image bytes with the Content-Type reported by either the upstream fetch or the fallback used on cache hits, and a long Cache-Control header so browsers avoid re-requesting the same avatar on every page load.

Parameter Type Description
State(state) State<AppState> the shared app state that holds the avatar cache
Path(hash) Path<String> the Gravatar hash from the path: 1 to 64 hex digits, in any case
returns Result<Response, web::Error> The image bytes with 200, Content-Type and Cache-Control; a 400 error for a malformed hash, or the cache’s error when the fetch fails.