refactor: 签名时按需使用 String

This commit is contained in:
amtoaer
2025-01-21 22:54:20 +08:00
parent 1a32e38dc3
commit ab84a8dad1
4 changed files with 66 additions and 17 deletions
Generated
+14
View File
@@ -136,6 +136,12 @@ version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
[[package]]
name = "assert_matches"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9"
[[package]] [[package]]
name = "async-attributes" name = "async-attributes"
version = "1.1.2" version = "1.1.2"
@@ -377,6 +383,7 @@ version = "2.2.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"arc-swap", "arc-swap",
"assert_matches",
"async-stream", "async-stream",
"async-trait", "async-trait",
"bili_sync_entity", "bili_sync_entity",
@@ -384,6 +391,7 @@ dependencies = [
"chrono", "chrono",
"clap", "clap",
"cookie", "cookie",
"cow-utils",
"dirs", "dirs",
"float-ord", "float-ord",
"futures", "futures",
@@ -674,6 +682,12 @@ version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "cow-utils"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "417bef24afe1460300965a25ff4a24b8b45ad011948302ec221e8a0a81eb2c79"
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.12" version = "0.2.12"
+2
View File
@@ -17,12 +17,14 @@ bili_sync_migration = { path = "crates/bili_sync_migration" }
anyhow = { version = "1.0.95", features = ["backtrace"] } anyhow = { version = "1.0.95", features = ["backtrace"] }
arc-swap = { version = "1.7.1", features = ["serde"] } arc-swap = { version = "1.7.1", features = ["serde"] }
assert_matches = "1.5"
async-std = { version = "1.13.0", features = ["attributes", "tokio1"] } async-std = { version = "1.13.0", features = ["attributes", "tokio1"] }
async-stream = "0.3.6" async-stream = "0.3.6"
async-trait = "0.1.85" async-trait = "0.1.85"
chrono = { version = "0.4.39", features = ["serde"] } chrono = { version = "0.4.39", features = ["serde"] }
clap = { version = "4.5.26", features = ["env"] } clap = { version = "4.5.26", features = ["env"] }
cookie = "0.18.1" cookie = "0.18.1"
cow-utils = "0.1.3"
dirs = "6.0.0" dirs = "6.0.0"
float-ord = "0.3.2" float-ord = "0.3.2"
futures = "0.3.31" futures = "0.3.31"
+4
View File
@@ -18,6 +18,7 @@ bili_sync_migration = { workspace = true }
chrono = { workspace = true } chrono = { workspace = true }
clap = { workspace = true } clap = { workspace = true }
cookie = { workspace = true } cookie = { workspace = true }
cow-utils = { workspace = true }
dirs = { workspace = true } dirs = { workspace = true }
float-ord = { workspace = true } float-ord = { workspace = true }
futures = { workspace = true } futures = { workspace = true }
@@ -44,6 +45,9 @@ toml = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
tracing-subscriber = { workspace = true } tracing-subscriber = { workspace = true }
[dev-dependencies]
assert_matches = { workspace = true }
[package.metadata.release] [package.metadata.release]
release = true release = true
+46 -17
View File
@@ -3,6 +3,7 @@ use std::collections::HashSet;
use anyhow::{bail, ensure, Context, Result}; use anyhow::{bail, ensure, Context, Result};
use cookie::Cookie; use cookie::Cookie;
use cow_utils::CowUtils;
use regex::Regex; use regex::Regex;
use reqwest::{header, Method}; use reqwest::{header, Method};
use rsa::pkcs8::DecodePublicKey; use rsa::pkcs8::DecodePublicKey;
@@ -227,17 +228,16 @@ fn _encoded_query<'a>(
mixin_key: &str, mixin_key: &str,
timestamp: String, timestamp: String,
) -> Vec<(&'a str, Cow<'a, str>)> { ) -> Vec<(&'a str, Cow<'a, str>)> {
let disallowed = ['!', '\'', '(', ')', '*'];
let mut params: Vec<(&'a str, Cow<'a, str>)> = params let mut params: Vec<(&'a str, Cow<'a, str>)> = params
.into_iter() .into_iter()
.map(|(k, v)| { .map(|(k, v)| {
( (
k, k,
// FIXME: 总感觉这里不太好,即使 v 是 &str 也会被转换成 String match Into::<Cow<'a, str>>::into(v) {
v.into() Cow::Borrowed(v) => v.cow_replace(&disallowed[..], ""),
.chars() Cow::Owned(v) => v.replace(&disallowed[..], "").into(),
.filter(|&x| !"!'()*".contains(x)) },
.collect::<String>()
.into(),
) )
}) })
.collect(); .collect();
@@ -252,6 +252,8 @@ fn _encoded_query<'a>(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use assert_matches::assert_matches;
use super::*; use super::*;
#[test] #[test]
@@ -290,20 +292,47 @@ mod tests {
}; };
let key = Option::<String>::from(key).expect("fail to convert key"); let key = Option::<String>::from(key).expect("fail to convert key");
assert_eq!(key.as_str(), "ea1db124af3c7062474693fa704f4ff8"); assert_eq!(key.as_str(), "ea1db124af3c7062474693fa704f4ff8");
assert_eq!( // 没有特殊字符
dbg!(_encoded_query( assert_matches!(
&_encoded_query(
vec![("foo", "114"), ("bar", "514"), ("zab", "1919810")], vec![("foo", "114"), ("bar", "514"), ("zab", "1919810")],
key.as_str(), key.as_str(),
"1702204169".to_string(), "1702204169".to_string(),
)), )[..],
// 上面产生的结果全是 Cow::Owned,但 eq 只会比较值,这样写比较方便 [
vec![ ("bar", Cow::Borrowed(a)),
("bar", Cow::Borrowed("514")), ("foo", Cow::Borrowed(b)),
("foo", Cow::Borrowed("114")), ("wts", Cow::Owned(c)),
("wts", Cow::Borrowed("1702204169")), ("zab", Cow::Borrowed(d)),
("zab", Cow::Borrowed("1919810")), ("w_rid", Cow::Owned(e)),
("w_rid", Cow::Borrowed("8f6f2b5b3d485fe1886cec6a0be8c5d4")), ] => {
] assert_eq!(*a, "514");
assert_eq!(*b, "114");
assert_eq!(c, "1702204169");
assert_eq!(*d, "1919810");
assert_eq!(e, "8f6f2b5b3d485fe1886cec6a0be8c5d4");
}
);
// 有特殊字符
assert_matches!(
&_encoded_query(
vec![("foo", "'1(1)4'"), ("bar", "!5*1!14"), ("zab", "1919810")],
key.as_str(),
"1702204169".to_string(),
)[..],
[
("bar", Cow::Owned(a)),
("foo", Cow::Owned(b)),
("wts", Cow::Owned(c)),
("zab", Cow::Borrowed(d)),
("w_rid", Cow::Owned(e)),
] => {
assert_eq!(a, "5114");
assert_eq!(b, "114");
assert_eq!(c, "1702204169");
assert_eq!(*d, "1919810");
assert_eq!(e, "6a2c86c4b0648ce062ba0dac2de91a85");
}
); );
} }
} }