summaryrefslogtreecommitdiffstats
path: root/src/http/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/http/mod.rs')
-rw-r--r--src/http/mod.rs44
1 files changed, 14 insertions, 30 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs
index 81c526c..11ea107 100644
--- a/src/http/mod.rs
+++ b/src/http/mod.rs
@@ -1,16 +1,3 @@
-//! The web surface (§12).
-//!
-//! `tiny_http`, thread-per-connection, blocking. Handlers **read** through the
-//! thread-local read-only connections and never touch the write connection;
-//! mutations are handed to the writer actor as a transaction request. That is
-//! what keeps a slow browser from ever holding the database lock.
-//!
-//! Two guards on every POST: a session id, and a CSRF token that must match
-//! it. The session id is the only credential the browser holds, and it counts
-//! as authentication only if someone typed the password into a form on that
-//! exact session (§12). Forms are plain `<form method=post>`, so the whole
-//! approval flow works with JavaScript switched off.
-
mod auth;
mod forms;
mod render;
@@ -36,10 +23,8 @@ pub struct Req {
pub body: String,
pub cookies: Vec<(String, String)>,
pub query_pairs: Vec<(String, String)>,
- /// The body's form pairs, parsed once when the request is decoded (n-3).
- /// `form_get` used to re-parse the whole body on every call, and a single
- /// handler — approve, with its two hashes, its note and its CSRF token —
- /// asks five times.
+ /// The body's form pairs, parsed once when the request is decoded (n-3) — a
+ /// single handler asks for five of them.
pub form_pairs: Vec<(String, String)>,
/// Whether the client said it can take gzip. Used to decide whether to hand
/// out the precompressed static asset.
@@ -67,9 +52,9 @@ pub struct Res {
pub content_type: String,
pub body: Vec<u8>,
pub headers: Vec<(String, String)>,
- /// When set, the response body is produced by reading this to EOF instead of
- /// `body`. Used for the log tail (§13), which must stream bytes as a build
- /// appends them rather than buffering a response that never finishes.
+ /// When set, the body is produced by reading this to EOF instead of `body`.
+ /// Used for the log tail (§13), which must stream bytes as a build appends
+ /// them rather than buffering a response that never finishes.
pub stream: Option<Box<dyn std::io::Read + Send>>,
/// Length of `stream` when it is known up front, so the response can carry a
/// Content-Length instead of chunked transfer encoding.
@@ -84,8 +69,8 @@ impl Res {
Self { status, content_type: "text/plain; charset=utf-8".into(), body: body.into_bytes(), headers: Vec::new(), stream: None, stream_len: None }
}
/// A streamed response. `known_len` lets a static file be sent with a
- /// Content-Length instead of chunked, which is what a browser wants for a
- /// file it already knows the size of.
+ /// Content-Length, which is what a browser wants for a file whose size it
+ /// already knows.
pub fn stream(content_type: &str, reader: impl std::io::Read + Send + 'static, known_len: Option<usize>) -> Self {
Self {
status: 200,
@@ -221,8 +206,8 @@ fn serve(state: Arc<State>, mut request: tiny_http::Request) -> std::io::Result<
request.respond(builder)
}
-/// Route table (§12). Everything is one flat match: there is no middleware
-/// stack, just the session gate and then a path.
+/// Route table (§12). One flat match: no middleware stack, just the session gate
+/// and then a path.
fn dispatch(state: &State, req: &Req) -> Res {
let seg: Vec<&str> = req.path.trim_matches('/').split('/').filter(|s| !s.is_empty()).collect();
let is_get = req.method == "GET";
@@ -233,17 +218,16 @@ fn dispatch(state: &State, req: &Req) -> Res {
("GET", ["login"]) => return routes::login_form(state, req),
("POST", ["login"]) => return routes::login(state, req),
("GET", ["healthz"]) => return Res::text(200, "ok\n".into()),
- // Metrics are counts only — no package names, no versions, no audit
- // detail — and a Prometheus scraper has no session to present. Serving
- // them unauthenticated is what makes them scrapeable at all; the bind
- // address is loopback by default, which is where that is safe.
+ // Metrics are counts only — no package names, versions, or audit detail
+ // — and a Prometheus scraper has no session to present. The bind address
+ // is loopback by default, which is where this is safe.
("GET", ["metrics"]) => return routes::metrics(state),
_ => {}
}
// Session gate: mint a session for anonymous visitors so the CSRF token
- // exists even before anyone logs in. It is not a login — `gate` only lets
- // through a sid that a successful login marked authenticated.
+ // exists before anyone logs in. It is not a login — `gate` only lets through
+ // a sid that a successful login marked authenticated.
let (sid, new_session) = match req.cookie("sid") {
Some(s) if auth::is_valid_session(&s) => (s, false),
_ => match auth::new_session() {