an http based roguelike rpg about jelqing
Blame: src/http.rs
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1 | use std::{ |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 2 | collections::HashMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 3 | convert::Infallible, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 4 | fmt::Write as _, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 5 | io::Write as _, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 6 | sync::{Arc, Mutex}, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 7 | time::Instant, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 8 | }; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 9 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 10 | use axum::{ |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 11 | Json, Router, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 12 | body::{Body, Bytes}, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 13 | extract::{Path, Query, Request, State}, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 14 | http::{HeaderMap, HeaderName, HeaderValue, Method, StatusCode, Uri, Version, header}, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 15 | middleware::{self, Next}, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 16 | response::{Html, IntoResponse, Redirect, Response}, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 17 | routing::any, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 18 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 19 | use flate2::{Compression, write::GzEncoder}; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 20 | use hyper_util::rt::TokioIo; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 21 | use rand::Rng; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 22 | use serde::Deserialize; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 23 | use serde_json::{Value, json}; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 24 | use tokio::io::AsyncWriteExt; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 25 | use tower::ServiceExt; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 26 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 27 | use crate::{ |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 28 | game::{ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 29 | ELDER_LAST_MODIFIED, FORUM_THREAD, Floor, Grip, LUBRICANTS, Lube, MAX_STAMINA, MM_PER_INCH, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 30 | PatchOp, RUN_ALLOW, Run, SUSPICION_LIMIT, Technique, forum_thread, shaft, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 31 | }, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 32 | problem::{PROBLEM_BASE, Problem, reason}, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 33 | secret, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 34 | }; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 35 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 36 | const REL_BASE: &str = "https://jelq.dev/rels/"; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 37 | const COOKIE_ATTRIBUTES: &str = "Max-Age=31536000; Path=/; HttpOnly; SameSite=Strict"; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 38 | const THERAPY: &str = "Your options are listed in the Allow header. There are more of them than you think. You could also try being honest. It is not in the Allow header because it is not a method."; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 39 | const MAX_URI: usize = 512; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 40 | const MAX_HEADER: usize = 1024; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 41 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 42 | #[derive(Default)] |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 43 | pub struct AppState { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 44 | runs: Mutex<HashMap<String, Run>>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 45 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 46 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 47 | impl AppState { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 48 | pub fn insert(&self, run: Run) { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 49 | self.runs.lock().unwrap().insert(run.id.clone(), run); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 50 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 51 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 52 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 53 | type Shared = Arc<AppState>; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 54 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 55 | #[derive(Clone)] |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 56 | struct Compress; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 57 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 58 | /// Which household hazards stand between the request and the run. |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 59 | #[derive(Clone, Copy, PartialEq, Eq)] |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 60 | enum Scope { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 61 | Home, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 62 | Pockets, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 63 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 64 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 65 | pub fn app() -> Router { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 66 | router(Arc::default()) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 67 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 68 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 69 | pub fn router(state: Shared) -> Router { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 70 | let api = api(state); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 71 | let ui = Router::new() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 72 | .route("/ui", any(|| async { Redirect::permanent("/ui/") })) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 73 | .route("/ui/", any(ui)) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 74 | .route("/ui/send", any(ui_send)) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 75 | .with_state(api.clone()); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 76 | api.merge(ui) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 77 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 78 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 79 | fn api(state: Shared) -> Router { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 80 | Router::new() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 81 | .route("/", any(index)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 82 | .route("/runs", any(runs)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 83 | .route("/runs/{id}", any(run)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 84 | .route("/runs/{id}/length", any(length)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 85 | .route("/runs/{id}/foreskin", any(foreskin)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 86 | .route("/runs/{id}/inventory", any(inventory)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 87 | .route("/runs/{id}/inventory/{item}", any(item)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 88 | .route("/runs/{id}/techniques", any(techniques)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 89 | .route("/runs/{id}/drawer", any(drawer)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 90 | .route("/runs/{id}/branches/{choice}", any(branch)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 91 | .route("/runs/{id}/floor", any(floor)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 92 | .route("/runs/{id}/shop", any(shop)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 93 | .route("/runs/{id}/shop/{item}", any(buy)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 94 | .route("/runs/{id}/portal", any(portal)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 95 | .route("/runs/{id}/elder", any(elder)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 96 | .route("/runs/{id}/grip", any(grip)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 97 | .route("/runs/{id}/diagnosis", any(diagnosis)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 98 | .route("/forum/threads/{year}", any(thread)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 99 | .route("/graveyard", any(graveyard)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 100 | .route("/leaderboard", any(leaderboard)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 101 | .fallback(not_found) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 102 | .layer(middleware::from_fn(chrome)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 103 | .with_state(state) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 104 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 105 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 106 | fn reject(request: &Request) -> Option<Problem> { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 107 | if request.version() < Version::HTTP_11 { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 108 | return Some(Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 109 | StatusCode::HTTP_VERSION_NOT_SUPPORTED, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 110 | "pdf", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 111 | "HTTP/1.0 is the version the original jelqing PDF was written for. The server has read it.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 112 | )); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 113 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 114 | if request.uri().to_string().len() > MAX_URI { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 115 | return Some(Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 116 | StatusCode::URI_TOO_LONG, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 117 | "uri-too-long", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 118 | "The URI is longer than anything else here.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 119 | )); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 120 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 121 | if request.headers().values().any(|v| v.len() > MAX_HEADER) { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 122 | return Some(Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 123 | StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 124 | "head-too-large", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 125 | "Head too large.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 126 | )); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 127 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 128 | let expectation = request.headers().get(header::EXPECT)?; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 129 | if expectation.as_bytes().eq_ignore_ascii_case(b"100-continue") { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 130 | return None; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 131 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 132 | Some(Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 133 | StatusCode::EXPECTATION_FAILED, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 134 | "expectations", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 135 | format!( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 136 | "Expect: {} cannot be met.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 137 | expectation.to_str().unwrap_or("that") |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 138 | ), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 139 | )) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 140 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 141 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 142 | async fn compress(response: Response) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 143 | let (mut parts, body) = response.into_parts(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 144 | let Ok(bytes) = axum::body::to_bytes(body, usize::MAX).await else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 145 | return Response::from_parts(parts, Body::empty()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 146 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 147 | let mut encoder = GzEncoder::new(Vec::new(), Compression::default()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 148 | if encoder.write_all(&bytes).is_err() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 149 | return Response::from_parts(parts, Body::from(bytes)); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 150 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 151 | let Ok(gzipped) = encoder.finish() else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 152 | return Response::from_parts(parts, Body::from(bytes)); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 153 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 154 | parts |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 155 | .headers |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 156 | .insert(header::CONTENT_ENCODING, HeaderValue::from_static("gzip")); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 157 | parts.headers.remove(header::CONTENT_LENGTH); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 158 | parts |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 159 | .headers |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 160 | .append(header::VARY, HeaderValue::from_static("Accept-Encoding")); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 161 | Response::from_parts(parts, Body::from(gzipped)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 162 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 163 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 164 | async fn chrome(request: Request, next: Next) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 165 | let gzip = header_str(request.headers(), header::ACCEPT_ENCODING) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 166 | .is_some_and(|e| e.to_ascii_lowercase().contains("gzip")); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 167 | let mut response = match reject(&request) { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 168 | Some(problem) => problem.into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 169 | None => next.run(request).await, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 170 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 171 | if gzip && response.extensions().get::<Compress>().is_some() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 172 | response = compress(response).await; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 173 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 174 | let headers = response.headers_mut(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 175 | headers.insert(header::SERVER, HeaderValue::from_static("jelqd/0.6.9")); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 176 | headers.insert("x-powered-by", HeaderValue::from_static("blood")); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 177 | headers.insert( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 178 | header::CACHE_CONTROL, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 179 | HeaderValue::from_static("no-store, private"), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 180 | ); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 181 | response |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 182 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 183 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 184 | fn header_str(headers: &HeaderMap, name: HeaderName) -> Option<&str> { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 185 | headers.get(name).and_then(|v| v.to_str().ok()) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 186 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 187 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 188 | fn cookie<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 189 | headers |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 190 | .get_all(header::COOKIE) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 191 | .iter() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 192 | .filter_map(|v| v.to_str().ok()) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 193 | .flat_map(|v| v.split(';')) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 194 | .filter_map(|pair| pair.trim().split_once('=')) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 195 | .find(|(key, _)| *key == name) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 196 | .map(|(_, value)| value) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 197 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 198 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 199 | fn unsupported(method: &Method, allow: &'static str) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 200 | if *method == Method::CONNECT { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 201 | return StatusCode::NOT_IMPLEMENTED.into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 202 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 203 | let known = matches!( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 204 | method.as_str(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 205 | "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS" | "TRACE" | "JELQ" |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 206 | ); |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 207 | if method.as_str() == "BREW" { |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 208 | return Problem::new( |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 209 | StatusCode::IM_A_TEAPOT, |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 210 | "brew", |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 211 | "The server is a teapot. RFC 2324 is clear on this. It is not yet short and stout.", |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 212 | ) |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 213 | .into_response(); |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 214 | } |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 215 | if method.as_str().eq_ignore_ascii_case("jelq") { |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 216 | return Problem::new( |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 217 | StatusCode::NOT_IMPLEMENTED, |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 218 | "not-implemented", |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 219 | format!( |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 220 | "The server does not implement {method}. Methods are case-sensitive. JELQ is shouted for a reason." |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 221 | ), |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 222 | ) |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 223 | .into_response(); |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 224 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 225 | if !known { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 226 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 227 | StatusCode::NOT_IMPLEMENTED, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 228 | "not-implemented", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 229 | format!("The server does not implement {method}."), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 230 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 231 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 232 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 233 | Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 234 | StatusCode::METHOD_NOT_ALLOWED, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 235 | "method-not-allowed", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 236 | format!("{method} is not supported here."), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 237 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 238 | .header(header::ALLOW, allow) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 239 | .into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 240 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 241 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 242 | fn options(allow: &'static str, patchable: bool) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 243 | let mut response = ( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 244 | StatusCode::OK, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 245 | [(header::ALLOW, allow)], |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 246 | Json(json!({ "detail": THERAPY })), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 247 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 248 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 249 | if patchable { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 250 | response.headers_mut().insert( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 251 | "accept-patch", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 252 | HeaderValue::from_static("application/json-patch+json"), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 253 | ); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 254 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 255 | response |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 256 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 257 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 258 | fn trace(method: &Method, uri: &Uri, version: Version, headers: &HeaderMap) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 259 | let mut message = format!("{method} {uri} {version:?}\r\n"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 260 | for (name, value) in headers { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 261 | let _ = write!( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 262 | message, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 263 | "{name}: {}\r\n", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 264 | value.to_str().unwrap_or("<binary>") |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 265 | ); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 266 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 267 | message.push_str("\r\n"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 268 | ( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 269 | StatusCode::OK, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 270 | [(header::CONTENT_TYPE, "message/http")], |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 271 | message, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 272 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 273 | .into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 274 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 275 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 276 | fn links(run: &Run) -> String { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 277 | let id = &run.id; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 278 | let mut links = vec![format!("</runs/{id}>; rel=\"{REL_BASE}run\"")]; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 279 | for rel in [ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 280 | "floor", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 281 | "length", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 282 | "foreskin", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 283 | "inventory", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 284 | "techniques", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 285 | "drawer", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 286 | "grip", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 287 | "diagnosis", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 288 | ] { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 289 | links.push(format!("</runs/{id}/{rel}>; rel=\"{REL_BASE}{rel}\"")); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 290 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 291 | if run.floor.shop_open() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 292 | links.push(format!("</runs/{id}/shop>; rel=\"{REL_BASE}shop\"")); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 293 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 294 | links.join(", ") |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 295 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 296 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 297 | fn finish(run: &Run, mut response: Response) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 298 | let status = response.status(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 299 | if status == StatusCode::NOT_MODIFIED { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 300 | return response; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 301 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 302 | let mut stats = vec![ |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 303 | ( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 304 | "x-self-reported-length", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 305 | format!("{:.2}", run.self_reported()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 306 | ), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 307 | ("x-girth", format!("{:.2}", run.girth)), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 308 | ( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 309 | "x-foreskin", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 310 | run.foreskin.map_or("gone".into(), |f| format!("{f:.2}")), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 311 | ), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 312 | ("x-eq", run.eq.to_string()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 313 | ("x-stamina", format!("{}/{MAX_STAMINA}", run.stamina)), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 314 | ( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 315 | "x-suspicion", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 316 | format!("{}/{SUSPICION_LIMIT}", run.suspicion), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 317 | ), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 318 | ("x-shame", run.total_shame().to_string()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 319 | ("x-cash", run.cash.to_string()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 320 | ("x-floor", run.floor.number().to_string()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 321 | ("link", links(run)), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 322 | ]; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 323 | if let Some(observer) = run.observed { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 324 | stats.push(("x-observed-by", observer.into())); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 325 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 326 | if run.mom_present { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 327 | stats.push(("via", "1.1 mom".into())); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 328 | stats.push(("vary", "Mom".into())); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 329 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 330 | // RFC 9111 obsoleted Warning, which is why nobody heeds it. |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 331 | if run.eq > 70 && run.ended.is_none() { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 332 | stats.push(("warning", "199 - \"it is turning purple\"".into())); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 333 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 334 | stats.push(( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 335 | "set-cookie", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 336 | format!( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 337 | "shame={}; {COOKIE_ATTRIBUTES}", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 338 | secret::sign("shame", &run.total_shame().to_string()) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 339 | ), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 340 | )); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 341 | if run.items.ruler { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 342 | stats.push(( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 343 | "set-cookie", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 344 | format!("ruler={}; {COOKIE_ATTRIBUTES}", secret::sign("ruler", "1")), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 345 | )); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 346 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 347 | let headers = response.headers_mut(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 348 | for (name, value) in stats { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 349 | headers.append(name, HeaderValue::from_str(&value).expect("visible ascii")); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 350 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 351 | let bodiless = status.is_informational() || status == StatusCode::NO_CONTENT; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 352 | if run.items.ring && !bodiless { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 353 | response.extensions_mut().insert(Compress); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 354 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 355 | response |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 356 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 357 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 358 | fn gone(run: &Run) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 359 | let ending = run.ended.as_ref().expect("only called on ended runs"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 360 | Problem::new(StatusCode::GONE, "run-ended", "This run has ended.") |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 361 | .with("cause", format!("{PROBLEM_BASE}{}", ending.cause)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 362 | .with("turn", ending.turn) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 363 | .with("graveyard", "/graveyard") |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 364 | .into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 365 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 366 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 367 | fn captive_portal(run: &Run) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 368 | Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 369 | StatusCode::NETWORK_AUTHENTICATION_REQUIRED, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 370 | "captive-portal", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 371 | "The network requires authentication. The hotel would like you to agree to some terms.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 372 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 373 | .with("portal", format!("/runs/{}/portal", run.id)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 374 | .header( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 375 | header::LINK, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 376 | format!("</runs/{}/portal>; rel=\"{REL_BASE}portal\"", run.id), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 377 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 378 | .into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 379 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 380 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 381 | fn with_run( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 382 | state: &AppState, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 383 | id: &str, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 384 | headers: &HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 385 | scope: Scope, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 386 | handle: impl FnOnce(&mut Run) -> Response, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 387 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 388 | let mut runs = state.runs.lock().unwrap(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 389 | let Some(run) = runs.get_mut(id) else { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 390 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 391 | StatusCode::NOT_FOUND, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 392 | "no-such-run", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 393 | format!("There is no run {id}. There may never have been."), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 394 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 395 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 396 | }; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 397 | if run.ended.is_some() { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 398 | return gone(run); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 399 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 400 | run.observed = None; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 401 | if scope == Scope::Home { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 402 | if run.floor == Floor::Hotel && !run.wifi_accepted { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 403 | return finish(run, captive_portal(run)); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 404 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 405 | if run.floor.has_mom() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 406 | && run.mom_present |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 407 | && let Err(problem) = run.answer_door(header_str(headers, header::AUTHORIZATION)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 408 | { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 409 | return finish(run, problem.into_response()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 410 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 411 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 412 | let response = handle(run); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 413 | finish(run, response) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 414 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 415 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 416 | fn respond<T: IntoResponse, E: IntoResponse>(result: Result<T, E>) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 417 | match result { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 418 | Ok(ok) => ok.into_response(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 419 | Err(err) => err.into_response(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 420 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 421 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 422 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 423 | async fn runs( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 424 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 425 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 426 | Query(query): Query<HashMap<String, String>>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 427 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 428 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 429 | if method != Method::POST { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 430 | return unsupported(&method, "POST"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 431 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 432 | let seeded = query.contains_key("seed"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 433 | let seed = match query.get("seed").map(|s| s.parse::<u64>()) { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 434 | None => rand::rng().random(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 435 | Some(Ok(seed)) => seed, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 436 | Some(Err(_)) => { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 437 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 438 | StatusCode::BAD_REQUEST, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 439 | "bad-seed", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 440 | "The seed must be an unsigned 64-bit integer.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 441 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 442 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 443 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 444 | }; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 445 | let shame = cookie(&headers, "shame") |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 446 | .and_then(|s| secret::verify("shame", s)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 447 | .and_then(|s| s.parse().ok()) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 448 | .unwrap_or(0); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 449 | let id = format!("{:08x}", rand::rng().random::<u32>()); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 450 | let mut run = Run::new(id.clone(), secret::mix_seed(seed), shame); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 451 | run.seeded = seeded; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 452 | let response = ( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 453 | StatusCode::CREATED, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 454 | [ |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 455 | (header::LOCATION, format!("/runs/{id}")), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 456 | (header::ETAG, run.etag()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 457 | (HeaderName::from_static("x-seed"), seed.to_string()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 458 | ], |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 459 | Json(run.view()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 460 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 461 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 462 | let response = finish(&run, response); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 463 | state.insert(run); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 464 | response |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 465 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 466 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 467 | #[allow(clippy::too_many_arguments)] |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 468 | async fn run( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 469 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 470 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 471 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 472 | uri: Uri, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 473 | version: Version, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 474 | Query(query): Query<HashMap<String, String>>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 475 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 476 | body: Bytes, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 477 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 478 | let now = Instant::now(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 479 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 480 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 481 | "GET" | "HEAD" => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 482 | let accept = header_str(&headers, header::ACCEPT).unwrap_or_default(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 483 | if accept.contains("reassurance") { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 484 | return Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 485 | StatusCode::NOT_ACCEPTABLE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 486 | "no-reassurance", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 487 | "The server does not produce reassurance.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 488 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 489 | .into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 490 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 491 | ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 492 | StatusCode::OK, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 493 | [(header::ETAG, run.etag())], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 494 | Json(run.view()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 495 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 496 | .into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 497 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 498 | "JELQ" => jelq(run, &query, &headers, now), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 499 | "PUT" => run.put().into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 500 | "PATCH" => patch(run, &headers, &body), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 501 | "DELETE" => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 502 | run.end("abandoned", "The run was abandoned."); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 503 | StatusCode::NO_CONTENT.into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 504 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 505 | "OPTIONS" => options(RUN_ALLOW, true), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 506 | "TRACE" => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 507 | run.shame += 1; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 508 | trace(&method, &uri, version, &headers) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 509 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 510 | _ => unsupported(&method, RUN_ALLOW), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 511 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 512 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 513 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 514 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 515 | fn jelq( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 516 | run: &mut Run, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 517 | query: &HashMap<String, String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 518 | headers: &HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 519 | now: Instant, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 520 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 521 | let techniques_href = format!("/runs/{}/techniques", run.id); |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 522 | let Some(names) = query.get("technique").filter(|n| !n.trim().is_empty()) else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 523 | return Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 524 | StatusCode::BAD_REQUEST, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 525 | "technique-required", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 526 | "No technique was specified.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 527 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 528 | .with("techniques", techniques_href) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 529 | .into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 530 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 531 | let mut techniques = Vec::new(); |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 532 | for name in names.split(',').map(str::trim).filter(|n| !n.is_empty()) { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 533 | let Some(technique) = Technique::parse(name) else { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 534 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 535 | StatusCode::BAD_REQUEST, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 536 | "unknown-technique", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 537 | format!("{name} is not a technique."), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 538 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 539 | .with("techniques", techniques_href) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 540 | .into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 541 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 542 | techniques.push(technique); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 543 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 544 | let intensity = match query.get("intensity").map(|i| i.parse::<i32>()) { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 545 | None => 5, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 546 | Some(Ok(i)) if (1..=10).contains(&i) => i, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 547 | Some(_) => { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 548 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 549 | StatusCode::BAD_REQUEST, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 550 | "intensity-out-of-range", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 551 | "Intensity is measured from 1 to 10.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 552 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 553 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 554 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 555 | }; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 556 | let lube = match Lube::from_content_type(header_str(headers, header::CONTENT_TYPE)) { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 557 | Ok(lube) => lube, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 558 | Err(problem) => return problem.into_response(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 559 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 560 | respond(run.jelq(&techniques, intensity, lube, now)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 561 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 562 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 563 | fn patch(run: &mut Run, headers: &HeaderMap, body: &[u8]) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 564 | let content_type = header_str(headers, header::CONTENT_TYPE).unwrap_or_default(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 565 | if !content_type.starts_with("application/json-patch+json") { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 566 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 567 | StatusCode::UNSUPPORTED_MEDIA_TYPE, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 568 | "patch-format", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 569 | "Patches are accepted as application/json-patch+json.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 570 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 571 | .header( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 572 | HeaderName::from_static("accept-patch"), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 573 | "application/json-patch+json", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 574 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 575 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 576 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 577 | match serde_json::from_slice::<Vec<PatchOp>>(body) { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 578 | Ok(ops) => respond(run.patch(&ops)), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 579 | Err(_) => Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 580 | StatusCode::BAD_REQUEST, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 581 | "malformed-patch", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 582 | "The patch could not be parsed.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 583 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 584 | .into_response(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 585 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 586 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 587 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 588 | fn millimetres(inches: f64) -> usize { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 589 | (inches * MM_PER_INCH).round() as usize |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 590 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 591 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 592 | fn parse_inch_range(range: &str, total: f64) -> Option<(f64, f64)> { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 593 | let (start, end) = range.strip_prefix("inches=")?.split_once('-')?; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 594 | let start: f64 = start.trim().parse().ok()?; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 595 | let end: f64 = match end.trim() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 596 | "" => total, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 597 | end => end.parse().ok()?, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 598 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 599 | (start >= 0.0 && start < total && start <= end).then_some((start, end.min(total))) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 600 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 601 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 602 | fn length_response(run: &Run, headers: &HeaderMap) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 603 | let inches = if run.items.ruler { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 604 | run.current_length() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 605 | } else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 606 | run.self_reported() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 607 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 608 | let body = shaft(millimetres(inches)); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 609 | let content_type = ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 610 | header::CONTENT_TYPE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 611 | "text/plain; charset=us-ascii".to_owned(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 612 | ); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 613 | let accept_ranges = (header::ACCEPT_RANGES, "inches".to_owned()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 614 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 615 | if let Some(range) = header_str(headers, header::RANGE) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 616 | && range.starts_with("inches=") |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 617 | { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 618 | let Some((start, end)) = parse_inch_range(range, inches) else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 619 | return Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 620 | StatusCode::RANGE_NOT_SATISFIABLE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 621 | "range-not-satisfiable", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 622 | format!("The requested range is not available. {inches:.2} inches are."), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 623 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 624 | .header(header::CONTENT_RANGE, format!("inches */{inches:.2}")) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 625 | .into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 626 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 627 | let from = millimetres(start).min(body.len() - 1); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 628 | let to = millimetres(end).clamp(from + 1, body.len()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 629 | let content_range = ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 630 | header::CONTENT_RANGE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 631 | format!("inches {start:.2}-{end:.2}/{inches:.2}"), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 632 | ); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 633 | return ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 634 | StatusCode::PARTIAL_CONTENT, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 635 | [content_type, accept_ranges, content_range], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 636 | body[from..to].to_owned(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 637 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 638 | .into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 639 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 640 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 641 | if run.items.ruler { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 642 | return (StatusCode::OK, [content_type, accept_ranges], body).into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 643 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 644 | let chunks: Vec<Result<Bytes, Infallible>> = body |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 645 | .into_bytes() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 646 | .chunks(16) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 647 | .map(|c| Ok(Bytes::copy_from_slice(c))) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 648 | .collect(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 649 | let body = Body::from_stream(futures_util::stream::iter(chunks)); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 650 | (StatusCode::OK, [content_type, accept_ranges], body).into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 651 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 652 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 653 | async fn length( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 654 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 655 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 656 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 657 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 658 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 659 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 660 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 661 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 662 | "GET" | "HEAD" => length_response(run, &headers), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 663 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 664 | _ => unsupported(&method, ALLOW), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 665 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 666 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 667 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 668 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 669 | async fn foreskin( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 670 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 671 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 672 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 673 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 674 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 675 | const ALLOW: &str = "GET, HEAD, DELETE, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 676 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 677 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 678 | "GET" | "HEAD" => match run.foreskin { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 679 | Some(f) => ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 680 | StatusCode::OK, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 681 | [(header::ETAG, run.etag())], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 682 | Json(json!({ "foreskin": (f * 100.0).round() / 100.0 })), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 683 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 684 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 685 | None => Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 686 | StatusCode::GONE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 687 | "foreskin-gone", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 688 | "The foreskin is gone. It will not be returning.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 689 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 690 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 691 | }, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 692 | "DELETE" => respond(run.delete_foreskin(header_str(&headers, header::IF_MATCH))), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 693 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 694 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 695 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 696 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 697 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 698 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 699 | async fn inventory( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 700 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 701 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 702 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 703 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 704 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 705 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 706 | with_run(&state, &id, &headers, Scope::Pockets, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 707 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 708 | "GET" | "HEAD" => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 709 | let listed: Vec<Value> = run |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 710 | .inventory() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 711 | .into_iter() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 712 | .map(|(name, _)| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 713 | json!({ "name": name, "href": format!("/runs/{}/inventory/{name}", run.id) }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 714 | }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 715 | .collect(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 716 | Json(json!({ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 717 | "items": listed, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 718 | "ice_packs": run.items.ice_packs, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 719 | "cash": run.cash, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 720 | "excuses": run.excuses(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 721 | })) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 722 | .into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 723 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 724 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 725 | _ => unsupported(&method, ALLOW), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 726 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 727 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 728 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 729 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 730 | async fn item( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 731 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 732 | Path((id, name)): Path<(String, String)>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 733 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 734 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 735 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 736 | const ALLOW: &str = "GET, HEAD, DELETE, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 737 | with_run(&state, &id, &headers, Scope::Pockets, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 738 | let Some((name, description)) = run.inventory().into_iter().find(|(n, _)| *n == name) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 739 | else { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 740 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 741 | StatusCode::NOT_FOUND, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 742 | "no-such-item", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 743 | "You do not have that.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 744 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 745 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 746 | }; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 747 | match method.as_str() { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 748 | "GET" | "HEAD" => { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 749 | Json(json!({ "name": name, "description": description })).into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 750 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 751 | "DELETE" => respond(run.drop_item(name)), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 752 | "OPTIONS" => options(ALLOW, false), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 753 | _ => unsupported(&method, ALLOW), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 754 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 755 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 756 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 757 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 758 | async fn techniques( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 759 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 760 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 761 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 762 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 763 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 764 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 765 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 766 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 767 | "GET" | "HEAD" => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 768 | let listed: Vec<Value> = Technique::ALL |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 769 | .into_iter() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 770 | .map(|t| t.describe(run.forum_read)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 771 | .collect(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 772 | Json(json!({ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 773 | "usage": format!("JELQ /runs/{}?technique=<name>[,<name>]&intensity=<1-10>", run.id), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 774 | "lubricant": "Declared with Content-Type.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 775 | "lubricants": LUBRICANTS, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 776 | "eq_band": [50, 70], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 777 | "techniques": listed, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 778 | })) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 779 | .into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 780 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 781 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 782 | _ => unsupported(&method, ALLOW), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 783 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 784 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 785 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 786 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 787 | async fn drawer( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 788 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 789 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 790 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 791 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 792 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 793 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 794 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 795 | "POST" => respond(run.search_drawer()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 796 | "OPTIONS" => options("POST, OPTIONS", false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 797 | _ => unsupported(&method, "POST, OPTIONS"), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 798 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 799 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 800 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 801 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 802 | async fn branch( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 803 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 804 | Path((id, choice)): Path<(String, String)>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 805 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 806 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 807 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 808 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 809 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 810 | "POST" => respond(run.choose(&choice)), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 811 | "OPTIONS" => options("POST, OPTIONS", false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 812 | _ => unsupported(&method, "POST, OPTIONS"), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 813 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 814 | }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 815 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 816 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 817 | async fn floor( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 818 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 819 | Path(id): Path<String>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 820 | method: Method, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 821 | headers: HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 822 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 823 | const ALLOW: &str = "GET, HEAD, POST, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 824 | with_run(&state, &id, &headers, Scope::Pockets, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 825 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 826 | "GET" | "HEAD" => Json(run.floor_view()).into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 827 | "POST" => respond(run.advance()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 828 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 829 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 830 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 831 | }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 832 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 833 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 834 | async fn shop( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 835 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 836 | Path(id): Path<String>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 837 | method: Method, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 838 | headers: HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 839 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 840 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 841 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 842 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 843 | "GET" | "HEAD" => respond(run.shop_view().map(Json)), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 844 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 845 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 846 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 847 | }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 848 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 849 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 850 | async fn buy( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 851 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 852 | Path((id, item)): Path<(String, String)>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 853 | method: Method, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 854 | headers: HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 855 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 856 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 857 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 858 | "POST" => respond(run.buy(&item)), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 859 | "OPTIONS" => options("POST, OPTIONS", false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 860 | _ => unsupported(&method, "POST, OPTIONS"), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 861 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 862 | }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 863 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 864 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 865 | async fn portal( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 866 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 867 | Path(id): Path<String>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 868 | method: Method, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 869 | headers: HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 870 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 871 | const ALLOW: &str = "GET, HEAD, POST, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 872 | with_run(&state, &id, &headers, Scope::Pockets, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 873 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 874 | "GET" | "HEAD" => Json(json!({ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 875 | "terms": "By connecting you agree that the hotel is not responsible for anything that happens in this room, and that it does not want to know.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 876 | "accept": { "href": format!("/runs/{}/portal", run.id), "method": "POST" }, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 877 | })) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 878 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 879 | "POST" => respond(run.accept_terms()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 880 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 881 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 882 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 883 | }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 884 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 885 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 886 | async fn elder( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 887 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 888 | Path(id): Path<String>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 889 | method: Method, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 890 | headers: HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 891 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 892 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 893 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 894 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 895 | "GET" | "HEAD" => respond(run.elder( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 896 | header_str(&headers, header::PROXY_AUTHORIZATION), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 897 | header_str(&headers, header::IF_MODIFIED_SINCE), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 898 | )), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 899 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 900 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 901 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 902 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 903 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 904 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 905 | fn requested_grip(headers: &HeaderMap) -> Option<Grip> { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 906 | let connection = header_str(headers, header::CONNECTION)?.to_ascii_lowercase(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 907 | if !connection.split(',').any(|token| token.trim() == "upgrade") { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 908 | return None; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 909 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 910 | header_str(headers, header::UPGRADE)? |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 911 | .split(',') |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 912 | .find_map(|protocol| Grip::parse(protocol.trim())) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 913 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 914 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 915 | async fn grip( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 916 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 917 | Path(id): Path<String>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 918 | mut request: Request, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 919 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 920 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 921 | let method = request.method().clone(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 922 | let headers = request.headers().clone(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 923 | let wanted = requested_grip(&headers); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 924 | let upgrade = wanted.map(|_| hyper::upgrade::on(&mut request)); |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 925 | let detail = match header_str(&headers, header::UPGRADE) { |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 926 | Some(protocol) if wanted.is_none() => { |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 927 | format!("The server does not know the {protocol} grip. Grips are protocols.") |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 928 | } |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 929 | _ => "Grips are protocols. Change grip by upgrading the connection.".to_owned(), |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 930 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 931 | let response = with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 932 | match (method.as_str(), wanted) { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 933 | ("GET" | "HEAD", Some(grip)) => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 934 | run.set_grip(grip); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 935 | ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 936 | StatusCode::SWITCHING_PROTOCOLS, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 937 | [ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 938 | (header::UPGRADE, grip.name()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 939 | (header::CONNECTION, "upgrade"), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 940 | ], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 941 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 942 | .into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 943 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 944 | ("GET" | "HEAD", None) => ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 945 | StatusCode::OK, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 946 | [(header::UPGRADE, "overhand, underhand")], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 947 | Json(json!({ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 948 | "grip": run.grip.name(), |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 949 | "detail": detail, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 950 | })), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 951 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 952 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 953 | ("OPTIONS", _) => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 954 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 955 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 956 | }); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 957 | if response.status() == StatusCode::SWITCHING_PROTOCOLS |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 958 | && let (Some(upgrade), Some(grip)) = (upgrade, wanted) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 959 | { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 960 | tokio::spawn(async move { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 961 | if let Ok(upgraded) = upgrade.await { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 962 | let mut io = TokioIo::new(upgraded); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 963 | let line = format!("You are now jelqing {}.\r\n", grip.name()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 964 | let _ = io.write_all(line.as_bytes()).await; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 965 | let _ = io.shutdown().await; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 966 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 967 | }); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 968 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 969 | response |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 970 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 971 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 972 | async fn diagnosis( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 973 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 974 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 975 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 976 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 977 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 978 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 979 | with_run(&state, &id, &headers, Scope::Pockets, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 980 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 981 | "GET" | "HEAD" => respond(run.diagnose( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 982 | header_str(&headers, header::AUTHORIZATION), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 983 | header_str(&headers, header::IF_NONE_MATCH), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 984 | )), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 985 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 986 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 987 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 988 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 989 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 990 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 991 | async fn thread(Path(year): Path<String>, method: Method) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 992 | if method != Method::GET && method != Method::HEAD { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 993 | return unsupported(&method, "GET, HEAD"); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 994 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 995 | match year.parse::<u32>() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 996 | Ok(2009) => ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 997 | [(header::LAST_MODIFIED, ELDER_LAST_MODIFIED)], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 998 | Json(forum_thread()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 999 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1000 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1001 | Ok(y) if y > 2009 => Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1002 | StatusCode::NOT_FOUND, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1003 | "no-thread", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1004 | "Nobody has posted since 2009.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1005 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1006 | .with("thread", FORUM_THREAD) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1007 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1008 | _ => Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1009 | StatusCode::NOT_FOUND, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1010 | "no-thread", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1011 | "The forum did not exist yet. The problem did.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1012 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1013 | .with("thread", FORUM_THREAD) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1014 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1015 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1016 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1017 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1018 | async fn graveyard(State(state): State<Shared>, method: Method) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1019 | if method != Method::GET && method != Method::HEAD { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1020 | return unsupported(&method, "GET, HEAD"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1021 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1022 | let runs = state.runs.lock().unwrap(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1023 | let dead: Vec<Value> = runs |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1024 | .values() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1025 | .filter_map(|run| { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1026 | let ending = run.ended.as_ref()?; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1027 | Some(json!({ |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1028 | "id": run.id, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1029 | "cause": format!("{PROBLEM_BASE}{}", ending.cause), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1030 | "detail": ending.detail, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1031 | "turn": ending.turn, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1032 | })) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1033 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1034 | .collect(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1035 | Json(json!({ "runs": dead })).into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1036 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1037 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1038 | async fn leaderboard( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1039 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1040 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1041 | Query(query): Query<HashMap<String, String>>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1042 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1043 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1044 | if method != Method::GET && method != Method::HEAD { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1045 | return unsupported(&method, "GET, HEAD"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1046 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1047 | let truthful = match query.get("sort").map(String::as_str) { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1048 | None | Some("self-reported-length") => false, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1049 | Some("content-length") => match cookie(&headers, "ruler") { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1050 | Some(ruler) if secret::verify("ruler", ruler) == Some("1") => true, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1051 | Some(_) => { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1052 | return Problem::new( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1053 | StatusCode::BAD_REQUEST, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1054 | "exaggeration", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1055 | "The server has measured. Lying is not a technique.", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1056 | ) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1057 | .with("cookie", "ruler") |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1058 | .into_response(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1059 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1060 | None => { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1061 | return Problem::new( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1062 | StatusCode::FORBIDDEN, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1063 | "ruler-required", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1064 | "Sorting by the truth requires a Ruler.", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1065 | ) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1066 | .into_response(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1067 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1068 | }, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1069 | Some(_) => { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1070 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1071 | StatusCode::BAD_REQUEST, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1072 | "unknown-sort", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1073 | "The leaderboard sorts by self-reported-length or content-length.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1074 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1075 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1076 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1077 | }; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1078 | let include_seeded = query.get("seeded").is_some_and(|s| s == "include"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1079 | let runs = state.runs.lock().unwrap(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1080 | let mut entries: Vec<&Run> = runs |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1081 | .values() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1082 | .filter(|run| include_seeded || !run.seeded) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1083 | .collect(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1084 | let key = |r: &Run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1085 | if truthful { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1086 | r.current_length() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1087 | } else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1088 | r.self_reported() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1089 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1090 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1091 | entries.sort_by(|a, b| key(b).total_cmp(&key(a))); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1092 | let entries: Vec<Value> = entries |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1093 | .into_iter() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1094 | .map(|run| { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1095 | let mut entry = json!({ |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1096 | "id": run.id, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1097 | "status": run.status(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1098 | "floor": run.floor.number(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1099 | "self_reported_length": (run.self_reported() * 100.0).round() / 100.0, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1100 | "ranked": !run.seeded, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1101 | }); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1102 | if truthful { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1103 | entry["content_length"] = json!(millimetres(run.current_length())); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1104 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1105 | entry |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1106 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1107 | .collect(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1108 | Json(json!({ "entries": entries })).into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1109 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1110 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1111 | async fn index(method: Method, headers: HeaderMap) -> Response { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1112 | if method != Method::GET && method != Method::HEAD { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1113 | return unsupported(&method, "GET, HEAD"); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1114 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1115 | let wants_html = |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1116 | header_str(&headers, header::ACCEPT).is_some_and(|accept| accept.contains("text/html")); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1117 | if wants_html { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1118 | return ( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1119 | StatusCode::SEE_OTHER, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1120 | [(header::LOCATION, "/ui/"), (header::VARY, "Accept")], |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1121 | ) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1122 | .into_response(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1123 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1124 | ( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1125 | [(header::VARY, "Accept")], |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1126 | Json(json!({ |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1127 | "detail": "This is jelqd. It is a roguelike. It is played over HTTP.", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1128 | "start": { "href": "/runs", "method": "POST" }, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1129 | "leaderboard": "/leaderboard", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1130 | "graveyard": "/graveyard", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1131 | "forum": FORUM_THREAD, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1132 | "ui": "/ui/", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1133 | })), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1134 | ) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1135 | .into_response() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1136 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1137 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1138 | async fn ui(method: Method) -> Response { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1139 | if method != Method::GET && method != Method::HEAD { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1140 | return unsupported(&method, "GET, HEAD"); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1141 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1142 | Html(include_str!("ui.html")).into_response() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1143 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1144 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1145 | #[derive(Deserialize)] |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1146 | struct Envelope { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1147 | method: String, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1148 | path: String, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1149 | #[serde(default)] |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1150 | headers: Vec<(String, String)>, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1151 | #[serde(default)] |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1152 | body: String, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1153 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1154 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1155 | fn bad_envelope(detail: &str) -> Response { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1156 | Problem::new(StatusCode::BAD_REQUEST, "bad-envelope", detail).into_response() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1157 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1158 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1159 | // Browsers hide 3xx responses from fetch and refuse TRACE, Upgrade, and Expect, so the page |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1160 | // replays requests in-process to show the player exactly what the API said. |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1161 | async fn ui_send(State(api): State<Router>, headers: HeaderMap, body: Bytes) -> Response { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1162 | let Ok(envelope) = serde_json::from_slice::<Envelope>(&body) else { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1163 | return bad_envelope("The envelope could not be parsed."); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1164 | }; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1165 | if !envelope.path.starts_with('/') || envelope.path.starts_with("/ui") { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1166 | return bad_envelope("The path must be an API path."); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1167 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1168 | let Ok(method) = Method::from_bytes(envelope.method.as_bytes()) else { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1169 | return bad_envelope("That is not a method."); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1170 | }; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1171 | let mut request = Request::builder().method(method).uri(&envelope.path); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1172 | for (name, value) in envelope.headers.iter().filter(|(_, v)| !v.is_empty()) { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1173 | request = request.header(name, value); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1174 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1175 | for cookie in headers.get_all(header::COOKIE) { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1176 | request = request.header(header::COOKIE, cookie); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1177 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1178 | let Ok(request) = request.body(Body::from(envelope.body)) else { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1179 | return bad_envelope("The request could not be built. A header may be malformed."); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1180 | }; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1181 | let Ok(response) = api.oneshot(request).await; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1182 | let (parts, body) = response.into_parts(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1183 | let bytes = axum::body::to_bytes(body, 1 << 20) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1184 | .await |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1185 | .unwrap_or_default(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1186 | let listed: Vec<(String, String)> = parts |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1187 | .headers |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1188 | .iter() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1189 | .map(|(name, value)| { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1190 | ( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1191 | name.to_string(), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1192 | value.to_str().unwrap_or("<binary>").to_owned(), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1193 | ) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1194 | }) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1195 | .collect(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1196 | let mut response = Json(json!({ |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1197 | "status": parts.status.as_u16(), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1198 | "reason": reason(parts.status), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1199 | "headers": listed, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1200 | "body": String::from_utf8_lossy(&bytes), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1201 | })) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1202 | .into_response(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1203 | for cookie in parts.headers.get_all(header::SET_COOKIE) { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1204 | response |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1205 | .headers_mut() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1206 | .append(header::SET_COOKIE, cookie.clone()); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1207 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1208 | response |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1209 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1210 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1211 | async fn not_found() -> Problem { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1212 | Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1213 | StatusCode::NOT_FOUND, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1214 | "not-found", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1215 | "The resource does not exist. This is not about you.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1216 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1217 | } |