48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
use actix_web::{
|
|
HttpRequest, HttpResponse,
|
|
web::{self, Bytes},
|
|
};
|
|
use futures_util::stream;
|
|
|
|
use crate::consts;
|
|
|
|
fn mix_u32s(seed: impl Iterator<Item = u32>) -> u32 {
|
|
// from wyhash32, according to GPT, should just have a nice bit distribution anyways
|
|
const U32_BIT_MIXING_CONSTANT: u32 = 0x7feb_352d;
|
|
const U32_SELF_BIT_MIXING: u32 = 0x846c_a68b;
|
|
|
|
let mut random = seed.fold(0_u32, |acc, e| {
|
|
// mix bits
|
|
acc.wrapping_add(e.wrapping_mul(U32_BIT_MIXING_CONSTANT))
|
|
});
|
|
|
|
// GPT's suggestion for a nicer distribution, like 8 cpu instructions so whatever
|
|
random ^= random >> 16;
|
|
random = random.wrapping_mul(U32_SELF_BIT_MIXING);
|
|
random ^= random >> 15;
|
|
|
|
random
|
|
}
|
|
|
|
pub async fn not_found(req: HttpRequest) -> HttpResponse {
|
|
let seed = req.path().as_bytes();
|
|
let random = mix_u32s(seed.iter().copied().map(u32::from));
|
|
|
|
let res = consts::ERROR_ASCII_ARTS[random as usize % consts::ERROR_ASCII_ARTS.len()];
|
|
|
|
let method = req.method().as_str();
|
|
let url = req.path();
|
|
|
|
HttpResponse::NotFound()
|
|
.content_type("text/plain; charset=utf-8")
|
|
.streaming(stream::iter([
|
|
Ok::<_, !>(web::Bytes::from(format!(
|
|
"> {method} '{url}'\n404 NOT FOUND\nLet's all love lain\n\n"
|
|
))),
|
|
Ok(Bytes::from_static(res.as_bytes())),
|
|
]))
|
|
// .body(format!(
|
|
// "> {method} '{url}'\n404 NOT FOUND\nLet's all love lain\n\n{res}"
|
|
// ))
|
|
}
|