Repository: redlib-org/redlib (language: rust)

Issue:
🐛 Bug Report: Redlib responds with a 404 for HTTP HEAD requests
## Describe the bug
When making a HEAD request to Redlib it will return a 404, when it works perfectly fine with a GET request.

## Steps to reproduce the bug
Run `curl -I https://redlib.catsarch.com/` (you can use any instance)
See you get a 404.

## What's the expected behavior?
You'd get the same headers and status as a GET request.

## Additional context / screenshot
`curl -I` is a HEAD request, just `curl` is a GET request.
A HEAD request should return the same headers and HTTP status as a GET request would return, just without the body.
I can recreate it on other instances and accessing Redlib directly without going through my webserver too.

![Screenshot_20241015_160824](https://github.com/user-attachments/assets/de9d1aac-22f9-4037-94f4-ba1a28cf76d7)

<!-- Mandatory -->
- [x] I checked that the instance that this was reported on is running the latest git commit, or I can reproduce it locally on the latest git commit
🐛 Bug Report: Redlib responds with a 404 for HTTP HEAD requests
## Describe the bug
When making a HEAD request to Redlib it will return a 404, when it works perfectly fine with a GET request.

## Steps to reproduce the bug
Run `curl -I https://redlib.catsarch.com/` (you can use any instance)
See you get a 404.

## What's the expected behavior?
You'd get the same headers and status as a GET request.

## Additional context / screenshot
`curl -I` is a HEAD request, just `curl` is a GET request.
A HEAD request should return the same headers and HTTP status as a GET request would return, just without the body.
I can recreate it on other instances and accessing Redlib directly without going through my webserver too.

![Screenshot_20241015_160824](https://github.com/user-attachments/assets/de9d1aac-22f9-4037-94f4-ba1a28cf76d7)

<!-- Mandatory -->
- [x] I checked that the instance that this was reported on is running the latest git commit, or I can reproduce it locally on the latest git commit


Discussion / hints:

Fix patch:
diff --git a/src/server.rs b/src/server.rs
index 15c56ade..0160dfa5 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -240,8 +240,14 @@ impl Server {
 						path.pop();
 					}
 
+					// Replace HEAD with GET for routing
+					let (method, is_head) = match req.method() {
+						&Method::HEAD => (&Method::GET, true),
+						method => (method, false),
+					};
+
 					// Match the visited path with an added route
-					match router.recognize(&format!("/{}{}", req.method().as_str(), path)) {
+					match router.recognize(&format!("/{}{}", method.as_str(), path)) {
 						// If a route was configured for this path
 						Ok(found) => {
 							let mut parammed = req;
@@ -253,17 +259,21 @@ impl Server {
 								match func.await {
 									Ok(mut res) => {
 										res.headers_mut().extend(def_headers);
-										let _ = compress_response(&req_headers, &mut res).await;
+										if is_head {
+											*res.body_mut() = Body::empty();
+										} else {
+											let _ = compress_response(&req_headers, &mut res).await;
+										}
 
 										Ok(res)
 									}
-									Err(msg) => new_boilerplate(def_headers, req_headers, 500, Body::from(msg)).await,
+									Err(msg) => new_boilerplate(def_headers, req_headers, 500, if is_head { Body::empty() } else { Body::from(msg) }).await,
 								}
 							}
 							.boxed()
 						}
 						// If there was a routing error
-						Err(e) => new_boilerplate(def_headers, req_headers, 404, e.into()).boxed(),
+						Err(e) => new_boilerplate(def_headers, req_headers, 404, if is_head { Body::empty() } else { e.into() }).boxed(),
 					}
 				}))
 			}


Test patch: