use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use hyper::body::{Bytes, Incoming};
use hyper::header::{ALLOW, CONTENT_LENGTH, HeaderValue};
use hyper::{Method, Request, Response, StatusCode};
use ostrya::{ArchiveAnswer, ArchiveHead, ArchiveView, Repo};
use crate::auth::FileAuth;
use crate::body::{ReceiveBody, ServeBody};
use crate::endpoint::ReceiveEndpoint;
use crate::stall::Stall;
use crate::{receive, refs};
pub(crate) async fn handle(
view: &ArchiveView,
repo: &Repo,
listings: &Arc<AtomicUsize>,
receive: Option<&ReceiveEndpoint<FileAuth>>,
stall: &Arc<Stall>,
req: Request<Incoming>,
) -> Response<ServeBody> {
if let Some(receive) = receive {
let read = matches!(*req.method(), Method::GET | Method::HEAD);
if !read && req.uri().path().starts_with(receive::PREFIX) {
return receive.handle(req).await.map(ReceiveBody::into_serve);
}
}
if req.uri().path() == refs::PATH {
return refs::handle(repo, listings, req.method(), req.uri().query(), stall).await;
}
let head = match *req.method() {
Method::GET => false,
Method::HEAD => true,
_ => {
let mut response = empty(StatusCode::METHOD_NOT_ALLOWED);
response
.headers_mut()
.insert(ALLOW, HeaderValue::from_static("GET, HEAD"));
return response;
}
};
let Some(path) = decode_path(req.uri().path()) else {
return empty(StatusCode::NOT_FOUND);
};
if head {
respond_head(view.head(&path).await)
} else {
respond(view.get(&path).await, stall)
}
}
fn respond(answer: ostrya::Result<ArchiveAnswer>, stall: &Arc<Stall>) -> Response<ServeBody> {
let (len, body) = match answer {
Ok(ArchiveAnswer::Bytes(bytes)) => (
Some(bytes.len() as u64),
ServeBody::Full(Some(Bytes::from(bytes))),
),
Ok(ArchiveAnswer::Stream { len, body }) => {
(len, ServeBody::stream(body, len, stall.track()))
}
Ok(ArchiveAnswer::NotFound | ArchiveAnswer::Refused) => {
return empty(StatusCode::NOT_FOUND);
}
Err(_) => return empty(StatusCode::INTERNAL_SERVER_ERROR),
};
with_length(Response::new(body), len)
}
fn respond_head(answer: ostrya::Result<ArchiveHead>) -> Response<ServeBody> {
match answer {
Ok(ArchiveHead::Found { len }) => with_length(Response::new(ServeBody::Empty), len),
Ok(ArchiveHead::NotFound | ArchiveHead::Refused) => empty(StatusCode::NOT_FOUND),
Err(_) => empty(StatusCode::INTERNAL_SERVER_ERROR),
}
}
pub(crate) fn with_length(
mut response: Response<ServeBody>,
len: Option<u64>,
) -> Response<ServeBody> {
if let Some(len) = len {
response
.headers_mut()
.insert(CONTENT_LENGTH, HeaderValue::from(len));
}
response
}
pub(crate) fn empty(status: StatusCode) -> Response<ServeBody> {
let mut response = Response::new(ServeBody::Empty);
*response.status_mut() = status;
response
.headers_mut()
.insert(CONTENT_LENGTH, HeaderValue::from(0u64));
response
}
fn decode_path(raw: &str) -> Option<String> {
percent_decode(raw.strip_prefix('/')?)
}
pub(crate) fn percent_decode(raw: &str) -> Option<String> {
let raw = raw.as_bytes();
let mut out = Vec::with_capacity(raw.len());
let mut i = 0;
while i < raw.len() {
if raw[i] == b'%' {
let hi = hex_digit(*raw.get(i + 1)?)?;
let lo = hex_digit(*raw.get(i + 2)?)?;
out.push((hi << 4) | lo);
i += 3;
} else {
out.push(raw[i]);
i += 1;
}
}
if out.contains(&0) {
return None;
}
String::from_utf8(out).ok()
}
fn hex_digit(b: u8) -> Option<u8> {
match b {
b'0'..=b'9' => Some(b - b'0'),
b'a'..=b'f' => Some(b - b'a' + 10),
b'A'..=b'F' => Some(b - b'A' + 10),
_ => None,
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use hyper::body::Body;
use ostrya_rt::block_on;
use super::*;
use crate::body::tests::frames;
fn stall() -> Arc<Stall> {
Stall::new(Duration::from_secs(3600))
}
#[test]
fn paths_are_percent_decoded() {
assert_eq!(decode_path("/config").as_deref(), Some("config"));
assert_eq!(
decode_path("/refs%2Fheads/a%20b").as_deref(),
Some("refs/heads/a b")
);
assert_eq!(decode_path("/").as_deref(), Some(""));
for bad in ["config", "*", "/a%2", "/a%zz", "/a%00b", "/%ff"] {
assert_eq!(decode_path(bad), None, "{bad}");
}
assert_eq!(percent_decode("a%2fb+c").as_deref(), Some("a/b+c"));
assert_eq!(percent_decode("").as_deref(), Some(""));
}
#[test]
fn a_head_has_the_length_and_no_body() {
for len in [Some(42), None] {
let response = respond_head(Ok(ArchiveHead::Found { len }));
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get(CONTENT_LENGTH),
len.map(HeaderValue::from).as_ref()
);
let body = response.into_body();
assert!(body.is_end_stream());
assert!(block_on(frames(body)).unwrap().is_empty());
}
let parts = |answer| {
let (parts, _) = respond_head(answer).into_parts();
(parts.status, format!("{:?}", parts.headers))
};
assert_eq!(
parts(Ok(ArchiveHead::Refused)),
parts(Ok(ArchiveHead::NotFound))
);
assert_eq!(parts(Ok(ArchiveHead::NotFound)).0, StatusCode::NOT_FOUND);
assert_eq!(
parts(Err(ostrya::Error::InvalidFormat("x".into()))).0,
StatusCode::INTERNAL_SERVER_ERROR
);
}
#[test]
fn a_refusal_and_a_not_found_are_one_answer() {
let parts = |answer| {
let response = respond(Ok(answer), &stall());
let (parts, body) = response.into_parts();
(
parts.status,
format!("{:?}", parts.headers),
block_on(frames(body)).unwrap(),
)
};
let refused = parts(ArchiveAnswer::Refused);
assert_eq!(refused, parts(ArchiveAnswer::NotFound));
assert_eq!(refused.0, StatusCode::NOT_FOUND);
assert!(refused.2.is_empty());
let error = respond(Err(ostrya::Error::InvalidFormat("x".into())), &stall());
assert_eq!(error.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
#[test]
fn bytes_carry_their_length() {
let response = respond(Ok(ArchiveAnswer::Bytes(b"abc".to_vec())), &stall());
assert_eq!(
response.headers().get(CONTENT_LENGTH),
Some(&HeaderValue::from(3u64))
);
assert_eq!(
block_on(frames(response.into_body())).unwrap().concat(),
b"abc"
);
}
}