mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-29 10:10:30 +08:00
GitHub and GitLab will prefix archive payloads with the name of the repo and the refspec. This patch naively applies a prefix in similar fashion. The refspec might contain filesystem path separators, like a forward slash (`/`). GitHub and GitLab replace that with a hyphen (`-`). This patch applies that same replacement. The file name that is suggested to browsers (via the HTTP header `Content-Disposition`) is changed to match the prefix. The additional archive formats `tar` and `zip` are now supported as well. Users that relied on the archive *not* carrying the prefix introduced here, may pass the query `?prefix=false` to get the old behavior. This does not affect the `Content-Disposition` header, though. Users who relied on the downloaded file be named in a certain way after, and also used the file name from `Content-Disposition` for the name, cannot get back the old behavior, but must instead rename the file. Co-authored-by: Lorenz Leutgeb <lorenz.leutgeb@radicle.dev>
134 lines
3.4 KiB
Rust
134 lines
3.4 KiB
Rust
use std::process::ExitStatus;
|
|
|
|
use axum::http;
|
|
use axum::response::{IntoResponse, Response};
|
|
|
|
/// Errors relating to the Git backend.
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum GitError {
|
|
/// The entity was not found.
|
|
#[error("not found")]
|
|
NotFound,
|
|
|
|
/// I/O error.
|
|
#[error("i/o error: {0}")]
|
|
Io(#[from] std::io::Error),
|
|
|
|
/// The service is not available.
|
|
#[error("service '{0}' not available")]
|
|
ServiceUnavailable(&'static str),
|
|
|
|
/// Invalid identifier.
|
|
#[error("invalid radicle identifier: {0}")]
|
|
Id(#[from] radicle::identity::IdError),
|
|
|
|
/// Storage error.
|
|
#[error("storage: {0}")]
|
|
Storage(#[from] radicle::storage::Error),
|
|
|
|
/// Repository error.
|
|
#[error("repository: {0}")]
|
|
Repository(#[from] radicle::storage::RepositoryError),
|
|
|
|
/// Git backend error.
|
|
#[error("git-http-backend: exited with code {0}")]
|
|
BackendExited(ExitStatus),
|
|
|
|
/// Git backend error.
|
|
#[error("git-http-backend: invalid header returned: {0:?}")]
|
|
BackendHeader(String),
|
|
|
|
/// HeaderName error.
|
|
#[error(transparent)]
|
|
InvalidHeaderName(#[from] axum::http::header::InvalidHeaderName),
|
|
|
|
/// HeaderValue error.
|
|
#[error(transparent)]
|
|
InvalidHeaderValue(#[from] axum::http::header::InvalidHeaderValue),
|
|
}
|
|
|
|
impl GitError {
|
|
pub fn status(&self) -> http::StatusCode {
|
|
match self {
|
|
GitError::ServiceUnavailable(_) => http::StatusCode::SERVICE_UNAVAILABLE,
|
|
GitError::Id(_) => http::StatusCode::NOT_FOUND,
|
|
GitError::NotFound => http::StatusCode::NOT_FOUND,
|
|
_ => http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl IntoResponse for GitError {
|
|
fn into_response(self) -> Response {
|
|
tracing::error!("{}", self);
|
|
|
|
self.status().into_response()
|
|
}
|
|
}
|
|
|
|
/// Errors relating to the `/raw` route.
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum RawError {
|
|
/// Surf error.
|
|
#[error(transparent)]
|
|
Surf(#[from] radicle_surf::Error),
|
|
|
|
/// Payload error.
|
|
#[error(transparent)]
|
|
ProjectPayload(#[from] radicle::identity::PayloadError),
|
|
|
|
/// Io error.
|
|
#[error(transparent)]
|
|
Io(#[from] std::io::Error),
|
|
|
|
/// Archive error.
|
|
#[error("`git archive` exited with status {0}:\n{1}")]
|
|
Archive(ExitStatus, String),
|
|
|
|
/// Git error.
|
|
#[error(transparent)]
|
|
Git(#[from] radicle::git::raw::Error),
|
|
|
|
/// Radicle Storage error.
|
|
#[error(transparent)]
|
|
Storage(#[from] radicle::storage::Error),
|
|
|
|
/// Repository error.
|
|
#[error(transparent)]
|
|
Repository(#[from] radicle::storage::RepositoryError),
|
|
|
|
/// Http Headers error.
|
|
#[error(transparent)]
|
|
Headers(#[from] http::header::InvalidHeaderValue),
|
|
|
|
/// Surf file error.
|
|
#[error(transparent)]
|
|
SurfFile(#[from] radicle_surf::fs::error::File),
|
|
|
|
/// The entity was not found.
|
|
#[error("not found")]
|
|
NotFound,
|
|
|
|
/// Bad request.
|
|
#[error("bad request")]
|
|
BadRequest,
|
|
}
|
|
|
|
impl RawError {
|
|
pub fn status(&self) -> http::StatusCode {
|
|
match self {
|
|
RawError::BadRequest => http::StatusCode::BAD_REQUEST,
|
|
RawError::SurfFile(_) | RawError::NotFound => http::StatusCode::NOT_FOUND,
|
|
_ => http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl IntoResponse for RawError {
|
|
fn into_response(self) -> Response {
|
|
tracing::error!("{}", self);
|
|
|
|
self.status().into_response()
|
|
}
|
|
}
|