From f4f8d65a39ffec7b885e627813af6c063b0b2aae Mon Sep 17 00:00:00 2001 From: localhost Date: Sat, 14 Jun 2025 02:50:05 +0800 Subject: [PATCH] fix: event api content type only check type and subtype (#6317) * fix: event api content type only check type and subtype Signed-off-by: paomian * chore: make clippy happy Signed-off-by: paomian --------- Signed-off-by: paomian --- src/servers/src/http/event.rs | 16 ++++++++++------ src/servers/src/http/header.rs | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/servers/src/http/event.rs b/src/servers/src/http/event.rs index 1686b71362..2f838aee0c 100644 --- a/src/servers/src/http/event.rs +++ b/src/servers/src/http/event.rs @@ -33,6 +33,7 @@ use common_telemetry::{error, warn}; use datatypes::value::column_data_to_json; use headers::ContentType; use lazy_static::lazy_static; +use mime_guess::mime; use pipeline::util::to_pipeline_version; use pipeline::{ ContextReq, GreptimePipelineParams, PipelineContext, PipelineDefinition, Value as PipelineValue, @@ -47,7 +48,9 @@ use crate::error::{ status_code_to_http_status, Error, InvalidParameterSnafu, ParseJsonSnafu, PipelineSnafu, Result, }; use crate::http::header::constants::GREPTIME_PIPELINE_PARAMS_HEADER; -use crate::http::header::{CONTENT_TYPE_NDJSON_STR, CONTENT_TYPE_PROTOBUF_STR}; +use crate::http::header::{ + CONTENT_TYPE_NDJSON_STR, CONTENT_TYPE_NDJSON_SUBTYPE_STR, CONTENT_TYPE_PROTOBUF_STR, +}; use crate::http::result::greptime_manage_resp::GreptimedbManageResponse; use crate::http::result::greptime_result_v1::GreptimedbV1Response; use crate::http::HttpResponse; @@ -665,12 +668,13 @@ impl TryFrom<&ContentType> for EventPayloadResolverInner { type Error = Error; fn try_from(content_type: &ContentType) -> Result { - match content_type { - x if *x == *JSON_CONTENT_TYPE => Ok(EventPayloadResolverInner::Json), - x if *x == *NDJSON_CONTENT_TYPE => Ok(EventPayloadResolverInner::Ndjson), - x if *x == *TEXT_CONTENT_TYPE || *x == *TEXT_UTF8_CONTENT_TYPE => { - Ok(EventPayloadResolverInner::Text) + let mime: mime_guess::Mime = content_type.clone().into(); + match (mime.type_(), mime.subtype()) { + (mime::APPLICATION, mime::JSON) => Ok(EventPayloadResolverInner::Json), + (mime::APPLICATION, subtype) if subtype == CONTENT_TYPE_NDJSON_SUBTYPE_STR => { + Ok(EventPayloadResolverInner::Ndjson) } + (mime::TEXT, mime::PLAIN) => Ok(EventPayloadResolverInner::Text), _ => InvalidParameterSnafu { reason: format!( "invalid content type: {}, expected: one of {}", diff --git a/src/servers/src/http/header.rs b/src/servers/src/http/header.rs index 0170346e79..ccfdb7c0e8 100644 --- a/src/servers/src/http/header.rs +++ b/src/servers/src/http/header.rs @@ -87,6 +87,7 @@ pub static CONTENT_TYPE_PROTOBUF: HeaderValue = HeaderValue::from_static(CONTENT pub static CONTENT_ENCODING_SNAPPY: HeaderValue = HeaderValue::from_static("snappy"); pub static CONTENT_TYPE_NDJSON_STR: &str = "application/x-ndjson"; +pub static CONTENT_TYPE_NDJSON_SUBTYPE_STR: &str = "x-ndjson"; pub struct GreptimeDbName(Option);