diff options
Diffstat (limited to 'gitolfs3-server/src/api.rs')
| -rw-r--r-- | gitolfs3-server/src/api.rs | 223 |
1 files changed, 117 insertions, 106 deletions
diff --git a/gitolfs3-server/src/api.rs b/gitolfs3-server/src/api.rs index dba7ada..87c1856 100644 --- a/gitolfs3-server/src/api.rs +++ b/gitolfs3-server/src/api.rs | |||
| @@ -1,89 +1,33 @@ | |||
| 1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
| 2 | 2 | ||
| 3 | use axum::{ | 3 | use axum::{ |
| 4 | async_trait, | ||
| 5 | extract::{rejection, FromRequest, FromRequestParts, Request}, | ||
| 6 | http::{header, request::Parts, HeaderValue, StatusCode}, | ||
| 7 | response::{IntoResponse, Response}, | ||
| 8 | Extension, Json, | 4 | Extension, Json, |
| 5 | extract::{FromRequest, FromRequestParts, Request, rejection}, | ||
| 6 | http, | ||
| 7 | response::{IntoResponse, Response}, | ||
| 9 | }; | 8 | }; |
| 10 | use chrono::{DateTime, Utc}; | 9 | use chrono::{DateTime, Utc}; |
| 11 | use gitolfs3_common::{Oid, Operation}; | 10 | use gitolfs3_common::{Oid, Operation}; |
| 12 | use serde::{de::DeserializeOwned, Deserialize, Serialize}; | 11 | use serde::{Deserialize, Serialize, de::DeserializeOwned}; |
| 13 | |||
| 14 | pub const REPO_NOT_FOUND: GitLfsErrorResponse = | ||
| 15 | make_error_resp(StatusCode::NOT_FOUND, "Repository not found"); | ||
| 16 | |||
| 17 | #[derive(Clone)] | ||
| 18 | pub struct RepositoryName(pub String); | ||
| 19 | |||
| 20 | pub struct RepositoryNameRejection; | ||
| 21 | |||
| 22 | impl IntoResponse for RepositoryNameRejection { | ||
| 23 | fn into_response(self) -> Response { | ||
| 24 | (StatusCode::INTERNAL_SERVER_ERROR, "Missing repository name").into_response() | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | #[async_trait] | ||
| 29 | impl<S: Send + Sync> FromRequestParts<S> for RepositoryName { | ||
| 30 | type Rejection = RepositoryNameRejection; | ||
| 31 | |||
| 32 | async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { | ||
| 33 | let Ok(Extension(repo_name)) = Extension::<Self>::from_request_parts(parts, state).await | ||
| 34 | else { | ||
| 35 | return Err(RepositoryNameRejection); | ||
| 36 | }; | ||
| 37 | Ok(repo_name) | ||
| 38 | } | ||
| 39 | } | ||
| 40 | 12 | ||
| 41 | #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)] | 13 | // ----------------------- Generic facilities ---------------------- |
| 42 | pub enum TransferAdapter { | ||
| 43 | #[serde(rename = "basic")] | ||
| 44 | Basic, | ||
| 45 | #[serde(other)] | ||
| 46 | Unknown, | ||
| 47 | } | ||
| 48 | 14 | ||
| 49 | #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)] | 15 | pub type GitLfsErrorResponse<'a> = (http::StatusCode, GitLfsJson<GitLfsErrorData<'a>>); |
| 50 | pub enum HashAlgo { | ||
| 51 | #[serde(rename = "sha256")] | ||
| 52 | Sha256, | ||
| 53 | #[serde(other)] | ||
| 54 | Unknown, | ||
| 55 | } | ||
| 56 | 16 | ||
| 57 | impl Default for HashAlgo { | 17 | #[derive(Debug, Serialize)] |
| 58 | fn default() -> Self { | 18 | pub struct GitLfsErrorData<'a> { |
| 59 | Self::Sha256 | 19 | pub message: &'a str, |
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | #[derive(Debug, Deserialize, PartialEq, Eq, Clone)] | ||
| 64 | pub struct BatchRequestObject { | ||
| 65 | pub oid: Oid, | ||
| 66 | pub size: i64, | ||
| 67 | } | ||
| 68 | |||
| 69 | #[derive(Debug, Serialize, Deserialize, Clone)] | ||
| 70 | struct BatchRef { | ||
| 71 | name: String, | ||
| 72 | } | 20 | } |
| 73 | 21 | ||
| 74 | fn default_transfers() -> Vec<TransferAdapter> { | 22 | pub const fn make_error_resp<'a>( |
| 75 | vec![TransferAdapter::Basic] | 23 | code: http::StatusCode, |
| 24 | message: &'a str, | ||
| 25 | ) -> GitLfsErrorResponse<'a> { | ||
| 26 | (code, GitLfsJson(Json(GitLfsErrorData { message }))) | ||
| 76 | } | 27 | } |
| 77 | 28 | ||
| 78 | #[derive(Debug, Deserialize, PartialEq, Eq, Clone)] | 29 | pub const REPO_NOT_FOUND: GitLfsErrorResponse = |
| 79 | pub struct BatchRequest { | 30 | make_error_resp(http::StatusCode::NOT_FOUND, "Repository not found"); |
| 80 | pub operation: Operation, | ||
| 81 | #[serde(default = "default_transfers")] | ||
| 82 | pub transfers: Vec<TransferAdapter>, | ||
| 83 | pub objects: Vec<BatchRequestObject>, | ||
| 84 | #[serde(default)] | ||
| 85 | pub hash_algo: HashAlgo, | ||
| 86 | } | ||
| 87 | 31 | ||
| 88 | #[derive(Debug, Clone)] | 32 | #[derive(Debug, Clone)] |
| 89 | pub struct GitLfsJson<T>(pub Json<T>); | 33 | pub struct GitLfsJson<T>(pub Json<T>); |
| @@ -100,7 +44,7 @@ impl IntoResponse for GitLfsJsonRejection { | |||
| 100 | match self { | 44 | match self { |
| 101 | Self::Json(rej) => rej.into_response(), | 45 | Self::Json(rej) => rej.into_response(), |
| 102 | Self::MissingGitLfsJsonContentType => make_error_resp( | 46 | Self::MissingGitLfsJsonContentType => make_error_resp( |
| 103 | StatusCode::UNSUPPORTED_MEDIA_TYPE, | 47 | http::StatusCode::UNSUPPORTED_MEDIA_TYPE, |
| 104 | &format!("Expected request with `Content-Type: {LFS_MIME}`"), | 48 | &format!("Expected request with `Content-Type: {LFS_MIME}`"), |
| 105 | ) | 49 | ) |
| 106 | .into_response(), | 50 | .into_response(), |
| @@ -125,7 +69,7 @@ pub fn is_git_lfs_json_mimetype(mimetype: &str) -> bool { | |||
| 125 | } | 69 | } |
| 126 | 70 | ||
| 127 | fn has_git_lfs_json_content_type(req: &Request) -> bool { | 71 | fn has_git_lfs_json_content_type(req: &Request) -> bool { |
| 128 | let Some(content_type) = req.headers().get(header::CONTENT_TYPE) else { | 72 | let Some(content_type) = req.headers().get(http::header::CONTENT_TYPE) else { |
| 129 | return false; | 73 | return false; |
| 130 | }; | 74 | }; |
| 131 | let Ok(content_type) = content_type.to_str() else { | 75 | let Ok(content_type) = content_type.to_str() else { |
| @@ -134,7 +78,6 @@ fn has_git_lfs_json_content_type(req: &Request) -> bool { | |||
| 134 | is_git_lfs_json_mimetype(content_type) | 78 | is_git_lfs_json_mimetype(content_type) |
| 135 | } | 79 | } |
| 136 | 80 | ||
| 137 | #[async_trait] | ||
| 138 | impl<T, S> FromRequest<S> for GitLfsJson<T> | 81 | impl<T, S> FromRequest<S> for GitLfsJson<T> |
| 139 | where | 82 | where |
| 140 | T: DeserializeOwned, | 83 | T: DeserializeOwned, |
| @@ -158,46 +101,97 @@ impl<T: Serialize> IntoResponse for GitLfsJson<T> { | |||
| 158 | let GitLfsJson(json) = self; | 101 | let GitLfsJson(json) = self; |
| 159 | let mut resp = json.into_response(); | 102 | let mut resp = json.into_response(); |
| 160 | resp.headers_mut().insert( | 103 | resp.headers_mut().insert( |
| 161 | header::CONTENT_TYPE, | 104 | http::header::CONTENT_TYPE, |
| 162 | HeaderValue::from_static("application/vnd.git-lfs+json; charset=utf-8"), | 105 | http::HeaderValue::from_static("application/vnd.git-lfs+json; charset=utf-8"), |
| 163 | ); | 106 | ); |
| 164 | resp | 107 | resp |
| 165 | } | 108 | } |
| 166 | } | 109 | } |
| 167 | 110 | ||
| 168 | #[derive(Debug, Serialize)] | 111 | #[derive(Clone)] |
| 169 | pub struct GitLfsErrorData<'a> { | 112 | pub struct RepositoryName(pub String); |
| 170 | pub message: &'a str, | 113 | |
| 114 | pub struct RepositoryNameRejection; | ||
| 115 | |||
| 116 | impl IntoResponse for RepositoryNameRejection { | ||
| 117 | fn into_response(self) -> Response { | ||
| 118 | ( | ||
| 119 | http::StatusCode::INTERNAL_SERVER_ERROR, | ||
| 120 | "Missing repository name", | ||
| 121 | ) | ||
| 122 | .into_response() | ||
| 123 | } | ||
| 171 | } | 124 | } |
| 172 | 125 | ||
| 173 | pub type GitLfsErrorResponse<'a> = (StatusCode, GitLfsJson<GitLfsErrorData<'a>>); | 126 | impl<S: Send + Sync> FromRequestParts<S> for RepositoryName { |
| 127 | type Rejection = RepositoryNameRejection; | ||
| 174 | 128 | ||
| 175 | pub const fn make_error_resp(code: StatusCode, message: &str) -> GitLfsErrorResponse { | 129 | async fn from_request_parts( |
| 176 | (code, GitLfsJson(Json(GitLfsErrorData { message }))) | 130 | parts: &mut http::request::Parts, |
| 131 | state: &S, | ||
| 132 | ) -> Result<Self, Self::Rejection> { | ||
| 133 | let Ok(Extension(repo_name)) = Extension::<Self>::from_request_parts(parts, state).await | ||
| 134 | else { | ||
| 135 | return Err(RepositoryNameRejection); | ||
| 136 | }; | ||
| 137 | Ok(repo_name) | ||
| 138 | } | ||
| 177 | } | 139 | } |
| 178 | 140 | ||
| 179 | #[derive(Debug, Serialize, Clone)] | 141 | // ----------------------- Git LFS Batch API ----------------------- |
| 180 | pub struct BatchResponseObjectAction { | 142 | |
| 181 | pub href: String, | 143 | #[derive(Debug, Deserialize, PartialEq, Eq, Clone)] |
| 182 | #[serde(skip_serializing_if = "HashMap::is_empty")] | 144 | pub struct BatchRequest { |
| 183 | pub header: HashMap<String, String>, | 145 | pub operation: Operation, |
| 184 | pub expires_at: DateTime<Utc>, | 146 | #[serde(default = "default_transfers")] |
| 147 | pub transfers: Vec<TransferAdapter>, | ||
| 148 | pub objects: Vec<BatchRequestObject>, | ||
| 149 | #[serde(default)] | ||
| 150 | pub hash_algo: HashAlgo, | ||
| 185 | } | 151 | } |
| 186 | 152 | ||
| 187 | #[derive(Default, Debug, Serialize, Clone)] | 153 | #[derive(Debug, Deserialize, PartialEq, Eq, Clone)] |
| 188 | pub struct BatchResponseObjectActions { | 154 | pub struct BatchRequestObject { |
| 189 | #[serde(skip_serializing_if = "Option::is_none")] | 155 | pub oid: Oid, |
| 190 | pub upload: Option<BatchResponseObjectAction>, | 156 | pub size: i64, |
| 191 | #[serde(skip_serializing_if = "Option::is_none")] | ||
| 192 | pub download: Option<BatchResponseObjectAction>, | ||
| 193 | #[serde(skip_serializing_if = "Option::is_none")] | ||
| 194 | pub verify: Option<BatchResponseObjectAction>, | ||
| 195 | } | 157 | } |
| 196 | 158 | ||
| 197 | #[derive(Debug, Clone, Serialize)] | 159 | #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)] |
| 198 | pub struct BatchResponseObjectError { | 160 | pub enum TransferAdapter { |
| 199 | pub code: u16, | 161 | #[serde(rename = "basic")] |
| 200 | pub message: String, | 162 | Basic, |
| 163 | #[serde(other)] | ||
| 164 | Unknown, | ||
| 165 | } | ||
| 166 | |||
| 167 | fn default_transfers() -> Vec<TransferAdapter> { | ||
| 168 | vec![TransferAdapter::Basic] | ||
| 169 | } | ||
| 170 | |||
| 171 | #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)] | ||
| 172 | pub enum HashAlgo { | ||
| 173 | #[serde(rename = "sha256")] | ||
| 174 | Sha256, | ||
| 175 | #[serde(other)] | ||
| 176 | Unknown, | ||
| 177 | } | ||
| 178 | |||
| 179 | impl Default for HashAlgo { | ||
| 180 | fn default() -> Self { | ||
| 181 | Self::Sha256 | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | #[derive(Debug, Serialize, Deserialize, Clone)] | ||
| 186 | struct BatchRef { | ||
| 187 | name: String, | ||
| 188 | } | ||
| 189 | |||
| 190 | #[derive(Debug, Serialize, Clone)] | ||
| 191 | pub struct BatchResponse { | ||
| 192 | pub transfer: TransferAdapter, | ||
| 193 | pub objects: Vec<BatchResponseObject>, | ||
| 194 | pub hash_algo: HashAlgo, | ||
| 201 | } | 195 | } |
| 202 | 196 | ||
| 203 | #[derive(Debug, Serialize, Clone)] | 197 | #[derive(Debug, Serialize, Clone)] |
| @@ -211,10 +205,16 @@ pub struct BatchResponseObject { | |||
| 211 | pub error: Option<BatchResponseObjectError>, | 205 | pub error: Option<BatchResponseObjectError>, |
| 212 | } | 206 | } |
| 213 | 207 | ||
| 208 | #[derive(Debug, Clone, Serialize)] | ||
| 209 | pub struct BatchResponseObjectError { | ||
| 210 | pub code: u16, | ||
| 211 | pub message: String, | ||
| 212 | } | ||
| 213 | |||
| 214 | impl BatchResponseObject { | 214 | impl BatchResponseObject { |
| 215 | pub fn error( | 215 | pub fn error( |
| 216 | obj: &BatchRequestObject, | 216 | obj: &BatchRequestObject, |
| 217 | code: StatusCode, | 217 | code: http::StatusCode, |
| 218 | message: String, | 218 | message: String, |
| 219 | ) -> BatchResponseObject { | 219 | ) -> BatchResponseObject { |
| 220 | BatchResponseObject { | 220 | BatchResponseObject { |
| @@ -231,10 +231,21 @@ impl BatchResponseObject { | |||
| 231 | } | 231 | } |
| 232 | 232 | ||
| 233 | #[derive(Debug, Serialize, Clone)] | 233 | #[derive(Debug, Serialize, Clone)] |
| 234 | pub struct BatchResponse { | 234 | pub struct BatchResponseObjectAction { |
| 235 | pub transfer: TransferAdapter, | 235 | pub href: String, |
| 236 | pub objects: Vec<BatchResponseObject>, | 236 | #[serde(skip_serializing_if = "HashMap::is_empty")] |
| 237 | pub hash_algo: HashAlgo, | 237 | pub header: HashMap<String, String>, |
| 238 | pub expires_at: DateTime<Utc>, | ||
| 239 | } | ||
| 240 | |||
| 241 | #[derive(Default, Debug, Serialize, Clone)] | ||
| 242 | pub struct BatchResponseObjectActions { | ||
| 243 | #[serde(skip_serializing_if = "Option::is_none")] | ||
| 244 | pub upload: Option<BatchResponseObjectAction>, | ||
| 245 | #[serde(skip_serializing_if = "Option::is_none")] | ||
| 246 | pub download: Option<BatchResponseObjectAction>, | ||
| 247 | #[serde(skip_serializing_if = "Option::is_none")] | ||
| 248 | pub verify: Option<BatchResponseObjectAction>, | ||
| 238 | } | 249 | } |
| 239 | 250 | ||
| 240 | #[test] | 251 | #[test] |