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.rs72
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);
}