Skip to content

Commit e8bbad6

Browse files
dbnicholsonalexlarsson
authored andcommitted
repo: Allow /s in repo names
At Endless we have repos organized into staging subdirectories, but the repo resource was only allowing a single path element. This changes the parameter to a single glob and then walks backwards to find a repo name that matches a path prefix.
1 parent 391e0a9 commit e8bbad6

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/app.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,15 @@ impl Config {
299299
pub fn get_repoconfig(&self, name: &str) -> Result<&RepoConfig, ApiError> {
300300
self.repos.get(name).ok_or_else (|| ApiError::BadRequest("No such repo".to_string()))
301301
}
302+
303+
pub fn get_repoconfig_from_path(&self, path: &Path) -> Result<&RepoConfig, ApiError> {
304+
for (repo, config) in self.repos.iter() {
305+
if path.starts_with(repo) {
306+
return Ok(config)
307+
}
308+
}
309+
Err(ApiError::BadRequest("No such repo".to_string()))
310+
}
302311
}
303312

304313

@@ -423,10 +432,12 @@ fn verify_repo_token(req: &HttpRequest, commit: ostree::OstreeCommit, repoconfig
423432
fn handle_repo(config: Data<Config>,
424433
req: HttpRequest) -> Result<HttpResponse, actix_web::Error> {
425434
let tail = req.match_info().query("tail");
426-
let repo = req.match_info().query("repo");
427-
let repoconfig = config.get_repoconfig(&repo)?;
435+
let tailpath = canonicalize_path(tail.trim_start_matches('/'))?;
436+
let repoconfig = config.get_repoconfig_from_path(&tailpath)?;
428437

429-
let relpath = canonicalize_path(tail.trim_start_matches('/'))?;
438+
let namepath = Path::new(&repoconfig.name);
439+
let relpath = tailpath.strip_prefix(&namepath)
440+
.map_err(|e| ApiError::InternalServerError(e.to_string()))?;
430441
let path = Path::new(&repoconfig.path).join(&relpath);
431442
if path.is_dir() {
432443
return Err(ErrorNotFound("Ignoring directory"));
@@ -511,7 +522,7 @@ pub fn create_app (
511522
resp
512523
})
513524
})
514-
.service(web::resource("/{repo}/{tail:.*}").name("repo")
525+
.service(web::resource("/{tail:.*}").name("repo")
515526
.route(web::get().to(handle_repo))
516527
.route(web::head().to(handle_repo))
517528
.to(HttpResponse::MethodNotAllowed)

0 commit comments

Comments
 (0)