From 4eb3817b03b0d2495bed6153f7a637ba1086466d Mon Sep 17 00:00:00 2001 From: mpostma Date: Thu, 30 Sep 2021 11:29:27 +0200 Subject: [PATCH] missing payload error --- meilisearch-error/src/lib.rs | 10 +++++-- meilisearch-http/src/error.rs | 4 +-- .../src/routes/indexes/documents.rs | 26 ++++++++++++------- meilisearch-lib/src/index_controller/mod.rs | 11 ++++++++ .../src/index_controller/updates/error.rs | 5 +++- .../src/index_controller/updates/mod.rs | 10 +++++-- 6 files changed, 49 insertions(+), 17 deletions(-) diff --git a/meilisearch-error/src/lib.rs b/meilisearch-error/src/lib.rs index 4efba3cd9..41a58b9f5 100644 --- a/meilisearch-error/src/lib.rs +++ b/meilisearch-error/src/lib.rs @@ -87,6 +87,7 @@ pub enum Code { InvalidContentType, MissingContentType, MalformedPayload, + MissingPayload, } impl Code { @@ -158,9 +159,14 @@ impl Code { DumpProcessFailed => { ErrCode::internal("dump_process_failed", StatusCode::INTERNAL_SERVER_ERROR) } - MissingContentType => ErrCode::invalid("missing_content_type", StatusCode::UNSUPPORTED_MEDIA_TYPE), + MissingContentType => { + ErrCode::invalid("missing_content_type", StatusCode::UNSUPPORTED_MEDIA_TYPE) + } MalformedPayload => ErrCode::invalid("malformed_payload", StatusCode::BAD_REQUEST), - InvalidContentType => ErrCode::invalid("invalid_content_type", StatusCode::UNSUPPORTED_MEDIA_TYPE), + InvalidContentType => { + ErrCode::invalid("invalid_content_type", StatusCode::UNSUPPORTED_MEDIA_TYPE) + } + MissingPayload => ErrCode::invalid("missing_payload", StatusCode::BAD_REQUEST), } } diff --git a/meilisearch-http/src/error.rs b/meilisearch-http/src/error.rs index fb9847b87..cbe963615 100644 --- a/meilisearch-http/src/error.rs +++ b/meilisearch-http/src/error.rs @@ -11,9 +11,9 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, thiserror::Error)] pub enum MeilisearchHttpError { - #[error("A Content-Type header is missing. Accepted values for the Content-Type header are: \"application/json\", \"application/x-ndjson\", \"test/csv\"")] + #[error("A Content-Type header is missing. Accepted values for the Content-Type header are: \"application/json\", \"application/x-ndjson\", \"text/csv\"")] MissingContentType, - #[error("The Content-Type \"{0}\" is invalid. Accepted values for the Content-Type header are: \"application/json\", \"application/x-ndjson\", \"test/csv\"")] + #[error("The Content-Type \"{0}\" is invalid. Accepted values for the Content-Type header are: \"application/json\", \"application/x-ndjson\", \"text/csv\"")] InvalidContentType(String), } diff --git a/meilisearch-http/src/routes/indexes/documents.rs b/meilisearch-http/src/routes/indexes/documents.rs index 63318c676..50ba31c97 100644 --- a/meilisearch-http/src/routes/indexes/documents.rs +++ b/meilisearch-http/src/routes/indexes/documents.rs @@ -133,13 +133,16 @@ pub async fn add_documents( ) -> Result { debug!("called with params: {:?}", params); document_addition( - req.headers().get("Content-type").map(|s| s.to_str().unwrap_or("unkown")), + req.headers() + .get("Content-type") + .map(|s| s.to_str().unwrap_or("unkown")), meilisearch, path.into_inner().index_uid, params.into_inner().primary_key, body, - IndexDocumentsMethod::ReplaceDocuments) - .await + IndexDocumentsMethod::ReplaceDocuments, + ) + .await } pub async fn update_documents( @@ -151,13 +154,16 @@ pub async fn update_documents( ) -> Result { debug!("called with params: {:?}", params); document_addition( - req.headers().get("Content-type").map(|s| s.to_str().unwrap_or("unkown")), + req.headers() + .get("Content-type") + .map(|s| s.to_str().unwrap_or("unkown")), meilisearch, path.into_inner().index_uid, params.into_inner().primary_key, body, - IndexDocumentsMethod::UpdateDocuments) - .await + IndexDocumentsMethod::UpdateDocuments, + ) + .await } /// Route used when the payload type is "application/json" @@ -174,7 +180,9 @@ async fn document_addition( Some("application/json") => DocumentAdditionFormat::Json, Some("application/x-ndjson") => DocumentAdditionFormat::Ndjson, Some("text/csv") => DocumentAdditionFormat::Csv, - Some(other) => return Err(MeilisearchHttpError::InvalidContentType(other.to_string()).into()), + Some(other) => { + return Err(MeilisearchHttpError::InvalidContentType(other.to_string()).into()) + } None => return Err(MeilisearchHttpError::MissingContentType.into()), }; @@ -185,9 +193,7 @@ async fn document_addition( format, }; - let update_status = meilisearch - .register_update(index_uid, update, true) - .await?; + let update_status = meilisearch.register_update(index_uid, update, true).await?; debug!("returns: {:?}", update_status); Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() }))) diff --git a/meilisearch-lib/src/index_controller/mod.rs b/meilisearch-lib/src/index_controller/mod.rs index 050767c0f..f6fcda46c 100644 --- a/meilisearch-lib/src/index_controller/mod.rs +++ b/meilisearch-lib/src/index_controller/mod.rs @@ -1,4 +1,5 @@ use std::collections::BTreeMap; +use std::fmt; use std::path::{Path, PathBuf}; use std::sync::Arc; use std::time::Duration; @@ -75,6 +76,16 @@ pub enum DocumentAdditionFormat { Ndjson, } +impl fmt::Display for DocumentAdditionFormat { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + DocumentAdditionFormat::Json => write!(f, "json"), + DocumentAdditionFormat::Ndjson => write!(f, "ndjson"), + DocumentAdditionFormat::Csv => write!(f, "csv"), + } + } +} + #[derive(Serialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Stats { diff --git a/meilisearch-lib/src/index_controller/updates/error.rs b/meilisearch-lib/src/index_controller/updates/error.rs index 31ebe84be..eb539963e 100644 --- a/meilisearch-lib/src/index_controller/updates/error.rs +++ b/meilisearch-lib/src/index_controller/updates/error.rs @@ -5,7 +5,7 @@ use meilisearch_error::{Code, ErrorCode}; use crate::{ document_formats::DocumentFormatError, - index_controller::update_file_store::UpdateFileStoreError, + index_controller::{update_file_store::UpdateFileStoreError, DocumentAdditionFormat}, }; pub type Result = std::result::Result; @@ -26,6 +26,8 @@ pub enum UpdateLoopError { // TODO: The reference to actix has to go. #[error("{0}")] PayloadError(#[from] actix_web::error::PayloadError), + #[error("A {0} payload is missing.")] + MissingPayload(DocumentAdditionFormat), } impl From> for UpdateLoopError @@ -63,6 +65,7 @@ impl ErrorCode for UpdateLoopError { actix_web::error::PayloadError::Overflow => Code::PayloadTooLarge, _ => Code::Internal, }, + Self::MissingPayload(_) => Code::MissingPayload, } } } diff --git a/meilisearch-lib/src/index_controller/updates/mod.rs b/meilisearch-lib/src/index_controller/updates/mod.rs index c3b15e5af..037cf96b0 100644 --- a/meilisearch-lib/src/index_controller/updates/mod.rs +++ b/meilisearch-lib/src/index_controller/updates/mod.rs @@ -3,7 +3,7 @@ mod message; pub mod status; pub mod store; -use std::io; +use std::io::{self, BufRead, BufReader}; use std::path::{Path, PathBuf}; use std::sync::atomic::AtomicBool; use std::sync::Arc; @@ -191,9 +191,15 @@ impl UpdateLoop { method, format, } => { - let reader = StreamReader::new(payload); + let mut reader = BufReader::new(StreamReader::new(payload)); let (content_uuid, mut update_file) = self.update_file_store.new_update()?; tokio::task::spawn_blocking(move || -> Result<_> { + // check if the payload is empty, and return an error + reader.fill_buf()?; + if reader.buffer().is_empty() { + return Err(UpdateLoopError::MissingPayload(format)); + } + match format { DocumentAdditionFormat::Json => read_json(reader, &mut *update_file)?, DocumentAdditionFormat::Csv => read_csv(reader, &mut *update_file)?,