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