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