From f8d788f8bbf821aba788881b4058e11fff075244 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Mon, 3 Jun 2024 11:23:53 +0400 Subject: [PATCH 1/4] fix: update fred to v9.0.3 --- Cargo.lock | 51 ++++++++++++++++++++++++------------------- Cargo.toml | 2 +- src/web_server/mod.rs | 10 ++------- 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index edf33ac..f5f5973 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -113,12 +113,6 @@ version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" -[[package]] -name = "arcstr" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03918c3dbd7701a85c6b9887732e2921175f26c350b4563841d0958c21d57e6d" - [[package]] name = "arrayvec" version = "0.7.4" @@ -127,9 +121,9 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" [[package]] name = "async-broadcast" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "258b52a1aa741b9f09783b2d86cf0aeeb617bbf847f6933340a39644227acbdb" +checksum = "20cd0e2e25ea8e5f7e9df04578dc6cf5c83577fd09b1a46aaf5c85e1c33f2a7e" dependencies = [ "event-listener", "event-listener-strategy", @@ -434,12 +428,9 @@ dependencies = [ [[package]] name = "cookie-factory" -version = "0.3.3" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9885fa71e26b8ab7855e2ec7cae6e9b380edff76cd052e07c683a0319d51b3a2" -dependencies = [ - "futures 0.3.30", -] +checksum = "396de984970346b0d9e93d1415082923c679e5ae5c3ee3dcbd104f5610af126b" [[package]] name = "core-foundation-sys" @@ -468,6 +459,15 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7059fff8937831a9ae6f0fe4d658ffabf58f2ca96aa9dec1c889f936f705f216" +[[package]] +name = "crossbeam-queue" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.20" @@ -646,30 +646,29 @@ dependencies = [ [[package]] name = "fred" -version = "6.3.2" +version = "9.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a15cc18b56395b8b15ffcdcea7fe8586e3a3ccb3d9dc3b9408800d9814efb08e" +checksum = "915e065b377f6e16d5c01eae96bf31eeaf81e1e300b76f938761b3c21307cad8" dependencies = [ "arc-swap", - "arcstr", "async-trait", "bytes 1.6.0", "bytes-utils", - "cfg-if", + "crossbeam-queue", "float-cmp", "futures 0.3.30", - "lazy_static", "log", "nom", "parking_lot", "rand", "redis-protocol", "semver", - "sha-1", + "socket2", "tokio", "tokio-stream", "tokio-util", "url", + "urlencoding", ] [[package]] @@ -1675,9 +1674,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.84" +version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec96c6a92621310b51366f1e28d05ef11489516e93be030060e5fc12024a49d6" +checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" dependencies = [ "unicode-ident", ] @@ -1749,9 +1748,9 @@ dependencies = [ [[package]] name = "redis-protocol" -version = "4.1.0" +version = "5.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c31deddf734dc0a39d3112e73490e88b61a05e83e074d211f348404cee4d2c6" +checksum = "65deb7c9501fbb2b6f812a30d59c0253779480853545153a51d8e9e444ddc99f" dependencies = [ "bytes 1.6.0", "bytes-utils", @@ -2441,6 +2440,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + [[package]] name = "utf8parse" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 12fcd1c..340acea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ chrono = { version = "0.4.26", features = ["serde"] } clap = { version = "4.3.8", features = ["derive", "env"] } derive_more = "0.99.17" dotenvy = "0.15.7" -fred = { version = "6.3.0", features = ["nom"] } +fred = { version = "9.0.3", features = ["nom"] } heapless = { version = "0.7.16", features = ["ufmt-impl"] } hex = { version = "0.4.3", default-features = false, features = ["std", "alloc"] } hifitime = "3.8.2" diff --git a/src/web_server/mod.rs b/src/web_server/mod.rs index e8c4961..8f10a48 100644 --- a/src/web_server/mod.rs +++ b/src/web_server/mod.rs @@ -24,14 +24,8 @@ use crate::web_server::old_device_sensor_api::device_handler; use ntex::web; pub async fn server_main() { - let config = RedisConfig::default(); - let perf = PerformanceConfig::default(); - let policy = ReconnectPolicy::default(); - let client = RedisClient::new(config, Some(perf), Some(policy)); - - // connect to the server, returning a handle to the task that drives the connection - client.connect().await.unwrap().unwrap(); - client.wait_for_connect().await.unwrap(); + let client = RedisClient::default(); + client.init().await.unwrap(); let asd: Str = client.ping().await.unwrap(); From 1c0437b820a7eb4c3ddfb20c41e2b1f6f695d1e4 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Mon, 3 Jun 2024 11:26:59 +0400 Subject: [PATCH 2/4] 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 Date: Mon, 3 Jun 2024 16:12:33 +0400 Subject: [PATCH 3/4] style: error on invalid utf8 seq --- src/web_server/app_error.rs | 5 +++++ src/web_server/old_device_sensor_api/mod.rs | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/web_server/app_error.rs b/src/web_server/app_error.rs index ae02817..9285dcc 100644 --- a/src/web_server/app_error.rs +++ b/src/web_server/app_error.rs @@ -44,6 +44,9 @@ pub enum AppError { min: Option, }, + #[display(fmt = "UTF8")] + Utf8Error(std::str::Utf8Error), + #[display(fmt = "IDK")] UnknownBody { json_err: Option, @@ -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" } diff --git a/src/web_server/old_device_sensor_api/mod.rs b/src/web_server/old_device_sensor_api/mod.rs index ba258e7..33be26f 100644 --- a/src/web_server/old_device_sensor_api/mod.rs +++ b/src/web_server/old_device_sensor_api/mod.rs @@ -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()) { + 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]), From 19a4e0c2998ba01170ba601a3b83cf4b3107b4a4 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Mon, 3 Jun 2024 16:36:43 +0400 Subject: [PATCH 4/4] style: cleanup UTF8 error throwing code --- src/web_server/app_error.rs | 2 +- src/web_server/old_device_sensor_api/mod.rs | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/web_server/app_error.rs b/src/web_server/app_error.rs index 9285dcc..32ab50d 100644 --- a/src/web_server/app_error.rs +++ b/src/web_server/app_error.rs @@ -45,7 +45,7 @@ pub enum AppError { }, #[display(fmt = "UTF8")] - Utf8Error(std::str::Utf8Error), + Utf8Error(#[from] std::str::Utf8Error), #[display(fmt = "IDK")] UnknownBody { diff --git a/src/web_server/old_device_sensor_api/mod.rs b/src/web_server/old_device_sensor_api/mod.rs index 33be26f..bfc31ae 100644 --- a/src/web_server/old_device_sensor_api/mod.rs +++ b/src/web_server/old_device_sensor_api/mod.rs @@ -5,7 +5,6 @@ pub mod qs_parser; 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; @@ -50,12 +49,7 @@ pub async fn device_handler<'a>( Err(error) => json_error = Some(error), }, "application/x-www-form-urlencoded" => { - let body = match std::str::from_utf8(body.as_ref()) { - Ok(body) => body, - Err(error) => { - return Err(AppError::Utf8Error(error)); - } - }; + let body = std::str::from_utf8(body.as_ref())?; match qs_parser::parse_nm_qs_format(body).await { Ok(qs_body) => { real_body = Some(NMJsonPacket {