use std::future::Future;
use std::sync::Arc;
use futures_lite::future;
use hyper::body::{Body, Bytes, Frame};
use hyper::header::{
ALLOW, CONNECTION, CONTENT_LENGTH, HeaderName, HeaderValue, TRANSFER_ENCODING,
};
use hyper::{Method, Request, Response, StatusCode, Version};
use ostrya::remote::proto::{ErrorMessage, MAX_FRAME, Message};
use ostrya::remote::{self, ErrorCode};
use ostrya::{ReceiveReport, ReceiveService, ReceiveStep, ReceiveWarning, Repo};
use ostrya_rt as rt;
use crate::body::ServeBody;
use crate::receive_auth::{ReceiveAuth, Refusal, RequestKind, SessionSetup};
use crate::request::{RequestBody, drain, read_message};
use crate::router::empty;
use crate::session::{Active, Begin, Cancel, Deleted, SessionId, SessionTable};
pub(crate) const PREFIX: &str = "/_ostrya/receive/v1/";
const SESSION_HEADER: HeaderName = HeaderName::from_static("ostrya-session");
const OWN_HEADERS: [HeaderName; 3] = [CONTENT_LENGTH, TRANSFER_ENCODING, CONNECTION];
pub(crate) type OnReport = Arc<dyn Fn(ReceiveReport) + Send + Sync>;
pub(crate) struct Receive<A> {
pub(crate) repo: Repo,
pub(crate) auth: A,
pub(crate) parallel_uploads: u32,
pub(crate) on_report: Option<OnReport>,
pub(crate) table: Arc<SessionTable>,
}
#[derive(Clone, Copy)]
enum Route {
Open,
Session(SessionId),
Step(SessionId, Step),
}
#[derive(Clone, Copy)]
enum Step {
Have,
Objects,
Commit,
}
impl Route {
fn parse(path: &str) -> Option<Route> {
let rest = path.strip_prefix(PREFIX)?;
if rest == "session" {
return Some(Route::Open);
}
let rest = rest.strip_prefix("session/")?;
let (id, step) = match rest.split_once('/') {
Some((id, step)) => (id, Some(step)),
None => (rest, None),
};
let id = SessionId::parse(id)?;
Some(match step {
None => Route::Session(id),
Some("have") => Route::Step(id, Step::Have),
Some("objects") => Route::Step(id, Step::Objects),
Some("commit") => Route::Step(id, Step::Commit),
Some(_) => return None,
})
}
fn kind(self) -> RequestKind {
match self {
Route::Open => RequestKind::Open,
Route::Session(_) => RequestKind::Delete,
Route::Step(_, Step::Have) => RequestKind::Have,
Route::Step(_, Step::Objects) => RequestKind::Objects,
Route::Step(_, Step::Commit) => RequestKind::Commit,
}
}
fn method(self) -> Method {
match self {
Route::Session(_) => Method::DELETE,
Route::Open | Route::Step(..) => Method::POST,
}
}
fn allow(self) -> HeaderValue {
HeaderValue::from_static(match self {
Route::Session(_) => "DELETE",
Route::Open | Route::Step(..) => "POST",
})
}
}
impl<A: ReceiveAuth> Receive<A> {
pub(crate) async fn handle<B>(&self, req: Request<B>) -> Response<ServeBody>
where
B: Body<Data = Bytes> + Send + Unpin + 'static,
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
{
let version = req.version();
let Some(route) = Route::parse(req.uri().path()) else {
let response = empty(StatusCode::NOT_FOUND);
return self.refuse(version, req.into_body(), response).await;
};
if *req.method() != route.method() {
let mut response = empty(StatusCode::METHOD_NOT_ALLOWED);
response.headers_mut().insert(ALLOW, route.allow());
return self.refuse(version, req.into_body(), response).await;
}
let (parts, body) = req.into_parts();
let principal = self.auth.authenticate(&parts, route.kind()).await;
drop(parts);
let principal = match principal {
Ok(principal) => principal,
Err(refusal) => {
return self.refuse(version, body, refusal_response(refusal)).await;
}
};
let owner = A::owner(&principal);
match route {
Route::Open => self.open(&principal, body).await,
Route::Session(id) => {
let response = match self.table.delete(&id, owner) {
Deleted::Done => no_content(),
Deleted::Committing => failure(&committing()),
Deleted::NotFound => empty(StatusCode::NOT_FOUND),
};
self.refuse(version, body, response).await
}
Route::Step(id, step) => {
let Some(mut active) = self.table.lookup(&id, owner) else {
let response = empty(StatusCode::NOT_FOUND);
return self.refuse(version, body, response).await;
};
let body = RequestBody::new(body, Some(active.track()));
let response = match step {
Step::Have => have(&active, body).await,
Step::Objects => objects(&active, body).await,
Step::Commit => self.commit(&active, body).await,
};
active.complete();
response
}
}
}
async fn refuse<B>(
&self,
version: Version,
body: B,
mut response: Response<ServeBody>,
) -> Response<ServeBody>
where
B: Body<Data = Bytes> + Send + Unpin + 'static,
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
{
let drained = drain(body, self.table.idle()).await;
if !drained && version <= Version::HTTP_11 {
response
.headers_mut()
.insert(CONNECTION, HeaderValue::from_static("close"));
}
response
}
async fn open<B>(&self, principal: &A::Principal, body: B) -> Response<ServeBody>
where
B: Body<Data = Bytes> + Send + Unpin + 'static,
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
{
let mut body = RequestBody::new(body, None);
let read = future::or(async { Some(read_message(&mut body).await) }, async {
rt::Timer::after(self.table.idle()).await;
None
})
.await;
let hello = match read {
Some(Ok(Message::Hello(hello))) => hello,
Some(Ok(other)) => {
return failure(&protocol(format!("{:?} in place of Hello", other.kind())));
}
Some(Err(e)) => return failure(&e.into()),
None => {
return failure(&protocol(
"the request body did not arrive within the idle timeout",
));
}
};
drop(body);
if hello.one_way {
return failure(&protocol(
"a Hello with one-way true opens a one-way stream, which this session does not \
read",
));
}
if let Err(e) = ReceiveService::check_hello(&self.repo, &hello) {
return failure(&e);
}
let Some(slot) = self.table.reserve() else {
return no_more_sessions();
};
let id = match SessionId::new() {
Ok(id) => id,
Err(e) => {
let message = error_message(
ErrorCode::Internal,
format!("the random source of the session id failed: {e}"),
);
return frame(StatusCode::INTERNAL_SERVER_ERROR, &message);
}
};
let SessionSetup { policy, hooks } = match self.auth.open(principal, &hello).await {
Ok(setup) => setup,
Err(refusal) => return refusal_response(refusal),
};
let opened = ReceiveService::hello_with_hooks(
self.repo.clone(),
policy,
hooks,
self.parallel_uploads,
hello,
)
.await;
let (service, reply) = match opened {
Ok(opened) => opened,
Err(e) => return failure(&e),
};
let bytes = match encode(&Message::HelloReply(reply)) {
Ok(bytes) => bytes,
Err(e) => {
debug_assert!(false, "the reply of a Hello does not fit: {e}");
return failure(&e.into());
}
};
if !slot.insert(id, service, A::owner(principal).to_owned()) {
return no_more_sessions();
}
let mut response = full(StatusCode::OK, bytes);
let value = HeaderValue::from_str(&id.to_string()).expect("hex is a header value");
response.headers_mut().insert(SESSION_HEADER, value);
response
}
async fn commit<B>(&self, active: &Active, mut body: RequestBody<B>) -> Response<ServeBody>
where
B: Body<Data = Bytes> + Send + Unpin + 'static,
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
{
let read = cancellable(active.cancel(), async {
match read_message(&mut body).await? {
Message::Commit(request) => Ok(request),
other => Err(protocol(format!("{:?} in place of Commit", other.kind()))),
}
})
.await;
drop(body);
let request = match read {
None => return failure(&aborted(active.cancel())),
Some(Ok(request)) => request,
Some(Err(e)) => {
active.table().fail(&active.id());
return failure(&e);
}
};
let mut end = match active.begin_commit() {
Begin::Started(end) => end,
Begin::Committing => return failure(&committing()),
Begin::Ended => return failure(&aborted(active.cancel())),
};
let service = active.service().clone();
let on_report = self.on_report.clone();
let task = rt::spawn(async move {
let result = service.commit(request).await;
if result.is_ok() {
end.committed();
}
drop(end);
result.map(|report| Delivery {
report: Some(report),
on_report,
delivered: false,
})
});
match task.await {
Ok(delivery) => {
let reply = Message::CommitReply(delivery.report().refs.clone());
match encode(&reply) {
Ok(bytes) => reply_response(bytes, delivery),
Err(e) => {
debug_assert!(false, "the reply of a commit does not fit: {e}");
failure(&e.into())
}
}
}
Err(e) => failure(&e),
}
}
}
async fn have<B>(active: &Active, mut body: RequestBody<B>) -> Response<ServeBody>
where
B: Body<Data = Bytes> + Send + Unpin + 'static,
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
{
let result = cancellable(active.cancel(), async {
let names = match read_message(&mut body).await? {
Message::Have(names) => names,
other => return Err(protocol(format!("{:?} in place of Have", other.kind()))),
};
drop(body);
active.service().have(names).await
})
.await;
step_response(active, result.map(|r| r.map(Message::HaveReply)))
}
async fn objects<B>(active: &Active, body: RequestBody<B>) -> Response<ServeBody>
where
B: Body<Data = Bytes> + Send + Unpin + 'static,
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
{
let result = cancellable(active.cancel(), active.service().objects(body)).await;
step_response(active, result.map(|r| r.map(Message::ObjectsReply)))
}
fn step_response(active: &Active, result: Option<ostrya::Result<Message>>) -> Response<ServeBody> {
match result {
None => failure(&aborted(active.cancel())),
Some(Ok(reply)) => match encode(&reply) {
Ok(bytes) => full(StatusCode::OK, bytes),
Err(e) => {
active.table().fail(&active.id());
failure(&e.into())
}
},
Some(Err(e)) => {
active.table().fail(&active.id());
failure(&e)
}
}
}
async fn cancellable<T>(cancel: &Cancel, step: impl Future<Output = T>) -> Option<T> {
future::or(
async {
cancel.wait().await;
None
},
async { Some(step.await) },
)
.await
}
fn protocol(message: impl Into<String>) -> ostrya::Error {
ostrya::Error::Remote(remote::Error::Protocol(message.into()))
}
fn committing() -> ostrya::Error {
protocol("the session is committing")
}
fn aborted(cancel: &Cancel) -> ostrya::Error {
protocol(format!("the session was aborted: {}", cancel.cause()))
}
fn error_message(code: ErrorCode, message: String) -> ErrorMessage {
ErrorMessage {
code,
message,
missing: Vec::new(),
current: None,
}
}
pub(crate) fn status(e: &ostrya::Error) -> (StatusCode, ErrorMessage) {
let message = match e {
ostrya::Error::Remote(e) => e.to_message(),
other => error_message(ErrorCode::Internal, other.to_string()),
};
let status = match message.code {
ErrorCode::RefMismatch | ErrorCode::NonFastForward => StatusCode::CONFLICT,
ErrorCode::Internal => StatusCode::INTERNAL_SERVER_ERROR,
ErrorCode::Unauthorized => StatusCode::FORBIDDEN,
_ => StatusCode::UNPROCESSABLE_ENTITY,
};
(status, message)
}
fn refusal_response(refusal: Refusal) -> Response<ServeBody> {
let Refusal {
status,
message,
headers,
} = refusal;
let mut response = frame(status, &error_message(ErrorCode::Unauthorized, message));
let out = response.headers_mut();
for (name, value) in headers {
if !OWN_HEADERS.contains(&name) {
out.append(name, value);
}
}
response
}
fn no_more_sessions() -> Response<ServeBody> {
let message = error_message(
ErrorCode::LimitExceeded,
"the server serves no more sessions".into(),
);
frame(StatusCode::SERVICE_UNAVAILABLE, &message)
}
fn no_content() -> Response<ServeBody> {
let mut response = Response::new(ServeBody::Empty);
*response.status_mut() = StatusCode::NO_CONTENT;
response
}
fn failure(e: &ostrya::Error) -> Response<ServeBody> {
let (status, message) = status(e);
frame(status, &message)
}
fn frame(status: StatusCode, message: &ErrorMessage) -> Response<ServeBody> {
match encode(&Message::Error(message.clone())) {
Ok(bytes) => full(status, bytes),
Err(e) => {
let message = error_message(
ErrorCode::Internal,
format!("the error of the request does not fit in a frame: {e}"),
);
let bytes = encode(&Message::Error(message)).expect("a short error fits in a frame");
full(StatusCode::INTERNAL_SERVER_ERROR, bytes)
}
}
}
fn encode(msg: &Message) -> Result<Bytes, remote::Error> {
let body = msg.encode_body()?;
let len = u32::try_from(body.len() + 1)
.ok()
.filter(|len| *len <= MAX_FRAME)
.ok_or_else(|| {
remote::Error::LimitExceeded(format!(
"a reply frame of {} bytes is over the limit {MAX_FRAME}",
body.len() + 1
))
})?;
let mut frame = Vec::with_capacity(4 + len as usize);
frame.extend_from_slice(&len.to_be_bytes());
frame.push(msg.kind().as_u8());
frame.extend_from_slice(&body);
Ok(Bytes::from(frame))
}
fn full(status: StatusCode, bytes: Bytes) -> Response<ServeBody> {
let len = bytes.len() as u64;
let mut response = Response::new(ServeBody::Full(Some(bytes)));
*response.status_mut() = status;
response
.headers_mut()
.insert(CONTENT_LENGTH, HeaderValue::from(len));
response
}
fn reply_response(bytes: Bytes, delivery: Delivery) -> Response<ServeBody> {
let len = bytes.len() as u64;
let mut response = Response::new(ServeBody::Reply(ReplyBody {
frame: Some(bytes),
delivery,
}));
response
.headers_mut()
.insert(CONTENT_LENGTH, HeaderValue::from(len));
response
}
pub(crate) struct Delivery {
report: Option<ReceiveReport>,
on_report: Option<OnReport>,
delivered: bool,
}
impl Delivery {
fn report(&self) -> &ReceiveReport {
self.report.as_ref().expect("the report goes at the drop")
}
}
impl Drop for Delivery {
fn drop(&mut self) {
let Some(mut report) = self.report.take() else {
return;
};
if !self.delivered {
report.warnings.push(ReceiveWarning {
step: ReceiveStep::ReplyNotDelivered,
message: "the connection did not take the CommitReply response".into(),
});
}
if let Some(on_report) = &self.on_report {
on_report(report);
}
}
}
pub(crate) struct ReplyBody {
frame: Option<Bytes>,
delivery: Delivery,
}
impl ReplyBody {
pub(crate) fn poll_frame(&mut self) -> Option<Frame<Bytes>> {
let bytes = self.frame.take()?;
self.delivery.delivered = true;
Some(Frame::data(bytes))
}
pub(crate) fn remaining(&self) -> u64 {
self.frame.as_ref().map_or(0, |b| b.len() as u64)
}
}
#[cfg(test)]
mod tests {
use std::cell::Cell;
use std::collections::VecDeque;
use std::io;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::task::{Context, Poll};
use std::time::Duration;
use hyper::header::WWW_AUTHENTICATE;
use hyper::http::request::Parts;
use super::*;
use crate::SessionSetup;
use crate::auth::{GroupAuth, Peer};
use crate::body::tests::frames;
use crate::request::MAX_DRAIN;
use crate::session::tests::{ANONYMOUS_PUSH, TmpRepo, pushing_policy};
use ostrya::remote::proto::{CommitRequest, Hello, Kind};
use ostrya::remote::{Expected, RefOutcome, RefUpdate};
use ostrya::{Checksum, ObjectName, ObjectType, ReceivePolicy, TransactionStats};
use ostrya_rt::block_on;
#[test]
fn errors_map_to_their_status() {
let cases: Vec<(ostrya::Error, u16, ErrorCode)> = vec![
(
remote::Error::RefMismatch {
message: "m".into(),
name: "main".into(),
current: None,
}
.into(),
409,
ErrorCode::RefMismatch,
),
(
remote::Error::NonFastForward("m".into()).into(),
409,
ErrorCode::NonFastForward,
),
(
remote::Error::Internal("m".into()).into(),
500,
ErrorCode::Internal,
),
(
remote::Error::Protocol("m".into()).into(),
422,
ErrorCode::Protocol,
),
(
remote::Error::ModeRefused("m".into()).into(),
422,
ErrorCode::ModeRefused,
),
(
remote::Error::LimitExceeded("m".into()).into(),
422,
ErrorCode::LimitExceeded,
),
(
remote::Error::MissingObjects {
message: "m".into(),
missing: Vec::new(),
}
.into(),
422,
ErrorCode::MissingObjects,
),
(
remote::Error::Unauthorized("m".into()).into(),
403,
ErrorCode::Unauthorized,
),
(remote::Error::Aborted.into(), 500, ErrorCode::Internal),
(
ostrya::Error::Io(std::io::ErrorKind::Other.into()),
500,
ErrorCode::Internal,
),
(
ostrya::Error::InvalidFormat("bad".into()),
500,
ErrorCode::Internal,
),
];
for (e, want, code) in cases {
let (got, message) = status(&e);
assert_eq!(got.as_u16(), want, "{e}");
assert_eq!(message.code, code, "{e}");
}
let (_, message) = status(&ostrya::Error::InvalidFormat("bad".into()));
assert!(message.message.contains("bad"), "{}", message.message);
let (_, message) = status(
&remote::Error::RefMismatch {
message: "m".into(),
name: "main".into(),
current: None,
}
.into(),
);
assert_eq!(message.current.unwrap().name, "main");
}
#[test]
fn a_frame_response_has_its_length_and_no_type() {
let response = failure(&protocol("x"));
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
assert!(response.headers().get("content-type").is_none());
assert!(response.headers().get("retry-after").is_none());
let len: u64 = response.headers()[CONTENT_LENGTH]
.to_str()
.unwrap()
.parse()
.unwrap();
let bytes = block_on(frames(response.into_body())).unwrap().concat();
assert_eq!(bytes.len() as u64, len);
assert_eq!(&bytes[4..5], &[10u8], "the kind of an Error frame");
}
fn report() -> ReceiveReport {
ReceiveReport {
refs: Vec::new(),
stats: TransactionStats::default(),
warnings: Vec::new(),
group: None,
collection: None,
}
}
#[test]
fn an_undelivered_reply_adds_a_warning() {
let seen = Arc::new(Mutex::new(Vec::<ReceiveReport>::new()));
let sink = seen.clone();
let on_report: OnReport = Arc::new(move |report| sink.lock().unwrap().push(report));
let body = |on_report: &OnReport| ReplyBody {
frame: Some(Bytes::from_static(b"frame")),
delivery: Delivery {
report: Some(report()),
on_report: Some(on_report.clone()),
delivered: false,
},
};
drop(body(&on_report));
let mut taken = body(&on_report);
assert_eq!(taken.remaining(), 5);
assert!(taken.poll_frame().is_some());
assert_eq!(taken.remaining(), 0);
assert!(taken.poll_frame().is_none());
drop(taken);
let seen = seen.lock().unwrap();
assert_eq!(seen.len(), 2);
assert_eq!(seen[0].warnings.len(), 1);
assert_eq!(seen[0].warnings[0].step, ReceiveStep::ReplyNotDelivered);
assert!(seen[1].warnings.is_empty());
}
#[test]
fn routes_take_the_raw_path() {
let id = "ab".repeat(32);
let ok = |path: &str| Route::parse(path).map(|r| r.method());
assert_eq!(ok("/_ostrya/receive/v1/session"), Some(Method::POST));
assert_eq!(
ok(&format!("/_ostrya/receive/v1/session/{id}")),
Some(Method::DELETE)
);
for step in ["have", "objects", "commit"] {
assert_eq!(
ok(&format!("/_ostrya/receive/v1/session/{id}/{step}")),
Some(Method::POST)
);
}
let upper = id.to_ascii_uppercase();
for bad in [
"/_ostrya/receive/v1/".to_string(),
"/_ostrya/receive/v1/session/".to_string(),
"/_ostrya/receive/v1/%73ession".to_string(),
format!("/_ostrya/receive/v1/session/{upper}"),
format!("/_ostrya/receive/v1/session/{id}/"),
format!("/_ostrya/receive/v1/session/{id}/other"),
format!("/_ostrya/receive/v1/session/{id}/have/x"),
] {
assert!(ok(&bad).is_none(), "{bad}");
}
}
enum Piece {
Bytes(Bytes),
Wait,
Fail,
}
struct Pieces {
steps: VecDeque<Piece>,
_not_sync: PhantomData<Cell<()>>,
}
impl Pieces {
fn new(bytes: &[u8], size: usize, fail_after: Option<usize>) -> Pieces {
let mut steps = VecDeque::new();
for (index, piece) in bytes.chunks(size).enumerate() {
steps.push_back(Piece::Wait);
if fail_after == Some(index) {
steps.push_back(Piece::Fail);
break;
}
steps.push_back(Piece::Bytes(Bytes::copy_from_slice(piece)));
}
Pieces {
steps,
_not_sync: PhantomData,
}
}
}
impl Body for Pieces {
type Data = Bytes;
type Error = io::Error;
fn poll_frame(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<io::Result<Frame<Bytes>>>> {
match self.steps.pop_front() {
None => Poll::Ready(None),
Some(Piece::Bytes(bytes)) => Poll::Ready(Some(Ok(Frame::data(bytes)))),
Some(Piece::Wait) => {
cx.waker().wake_by_ref();
Poll::Pending
}
Some(Piece::Fail) => Poll::Ready(Some(Err(io::Error::other("the body broke")))),
}
}
}
const _: fn() = || {
fn send_unpin<T: Send + Unpin>() {}
send_unpin::<Pieces>();
trait NotSync<A> {
fn check() {}
}
impl<T: ?Sized> NotSync<()> for T {}
struct IsSync;
impl<T: ?Sized + Sync> NotSync<IsSync> for T {}
<Pieces as NotSync<_>>::check();
};
fn send<F: Future + Send>(future: F) -> F {
future
}
fn endpoint_with<A: ReceiveAuth>(tmp: &TmpRepo, max: usize, auth: A) -> Receive<A> {
Receive {
repo: tmp.repo.clone(),
auth,
parallel_uploads: 1,
on_report: None,
table: SessionTable::new(max, Duration::from_secs(60)),
}
}
fn endpoint(tmp: &TmpRepo, max: usize) -> Receive<GroupAuth> {
let auth = GroupAuth::new(tmp.policies(ANONYMOUS_PUSH), false);
endpoint_with(tmp, max, auth)
}
async fn post<A: ReceiveAuth>(
receive: &Receive<A>,
path: &str,
message: &Message,
fail_after: Option<usize>,
) -> Response<ServeBody> {
post_as(receive, "anonymous", path, message, fail_after).await
}
async fn post_as<A: ReceiveAuth>(
receive: &Receive<A>,
principal: &str,
path: &str,
message: &Message,
fail_after: Option<usize>,
) -> Response<ServeBody> {
let bytes = encode(message).unwrap();
let req = Request::builder()
.method(Method::POST)
.uri(format!("{PREFIX}{path}"))
.header("x-principal", principal)
.extension(Peer {
tls: false,
addr: std::net::Ipv4Addr::LOCALHOST.into(),
cert: None,
})
.body(Pieces::new(&bytes, 3, fail_after))
.unwrap();
send(receive.handle(req)).await
}
async fn answer(response: Response<ServeBody>) -> (StatusCode, Message) {
let status = response.status();
let bytes = frames(response.into_body()).await.unwrap().concat();
let message = Message::decode(Kind::from_u8(bytes[4]).unwrap(), &bytes[5..]).unwrap();
(status, message)
}
fn hello() -> Message {
Message::Hello(Hello {
version: 1,
agent: None,
refs: vec!["main".into()],
collection: None,
one_way: false,
})
}
fn delete_main() -> Message {
Message::Commit(CommitRequest {
updates: vec![RefUpdate {
name: "main".into(),
expected: Expected::Absent,
new: None,
}],
force: false,
server_commit: None,
})
}
async fn open<A: ReceiveAuth>(receive: &Receive<A>) -> String {
open_as(receive, "anonymous").await
}
async fn open_as<A: ReceiveAuth>(receive: &Receive<A>, principal: &str) -> String {
let response = post_as(receive, principal, "session", &hello(), None).await;
let id = response.headers()[SESSION_HEADER]
.to_str()
.unwrap()
.to_owned();
let (status, reply) = answer(response).await;
assert_eq!(status, StatusCode::OK);
assert!(matches!(reply, Message::HelloReply(_)), "{reply:?}");
id
}
fn error(reply: Message) -> ErrorMessage {
match reply {
Message::Error(e) => e,
other => panic!("{other:?}"),
}
}
#[test]
fn the_steps_take_a_body_that_is_not_sync() {
let tmp = TmpRepo::new("receive-steps");
let receive = endpoint(&tmp, 2);
block_on(async {
let id = open(&receive).await;
let absent = ObjectName::new(Checksum::sha256(b"absent"), ObjectType::DirTree);
let have = Message::Have(vec![absent]);
let response = post(&receive, &format!("session/{id}/have"), &have, None).await;
let (status, reply) = answer(response).await;
assert_eq!(status, StatusCode::OK);
let Message::HaveReply(reply) = reply else {
panic!("{reply:?}");
};
assert!(reply.is_missing(0));
let end = Message::ObjectsEnd;
let response = post(&receive, &format!("session/{id}/objects"), &end, None).await;
let (status, reply) = answer(response).await;
assert_eq!(status, StatusCode::OK);
let Message::ObjectsReply(reply) = reply else {
panic!("{reply:?}");
};
assert_eq!(reply.objects, 0);
let commit = delete_main();
let response = post(&receive, &format!("session/{id}/commit"), &commit, None).await;
let (status, reply) = answer(response).await;
assert_eq!(status, StatusCode::OK);
let Message::CommitReply(refs) = reply else {
panic!("{reply:?}");
};
let outcome = RefOutcome {
name: "main".into(),
old: None,
new: None,
};
assert_eq!(refs, vec![outcome]);
let id = SessionId::parse(&id).unwrap();
assert!(receive.table.lookup(&id, "anonymous").is_none());
let id = open(&receive).await;
let response = post(&receive, &format!("session/{id}/commit"), &commit, Some(2)).await;
let (status, reply) = answer(response).await;
assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
let e = error(reply);
assert_eq!(e.code, ErrorCode::Internal);
assert!(e.message.contains("the body broke"), "{}", e.message);
let response = post(&receive, &format!("session/{id}/have"), &have, None).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let response = post(&receive, "session", &hello(), Some(1)).await;
let (status, reply) = answer(response).await;
assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
let e = error(reply);
assert_eq!(e.code, ErrorCode::Internal);
assert!(e.message.contains("the body broke"), "{}", e.message);
open(&receive).await;
open(&receive).await;
let response = post(&receive, "session", &hello(), None).await;
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
receive.table.close_all();
});
}
#[test]
fn a_refusal_drains_a_body_that_is_not_sync() {
let tmp = TmpRepo::new("receive-refusal");
let receive = endpoint(&tmp, 1);
block_on(async {
let response = post(&receive, "nope", &hello(), None).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
assert!(response.headers().get(CONNECTION).is_none());
let response = post(&receive, "nope", &hello(), Some(1)).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
assert_eq!(response.headers()[CONNECTION], "close");
});
}
struct TestAuth {
polled: Arc<AtomicU64>,
polled_at_authenticate: AtomicU64,
opens: AtomicUsize,
}
impl TestAuth {
fn new() -> TestAuth {
TestAuth {
polled: Arc::new(AtomicU64::new(0)),
polled_at_authenticate: AtomicU64::new(u64::MAX),
opens: AtomicUsize::new(0),
}
}
fn opens(&self) -> usize {
self.opens.load(Ordering::SeqCst)
}
}
const REASON: HeaderName = HeaderName::from_static("x-reason");
impl ReceiveAuth for TestAuth {
type Principal = String;
fn owner(principal: &String) -> &str {
principal
}
async fn authenticate(&self, parts: &Parts, _kind: RequestKind) -> Result<String, Refusal> {
let polled = self.polled.load(Ordering::SeqCst);
self.polled_at_authenticate.store(polled, Ordering::SeqCst);
let principal = parts.headers["x-principal"].to_str().unwrap();
if principal == "deny" {
let refusal = Refusal::unauthorized("the test denies the request")
.with_header(REASON, HeaderValue::from_static("deny"));
return Err(refusal);
}
Ok(principal.to_owned())
}
async fn open(&self, principal: &String, _hello: &Hello) -> Result<SessionSetup, Refusal> {
self.opens.fetch_add(1, Ordering::SeqCst);
let policy = match principal.as_str() {
"forbidden" => {
let value = HeaderValue::from_static;
let refusal = Refusal::forbidden("the test forbids the session")
.with_header(WWW_AUTHENTICATE, value("Test first"))
.with_header(CONTENT_LENGTH, value("999"))
.with_header(TRANSFER_ENCODING, value("chunked"))
.with_header(CONNECTION, value("keep-alive"))
.with_header(WWW_AUTHENTICATE, value("Test second"))
.with_header(REASON, value("forbidden"));
return Err(refusal);
}
"strict" => ReceivePolicy {
rules: ostrya::access::AccessRules {
allow: ["r:**", "w:other/**"]
.iter()
.map(|item| ostrya::access::AllowItem::parse(item).unwrap())
.collect(),
deny: Vec::new(),
},
..ReceivePolicy::default()
},
_ => pushing_policy(),
};
Ok(SessionSetup {
policy: Arc::new(policy),
hooks: None,
})
}
}
static CHUNK: [u8; 64 * 1024] = [0; 64 * 1024];
struct Endless {
polled: Arc<AtomicU64>,
_not_sync: PhantomData<Cell<()>>,
}
impl Body for Endless {
type Data = Bytes;
type Error = io::Error;
fn poll_frame(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<io::Result<Frame<Bytes>>>> {
self.polled.fetch_add(CHUNK.len() as u64, Ordering::SeqCst);
Poll::Ready(Some(Ok(Frame::data(Bytes::from_static(&CHUNK)))))
}
}
#[test]
fn a_refusal_of_authenticate_reads_no_byte_of_the_body() {
let tmp = TmpRepo::new("receive-auth-refused");
let receive = endpoint_with(&tmp, 1, TestAuth::new());
block_on(async {
let unknown = "ab".repeat(32);
for path in ["session".to_string(), format!("session/{unknown}/have")] {
receive.auth.polled.store(0, Ordering::SeqCst);
let req = Request::builder()
.method(Method::POST)
.uri(format!("{PREFIX}{path}"))
.header("x-principal", "deny")
.body(Endless {
polled: receive.auth.polled.clone(),
_not_sync: PhantomData,
})
.unwrap();
let response = send(receive.handle(req)).await;
let at_authenticate = receive.auth.polled_at_authenticate.load(Ordering::SeqCst);
assert_eq!(at_authenticate, 0, "{path}");
let polled = receive.auth.polled.load(Ordering::SeqCst);
assert!(polled > MAX_DRAIN, "{path}: {polled}");
assert!(polled <= MAX_DRAIN + CHUNK.len() as u64, "{path}: {polled}");
assert_eq!(response.status(), StatusCode::UNAUTHORIZED, "{path}");
let headers = response.headers();
assert_eq!(headers[REASON], "deny");
assert!(headers.get(WWW_AUTHENTICATE).is_none());
assert_eq!(headers[CONNECTION], "close");
let (_, reply) = answer(response).await;
let e = error(reply);
assert_eq!(e.code, ErrorCode::Unauthorized);
assert_eq!(e.message, "the test denies the request");
}
assert_eq!(receive.auth.opens(), 0);
});
}
#[test]
fn a_refusal_of_open_gets_its_status_and_headers_and_frees_the_slot() {
let tmp = TmpRepo::new("receive-open-refused");
let receive = endpoint_with(&tmp, 1, TestAuth::new());
block_on(async {
let response = post_as(&receive, "forbidden", "session", &hello(), None).await;
let headers = response.headers().clone();
let (status, reply) = answer(response).await;
assert_eq!(status, StatusCode::FORBIDDEN);
let challenges: Vec<_> = headers.get_all(WWW_AUTHENTICATE).iter().collect();
assert_eq!(challenges, ["Test first", "Test second"]);
assert_eq!(headers[REASON], "forbidden");
assert!(headers.get(TRANSFER_ENCODING).is_none());
assert!(headers.get(CONNECTION).is_none());
assert!(headers.get(SESSION_HEADER).is_none());
let lengths: Vec<_> = headers.get_all(CONTENT_LENGTH).iter().collect();
let frame = encode(&reply).unwrap();
assert_eq!(lengths, [frame.len().to_string().as_str()]);
let e = error(reply);
assert_eq!(e.code, ErrorCode::Unauthorized);
assert_eq!(e.message, "the test forbids the session");
assert_eq!(receive.auth.opens(), 1);
open_as(&receive, "alice").await;
assert_eq!(receive.auth.opens(), 2);
receive.table.close_all();
});
}
#[test]
fn a_long_refusal_message_is_cut_to_fit_its_frame() {
let long = format!("a{}", "é".repeat(1 << 20));
let refusal = Refusal::unauthorized(long.clone())
.with_header(REASON, HeaderValue::from_static("long"));
let response = refusal_response(refusal);
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
assert_eq!(response.headers()[REASON], "long");
let (_, reply) = block_on(answer(response));
let e = error(reply);
assert_eq!(e.code, ErrorCode::Unauthorized);
assert!(e.message.len() <= 4096, "{}", e.message.len());
assert!(long.starts_with(&e.message));
assert!(e.message.len() > 4000, "{}", e.message.len());
}
#[test]
fn the_policy_of_open_is_the_policy_of_the_session() {
let tmp = TmpRepo::new("receive-open-policy");
let receive = endpoint_with(&tmp, 2, TestAuth::new());
block_on(async {
let strict = open_as(&receive, "strict").await;
let alice = open_as(&receive, "alice").await;
let commit = Message::Commit(CommitRequest {
updates: vec![RefUpdate {
name: "main".into(),
expected: Expected::Absent,
new: Some(Checksum::from_bytes([7; 32])),
}],
force: false,
server_commit: None,
});
let path = format!("session/{strict}/commit");
let response = post_as(&receive, "alice", &path, &commit, None).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let (status, reply) =
answer(post_as(&receive, "strict", &path, &commit, None).await).await;
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
assert_eq!(error(reply).code, ErrorCode::RefDenied);
let path = format!("session/{alice}/commit");
let (status, reply) =
answer(post_as(&receive, "alice", &path, &commit, None).await).await;
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
assert_eq!(error(reply).code, ErrorCode::MissingObjects);
});
}
#[test]
fn a_bad_ref_name_is_invalid_ref_before_open() {
let tmp = TmpRepo::new("receive-bad-name");
let receive = endpoint_with(&tmp, 1, TestAuth::new());
block_on(async {
let bad = Message::Hello(Hello {
version: 1,
agent: None,
refs: vec!["main".into(), "a//b".into()],
collection: None,
one_way: false,
});
for open_before in [false, true] {
if open_before {
open_as(&receive, "alice").await;
}
let opens = receive.auth.opens();
let (status, reply) =
answer(post_as(&receive, "alice", "session", &bad, None).await).await;
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
assert_eq!(error(reply).code, ErrorCode::InvalidRef);
assert_eq!(receive.auth.opens(), opens);
}
receive.table.close_all();
});
}
#[test]
fn a_one_way_hello_is_protocol_before_its_version() {
let tmp = TmpRepo::new("receive-one-way");
let receive = endpoint_with(&tmp, 1, TestAuth::new());
block_on(async {
let one_way = Message::Hello(Hello {
version: 2,
agent: None,
refs: vec!["main".into()],
collection: None,
one_way: true,
});
let (status, reply) =
answer(post_as(&receive, "alice", "session", &one_way, None).await).await;
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
let e = error(reply);
assert_eq!(e.code, ErrorCode::Protocol);
assert_eq!(
e.message,
"a Hello with one-way true opens a one-way stream, which this session does not read"
);
assert_eq!(receive.auth.opens(), 0);
});
}
#[test]
fn a_full_table_gets_503_before_open() {
let tmp = TmpRepo::new("receive-full");
let receive = endpoint_with(&tmp, 1, TestAuth::new());
block_on(async {
open_as(&receive, "alice").await;
assert_eq!(receive.auth.opens(), 1);
for stopped in [false, true] {
if stopped {
receive.table.close_all();
}
let (status, reply) =
answer(post_as(&receive, "alice", "session", &hello(), None).await).await;
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "stopped {stopped}");
assert_eq!(error(reply).code, ErrorCode::LimitExceeded);
assert_eq!(receive.auth.opens(), 1);
}
});
}
const _: fn() = || {
fn handle_is_send<A: ReceiveAuth>(
receive: &Receive<A>,
req: Request<Pieces>,
) -> impl Future<Output = Response<ServeBody>> + Send {
receive.handle(req)
}
let _ = handle_is_send::<TestAuth>;
};
}