From a478b4eb46c50ae4d8ae7fa58116b652df8d24a2 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 19 Nov 2026 12:46:05 +0100 Subject: [PATCH] common: accept application/json with parameters Since 4c21931, a JSON body is accepted only if its Content-Type is exactly "application/json", so clients that send "application/json; charset=utf-8" (OkHttp appends the charset) get 415. Compare the media type case-insensitively and ignore its parameters (RFC 9110, section 8.3.1; RFC 8259, section 11). Assisted by LLM --- common/taler-api/src/extract.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/common/taler-api/src/extract.rs b/common/taler-api/src/extract.rs index 00f5f8d..2fc9165 100644 --- a/common/taler-api/src/extract.rs +++ b/common/taler-api/src/extract.rs @@ -18,7 +18,7 @@ use axum::{ body::{Body, Bytes, HttpBody}, extract::{FromRequest, FromRequestParts, Request}, http::{ - HeaderMap, StatusCode, + HeaderMap, HeaderValue, StatusCode, header::{self, CONTENT_TYPE}, request::Parts, }, @@ -43,6 +43,17 @@ fn unsupported_media_type(message: impl std::fmt::Display) -> ApiError { ) } +/// Whether a Content-Type names the media type `application/json`. +/// +/// The type and subtype are compared case-insensitively, and parameters are +/// ignored (RFC 9110, section 8.3.1): JSON defines none, and a charset +/// "really has no effect on compliant recipients" (RFC 8259, section 11). +/// Clients built on OkHttp, for one, send `application/json; charset=utf-8`. +fn is_json(content_type: &HeaderValue) -> bool { + let media_type = content_type.as_bytes().split(|b| *b == b';').next(); + media_type.is_some_and(|t| t.trim_ascii().eq_ignore_ascii_case(b"application/json")) +} + pub async fn decompressed_strict_body( headers: &HeaderMap, body: Body, @@ -55,7 +66,7 @@ pub async fn decompressed_strict_body( }; match headers.get(header::CONTENT_TYPE) { - Some(value) if value.as_bytes() == b"application/json" => {} + Some(value) if is_json(value) => {} Some(_) => return Err(unsupported_media_type("Bad Content-Type header")), None => return Err(unsupported_media_type("Missing Content-Type header")), } -- 2.47.3