Skip to content

Commit

Permalink
repo: Allow /s in repo names
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dbnicholson committed Dec 10, 2019
1 parent c072cde commit 687e810
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,20 @@ impl Config {
pub fn get_repoconfig(&self, name: &str) -> Result<&RepoConfig, ApiError> {
self.repos.get(name).ok_or_else (|| ApiError::BadRequest("No such repo".to_string()))
}

pub fn get_repoconfig_from_path(&self, path: &Path) -> Result<&RepoConfig, ApiError> {
let mut buf = PathBuf::new();

for part in path.iter() {
buf.push(part);
if let Some(name) = buf.to_str() {
if let Some(config) = self.repos.get(name) {
return Ok(config)
}
}
}
Err(ApiError::BadRequest("No such repo".to_string()))
}
}


Expand Down Expand Up @@ -423,10 +437,12 @@ fn verify_repo_token(req: &HttpRequest, commit: ostree::OstreeCommit, repoconfig
fn handle_repo(config: Data<Config>,
req: HttpRequest) -> Result<HttpResponse, actix_web::Error> {
let tail = req.match_info().query("tail");
let repo = req.match_info().query("repo");
let repoconfig = config.get_repoconfig(&repo)?;
let tailpath = canonicalize_path(tail.trim_start_matches('/'))?;
let repoconfig = config.get_repoconfig_from_path(&tailpath)?;

let relpath = canonicalize_path(tail.trim_start_matches('/'))?;
let namepath = Path::new(&repoconfig.name);
let relpath = tailpath.strip_prefix(&namepath)
.map_err(|e| ApiError::InternalServerError(e.to_string()))?;
let path = Path::new(&repoconfig.path).join(&relpath);
if path.is_dir() {
return Err(ErrorNotFound("Ignoring directory"));
Expand Down Expand Up @@ -511,7 +527,7 @@ pub fn create_app (
resp
})
})
.service(web::resource("/{repo}/{tail:.*}").name("repo")
.service(web::resource("/{tail:.*}").name("repo")
.route(web::get().to(handle_repo))
.route(web::head().to(handle_repo))
.to(HttpResponse::MethodNotAllowed)
Expand Down

0 comments on commit 687e810

Please sign in to comment.