fix: /get and /post urlencoded string parsing

This commit is contained in:
DarkCat09 2024-06-03 11:26:59 +04:00
parent f8d788f8bb
commit 1c0437b820
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3
3 changed files with 10 additions and 6 deletions

View file

@ -13,6 +13,8 @@ use crate::web_server::old_device_sensor_api::qs_parser::QSParserError;
use rust_decimal::Decimal; use rust_decimal::Decimal;
use serde_json::json; use serde_json::json;
use super::old_device_sensor_api::qs_parser;
/// Главный объект ошибки [std::error::Error] для всего Web API. /// Главный объект ошибки [std::error::Error] для всего Web API.
/// ///
/// В целом, все Result у Web сервера должны использовать этот Error. /// В целом, все Result у Web сервера должны использовать этот Error.
@ -45,7 +47,7 @@ pub enum AppError {
#[display(fmt = "IDK")] #[display(fmt = "IDK")]
UnknownBody { UnknownBody {
json_err: Option<serde_json::Error>, json_err: Option<serde_json::Error>,
query_error: Option<serde_qs::Error>, query_error: Option<qs_parser::QSParserError>,
}, },
#[display(fmt = "IDK")] #[display(fmt = "IDK")]

View file

@ -2,9 +2,10 @@
pub mod qs_parser; pub mod qs_parser;
use crate::ingest_protocol::{NMDeviceDataPacket, NMJsonPacket}; use crate::ingest_protocol::NMJsonPacket;
use crate::web_server::app_error::AppError; use crate::web_server::app_error::AppError;
use bstr::ByteSlice;
use ntex::http::{HttpMessage, StatusCode}; use ntex::http::{HttpMessage, StatusCode};
use ntex::util::Bytes; use ntex::util::Bytes;
use ntex::web::types::State; use ntex::web::types::State;
@ -12,7 +13,6 @@ use ntex::{http, web};
use qs_parser::QSParserError; use qs_parser::QSParserError;
use thiserror::Error; use thiserror::Error;
use super::NMAppState; use super::NMAppState;
#[derive(Error, Debug)] #[derive(Error, Debug)]
@ -50,7 +50,7 @@ pub async fn device_handler<'a>(
Err(error) => json_error = Some(error), Err(error) => json_error = Some(error),
}, },
"application/x-www-form-urlencoded" => { "application/x-www-form-urlencoded" => {
match serde_qs::from_bytes::<NMDeviceDataPacket>(body.as_ref()) { match qs_parser::parse_nm_qs_format(&body.as_ref().to_str_lossy()).await {
Ok(qs_body) => { Ok(qs_body) => {
real_body = Some(NMJsonPacket { real_body = Some(NMJsonPacket {
devices: Vec::from([qs_body]), devices: Vec::from([qs_body]),
@ -62,7 +62,7 @@ pub async fn device_handler<'a>(
_ => {} _ => {}
} }
} else if request.method() == http::Method::GET { } else if request.method() == http::Method::GET {
match serde_qs::from_str::<NMDeviceDataPacket>(request.query_string()) { match qs_parser::parse_nm_qs_format(request.query_string()).await {
Ok(qs_body) => { Ok(qs_body) => {
real_body = Some(NMJsonPacket { real_body = Some(NMJsonPacket {
devices: Vec::from([qs_body]), devices: Vec::from([qs_body]),

View file

@ -45,7 +45,7 @@ impl From<serde_qs::Error> for QSParserError {
/// [SensorValue]. /// [SensorValue].
/// ///
/// Формат: `<SENSOR_MAC>=<SENSOR_VALUE>`. /// Формат: `<SENSOR_MAC>=<SENSOR_VALUE>`.
/// Других данных на подобии названия и времени нет. /// Других данных наподобие названия и времени нет.
pub fn qs_rest_to_values( pub fn qs_rest_to_values(
parsed: HashMap<String, String>, parsed: HashMap<String, String>,
) -> Result<HashSet<SensorValue>, QSParserError> { ) -> Result<HashSet<SensorValue>, QSParserError> {
@ -96,6 +96,8 @@ pub async fn parse_nm_qs_format(input: &str) -> Result<NMDeviceDataPacket, QSPar
return Err(QSParserError::NoMAC); return Err(QSParserError::NoMAC);
}; };
parsed.remove("ID");
let device_data = NMDeviceDataPacket { let device_data = NMDeviceDataPacket {
mac: device_mac, mac: device_mac,
name: parsed.remove("name").map(|v| v.to_owned()), name: parsed.remove("name").map(|v| v.to_owned()),