aboutsummaryrefslogtreecommitdiff
path: root/src/get_routes.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-06-24 19:04:23 +0200
committerMartin Fischer <martin@push-f.com>2021-06-24 19:41:05 +0200
commit26298bcd7ef204db4396ca2d0e603fc183220cd2 (patch)
tree9478019c3a365cb71b7cc41e12885ad25d7592b5 /src/get_routes.rs
parentd49d835e63ec654e3a5bf75b3b365354460382e8 (diff)
refactor: simplify Page and Context structs
Previously the Page struct contained references to the Controller and the http::request::Parts, so that page.render() could call controller.user_info_html(parts). This commit removes these references from the Page struct, so that it can implement Default in the future. The Context struct needs to be moved around since it contains git2::Repository, which isn't Send. Previously the Context struct also contained the http::request::Parts, so they were moved along. This commit extracts Parts out of the Context struct, so that our service function can access Parts after invoking our build_request method, allowing us to easily log request details for errors in the future.
Diffstat (limited to 'src/get_routes.rs')
-rw-r--r--src/get_routes.rs77
1 files changed, 39 insertions, 38 deletions
diff --git a/src/get_routes.rs b/src/get_routes.rs
index ff86869..3661896 100644
--- a/src/get_routes.rs
+++ b/src/get_routes.rs
@@ -7,6 +7,7 @@ use git2::Repository;
use git2::Tree;
use git2::TreeEntry;
use hyper::header;
+use hyper::http::request::Parts;
use hyper::http::response::Builder;
use hyper::StatusCode;
use serde::Deserialize;
@@ -22,6 +23,7 @@ use crate::get_renderer;
use crate::ActionParam;
use crate::Context;
use crate::Error;
+use crate::HyperResponse;
use crate::Page;
use crate::Response;
@@ -30,16 +32,17 @@ pub(crate) fn get_blob<C: Controller>(
params: ActionParam,
controller: &C,
ctx: Context,
+ parts: &Parts,
) -> Result<Response, Error> {
match params.action.as_ref() {
- "view" => view_blob(entr, params, controller, ctx),
- "edit" => edit_blob(entr, params, controller, ctx),
- "upload" => Ok(forms::upload_form(true, controller, &ctx).into()),
- "log" => log_blob(entr, params, controller, ctx),
- "diff" => diff_blob(entr, params, controller, ctx),
- "raw" => raw_blob(entr, params, controller, ctx),
- "move" => move_blob(entr, params, controller, ctx),
- "remove" => remove_blob(entr, params, controller, ctx),
+ "view" => view_blob(entr, params, controller, ctx, parts),
+ "edit" => edit_blob(entr, params, controller, ctx, parts),
+ "upload" => Ok(forms::upload_form(true, controller, &ctx, parts).into()),
+ "log" => log_blob(entr, params, controller, ctx, parts),
+ "diff" => diff_blob(entr, params, controller, ctx, parts),
+ "raw" => raw_blob(entr, params, controller, ctx, parts),
+ "move" => move_blob(entr, params, controller, ctx, parts),
+ "remove" => remove_blob(entr, params, controller, ctx, parts),
_ => Err(Error::BadRequest("unknown action".into())),
}
}
@@ -49,16 +52,15 @@ fn view_blob<C: Controller>(
params: ActionParam,
controller: &C,
ctx: Context,
+ parts: &Parts,
) -> Result<Response, Error> {
let mut page = Page {
title: ctx.file_name().unwrap().to_owned(),
body: String::new(),
- header: Some(action_links(&params.action, controller, &ctx)),
- controller,
- parts: &ctx.parts,
+ header: Some(action_links(&params.action, controller, &ctx, parts)),
};
- if let Some(access_info_html) = controller.access_info_html(&ctx) {
+ if let Some(access_info_html) = controller.access_info_html(&ctx, parts) {
page.body.push_str(&access_info_html);
}
@@ -96,8 +98,9 @@ fn edit_blob<C: Controller>(
_params: ActionParam,
controller: &C,
ctx: Context,
+ parts: &Parts,
) -> Result<Response, Error> {
- if !controller.may_write_path(&ctx) {
+ if !controller.may_write_path(&ctx, parts) {
return Err(Error::Unauthorized(
"you are not authorized to edit this file".into(),
));
@@ -113,10 +116,11 @@ fn edit_blob<C: Controller>(
None,
controller,
&ctx,
+ parts,
)
.into());
} else {
- return Ok(forms::upload_form(true, controller, &ctx).into());
+ Ok(forms::upload_form(true, controller, &ctx, parts).into())
}
}
@@ -125,15 +129,14 @@ fn log_blob<C: Controller>(
params: ActionParam,
controller: &C,
ctx: Context,
+ parts: &Parts,
) -> Result<Response, Error> {
let filename = ctx.file_name().unwrap();
let mut page = Page {
title: format!("Log for {}", filename),
body: String::new(),
- header: Some(action_links(&params.action, controller, &ctx)),
- controller,
- parts: &ctx.parts,
+ header: Some(action_links(&params.action, controller, &ctx, parts)),
};
let mut walk = ctx.repo.revwalk()?;
@@ -221,8 +224,9 @@ fn diff_blob<C: Controller>(
action_param: ActionParam,
controller: &C,
ctx: Context,
+ parts: &Parts,
) -> Result<Response, Error> {
- let params: DiffParams = ctx.parts.query()?;
+ let params: DiffParams = parts.query()?;
let branch_commit = ctx.branch_head()?;
let mut commit = find_commit(&ctx.repo, &params.id, &branch_commit.id())
@@ -251,9 +255,7 @@ fn diff_blob<C: Controller>(
return Ok(Page {
title: format!("Commit for {}", ctx.file_name().unwrap()),
body: "file removed".into(),
- header: Some(action_links(&action_param.action, controller, &ctx)),
- controller,
- parts: &ctx.parts,
+ header: Some(action_links(&action_param.action, controller, &ctx, parts)),
}
.into());
};
@@ -276,10 +278,8 @@ fn diff_blob<C: Controller>(
},
ctx.file_name().unwrap()
),
- header: Some(action_links(&action_param.action, controller, &ctx)),
+ header: Some(action_links(&action_param.action, controller, &ctx, parts)),
body: String::new(),
- controller,
- parts: &ctx.parts,
};
page.body.push_str("<div>");
if params.oldid.is_none() {
@@ -312,9 +312,9 @@ fn raw_blob<C: Controller>(
_params: ActionParam,
_controller: &C,
ctx: Context,
+ parts: &Parts,
) -> Result<Response, Error> {
- if let Some(etag) = ctx
- .parts
+ if let Some(etag) = parts
.headers
.get(header::IF_NONE_MATCH)
.and_then(|v| v.to_str().ok())
@@ -323,12 +323,13 @@ fn raw_blob<C: Controller>(
return Ok(Builder::new()
.status(StatusCode::NOT_MODIFIED)
.body("".into())
- .unwrap());
+ .unwrap()
+ .into());
}
}
let blob = ctx.repo.find_blob(entr.id()).unwrap();
- let mut resp = Response::new(blob.content().to_owned().into());
+ let mut resp = HyperResponse::new(blob.content().to_owned().into());
resp.headers_mut()
.insert(header::ETAG, format!("\"{}\"", entr.id()).parse().unwrap());
@@ -346,7 +347,7 @@ fn raw_blob<C: Controller>(
.insert(header::CONTENT_TYPE, mime.to_string().parse().unwrap());
}
}
- Ok(resp)
+ Ok(resp.into())
}
fn move_blob<C: Controller>(
@@ -354,8 +355,9 @@ fn move_blob<C: Controller>(
_params: ActionParam,
controller: &C,
ctx: Context,
+ parts: &Parts,
) -> Result<Response, Error> {
- if !controller.may_move_path(&ctx) {
+ if !controller.may_move_path(&ctx, parts) {
return Err(Error::Unauthorized(
"you are not authorized to move this file".into(),
));
@@ -372,6 +374,7 @@ fn move_blob<C: Controller>(
None,
controller,
&ctx,
+ parts,
);
}
@@ -380,8 +383,9 @@ fn remove_blob<C: Controller>(
params: ActionParam,
controller: &C,
ctx: Context,
+ parts: &Parts,
) -> Result<Response, Error> {
- if !controller.may_move_path(&ctx) {
+ if !controller.may_move_path(&ctx, parts) {
return Err(Error::Unauthorized(
"you are not authorized to remove this file".into(),
));
@@ -391,9 +395,7 @@ fn remove_blob<C: Controller>(
let page = Page {
title: format!("Remove {}", filename),
- controller,
- parts: &ctx.parts,
- header: Some(action_links(&params.action, controller, &ctx)),
+ header: Some(action_links(&params.action, controller, &ctx, parts)),
body: "<form method=post autocomplete=off>\
<label>Message <input name=msg autofocus></label>\
<button>Remove</button></form>"
@@ -407,17 +409,16 @@ pub fn view_tree<C: Controller>(
tree: Result<Tree, git2::Error>,
controller: &C,
ctx: &Context,
+ parts: &Parts,
) -> Result<Response, Error> {
let mut page = Page {
title: ctx.path.to_string_lossy().to_string(),
- controller,
- parts: &ctx.parts,
body: String::new(),
header: None,
};
page.body.push_str("<ul>");
- if ctx.parts.uri.path() != "/" {
+ if parts.uri.path() != "/" {
page.body
.push_str("<li><a href='..' title='go to parent directory'>../</a></li>");
}
@@ -445,7 +446,7 @@ pub fn view_tree<C: Controller>(
}
page.body.push_str("</ul>");
- controller.before_return_tree_page(&mut page, tree.ok(), ctx);
+ controller.before_return_tree_page(&mut page, tree.ok(), ctx, parts);
Ok(page.into())
}