fix: redis, /get & /post parsing #15

Merged
nm17 merged 4 commits from dev/dc09 into master 2024-06-03 16:23:02 +03:00
2 changed files with 12 additions and 1 deletions
Showing only changes of commit 803acce7e9 - Show all commits

View file

@ -44,6 +44,9 @@ pub enum AppError {
min: Option<Decimal>,
},
#[display(fmt = "UTF8")]
Utf8Error(std::str::Utf8Error),
#[display(fmt = "IDK")]
UnknownBody {
json_err: Option<serde_json::Error>,
@ -63,6 +66,7 @@ impl web::error::WebResponseError for AppError {
AppError::RequestTooLarge => StatusCode::PAYLOAD_TOO_LARGE,
AppError::ServerRedisError(_) => StatusCode::INTERNAL_SERVER_ERROR,
AppError::ApiKeyInvalid { .. } => StatusCode::BAD_REQUEST,
AppError::Utf8Error(_) => StatusCode::BAD_REQUEST,
AppError::UnknownBody { .. } => StatusCode::BAD_REQUEST,
AppError::QSError(..) => StatusCode::BAD_REQUEST,
AppError::DeviceNotFound(..) => StatusCode::BAD_REQUEST,
@ -77,6 +81,7 @@ impl web::error::WebResponseError for AppError {
AppError::RequestTooLarge => "Request is too large",
AppError::ServerRedisError(_) => "Internal server error",
AppError::ApiKeyInvalid { .. } => "API Key invalid",
AppError::Utf8Error(_) => "Invalid UTF8 sequence",
AppError::UnknownBody { .. } => {
"Can't figure out where and in what encoding the main data is"
}

View file

@ -50,7 +50,13 @@ pub async fn device_handler<'a>(
Err(error) => json_error = Some(error),
},
"application/x-www-form-urlencoded" => {
match qs_parser::parse_nm_qs_format(&body.as_ref().to_str_lossy()).await {
let body = match std::str::from_utf8(body.as_ref()) {
DarkCat09 marked this conversation as resolved Outdated
Outdated
Review

Может быть всё-таки std::str::from_utf8 используешь? Я не хочу работать с не utf-8 данными, так что заменять невалидные вещи на � не лучшее решение.

Может быть всё-таки `std::str::from_utf8` используешь? Я не хочу работать с не utf-8 данными, так что заменять невалидные вещи на � не лучшее решение.
Ok(body) => body,
Err(error) => {
return Err(AppError::Utf8Error(error));
}
};
match qs_parser::parse_nm_qs_format(body).await {
Ok(qs_body) => {
real_body = Some(NMJsonPacket {
devices: Vec::from([qs_body]),