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::{ |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 29 | ELDER_LAST_MODIFIED, FORUM_THREAD, Floor, Grip, LUBRICANTS, Lube, MM_PER_INCH, PatchOp, |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 30 | 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)) |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 98 | .route("/runs/{id}/footage", any(footage)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 99 | .route("/forum/threads/{year}", any(thread)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 100 | .route("/graveyard", any(graveyard)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 101 | .route("/leaderboard", any(leaderboard)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 102 | .fallback(not_found) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 103 | .layer(middleware::from_fn(chrome)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 104 | .with_state(state) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 105 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 106 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 107 | fn reject(request: &Request) -> Option<Problem> { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 108 | if request.version() < Version::HTTP_11 { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 109 | return Some(Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 110 | StatusCode::HTTP_VERSION_NOT_SUPPORTED, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 111 | "pdf", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 112 | "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 | 113 | )); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 114 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 115 | if request.uri().to_string().len() > MAX_URI { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 116 | return Some(Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 117 | StatusCode::URI_TOO_LONG, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 118 | "uri-too-long", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 119 | "The URI is longer than anything else here.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 120 | )); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 121 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 122 | if request.headers().values().any(|v| v.len() > MAX_HEADER) { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 123 | return Some(Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 124 | StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 125 | "head-too-large", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 126 | "Head too large.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 127 | )); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 128 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 129 | let expectation = request.headers().get(header::EXPECT)?; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 130 | if expectation.as_bytes().eq_ignore_ascii_case(b"100-continue") { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 131 | return None; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 132 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 133 | Some(Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 134 | StatusCode::EXPECTATION_FAILED, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 135 | "expectations", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 136 | format!( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 137 | "Expect: {} cannot be met.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 138 | expectation.to_str().unwrap_or("that") |
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 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 143 | async fn compress(response: Response) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 144 | let (mut parts, body) = response.into_parts(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 145 | let Ok(bytes) = axum::body::to_bytes(body, usize::MAX).await else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 146 | return Response::from_parts(parts, Body::empty()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 147 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 148 | let mut encoder = GzEncoder::new(Vec::new(), Compression::default()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 149 | if encoder.write_all(&bytes).is_err() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 150 | return Response::from_parts(parts, Body::from(bytes)); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 151 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 152 | let Ok(gzipped) = encoder.finish() else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 153 | return Response::from_parts(parts, Body::from(bytes)); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 154 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 155 | parts |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 156 | .headers |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 157 | .insert(header::CONTENT_ENCODING, HeaderValue::from_static("gzip")); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 158 | parts.headers.remove(header::CONTENT_LENGTH); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 159 | parts |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 160 | .headers |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 161 | .append(header::VARY, HeaderValue::from_static("Accept-Encoding")); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 162 | Response::from_parts(parts, Body::from(gzipped)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 163 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 164 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 165 | async fn chrome(request: Request, next: Next) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 166 | let gzip = header_str(request.headers(), header::ACCEPT_ENCODING) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 167 | .is_some_and(|e| e.to_ascii_lowercase().contains("gzip")); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 168 | let mut response = match reject(&request) { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 169 | Some(problem) => problem.into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 170 | None => next.run(request).await, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 171 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 172 | if gzip && response.extensions().get::<Compress>().is_some() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 173 | response = compress(response).await; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 174 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 175 | let headers = response.headers_mut(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 176 | headers.insert(header::SERVER, HeaderValue::from_static("jelqd/0.6.9")); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 177 | headers.insert("x-powered-by", HeaderValue::from_static("blood")); |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 178 | headers |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 179 | .entry(header::CACHE_CONTROL) |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 180 | .or_insert(HeaderValue::from_static("no-store, private")); |
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()), |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 313 | ( |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 314 | "x-stamina", |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 315 | format!("{}/{}", run.stamina, run.max_stamina()), |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 316 | ), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 317 | ( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 318 | "x-suspicion", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 319 | format!("{}/{SUSPICION_LIMIT}", run.suspicion), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 320 | ), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 321 | ("x-shame", run.total_shame().to_string()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 322 | ("x-cash", run.cash.to_string()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 323 | ("x-floor", run.floor.number().to_string()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 324 | ("link", links(run)), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 325 | ]; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 326 | if let Some(observer) = run.observed { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 327 | stats.push(("x-observed-by", observer.into())); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 328 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 329 | if run.mom_present { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 330 | stats.push(("via", "1.1 mom".into())); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 331 | stats.push(("vary", "Mom".into())); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 332 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 333 | // RFC 9111 obsoleted Warning, which is why nobody heeds it. |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 334 | if run.pulled_muscle { |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 335 | stats.push(("warning", "199 - \"a muscle has been pulled\"".into())); |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 336 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 337 | if run.eq > 70 && run.ended.is_none() { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 338 | stats.push(("warning", "199 - \"it is turning purple\"".into())); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 339 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 340 | stats.push(( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 341 | "set-cookie", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 342 | format!( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 343 | "shame={}; {COOKIE_ATTRIBUTES}", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 344 | secret::sign("shame", &run.total_shame().to_string()) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 345 | ), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 346 | )); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 347 | if run.items.ruler { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 348 | stats.push(( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 349 | "set-cookie", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 350 | format!("ruler={}; {COOKIE_ATTRIBUTES}", secret::sign("ruler", "1")), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 351 | )); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 352 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 353 | let headers = response.headers_mut(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 354 | for (name, value) in stats { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 355 | headers.append(name, HeaderValue::from_str(&value).expect("visible ascii")); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 356 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 357 | let bodiless = status.is_informational() || status == StatusCode::NO_CONTENT; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 358 | if run.items.ring && !bodiless { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 359 | response.extensions_mut().insert(Compress); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 360 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 361 | response |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 362 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 363 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 364 | fn gone(run: &Run) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 365 | let ending = run.ended.as_ref().expect("only called on ended runs"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 366 | Problem::new(StatusCode::GONE, "run-ended", "This run has ended.") |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 367 | .with("cause", format!("{PROBLEM_BASE}{}", ending.cause)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 368 | .with("turn", ending.turn) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 369 | .with("graveyard", "/graveyard") |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 370 | .into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 371 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 372 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 373 | fn captive_portal(run: &Run) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 374 | Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 375 | StatusCode::NETWORK_AUTHENTICATION_REQUIRED, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 376 | "captive-portal", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 377 | "The network requires authentication. The hotel would like you to agree to some terms.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 378 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 379 | .with("portal", format!("/runs/{}/portal", run.id)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 380 | .header( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 381 | header::LINK, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 382 | format!("</runs/{}/portal>; rel=\"{REL_BASE}portal\"", run.id), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 383 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 384 | .into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 385 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 386 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 387 | fn with_run( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 388 | state: &AppState, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 389 | id: &str, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 390 | headers: &HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 391 | scope: Scope, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 392 | handle: impl FnOnce(&mut Run) -> Response, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 393 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 394 | let mut runs = state.runs.lock().unwrap(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 395 | let Some(run) = runs.get_mut(id) else { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 396 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 397 | StatusCode::NOT_FOUND, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 398 | "no-such-run", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 399 | format!("There is no run {id}. There may never have been."), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 400 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 401 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 402 | }; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 403 | if run.ended.is_some() { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 404 | return gone(run); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 405 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 406 | run.observed = None; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 407 | if scope == Scope::Home { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 408 | if run.floor == Floor::Hotel && !run.wifi_accepted { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 409 | return finish(run, captive_portal(run)); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 410 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 411 | if run.floor.has_mom() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 412 | && run.mom_present |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 413 | && let Err(problem) = run.answer_door(header_str(headers, header::AUTHORIZATION)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 414 | { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 415 | return finish(run, problem.into_response()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 416 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 417 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 418 | let response = handle(run); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 419 | finish(run, 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 | fn respond<T: IntoResponse, E: IntoResponse>(result: Result<T, E>) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 423 | match result { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 424 | Ok(ok) => ok.into_response(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 425 | Err(err) => err.into_response(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 426 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 427 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 428 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 429 | async fn runs( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 430 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 431 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 432 | Query(query): Query<HashMap<String, String>>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 433 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 434 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 435 | if method != Method::POST { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 436 | return unsupported(&method, "POST"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 437 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 438 | let seeded = query.contains_key("seed"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 439 | let seed = match query.get("seed").map(|s| s.parse::<u64>()) { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 440 | None => rand::rng().random(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 441 | Some(Ok(seed)) => seed, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 442 | Some(Err(_)) => { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 443 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 444 | StatusCode::BAD_REQUEST, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 445 | "bad-seed", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 446 | "The seed must be an unsigned 64-bit integer.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 447 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 448 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 449 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 450 | }; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 451 | let shame = cookie(&headers, "shame") |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 452 | .and_then(|s| secret::verify("shame", s)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 453 | .and_then(|s| s.parse().ok()) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 454 | .unwrap_or(0); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 455 | let id = format!("{:08x}", rand::rng().random::<u32>()); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 456 | let mut run = Run::new(id.clone(), secret::mix_seed(seed), shame); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 457 | run.seeded = seeded; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 458 | let response = ( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 459 | StatusCode::CREATED, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 460 | [ |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 461 | (header::LOCATION, format!("/runs/{id}")), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 462 | (header::ETAG, run.etag()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 463 | (HeaderName::from_static("x-seed"), seed.to_string()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 464 | ], |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 465 | Json(run.view()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 466 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 467 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 468 | let response = finish(&run, response); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 469 | state.insert(run); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 470 | response |
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 | #[allow(clippy::too_many_arguments)] |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 474 | async fn run( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 475 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 476 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 477 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 478 | uri: Uri, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 479 | version: Version, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 480 | Query(query): Query<HashMap<String, String>>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 481 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 482 | body: Bytes, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 483 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 484 | let now = Instant::now(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 485 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 486 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 487 | "GET" | "HEAD" => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 488 | let accept = header_str(&headers, header::ACCEPT).unwrap_or_default(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 489 | if accept.contains("reassurance") { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 490 | return Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 491 | StatusCode::NOT_ACCEPTABLE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 492 | "no-reassurance", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 493 | "The server does not produce reassurance.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 494 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 495 | .into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 496 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 497 | ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 498 | StatusCode::OK, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 499 | [(header::ETAG, run.etag())], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 500 | Json(run.view()), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 501 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 502 | .into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 503 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 504 | "JELQ" => jelq(run, &query, &headers, now), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 505 | "PUT" => run.put().into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 506 | "PATCH" => patch(run, &headers, &body), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 507 | "DELETE" => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 508 | run.end("abandoned", "The run was abandoned."); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 509 | StatusCode::NO_CONTENT.into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 510 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 511 | "OPTIONS" => options(RUN_ALLOW, true), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 512 | "TRACE" => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 513 | run.shame += 1; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 514 | trace(&method, &uri, version, &headers) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 515 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 516 | _ => unsupported(&method, RUN_ALLOW), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 517 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 518 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 519 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 520 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 521 | fn jelq( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 522 | run: &mut Run, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 523 | query: &HashMap<String, String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 524 | headers: &HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 525 | now: Instant, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 526 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 527 | let techniques_href = format!("/runs/{}/techniques", run.id); |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 528 | let Some(names) = query.get("technique").filter(|n| !n.trim().is_empty()) else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 529 | return Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 530 | StatusCode::BAD_REQUEST, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 531 | "technique-required", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 532 | "No technique was specified.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 533 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 534 | .with("techniques", techniques_href) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 535 | .into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 536 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 537 | let mut techniques = Vec::new(); |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 538 | for name in names.split(',').map(str::trim).filter(|n| !n.is_empty()) { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 539 | let Some(technique) = Technique::parse(name) else { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 540 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 541 | StatusCode::BAD_REQUEST, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 542 | "unknown-technique", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 543 | format!("{name} is not a technique."), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 544 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 545 | .with("techniques", techniques_href) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 546 | .into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 547 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 548 | techniques.push(technique); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 549 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 550 | let intensity = match query.get("intensity").map(|i| i.parse::<i32>()) { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 551 | None => 5, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 552 | Some(Ok(i)) if (1..=10).contains(&i) => i, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 553 | Some(_) => { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 554 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 555 | StatusCode::BAD_REQUEST, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 556 | "intensity-out-of-range", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 557 | "Intensity is measured from 1 to 10.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 558 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 559 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 560 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 561 | }; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 562 | let lube = match Lube::from_content_type(header_str(headers, header::CONTENT_TYPE)) { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 563 | Ok(lube) => lube, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 564 | Err(problem) => return problem.into_response(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 565 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 566 | respond(run.jelq(&techniques, intensity, lube, now)) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 567 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 568 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 569 | fn patch(run: &mut Run, headers: &HeaderMap, body: &[u8]) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 570 | let content_type = header_str(headers, header::CONTENT_TYPE).unwrap_or_default(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 571 | if !content_type.starts_with("application/json-patch+json") { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 572 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 573 | StatusCode::UNSUPPORTED_MEDIA_TYPE, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 574 | "patch-format", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 575 | "Patches are accepted as application/json-patch+json.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 576 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 577 | .header( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 578 | HeaderName::from_static("accept-patch"), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 579 | "application/json-patch+json", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 580 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 581 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 582 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 583 | match serde_json::from_slice::<Vec<PatchOp>>(body) { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 584 | Ok(ops) => respond(run.patch(&ops)), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 585 | Err(_) => Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 586 | StatusCode::BAD_REQUEST, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 587 | "malformed-patch", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 588 | "The patch could not be parsed.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 589 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 590 | .into_response(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 591 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 592 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 593 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 594 | fn millimetres(inches: f64) -> usize { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 595 | (inches * MM_PER_INCH).round() as usize |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 596 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 597 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 598 | fn parse_inch_range(range: &str, total: f64) -> Option<(f64, f64)> { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 599 | let (start, end) = range.strip_prefix("inches=")?.split_once('-')?; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 600 | let start: f64 = start.trim().parse().ok()?; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 601 | let end: f64 = match end.trim() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 602 | "" => total, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 603 | end => end.parse().ok()?, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 604 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 605 | (start >= 0.0 && start < total && start <= end).then_some((start, end.min(total))) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 606 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 607 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 608 | fn length_response(run: &Run, headers: &HeaderMap) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 609 | let inches = if run.items.ruler { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 610 | run.current_length() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 611 | } else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 612 | run.self_reported() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 613 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 614 | let body = shaft(millimetres(inches)); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 615 | let content_type = ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 616 | header::CONTENT_TYPE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 617 | "text/plain; charset=us-ascii".to_owned(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 618 | ); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 619 | let accept_ranges = (header::ACCEPT_RANGES, "inches".to_owned()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 620 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 621 | if let Some(range) = header_str(headers, header::RANGE) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 622 | && range.starts_with("inches=") |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 623 | { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 624 | let Some((start, end)) = parse_inch_range(range, inches) else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 625 | return Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 626 | StatusCode::RANGE_NOT_SATISFIABLE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 627 | "range-not-satisfiable", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 628 | format!("The requested range is not available. {inches:.2} inches are."), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 629 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 630 | .header(header::CONTENT_RANGE, format!("inches */{inches:.2}")) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 631 | .into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 632 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 633 | let from = millimetres(start).min(body.len() - 1); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 634 | let to = millimetres(end).clamp(from + 1, body.len()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 635 | let content_range = ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 636 | header::CONTENT_RANGE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 637 | format!("inches {start:.2}-{end:.2}/{inches:.2}"), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 638 | ); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 639 | return ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 640 | StatusCode::PARTIAL_CONTENT, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 641 | [content_type, accept_ranges, content_range], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 642 | body[from..to].to_owned(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 643 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 644 | .into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 645 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 646 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 647 | if run.items.ruler { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 648 | return (StatusCode::OK, [content_type, accept_ranges], body).into_response(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 649 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 650 | let chunks: Vec<Result<Bytes, Infallible>> = body |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 651 | .into_bytes() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 652 | .chunks(16) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 653 | .map(|c| Ok(Bytes::copy_from_slice(c))) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 654 | .collect(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 655 | let body = Body::from_stream(futures_util::stream::iter(chunks)); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 656 | (StatusCode::OK, [content_type, accept_ranges], body).into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 657 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 658 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 659 | async fn length( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 660 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 661 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 662 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 663 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 664 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 665 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 666 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 667 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 668 | "GET" | "HEAD" => length_response(run, &headers), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 669 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 670 | _ => unsupported(&method, ALLOW), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 671 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 672 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 673 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 674 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 675 | async fn foreskin( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 676 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 677 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 678 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 679 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 680 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 681 | const ALLOW: &str = "GET, HEAD, DELETE, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 682 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 683 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 684 | "GET" | "HEAD" => match run.foreskin { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 685 | Some(f) => ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 686 | StatusCode::OK, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 687 | [(header::ETAG, run.etag())], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 688 | Json(json!({ "foreskin": (f * 100.0).round() / 100.0 })), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 689 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 690 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 691 | None => Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 692 | StatusCode::GONE, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 693 | "foreskin-gone", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 694 | "The foreskin is gone. It will not be returning.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 695 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 696 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 697 | }, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 698 | "DELETE" => respond(run.delete_foreskin(header_str(&headers, header::IF_MATCH))), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 699 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 700 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 701 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 702 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 703 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 704 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 705 | async fn inventory( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 706 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 707 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 708 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 709 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 710 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 711 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 712 | with_run(&state, &id, &headers, Scope::Pockets, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 713 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 714 | "GET" | "HEAD" => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 715 | let listed: Vec<Value> = run |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 716 | .inventory() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 717 | .into_iter() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 718 | .map(|(name, _)| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 719 | json!({ "name": name, "href": format!("/runs/{}/inventory/{name}", run.id) }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 720 | }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 721 | .collect(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 722 | Json(json!({ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 723 | "items": listed, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 724 | "ice_packs": run.items.ice_packs, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 725 | "cash": run.cash, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 726 | "excuses": run.excuses(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 727 | })) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 728 | .into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 729 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 730 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 731 | _ => unsupported(&method, ALLOW), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 732 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 733 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 734 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 735 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 736 | async fn item( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 737 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 738 | Path((id, name)): Path<(String, String)>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 739 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 740 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 741 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 742 | const ALLOW: &str = "GET, HEAD, DELETE, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 743 | with_run(&state, &id, &headers, Scope::Pockets, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 744 | let Some((name, description)) = run.inventory().into_iter().find(|(n, _)| *n == name) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 745 | else { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 746 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 747 | StatusCode::NOT_FOUND, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 748 | "no-such-item", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 749 | "You do not have that.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 750 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 751 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 752 | }; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 753 | match method.as_str() { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 754 | "GET" | "HEAD" => { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 755 | Json(json!({ "name": name, "description": description })).into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 756 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 757 | "DELETE" => respond(run.drop_item(name)), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 758 | "OPTIONS" => options(ALLOW, false), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 759 | _ => unsupported(&method, ALLOW), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 760 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 761 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 762 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 763 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 764 | async fn techniques( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 765 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 766 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 767 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 768 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 769 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 770 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 771 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 772 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 773 | "GET" | "HEAD" => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 774 | let listed: Vec<Value> = Technique::ALL |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 775 | .into_iter() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 776 | .map(|t| t.describe(run.forum_read)) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 777 | .collect(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 778 | Json(json!({ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 779 | "usage": format!("JELQ /runs/{}?technique=<name>[,<name>]&intensity=<1-10>", run.id), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 780 | "lubricant": "Declared with Content-Type.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 781 | "lubricants": LUBRICANTS, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 782 | "eq_band": [50, 70], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 783 | "techniques": listed, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 784 | })) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 785 | .into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 786 | } |
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), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 789 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 790 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 791 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 792 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 793 | async fn drawer( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 794 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 795 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 796 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 797 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 798 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 799 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 800 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 801 | "POST" => respond(run.search_drawer()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 802 | "OPTIONS" => options("POST, OPTIONS", false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 803 | _ => unsupported(&method, "POST, OPTIONS"), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 804 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 805 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 806 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 807 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 808 | async fn branch( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 809 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 810 | Path((id, choice)): Path<(String, String)>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 811 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 812 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 813 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 814 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 815 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 816 | "POST" => respond(run.choose(&choice)), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 817 | "OPTIONS" => options("POST, OPTIONS", false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 818 | _ => unsupported(&method, "POST, OPTIONS"), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 819 | } |
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 | async fn floor( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 824 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 825 | Path(id): Path<String>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 826 | method: Method, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 827 | headers: HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 828 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 829 | const ALLOW: &str = "GET, HEAD, POST, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 830 | with_run(&state, &id, &headers, Scope::Pockets, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 831 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 832 | "GET" | "HEAD" => Json(run.floor_view()).into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 833 | "POST" => respond(run.advance()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 834 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 835 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 836 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 837 | }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 838 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 839 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 840 | async fn shop( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 841 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 842 | Path(id): Path<String>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 843 | method: Method, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 844 | headers: HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 845 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 846 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 847 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 848 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 849 | "GET" | "HEAD" => respond(run.shop_view().map(Json)), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 850 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 851 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 852 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 853 | }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 854 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 855 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 856 | async fn buy( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 857 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 858 | Path((id, item)): Path<(String, String)>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 859 | method: Method, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 860 | headers: HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 861 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 862 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 863 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 864 | "POST" => respond(run.buy(&item)), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 865 | "OPTIONS" => options("POST, OPTIONS", false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 866 | _ => unsupported(&method, "POST, OPTIONS"), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 867 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 868 | }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 869 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 870 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 871 | async fn portal( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 872 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 873 | Path(id): Path<String>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 874 | method: Method, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 875 | headers: HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 876 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 877 | const ALLOW: &str = "GET, HEAD, POST, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 878 | with_run(&state, &id, &headers, Scope::Pockets, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 879 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 880 | "GET" | "HEAD" => Json(json!({ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 881 | "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 | 882 | "accept": { "href": format!("/runs/{}/portal", run.id), "method": "POST" }, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 883 | })) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 884 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 885 | "POST" => respond(run.accept_terms()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 886 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 887 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 888 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 889 | }) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 890 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 891 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 892 | async fn elder( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 893 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 894 | Path(id): Path<String>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 895 | method: Method, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 896 | headers: HeaderMap, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 897 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 898 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 899 | with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 900 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 901 | "GET" | "HEAD" => respond(run.elder( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 902 | header_str(&headers, header::PROXY_AUTHORIZATION), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 903 | header_str(&headers, header::IF_MODIFIED_SINCE), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 904 | )), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 905 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 906 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 907 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 908 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 909 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 910 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 911 | fn requested_grip(headers: &HeaderMap) -> Option<Grip> { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 912 | let connection = header_str(headers, header::CONNECTION)?.to_ascii_lowercase(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 913 | if !connection.split(',').any(|token| token.trim() == "upgrade") { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 914 | return None; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 915 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 916 | header_str(headers, header::UPGRADE)? |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 917 | .split(',') |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 918 | .find_map(|protocol| Grip::parse(protocol.trim())) |
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 | async fn grip( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 922 | State(state): State<Shared>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 923 | Path(id): Path<String>, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 924 | mut request: Request, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 925 | ) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 926 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 927 | let method = request.method().clone(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 928 | let headers = request.headers().clone(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 929 | let wanted = requested_grip(&headers); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 930 | let upgrade = wanted.map(|_| hyper::upgrade::on(&mut request)); |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 931 | let detail = match header_str(&headers, header::UPGRADE) { |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 932 | Some(protocol) if wanted.is_none() => { |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 933 | format!("The server does not know the {protocol} grip. Grips are protocols.") |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 934 | } |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 935 | _ => "Grips are protocols. Change grip by upgrading the connection.".to_owned(), |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 936 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 937 | let response = with_run(&state, &id, &headers, Scope::Home, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 938 | match (method.as_str(), wanted) { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 939 | ("GET" | "HEAD", Some(grip)) => { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 940 | run.set_grip(grip); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 941 | ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 942 | StatusCode::SWITCHING_PROTOCOLS, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 943 | [ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 944 | (header::UPGRADE, grip.name()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 945 | (header::CONNECTION, "upgrade"), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 946 | ], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 947 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 948 | .into_response() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 949 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 950 | ("GET" | "HEAD", None) => ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 951 | StatusCode::OK, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 952 | [(header::UPGRADE, "overhand, underhand")], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 953 | Json(json!({ |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 954 | "grip": run.grip.name(), |
177535ac8abf | atagen | 2026-10-10 10:39 UTC | 955 | "detail": detail, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 956 | })), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 957 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 958 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 959 | ("OPTIONS", _) => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 960 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 961 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 962 | }); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 963 | if response.status() == StatusCode::SWITCHING_PROTOCOLS |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 964 | && let (Some(upgrade), Some(grip)) = (upgrade, wanted) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 965 | { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 966 | tokio::spawn(async move { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 967 | if let Ok(upgraded) = upgrade.await { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 968 | let mut io = TokioIo::new(upgraded); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 969 | let line = format!("You are now jelqing {}.\r\n", grip.name()); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 970 | let _ = io.write_all(line.as_bytes()).await; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 971 | let _ = io.shutdown().await; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 972 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 973 | }); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 974 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 975 | response |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 976 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 977 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 978 | async fn diagnosis( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 979 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 980 | Path(id): Path<String>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 981 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 982 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 983 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 984 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 985 | with_run(&state, &id, &headers, Scope::Pockets, |run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 986 | match method.as_str() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 987 | "GET" | "HEAD" => respond(run.diagnose( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 988 | header_str(&headers, header::AUTHORIZATION), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 989 | header_str(&headers, header::IF_NONE_MATCH), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 990 | )), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 991 | "OPTIONS" => options(ALLOW, false), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 992 | _ => unsupported(&method, ALLOW), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 993 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 994 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 995 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 996 | |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 997 | async fn footage( |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 998 | State(state): State<Shared>, |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 999 | Path(id): Path<String>, |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1000 | method: Method, |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1001 | headers: HeaderMap, |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1002 | ) -> Response { |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1003 | const ALLOW: &str = "GET, HEAD, OPTIONS"; |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1004 | with_run(&state, &id, &headers, Scope::Pockets, |run| { |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1005 | match method.as_str() { |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1006 | "GET" | "HEAD" => { |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1007 | let Some((filmed, age)) = run.footage() else { |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1008 | return Problem::new( |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1009 | StatusCode::NOT_FOUND, |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1010 | "no-footage", |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1011 | "There is no footage. Yet.", |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1012 | ) |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1013 | .into_response(); |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1014 | }; |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1015 | ( |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1016 | [ |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1017 | (header::CACHE_CONTROL, "public, max-age=31536000, immutable".to_owned()), |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1018 | (header::AGE, age.to_string()), |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1019 | (header::LAST_MODIFIED, httpdate::fmt_http_date(filmed)), |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1020 | ], |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1021 | Json(json!({ |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1022 | "detail": "A recording. It is grainy. It is unmistakably you. It has been shared.", |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1023 | "views": 1 + age * 37, |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1024 | })), |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1025 | ) |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1026 | .into_response() |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1027 | } |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1028 | "DELETE" => Problem::new( |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1029 | StatusCode::METHOD_NOT_ALLOWED, |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1030 | "the-internet", |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1031 | "The footage has been cached by intermediaries. It cannot be deleted.", |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1032 | ) |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1033 | .header(header::ALLOW, ALLOW) |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1034 | .into_response(), |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1035 | "OPTIONS" => options(ALLOW, false), |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1036 | _ => unsupported(&method, ALLOW), |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1037 | } |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1038 | }) |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1039 | } |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1040 | |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1041 | async fn thread(Path(year): Path<String>, method: Method) -> Response { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1042 | if method != Method::GET && method != Method::HEAD { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1043 | return unsupported(&method, "GET, HEAD"); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1044 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1045 | match year.parse::<u32>() { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1046 | Ok(2009) => ( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1047 | [(header::LAST_MODIFIED, ELDER_LAST_MODIFIED)], |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1048 | Json(forum_thread()), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1049 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1050 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1051 | Ok(y) if y > 2009 => Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1052 | StatusCode::NOT_FOUND, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1053 | "no-thread", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1054 | "Nobody has posted since 2009.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1055 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1056 | .with("thread", FORUM_THREAD) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1057 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1058 | _ => Problem::new( |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1059 | StatusCode::NOT_FOUND, |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1060 | "no-thread", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1061 | "The forum did not exist yet. The problem did.", |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1062 | ) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1063 | .with("thread", FORUM_THREAD) |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1064 | .into_response(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1065 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1066 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1067 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1068 | async fn graveyard(State(state): State<Shared>, method: Method) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1069 | if method != Method::GET && method != Method::HEAD { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1070 | return unsupported(&method, "GET, HEAD"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1071 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1072 | let runs = state.runs.lock().unwrap(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1073 | let dead: Vec<Value> = runs |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1074 | .values() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1075 | .filter_map(|run| { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1076 | let ending = run.ended.as_ref()?; |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1077 | Some(json!({ |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1078 | "id": run.id, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1079 | "cause": format!("{PROBLEM_BASE}{}", ending.cause), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1080 | "detail": ending.detail, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1081 | "turn": ending.turn, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1082 | })) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1083 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1084 | .collect(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1085 | Json(json!({ "runs": dead })).into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1086 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1087 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1088 | async fn leaderboard( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1089 | State(state): State<Shared>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1090 | method: Method, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1091 | Query(query): Query<HashMap<String, String>>, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1092 | headers: HeaderMap, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1093 | ) -> Response { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1094 | if method != Method::GET && method != Method::HEAD { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1095 | return unsupported(&method, "GET, HEAD"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1096 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1097 | let truthful = match query.get("sort").map(String::as_str) { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1098 | None | Some("self-reported-length") => false, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1099 | Some("content-length") => match cookie(&headers, "ruler") { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1100 | Some(ruler) if secret::verify("ruler", ruler) == Some("1") => true, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1101 | Some(_) => { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1102 | return Problem::new( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1103 | StatusCode::BAD_REQUEST, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1104 | "exaggeration", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1105 | "The server has measured. Lying is not a technique.", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1106 | ) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1107 | .with("cookie", "ruler") |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1108 | .into_response(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1109 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1110 | None => { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1111 | return Problem::new( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1112 | StatusCode::FORBIDDEN, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1113 | "ruler-required", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1114 | "Sorting by the truth requires a Ruler.", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1115 | ) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1116 | .into_response(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1117 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1118 | }, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1119 | Some(_) => { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1120 | return Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1121 | StatusCode::BAD_REQUEST, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1122 | "unknown-sort", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1123 | "The leaderboard sorts by self-reported-length or content-length.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1124 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1125 | .into_response(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1126 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1127 | }; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1128 | let include_seeded = query.get("seeded").is_some_and(|s| s == "include"); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1129 | let runs = state.runs.lock().unwrap(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1130 | let mut entries: Vec<&Run> = runs |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1131 | .values() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1132 | .filter(|run| include_seeded || !run.seeded) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1133 | .collect(); |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1134 | let key = |r: &Run| { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1135 | if truthful { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1136 | r.current_length() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1137 | } else { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1138 | r.self_reported() |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1139 | } |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1140 | }; |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1141 | entries.sort_by(|a, b| key(b).total_cmp(&key(a))); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1142 | let entries: Vec<Value> = entries |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1143 | .into_iter() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1144 | .map(|run| { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1145 | let mut entry = json!({ |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1146 | "id": run.id, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1147 | "status": run.status(), |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1148 | "floor": run.floor.number(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1149 | "self_reported_length": (run.self_reported() * 100.0).round() / 100.0, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1150 | "ranked": !run.seeded, |
c3e0c27cf8fd | atagen | 2026-10-10 11:28 UTC | 1151 | "viral": run.filmed_at.is_some(), |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1152 | }); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1153 | if truthful { |
dfa38cf4cd4b | atagen | 2026-10-10 09:17 UTC | 1154 | entry["content_length"] = json!(millimetres(run.current_length())); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1155 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1156 | entry |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1157 | }) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1158 | .collect(); |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1159 | Json(json!({ "entries": entries })).into_response() |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1160 | } |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1161 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1162 | async fn index(method: Method, headers: HeaderMap) -> Response { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1163 | if method != Method::GET && method != Method::HEAD { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1164 | return unsupported(&method, "GET, HEAD"); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1165 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1166 | let wants_html = |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1167 | header_str(&headers, header::ACCEPT).is_some_and(|accept| accept.contains("text/html")); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1168 | if wants_html { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1169 | return ( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1170 | StatusCode::SEE_OTHER, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1171 | [(header::LOCATION, "/ui/"), (header::VARY, "Accept")], |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1172 | ) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1173 | .into_response(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1174 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1175 | ( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1176 | [(header::VARY, "Accept")], |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1177 | Json(json!({ |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1178 | "detail": "This is jelqd. It is a roguelike. It is played over HTTP.", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1179 | "start": { "href": "/runs", "method": "POST" }, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1180 | "leaderboard": "/leaderboard", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1181 | "graveyard": "/graveyard", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1182 | "forum": FORUM_THREAD, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1183 | "ui": "/ui/", |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1184 | })), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1185 | ) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1186 | .into_response() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1187 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1188 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1189 | async fn ui(method: Method) -> Response { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1190 | if method != Method::GET && method != Method::HEAD { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1191 | return unsupported(&method, "GET, HEAD"); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1192 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1193 | Html(include_str!("ui.html")).into_response() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1194 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1195 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1196 | #[derive(Deserialize)] |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1197 | struct Envelope { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1198 | method: String, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1199 | path: String, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1200 | #[serde(default)] |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1201 | headers: Vec<(String, String)>, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1202 | #[serde(default)] |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1203 | body: String, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1204 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1205 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1206 | fn bad_envelope(detail: &str) -> Response { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1207 | Problem::new(StatusCode::BAD_REQUEST, "bad-envelope", detail).into_response() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1208 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1209 | |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1210 | // Browsers hide 3xx responses from fetch and refuse TRACE, Upgrade, and Expect, so the page |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1211 | // replays requests in-process to show the player exactly what the API said. |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1212 | async fn ui_send(State(api): State<Router>, headers: HeaderMap, body: Bytes) -> Response { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1213 | let Ok(envelope) = serde_json::from_slice::<Envelope>(&body) else { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1214 | return bad_envelope("The envelope could not be parsed."); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1215 | }; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1216 | if !envelope.path.starts_with('/') || envelope.path.starts_with("/ui") { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1217 | return bad_envelope("The path must be an API path."); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1218 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1219 | let Ok(method) = Method::from_bytes(envelope.method.as_bytes()) else { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1220 | return bad_envelope("That is not a method."); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1221 | }; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1222 | let mut request = Request::builder().method(method).uri(&envelope.path); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1223 | for (name, value) in envelope.headers.iter().filter(|(_, v)| !v.is_empty()) { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1224 | request = request.header(name, value); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1225 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1226 | for cookie in headers.get_all(header::COOKIE) { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1227 | request = request.header(header::COOKIE, cookie); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1228 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1229 | let Ok(request) = request.body(Body::from(envelope.body)) else { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1230 | return bad_envelope("The request could not be built. A header may be malformed."); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1231 | }; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1232 | let Ok(response) = api.oneshot(request).await; |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1233 | let (parts, body) = response.into_parts(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1234 | let bytes = axum::body::to_bytes(body, 1 << 20) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1235 | .await |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1236 | .unwrap_or_default(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1237 | let listed: Vec<(String, String)> = parts |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1238 | .headers |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1239 | .iter() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1240 | .map(|(name, value)| { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1241 | ( |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1242 | name.to_string(), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1243 | value.to_str().unwrap_or("<binary>").to_owned(), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1244 | ) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1245 | }) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1246 | .collect(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1247 | let mut response = Json(json!({ |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1248 | "status": parts.status.as_u16(), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1249 | "reason": reason(parts.status), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1250 | "headers": listed, |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1251 | "body": String::from_utf8_lossy(&bytes), |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1252 | })) |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1253 | .into_response(); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1254 | for cookie in parts.headers.get_all(header::SET_COOKIE) { |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1255 | response |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1256 | .headers_mut() |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1257 | .append(header::SET_COOKIE, cookie.clone()); |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1258 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1259 | response |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1260 | } |
99524cf9f17c | atagen | 2026-10-10 10:53 UTC | 1261 | |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1262 | async fn not_found() -> Problem { |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1263 | Problem::new( |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1264 | StatusCode::NOT_FOUND, |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1265 | "not-found", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1266 | "The resource does not exist. This is not about you.", |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1267 | ) |
a9b016e4b248 | atagen | 2026-10-10 08:12 UTC | 1268 | } |