Make the hashing run on spawn_blocking.

This commit is contained in:
Kavin 2023-11-17 17:04:35 +00:00
parent faafbcb737
commit 2abd43bfb2
No known key found for this signature in database
GPG key ID: 6E4598CA5C92C41F

View file

@ -151,32 +151,39 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
let qhash = qhash.unwrap(); let qhash = qhash.unwrap();
// check that qhash is valid
if qhash.len() != 8 { if qhash.len() != 8 {
return Err("Invalid qhash provided".into()); return Err("Invalid qhash provided".into());
} }
// store sorted key-value pairs // Store sorted key-value pairs
let mut set = BTreeSet::new(); let mut set = BTreeSet::new();
{
query.to_pairs().iter().for_each(|(key, value)| { let pairs = query.to_pairs();
for (key, value) in &pairs {
if matches!(*key, "qhash" | "range" | "rewrite") { if matches!(*key, "qhash" | "range" | "rewrite") {
return; continue;
}
set.insert((key.as_bytes().to_owned(), value.as_bytes().to_owned()));
}
} }
set.insert((key.as_bytes(), value.as_bytes()));
});
let (tx, rx) = oneshot::channel::<String>();
spawn_blocking(move || {
let mut hasher = blake3::Hasher::new(); let mut hasher = blake3::Hasher::new();
for (key, value) in set { for (key, value) in set {
hasher.update(key); hasher.update(&key);
hasher.update(value); hasher.update(&value);
} }
hasher.update(secret.as_bytes()); hasher.update(secret.as_bytes());
let hash = hasher.finalize().to_hex(); let hash = hasher.finalize().to_hex();
let hash = &hash[..8]; let hash = hash[..8].to_owned();
tx.send(hash).unwrap();
});
let hash = rx.await.unwrap();
if hash != qhash { if hash != qhash {
return Err("Invalid qhash provided".into()); return Err("Invalid qhash provided".into());