diff options
| author | aur-build <aur-build@localhost> | 2026-11-22 10:36:24 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-22 10:36:24 +0000 |
| commit | 3dc71b2f5160ef9bd46143eed0edb5d2557b58e6 (patch) | |
| tree | 7cc7169181b4bdd73fd8817f8c1d7cce6e6998a8 /src/http/mod.rs | |
| parent | 9dc9087d89891c6e5ef7e5dc084dd13c8611843e (diff) | |
| download | aur-build-3dc71b2f5160ef9bd46143eed0edb5d2557b58e6.tar.gz aur-build-3dc71b2f5160ef9bd46143eed0edb5d2557b58e6.zip | |
rustfmt,lints: pin the formatting, adopt the idiom lints, pay them off
rustfmt.toml fixes the shape the tree is written in (120 columns, small
items kept on one line) and one canonical pass makes it the truth.
[lints.clippy] names the idiom lints worth enforcing and leaves out the
two that would add doc-comment boilerplate rather than remove it. The
tree pays them off here: write!/writeln! instead of push_str(&format!()),
let-else instead of match-on-Option, borrows where a thread entry point
only reads what it owns, no `&self` on a Copy state and no `self` nobody
reads.
ops::to_prometheus gains one gauge helper: a metric's HELP, TYPE and
value are now emitted by the same call instead of three.
Diffstat (limited to 'src/http/mod.rs')
| -rw-r--r-- | src/http/mod.rs | 72 |
1 files changed, 38 insertions, 34 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs index f10d4e2..a437978 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -24,7 +24,7 @@ struct Req { } impl Req { -/// Read by the route handlers, which are in this module; the word `form` in a test is a local parameter. + /// Read by the route handlers, which are in this module; the word `form` in a test is a local parameter. fn form(&self) -> &[(String, String)] { &self.form_pairs } @@ -65,10 +65,24 @@ struct Res { impl Res { /// `pub(crate)`, not `pub`: the census counted it as test-only because the word appears in `tests/http.rs`, but only inside a string — a route path, a content type, the word "redirect" in an assertion message. Nothing in the test tier calls it. fn html(status: u16, body: String) -> Self { - Self { status, content_type: "text/html; charset=utf-8".into(), body: body.into_bytes(), headers: Vec::new(), stream: None, stream_len: None } + Self { + status, + content_type: "text/html; charset=utf-8".into(), + body: body.into_bytes(), + headers: Vec::new(), + stream: None, + stream_len: None, + } } pub fn text(status: u16, body: String) -> Self { - Self { status, content_type: "text/plain; charset=utf-8".into(), body: body.into_bytes(), headers: Vec::new(), stream: None, stream_len: None } + Self { + status, + content_type: "text/plain; charset=utf-8".into(), + body: body.into_bytes(), + headers: Vec::new(), + stream: None, + stream_len: None, + } } pub fn stream(content_type: &str, reader: impl std::io::Read + Send + 'static, known_len: Option<usize>) -> Self { Self { @@ -118,32 +132,29 @@ pub use server::Server; pub(crate) use server::Server; pub fn spawn(cfg: Config, db: Db) -> std::io::Result<Server> { - let server = tiny_http::Server::http(&cfg.bind) - .map_err(|e| std::io::Error::other(e.to_string()))?; - let addr = server.server_addr().to_ip().map(|a| a.to_string()).unwrap_or_else(|| cfg.bind.clone()); + let server = tiny_http::Server::http(&cfg.bind).map_err(|e| std::io::Error::other(e.to_string()))?; + let addr = server.server_addr().to_ip().map_or_else(|| cfg.bind.clone(), |a| a.to_string()); let state = Arc::new(State { cfg, db }); eprintln!("aur-build listening on http://{addr}"); // Detached on purpose: the accept loop owns the listener and runs until the // process ends, and nothing joins it, so the handle would be stored unread. - std::thread::Builder::new() - .name("http-accept".into()) - .spawn(crate::time::inherit(move || { - for request in server.incoming_requests() { - let state = state.clone(); - std::thread::Builder::new() - .name("http-conn".into()) - .spawn(crate::time::inherit(move || { - if let Err(e) = serve(state, request) { - eprintln!("http: {e}"); - } - })) - .ok(); - } - }))?; + std::thread::Builder::new().name("http-accept".into()).spawn(crate::time::inherit(move || { + for request in server.incoming_requests() { + let state = state.clone(); + std::thread::Builder::new() + .name("http-conn".into()) + .spawn(crate::time::inherit(move || { + if let Err(e) = serve(&state, request) { + eprintln!("http: {e}"); + } + })) + .ok(); + } + }))?; Ok(Server { addr }) } -fn serve(state: Arc<State>, mut request: tiny_http::Request) -> std::io::Result<()> { +fn serve(state: &State, mut request: tiny_http::Request) -> std::io::Result<()> { let url = request.url().to_string(); let (path, query) = match url.split_once('?') { Some((p, q)) => (p.to_string(), q.to_string()), @@ -155,14 +166,12 @@ fn serve(state: Arc<State>, mut request: tiny_http::Request) -> std::io::Result< .headers() .iter() .find(|h| h.field.equiv("Cookie")) - .map(|h| auth::parse_cookies(h.value.as_str())) - .unwrap_or_default(); + .map_or_default(|h| auth::parse_cookies(h.value.as_str())); let accept_gzip = request .headers() .iter() .find(|h| h.field.equiv("Accept-Encoding")) - .map(|h| h.value.as_str().contains("gzip")) - .unwrap_or(false); + .is_some_and(|h| h.value.as_str().contains("gzip")); let form_pairs = forms::parse(&body); let req = Req { method: request.method().as_str().to_string(), @@ -173,7 +182,7 @@ fn serve(state: Arc<State>, mut request: tiny_http::Request) -> std::io::Result< accept_gzip, }; - let mut res = dispatch(&state, &req); + let mut res = dispatch(state, &req); if let Some(sid) = res.headers.iter().find(|(k, _)| k == "X-New-Session").map(|(_, v)| v.clone()) { res.headers.retain(|(k, _)| k != "X-New-Session"); res.header("Set-Cookie", &format!("sid={sid}; Path=/; HttpOnly; SameSite=Strict")); @@ -188,13 +197,8 @@ fn serve(state: Arc<State>, mut request: tiny_http::Request) -> std::io::Result< } } if let Some(reader) = res.stream.take() { - let mut resp = tiny_http::Response::new( - tiny_http::StatusCode(res.status), - headers, - reader, - res.stream_len, - None, - ); + let mut resp = + tiny_http::Response::new(tiny_http::StatusCode(res.status), headers, reader, res.stream_len, None); if res.stream_len.is_some() { resp = resp.with_chunked_threshold(usize::MAX); } |
