an http based roguelike rpg about jelqing
Blame: src/http/ui.rs
782069860236 | atagen | 2026-10-10 12:44 UTC | 1 | use std::io::Read as _; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 2 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 3 | use axum::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 4 | Json, Router, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | body::{Body, Bytes}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | extract::{Request, State}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | http::{HeaderMap, HeaderValue, Method, StatusCode, header}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 8 | response::{Html, IntoResponse, Response}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | use flate2::read::GzDecoder; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 11 | use serde::Deserialize; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 12 | use serde_json::json; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 13 | use tower::ServiceExt; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 14 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 15 | use super::{ctx::BODY_LIMIT, resource::unsupported}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 16 | use crate::problem::{Problem, reason}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 17 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 18 | const RESPONSE_LIMIT: usize = 1 << 20; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 19 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 20 | #[derive(Deserialize)] |
782069860236 | atagen | 2026-10-10 12:44 UTC | 21 | struct Envelope { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 22 | method: String, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 23 | path: String, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 24 | #[serde(default)] |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | headers: Vec<(String, String)>, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | #[serde(default)] |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | body: String, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 29 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 30 | pub async fn page(method: Method) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 31 | if method != Method::GET && method != Method::HEAD { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 32 | return unsupported(&method, "GET, HEAD, OPTIONS"); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 33 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 34 | Html(include_str!("../ui.html")).into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 36 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | fn bad_envelope(detail: &str) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | Problem::new(StatusCode::BAD_REQUEST, "bad-envelope", detail).into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 39 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 40 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 41 | fn decode(headers: &HeaderMap, bytes: Bytes) -> Vec<u8> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 42 | let gzipped = headers.get(header::CONTENT_ENCODING) == Some(&HeaderValue::from_static("gzip")); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | let mut decoded = Vec::new(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | if gzipped |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | && GzDecoder::new(bytes.as_ref()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 46 | .read_to_end(&mut decoded) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 47 | .is_ok() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 48 | { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 49 | return decoded; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 50 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 51 | bytes.to_vec() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 52 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 53 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 54 | // Browsers hide 3xx responses from fetch and refuse TRACE, Upgrade, and Expect, so the page |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | // replays requests in-process to show the player exactly what the API said. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | pub async fn send(State(api): State<Router>, headers: HeaderMap, body: Bytes) -> Response { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 57 | if body.len() > BODY_LIMIT { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 58 | return bad_envelope("The envelope is larger than any request the server would take."); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 59 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 60 | let Ok(envelope) = serde_json::from_slice::<Envelope>(&body) else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 61 | return bad_envelope("The envelope could not be parsed."); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 62 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 63 | if !envelope.path.starts_with('/') || envelope.path.starts_with("/ui") { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 64 | return bad_envelope("The path must be an API path."); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 65 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 66 | let Ok(method) = Method::from_bytes(envelope.method.as_bytes()) else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 67 | return bad_envelope("That is not a method."); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 68 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 69 | let mut request = Request::builder().method(method).uri(&envelope.path); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 70 | for (name, value) in envelope.headers.iter().filter(|(_, v)| !v.is_empty()) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 71 | request = request.header(name, value); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 72 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 73 | for cookie in headers.get_all(header::COOKIE) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 74 | request = request.header(header::COOKIE, cookie); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 75 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 76 | let Ok(request) = request.body(Body::from(envelope.body)) else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 77 | return bad_envelope("The request could not be built. A header may be malformed."); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 78 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 79 | let Ok(response) = api.oneshot(request).await; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 80 | let (parts, body) = response.into_parts(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 81 | let bytes = axum::body::to_bytes(body, RESPONSE_LIMIT) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 82 | .await |
782069860236 | atagen | 2026-10-10 12:44 UTC | 83 | .unwrap_or_default(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 84 | let shown = decode(&parts.headers, bytes); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 85 | let listed: Vec<(String, String)> = parts |
782069860236 | atagen | 2026-10-10 12:44 UTC | 86 | .headers |
782069860236 | atagen | 2026-10-10 12:44 UTC | 87 | .iter() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 88 | .map(|(name, value)| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 89 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 90 | name.to_string(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 91 | value.to_str().unwrap_or("<binary>").to_owned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 92 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 93 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 94 | .collect(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 95 | let mut response = Json(json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 96 | "status": parts.status.as_u16(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 97 | "reason": reason(parts.status), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 98 | "headers": listed, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 99 | "body": String::from_utf8_lossy(&shown), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 100 | })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 101 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 102 | for cookie in parts.headers.get_all(header::SET_COOKIE) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 103 | response |
782069860236 | atagen | 2026-10-10 12:44 UTC | 104 | .headers_mut() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 105 | .append(header::SET_COOKIE, cookie.clone()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 106 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 107 | response |
782069860236 | atagen | 2026-10-10 12:44 UTC | 108 | } |