From 1c0437b820a7eb4c3ddfb20c41e2b1f6f695d1e4 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Mon, 3 Jun 2024 11:26:59 +0400 Subject: [PATCH] fix: /get and /post urlencoded string parsing --- src/web_server/app_error.rs | 4 +++- src/web_server/old_device_sensor_api/mod.rs | 8 ++++---- src/web_server/old_device_sensor_api/qs_parser.rs | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/web_server/app_error.rs b/src/web_server/app_error.rs index ebe4a28..ae02817 100644 --- a/src/web_server/app_error.rs +++ b/src/web_server/app_error.rs @@ -13,6 +13,8 @@ use crate::web_server::old_device_sensor_api::qs_parser::QSParserError; use rust_decimal::Decimal; use serde_json::json; +use super::old_device_sensor_api::qs_parser; + /// Главный объект ошибки [std::error::Error] для всего Web API. /// /// В целом, все Result у Web сервера должны использовать этот Error. @@ -45,7 +47,7 @@ pub enum AppError { #[display(fmt = "IDK")] UnknownBody { json_err: Option, - query_error: Option, + query_error: Option, }, #[display(fmt = "IDK")] diff --git a/src/web_server/old_device_sensor_api/mod.rs b/src/web_server/old_device_sensor_api/mod.rs index ab8f1e9..ba258e7 100644 --- a/src/web_server/old_device_sensor_api/mod.rs +++ b/src/web_server/old_device_sensor_api/mod.rs @@ -2,9 +2,10 @@ pub mod qs_parser; -use crate::ingest_protocol::{NMDeviceDataPacket, NMJsonPacket}; +use crate::ingest_protocol::NMJsonPacket; use crate::web_server::app_error::AppError; +use bstr::ByteSlice; use ntex::http::{HttpMessage, StatusCode}; use ntex::util::Bytes; use ntex::web::types::State; @@ -12,7 +13,6 @@ use ntex::{http, web}; use qs_parser::QSParserError; use thiserror::Error; - use super::NMAppState; #[derive(Error, Debug)] @@ -50,7 +50,7 @@ pub async fn device_handler<'a>( Err(error) => json_error = Some(error), }, "application/x-www-form-urlencoded" => { - match serde_qs::from_bytes::(body.as_ref()) { + match qs_parser::parse_nm_qs_format(&body.as_ref().to_str_lossy()).await { Ok(qs_body) => { real_body = Some(NMJsonPacket { devices: Vec::from([qs_body]), @@ -62,7 +62,7 @@ pub async fn device_handler<'a>( _ => {} } } else if request.method() == http::Method::GET { - match serde_qs::from_str::(request.query_string()) { + match qs_parser::parse_nm_qs_format(request.query_string()).await { Ok(qs_body) => { real_body = Some(NMJsonPacket { devices: Vec::from([qs_body]), diff --git a/src/web_server/old_device_sensor_api/qs_parser.rs b/src/web_server/old_device_sensor_api/qs_parser.rs index 67208b9..07fd62c 100644 --- a/src/web_server/old_device_sensor_api/qs_parser.rs +++ b/src/web_server/old_device_sensor_api/qs_parser.rs @@ -45,7 +45,7 @@ impl From for QSParserError { /// [SensorValue]. /// /// Формат: `=`. -/// Других данных на подобии названия и времени нет. +/// Других данных наподобие названия и времени нет. pub fn qs_rest_to_values( parsed: HashMap, ) -> Result, QSParserError> { @@ -96,6 +96,8 @@ pub async fn parse_nm_qs_format(input: &str) -> Result