| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | | pub mod witness; |
| 15 | |
|
| 16 | | use axum::body::Bytes; |
| 17 | | use axum::extract::{Path, State}; |
| 18 | | use axum::http::{header, HeaderMap, Method, StatusCode}; |
| 19 | | use axum::response::{IntoResponse, Response}; |
| 20 | | use axum::routing::any; |
| 21 | | use axum::Router; |
| 22 | | use oxy_core::protocol::{wire, CONTENT_TYPE}; |
| 23 | | use oxy_core::repo::Repo; |
| 24 | | use std::collections::HashMap; |
| 25 | | use std::path::PathBuf; |
| 26 | | use std::sync::{Arc, Mutex}; |
| 27 | |
|
| 28 | | #[derive(Clone, Debug)] |
| 29 | | pub struct ServerConfig { |
| 30 | | |
| 31 | | pub root: PathBuf, |
| 32 | | |
| 33 | | pub tokens: Vec<String>, |
| 34 | | |
| 35 | | pub tls: Option<TlsFiles>, |
| 36 | | } |
| 37 | |
|
| 38 | | #[derive(Clone, Debug)] |
| 39 | | pub struct TlsFiles { |
| 40 | | pub cert: PathBuf, |
| 41 | | pub key: PathBuf, |
| 42 | | } |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 47 | | pub enum Access { |
| 48 | | Read, |
| 49 | | Push, |
| 50 | | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | | pub trait Backend: Send + Sync + 'static { |
| 56 | | |
| 57 | | |
| 58 | | |
| 59 | | fn open(&self, name: &str) -> Result<Repo, Failure>; |
| 60 | |
|
| 61 | | |
| 62 | | fn authorize(&self, name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure>; |
| 63 | |
|
| 64 | | |
| 65 | | |
| 66 | | |
| 67 | 35× | fn after_push(&self, _name: &str, _report: &oxy_core::repo::PushReport, _headers: &HeaderMap) {} |
| 68 | |
|
| 69 | | |
| 70 | | |
| 71 | | |
| 72 | | |
| 73 | | |
| 74 | | |
| 75 | 36× | fn push_notes( |
| 76 | 36× | &self, |
| 77 | 36× | _name: &str, |
| 78 | 36× | _report: &oxy_core::repo::PushReport, |
| 79 | 36× | _headers: &HeaderMap, |
| 80 | 36× | ) -> Vec<(String, String)> { |
| 81 | 36× | Vec::new() |
| 82 | 36× | } |
| 83 | |
|
| 84 | | |
| 85 | | |
| 86 | | |
| 87 | | |
| 88 | | |
| 89 | | |
| 90 | | |
| 91 | | |
| 92 | | |
| 93 | | |
| 94 | 37× | fn vet_push( |
| 95 | 37× | &self, |
| 96 | 37× | _name: &str, |
| 97 | 37× | _repo: &Repo, |
| 98 | 37× | _bundle: &oxy_core::protocol::Bundle, |
| 99 | 37× | _updates: &[oxy_core::protocol::RefUpdate], |
| 100 | 37× | _headers: &HeaderMap, |
| 101 | 37× | ) -> Result<(), Failure> { |
| 102 | 37× | Ok(()) |
| 103 | 37× | } |
| 104 | |
|
| 105 | | |
| 106 | | |
| 107 | | |
| 108 | 149× | fn add_log(&self, _name: &str, _refs: &mut oxy_core::protocol::RefsResponse) {} |
| 109 | |
|
| 110 | | |
| 111 | | |
| 112 | | |
| 113 | | |
| 114 | | |
| 115 | | |
| 116 | 149× | fn adjust_refs( |
| 117 | 149× | &self, |
| 118 | 149× | _name: &str, |
| 119 | 149× | repo: &Repo, |
| 120 | 149× | refs: &mut oxy_core::protocol::RefsResponse, |
| 121 | 149× | headers: &HeaderMap, |
| 122 | 149× | ) { |
| 123 | 149× | tailor_refs(repo, refs, headers); |
| 124 | 149× | } |
| 125 | |
|
| 126 | | |
| 127 | | |
| 128 | | |
| 129 | | |
| 130 | | |
| 131 | 0× | fn vet_tag_withdrawal(&self, _name: &str, _tag: &str) -> Result<(), String> { |
| 132 | 0× | Ok(()) |
| 133 | 0× | } |
| 134 | |
|
| 135 | | |
| 136 | | |
| 137 | 32× | fn default_bookmark(&self, _name: &str) -> Option<String> { |
| 138 | 32× | None |
| 139 | 32× | } |
| 140 | | } |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | | pub struct Server { |
| 145 | | backend: Box<dyn Backend>, |
| 146 | | locks: Mutex<HashMap<String, Arc<Mutex<()>>>>, |
| 147 | | } |
| 148 | |
|
| 149 | |
|
| 150 | | #[derive(Debug)] |
| 151 | | pub struct Failure(pub StatusCode, pub String); |
| 152 | |
|
| 153 | | impl IntoResponse for Failure { |
| 154 | 11× | fn into_response(self) -> Response { |
| 155 | 11× | (self.0, self.1).into_response() |
| 156 | 11× | } |
| 157 | | } |
| 158 | |
|
| 159 | | impl From<oxy_core::repo::RepoError> for Failure { |
| 160 | 0× | fn from(e: oxy_core::repo::RepoError) -> Failure { |
| 161 | | use oxy_core::repo::RepoError; |
| 162 | | |
| 163 | 0× | let status = match &e { |
| 164 | 0× | RepoError::Io(_) | RepoError::Store(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 165 | 0× | _ => StatusCode::BAD_REQUEST, |
| 166 | | }; |
| 167 | 0× | Failure(status, e.to_string()) |
| 168 | 0× | } |
| 169 | | } |
| 170 | |
|
| 171 | |
|
| 172 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 173 | | pub enum Endpoint { |
| 174 | | Capabilities, |
| 175 | | Refs, |
| 176 | | Negotiate, |
| 177 | | Fetch, |
| 178 | | Objects, |
| 179 | | Checkpoint, |
| 180 | | CheckpointIndex, |
| 181 | | Shallow, |
| 182 | | Push, |
| 183 | | } |
| 184 | |
|
| 185 | | impl Endpoint { |
| 186 | 419× | pub fn parse(name: &str) -> Option<Endpoint> { |
| 187 | 419× | Some(match name { |
| 188 | 419× | "capabilities" => Endpoint::Capabilities, |
| 189 | 268× | "refs" => Endpoint::Refs, |
| 190 | 119× | "negotiate" => Endpoint::Negotiate, |
| 191 | 83× | "fetch" => Endpoint::Fetch, |
| 192 | 47× | "objects" => Endpoint::Objects, |
| 193 | 46× | "checkpoint" => Endpoint::Checkpoint, |
| 194 | 46× | "checkpoint-index" => Endpoint::CheckpointIndex, |
| 195 | 46× | "shallow" => Endpoint::Shallow, |
| 196 | 46× | "push" => Endpoint::Push, |
| 197 | 0× | _ => return None, |
| 198 | | }) |
| 199 | 419× | } |
| 200 | |
|
| 201 | 3× | pub fn name(self) -> &'static str { |
| 202 | 3× | match self { |
| 203 | 2× | Endpoint::Capabilities => "capabilities", |
| 204 | 0× | Endpoint::Refs => "refs", |
| 205 | 0× | Endpoint::Negotiate => "negotiate", |
| 206 | 0× | Endpoint::Fetch => "fetch", |
| 207 | 0× | Endpoint::Objects => "objects", |
| 208 | 0× | Endpoint::Checkpoint => "checkpoint", |
| 209 | 0× | Endpoint::CheckpointIndex => "checkpoint-index", |
| 210 | 0× | Endpoint::Shallow => "shallow", |
| 211 | 1× | Endpoint::Push => "push", |
| 212 | | } |
| 213 | 3× | } |
| 214 | |
|
| 215 | | |
| 216 | 419× | pub fn method(self) -> Method { |
| 217 | 419× | match self { |
| 218 | 300× | Endpoint::Capabilities | Endpoint::Refs => Method::GET, |
| 219 | 119× | _ => Method::POST, |
| 220 | | } |
| 221 | 419× | } |
| 222 | |
|
| 223 | 419× | fn access(self) -> Access { |
| 224 | 419× | match self { |
| 225 | 46× | Endpoint::Push => Access::Push, |
| 226 | 373× | _ => Access::Read, |
| 227 | | } |
| 228 | 419× | } |
| 229 | | } |
| 230 | |
|
| 231 | 408× | fn ok(body: Vec<u8>) -> Response { |
| 232 | 408× | ([(header::CONTENT_TYPE, CONTENT_TYPE)], body).into_response() |
| 233 | 408× | } |
| 234 | |
|
| 235 | 515× | pub fn valid_repo_name(name: &str) -> bool { |
| 236 | 515× | !name.is_empty() |
| 237 | 515× | && !name.starts_with('.') |
| 238 | 514× | && name |
| 239 | 514× | .chars() |
| 240 | 2820× | .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-')) |
| 241 | 515× | } |
| 242 | |
|
| 243 | | impl Server { |
| 244 | 17× | pub fn new(backend: impl Backend) -> Arc<Server> { |
| 245 | 17× | Arc::new(Server { |
| 246 | 17× | backend: Box::new(backend), |
| 247 | 17× | locks: Mutex::new(HashMap::new()), |
| 248 | 17× | }) |
| 249 | 17× | } |
| 250 | |
|
| 251 | 0× | pub fn backend(&self) -> &dyn Backend { |
| 252 | 0× | self.backend.as_ref() |
| 253 | 0× | } |
| 254 | |
|
| 255 | 38× | fn lock_for(&self, name: &str) -> Arc<Mutex<()>> { |
| 256 | 38× | self.locks |
| 257 | 38× | .lock() |
| 258 | 38× | .unwrap() |
| 259 | 38× | .entry(name.to_string()) |
| 260 | 38× | .or_default() |
| 261 | 38× | .clone() |
| 262 | 38× | } |
| 263 | |
|
| 264 | | |
| 265 | | |
| 266 | 419× | pub async fn handle( |
| 267 | 419× | self: &Arc<Self>, |
| 268 | 419× | name: String, |
| 269 | 419× | endpoint: Endpoint, |
| 270 | 419× | method: &Method, |
| 271 | 419× | headers: &HeaderMap, |
| 272 | 419× | body: Bytes, |
| 273 | 419× | ) -> Response { |
| 274 | 419× | if *method != endpoint.method() { |
| 275 | 0× | return Failure( |
| 276 | 0× | StatusCode::METHOD_NOT_ALLOWED, |
| 277 | 0× | format!("{} is {}", endpoint.name(), endpoint.method()), |
| 278 | 0× | ) |
| 279 | 0× | .into_response(); |
| 280 | 419× | } |
| 281 | 419× | if let Err(f) = self.backend.authorize(&name, endpoint.access(), headers) { |
| 282 | 8× | return f.into_response(); |
| 283 | 411× | } |
| 284 | 411× | let claimed = headers |
| 285 | 411× | .get(oxy_core::protocol::REPO_ID_HEADER) |
| 286 | 411× | .and_then(|v| v.to_str().ok()) |
| 287 | 411× | .and_then(hex_id); |
| 288 | 411× | let server = Arc::clone(self); |
| 289 | 411× | let request_headers = headers.clone(); |
| 290 | 411× | let repo = name.clone(); |
| 291 | 411× | let result = tokio::task::spawn_blocking(move || { |
| 292 | 411× | server.respond(&repo, endpoint, body, claimed, &request_headers) |
| 293 | 411× | }) |
| 294 | 411× | .await; |
| 295 | 411× | let failure = match result { |
| 296 | 408× | Ok(Ok(r)) => return r, |
| 297 | 3× | Ok(Err(f)) => f, |
| 298 | 0× | Err(e) => Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), |
| 299 | | }; |
| 300 | | |
| 301 | 3× | let f = Failure( |
| 302 | 3× | failure.0, |
| 303 | 3× | format!("{} of {name}: {}", endpoint.name(), failure.1), |
| 304 | 3× | ); |
| 305 | 3× | eprintln!("oxy-server: {}: {}", f.0.as_u16(), f.1); |
| 306 | 3× | f.into_response() |
| 307 | 419× | } |
| 308 | |
|
| 309 | 411× | fn respond( |
| 310 | 411× | &self, |
| 311 | 411× | name: &str, |
| 312 | 411× | endpoint: Endpoint, |
| 313 | 411× | body: Bytes, |
| 314 | 411× | claimed: Option<[u8; 16]>, |
| 315 | 411× | headers: &HeaderMap, |
| 316 | 411× | ) -> Result<Response, Failure> { |
| 317 | 411× | let r = self.backend.open(name)?; |
| 318 | 409× | Ok(match endpoint { |
| 319 | 148× | Endpoint::Capabilities => ok(wire::encode_capabilities(r.id()?)), |
| 320 | | Endpoint::Refs => { |
| 321 | 149× | let mut refs = r.remote_refs()?; |
| 322 | 149× | if let Some(d) = self.backend.default_bookmark(name) { |
| 323 | 125× | refs.default_bookmark = Some(d); |
| 324 | 125× | } |
| 325 | 149× | self.backend.add_log(name, &mut refs); |
| 326 | 149× | self.backend.adjust_refs(name, &r, &mut refs, headers); |
| 327 | 149× | ok(wire::encode_refs(r.id()?, &refs)) |
| 328 | | } |
| 329 | | Endpoint::Negotiate => { |
| 330 | 36× | let (have, want) = wire::decode_negotiate(&body)?; |
| 331 | 36× | ok(wire::encode_hashes(&r.negotiate(&have, &want)?)) |
| 332 | | } |
| 333 | | Endpoint::Fetch => { |
| 334 | 36× | let (changes, max_blob) = wire::decode_fetch(&body)?; |
| 335 | 36× | ok(wire::encode_bundle( |
| 336 | 36× | &r.bundle_for_filtered(&changes, max_blob)?, |
| 337 | | )) |
| 338 | | } |
| 339 | | Endpoint::Objects => { |
| 340 | 1× | let refs = wire::decode_hashes(&body)?; |
| 341 | 1× | let bundle = oxy_core::protocol::Bundle { |
| 342 | 1× | objects: r.objects_for(&refs)?, |
| 343 | 1× | ..Default::default() |
| 344 | | }; |
| 345 | 1× | ok(wire::encode_bundle(&bundle)) |
| 346 | | } |
| 347 | | Endpoint::Checkpoint => { |
| 348 | 0× | let heads = wire::decode_hashes(&body)?; |
| 349 | 0× | let lock = self.lock_for(name); |
| 350 | 0× | let _guard = lock.lock().unwrap(); |
| 351 | 0× | let bundle = r.checkpoint_bundle_for(&heads)?; |
| 352 | 0× | let _t = oxy_core::timing::phase("serve: encode the checkpoint"); |
| 353 | 0× | ok(wire::encode_bundle(&bundle)) |
| 354 | | } |
| 355 | | Endpoint::CheckpointIndex => { |
| 356 | 0× | let heads = wire::decode_hashes(&body)?; |
| 357 | 0× | let lock = self.lock_for(name); |
| 358 | 0× | let _guard = lock.lock().unwrap(); |
| 359 | 0× | ok(wire::encode_bundle(&r.checkpoint_index_for(&heads)?)) |
| 360 | | } |
| 361 | | Endpoint::Shallow => { |
| 362 | 0× | let (heads, depth) = wire::decode_shallow(&body)?; |
| 363 | 0× | ok(wire::encode_hashes(&r.shallow_changes(&heads, depth)?)) |
| 364 | | } |
| 365 | | Endpoint::Push => { |
| 366 | 39× | let (bundle, updates, policy, tags) = wire::decode_push(&body)?; |
| 367 | | |
| 368 | | |
| 369 | 39× | self.backend |
| 370 | 39× | .vet_push(name, &r, &bundle, &updates, headers)?; |
| 371 | 38× | let lock = self.lock_for(name); |
| 372 | 38× | let _guard = lock.lock().unwrap(); |
| 373 | 38× | if r.is_unbound() { |
| 374 | | |
| 375 | 4× | match claimed { |
| 376 | 4× | Some(id) => r.bind_id(id)?, |
| 377 | | None => { |
| 378 | 0× | return Err(Failure( |
| 379 | 0× | StatusCode::BAD_REQUEST, |
| 380 | 0× | format!( |
| 381 | 0× | "this repository is unbound; push with a client that sends {}", |
| 382 | 0× | oxy_core::protocol::REPO_ID_HEADER |
| 383 | 0× | ), |
| 384 | 0× | )) |
| 385 | | } |
| 386 | | } |
| 387 | 34× | } |
| 388 | 38× | let protected = self.backend.default_bookmark(name); |
| 389 | 38× | let (refused, updates): (Vec<_>, Vec<_>) = updates.into_iter().partition(|u| { |
| 390 | 38× | u.new_state.is_zero() && protected.as_deref() == Some(u.name.as_str()) |
| 391 | 38× | }); |
| 392 | 38× | let mut kept_tags = Vec::new(); |
| 393 | 38× | let mut withdrawals_refused = Vec::new(); |
| 394 | 38× | for t in tags { |
| 395 | 3× | match t |
| 396 | 3× | .is_withdrawal() |
| 397 | 3× | .then(|| self.backend.vet_tag_withdrawal(name, &t.name)) |
| 398 | | { |
| 399 | 0× | Some(Err(reason)) => withdrawals_refused.push((t.name, reason)), |
| 400 | 3× | _ => kept_tags.push(t), |
| 401 | | } |
| 402 | | } |
| 403 | 38× | let mut report = r.receive_push_report(bundle, updates, policy, kept_tags)?; |
| 404 | | |
| 405 | | |
| 406 | 38× | if !refused.is_empty() || !withdrawals_refused.is_empty() { |
| 407 | 1× | report |
| 408 | 1× | .results |
| 409 | 1× | .retain(|r| r.name != oxy_core::repo::OBJECTS_RESULT); |
| 410 | 37× | } |
| 411 | 38× | for u in refused { |
| 412 | 1× | report.results.push(oxy_core::protocol::RefResult { |
| 413 | 1× | name: u.name.clone(), |
| 414 | 1× | ok: false, |
| 415 | 1× | reason: format!( |
| 416 | 1× | "{:?} is this repository's default bookmark and cannot be deleted", |
| 417 | 1× | u.name |
| 418 | 1× | ), |
| 419 | 1× | }); |
| 420 | 1× | } |
| 421 | 38× | for (tag, reason) in withdrawals_refused { |
| 422 | 0× | report.results.push(oxy_core::protocol::RefResult { |
| 423 | 0× | name: format!("tags/{tag}"), |
| 424 | 0× | ok: false, |
| 425 | 0× | reason, |
| 426 | 0× | }); |
| 427 | 0× | } |
| 428 | 44× | for r in report.results.iter().filter(|r| !r.ok) { |
| 429 | 9× | eprintln!( |
| 430 | 9× | "oxy-server: push to {name}: {} refused: {}", |
| 431 | 9× | r.name, r.reason |
| 432 | 9× | ); |
| 433 | 9× | } |
| 434 | 38× | for (reference, note) in self.backend.push_notes(name, &report, headers) { |
| 435 | 1× | if let Some(r) = report |
| 436 | 1× | .results |
| 437 | 1× | .iter_mut() |
| 438 | 1× | .find(|r| r.ok && r.name == reference) |
| 439 | | { |
| 440 | 1× | r.reason = if r.reason.is_empty() { |
| 441 | 1× | note |
| 442 | | } else { |
| 443 | 0× | format!("{}; {note}", r.reason) |
| 444 | | }; |
| 445 | 0× | } |
| 446 | | } |
| 447 | 38× | self.backend.after_push(name, &report, headers); |
| 448 | 38× | ok(wire::encode_push_response(&report.results)) |
| 449 | | } |
| 450 | | }) |
| 451 | 411× | } |
| 452 | | } |
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | | pub struct DirBackend { |
| 457 | | pub root: PathBuf, |
| 458 | | pub tokens: Vec<String>, |
| 459 | | } |
| 460 | |
|
| 461 | | impl Backend for DirBackend { |
| 462 | 515× | fn open(&self, name: &str) -> Result<Repo, Failure> { |
| 463 | 515× | if !valid_repo_name(name) { |
| 464 | 1× | return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into())); |
| 465 | 514× | } |
| 466 | 514× | let dir = self.root.join(name); |
| 467 | 514× | if !dir.join(".oxy").is_dir() { |
| 468 | 1× | return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into())); |
| 469 | 513× | } |
| 470 | 513× | Repo::open(&dir).map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())) |
| 471 | 515× | } |
| 472 | |
|
| 473 | 155× | fn default_bookmark(&self, name: &str) -> Option<String> { |
| 474 | 155× | self.open(name).ok().map(|r| r.config.bookmark.clone()) |
| 475 | 155× | } |
| 476 | |
|
| 477 | 366× | fn authorize(&self, _name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure> { |
| 478 | 366× | if access == Access::Read || self.tokens.is_empty() { |
| 479 | 357× | return Ok(()); |
| 480 | 9× | } |
| 481 | 9× | match bearer_token(headers) { |
| 482 | 8× | Some(t) if self.tokens.iter().any(|x| x == t) => Ok(()), |
| 483 | 6× | _ => Err(Failure( |
| 484 | 6× | StatusCode::UNAUTHORIZED, |
| 485 | 6× | "a valid bearer token is required to push".into(), |
| 486 | 6× | )), |
| 487 | | } |
| 488 | 366× | } |
| 489 | | } |
| 490 | |
|
| 491 | 378× | fn hex_id(h: &str) -> Option<[u8; 16]> { |
| 492 | 378× | let h = h.trim(); |
| 493 | 378× | if h.len() != 32 { |
| 494 | 0× | return None; |
| 495 | 378× | } |
| 496 | 378× | let mut out = [0u8; 16]; |
| 497 | 6048× | for (i, byte) in out.iter_mut().enumerate() { |
| 498 | 6048× | *byte = u8::from_str_radix(&h[2 * i..2 * i + 2], 16).ok()?; |
| 499 | | } |
| 500 | 378× | Some(out) |
| 501 | 378× | } |
| 502 | |
|
| 503 | |
|
| 504 | |
|
| 505 | 149× | pub fn client_features(headers: &HeaderMap) -> Option<&str> { |
| 506 | 149× | headers |
| 507 | 149× | .get(oxy_core::protocol::FEATURES_HEADER) |
| 508 | 149× | .and_then(|v| v.to_str().ok()) |
| 509 | 149× | } |
| 510 | |
|
| 511 | |
|
| 512 | |
|
| 513 | 149× | pub fn tailor_refs(repo: &Repo, refs: &mut oxy_core::protocol::RefsResponse, headers: &HeaderMap) { |
| 514 | 149× | repo.refs_for_client(refs, client_features(headers)); |
| 515 | 149× | } |
| 516 | |
|
| 517 | |
|
| 518 | 20× | pub fn bearer_token(headers: &HeaderMap) -> Option<&str> { |
| 519 | 20× | headers |
| 520 | 20× | .get(header::AUTHORIZATION) |
| 521 | 20× | .and_then(|v| v.to_str().ok()) |
| 522 | 20× | .and_then(|v| v.strip_prefix("Bearer ")) |
| 523 | 20× | .map(str::trim) |
| 524 | 20× | } |
| 525 | |
|
| 526 | 354× | async fn dispatch( |
| 527 | 354× | State(server): State<Arc<Server>>, |
| 528 | 354× | Path((repo, endpoint)): Path<(String, String)>, |
| 529 | 354× | method: Method, |
| 530 | 354× | headers: HeaderMap, |
| 531 | 354× | body: Bytes, |
| 532 | 354× | ) -> Response { |
| 533 | 354× | match Endpoint::parse(&endpoint) { |
| 534 | 354× | Some(e) => server.handle(repo, e, &method, &headers, body).await, |
| 535 | 0× | None => Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response(), |
| 536 | | } |
| 537 | 354× | } |
| 538 | |
|
| 539 | |
|
| 540 | |
|
| 541 | |
|
| 542 | | pub const BODY_LIMIT: usize = if usize::BITS > 32 { |
| 543 | | 4 << 30 |
| 544 | | } else { |
| 545 | | usize::MAX |
| 546 | | }; |
| 547 | |
|
| 548 | |
|
| 549 | 13× | pub fn router(cfg: ServerConfig) -> Router { |
| 550 | 13× | let server = Server::new(DirBackend { |
| 551 | 13× | root: cfg.root, |
| 552 | 13× | tokens: cfg.tokens, |
| 553 | 13× | }); |
| 554 | | |
| 555 | | |
| 556 | 13× | Router::new() |
| 557 | 13× | .route("/{repo}/oxy/v1/{endpoint}", any(dispatch)) |
| 558 | 13× | .layer(axum::extract::DefaultBodyLimit::max(BODY_LIMIT)) |
| 559 | 13× | .with_state(server) |
| 560 | 13× | } |
| 561 | |
|
| 562 | |
|
| 563 | |
|
| 564 | 13× | pub async fn serve(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> { |
| 565 | 13× | listener.set_nonblocking(true)?; |
| 566 | 13× | match cfg.tls.clone() { |
| 567 | 1× | Some(tls) => { |
| 568 | 1× | let _ = rustls::crypto::ring::default_provider().install_default(); |
| 569 | 1× | let rustls_cfg = |
| 570 | 1× | axum_server::tls_rustls::RustlsConfig::from_pem_file(&tls.cert, &tls.key) |
| 571 | 1× | .await |
| 572 | 1× | .map_err(|e| std::io::Error::other(format!("loading TLS files: {e}")))?; |
| 573 | 1× | axum_server::from_tcp_rustls(listener, rustls_cfg)? |
| 574 | 1× | .serve(router(cfg).into_make_service()) |
| 575 | 1× | .await |
| 576 | | } |
| 577 | | None => { |
| 578 | 12× | let listener = tokio::net::TcpListener::from_std(listener)?; |
| 579 | 12× | axum::serve(listener, router(cfg)).await |
| 580 | | } |
| 581 | | } |
| 582 | 0× | } |
| 583 | |
|
| 584 | |
|
| 585 | 13× | pub fn run_blocking(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> { |
| 586 | 13× | let rt = tokio::runtime::Builder::new_multi_thread() |
| 587 | 13× | .enable_all() |
| 588 | 13× | .build()?; |
| 589 | 13× | rt.block_on(serve(listener, cfg)) |
| 590 | 13× | } |