use std::sync::Arc;
use hyper::body::{Bytes, Incoming};
use hyper::header::{ALLOW, CACHE_CONTROL, CONTENT_LENGTH, HeaderValue};
use hyper::{Method, Request, Response, StatusCode};
use ostrya::access::ReadAccess;
use ostrya::{ArchiveAnswer, ArchiveHead, ArchiveView, Repo};
use crate::auth::{GroupAuth, peer_of};
use crate::body::{ReceiveBody, ServeBody};
use crate::endpoint::ReceiveEndpoint;
use crate::event::{EventSink, StaleSummaries, report_read_failed};
use crate::receive_auth::Refusal;
use crate::refs::Listings;
use crate::stall::Stall;
use crate::{receive, refs};
pub(crate) enum Clients {
Groups(Arc<ReceiveEndpoint<GroupAuth>>),
Anonymous(ReadAccess),
}
impl Clients {
pub(crate) fn endpoint(&self) -> Option<&Arc<ReceiveEndpoint<GroupAuth>>> {
match self {
Clients::Groups(endpoint) => Some(endpoint),
Clients::Anonymous(_) => None,
}
}
pub(crate) fn read_access<B>(&self, req: &Request<B>) -> Result<ReadAccess, Refusal> {
match self {
Clients::Groups(endpoint) => endpoint
.auth()
.identify_read(req.headers(), &peer_of(req.extensions())),
Clients::Anonymous(access) => Ok(access.clone()),
}
}
}
pub(crate) fn refused(refusal: Refusal) -> Response<ServeBody> {
let mut response = empty(refusal.status);
for (name, value) in refusal.headers {
response.headers_mut().append(name, value);
}
response
}
pub(crate) async fn handle(
view: &ArchiveView,
repo: &Repo,
listings: &Listings,
stale: &StaleSummaries,
clients: &Clients,
stall: &Arc<Stall>,
req: Request<Incoming>,
) -> Response<ServeBody> {
if let Some(receive) = clients.endpoint() {
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 {
let identify = || clients.read_access(&req);
return refs::handle(
repo,
listings,
req.method(),
req.uri().query(),
stall,
identify,
)
.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 access = match clients.read_access(&req) {
Ok(access) => access,
Err(refusal) => return refused(refusal),
};
let Some(path) = decode_path(req.uri().path()) else {
return empty(StatusCode::NOT_FOUND);
};
let method = req.method().as_str();
let raw = req.uri().path();
let response = if head {
let answer = view.head(&path, &access).await;
if matches!(answer, Ok(ArchiveHead::StaleSummary)) {
stale.report(access.item());
}
respond_head(answer, stale.on_event(), method, raw)
} else {
let answer = view.get(&path, &access).await;
if let Ok(ArchiveAnswer::StaleSummary { item }) = &answer {
stale.report(item);
}
respond(answer, stall, stale.on_event(), method, raw)
};
private_summary(&path, response)
}
fn private_summary(path: &str, mut response: Response<ServeBody>) -> Response<ServeBody> {
let summary = matches!(path, "summary" | "summary.sig");
let status = response.status();
if summary && (status == StatusCode::OK || status == StatusCode::NOT_FOUND) {
response
.headers_mut()
.insert(CACHE_CONTROL, HeaderValue::from_static("private"));
}
response
}
fn respond(
answer: ostrya::Result<ArchiveAnswer>,
stall: &Arc<Stall>,
on_event: Option<&EventSink>,
method: &str,
path: &str,
) -> 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 | ArchiveAnswer::StaleSummary { .. },
) => {
return empty(StatusCode::NOT_FOUND);
}
Err(error) => {
report_read_failed(on_event, method, path, &error);
return empty(StatusCode::INTERNAL_SERVER_ERROR);
}
};
with_length(Response::new(body), len)
}
fn respond_head(
answer: ostrya::Result<ArchiveHead>,
on_event: Option<&EventSink>,
method: &str,
path: &str,
) -> Response<ServeBody> {
match answer {
Ok(ArchiveHead::Found { len }) => with_length(Response::new(ServeBody::Empty), len),
Ok(ArchiveHead::NotFound | ArchiveHead::Refused | ArchiveHead::StaleSummary) => {
empty(StatusCode::NOT_FOUND)
}
Err(error) => {
report_read_failed(on_event, method, path, &error);
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::sync::Mutex;
use std::time::Duration;
use hyper::body::Body;
use ostrya_rt::block_on;
use super::*;
use crate::body::tests::frames;
use crate::event::ServerEvent;
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 }), None, "HEAD", "/x");
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, None, "HEAD", "/x").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(), None, "GET", "/x");
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(),
None,
"GET",
"/x",
);
assert_eq!(error.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
#[test]
fn an_error_gets_500_and_one_event() {
let seen = Arc::new(Mutex::new(Vec::new()));
let sink = seen.clone();
let on_event: EventSink = Arc::new(move |event: &ServerEvent| {
sink.lock().unwrap().push(event.clone());
});
let denied =
|| ostrya::Error::Io(std::io::Error::from(std::io::ErrorKind::PermissionDenied));
let path = "/objects/ab/cd.filez";
let responses = [
respond(Err(denied()), &stall(), Some(&on_event), "GET", path),
respond_head(Err(denied()), Some(&on_event), "HEAD", path),
];
for response in responses {
let (parts, body) = response.into_parts();
assert_eq!(parts.status, StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(
parts.headers.get(CONTENT_LENGTH),
Some(&HeaderValue::from(0u64))
);
assert!(block_on(frames(body)).unwrap().is_empty());
}
let seen = seen.lock().unwrap();
assert_eq!(seen.len(), 2, "{seen:?}");
for (event, method) in seen.iter().zip(["GET", "HEAD"]) {
assert!(!event.is_warning());
assert_eq!(
event,
&ServerEvent::ReadFailed {
method: method.to_owned(),
path: path.to_owned(),
error: denied().to_string(),
}
);
}
}
#[test]
fn bytes_carry_their_length() {
let response = respond(
Ok(ArchiveAnswer::Bytes(b"abc".to_vec())),
&stall(),
None,
"GET",
"/x",
);
assert_eq!(
response.headers().get(CONTENT_LENGTH),
Some(&HeaderValue::from(3u64))
);
assert_eq!(
block_on(frames(response.into_body())).unwrap().concat(),
b"abc"
);
}
}