aboutsummaryrefslogtreecommitdiff
path: root/src/get_routes.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-07-03 20:52:13 +0200
committerMartin Fischer <martin@push-f.com>2021-07-03 21:14:36 +0200
commit17d98a70a76efc02f643e9cfe13836120a2c5114 (patch)
treefc86587c7444304c70313a6b8fc289d83cfb8a0f /src/get_routes.rs
parent973acc037b2a78b1ad3d2ccb0c1dd38098335269 (diff)
refactor: introduce build_raw_response helper
Diffstat (limited to 'src/get_routes.rs')
-rw-r--r--src/get_routes.rs59
1 files changed, 31 insertions, 28 deletions
diff --git a/src/get_routes.rs b/src/get_routes.rs
index 57d7e56..cde88c3 100644
--- a/src/get_routes.rs
+++ b/src/get_routes.rs
@@ -8,8 +8,6 @@ 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;
use sputnik::html_escape;
use sputnik::request::SputnikParts;
@@ -310,11 +308,9 @@ fn diff_blob<C: Controller>(
Ok(page.into())
}
-fn raw_blob<C: Controller>(
+fn build_raw_response(
entr: TreeEntry,
- _params: ActionParam,
- _controller: &C,
- ctx: Context,
+ ctx: &Context,
parts: &Parts,
) -> Result<HyperResponse, Error> {
if let Some(etag) = parts
@@ -323,11 +319,7 @@ fn raw_blob<C: Controller>(
.and_then(|v| v.to_str().ok())
{
if etag.trim_matches('"') == entr.id().to_string() {
- return Ok(Builder::new()
- .status(StatusCode::NOT_MODIFIED)
- .body("".into())
- .unwrap()
- .into());
+ return Err(Error::NotModified);
}
}
@@ -338,25 +330,36 @@ fn raw_blob<C: Controller>(
.insert(header::ETAG, format!("\"{}\"", entr.id()).parse().unwrap());
resp.headers_mut()
.insert(header::CACHE_CONTROL, "no-cache".parse().unwrap());
+ Ok(resp)
+}
- if let Some(mime) = mime_guess::from_path(&ctx.path).first() {
- if mime.type_() == "text" {
- // workaround for Firefox, which downloads non-plain text subtypes
- // instead of displaying them (https://bugzilla.mozilla.org/1319262)
- resp.headers_mut()
- .insert(header::CONTENT_TYPE, "text/plain".parse().unwrap());
- } else {
- resp.headers_mut()
- .insert(header::CONTENT_TYPE, mime.to_string().parse().unwrap());
+fn raw_blob<C: Controller>(
+ entr: TreeEntry,
+ _params: ActionParam,
+ _controller: &C,
+ ctx: Context,
+ parts: &Parts,
+) -> Result<HyperResponse, Error> {
+ build_raw_response(entr, &ctx, parts).map(|mut resp| {
+ if let Some(mime) = mime_guess::from_path(&ctx.path).first() {
+ if mime.type_() == "text" {
+ // workaround for Firefox, which downloads non-plain text subtypes
+ // instead of displaying them (https://bugzilla.mozilla.org/1319262)
+ resp.headers_mut()
+ .insert(header::CONTENT_TYPE, "text/plain".parse().unwrap());
+ } else {
+ resp.headers_mut()
+ .insert(header::CONTENT_TYPE, mime.to_string().parse().unwrap());
+ }
}
- }
- Ok(resp)
+ resp
+ })
}
fn run_blob<C: Controller>(
entr: TreeEntry,
- params: ActionParam,
- controller: &C,
+ _params: ActionParam,
+ _controller: &C,
ctx: Context,
parts: &Parts,
) -> Result<HyperResponse, Error> {
@@ -365,18 +368,18 @@ fn run_blob<C: Controller>(
"run action only available for .html files".into(),
));
}
- raw_blob(entr, params, controller, ctx, parts).map(|mut r| {
- r.headers_mut()
+ build_raw_response(entr, &ctx, parts).map(|mut resp| {
+ resp.headers_mut()
.insert(header::CONTENT_TYPE, "text/html".parse().unwrap());
// We want users to be able to view .html applications of other users
// without having to worry about the application accessing their private
// files. So we set the CSP: sandbox header which makes browsers treat
// the page as a unique origin as per the same-origin policy.
- r.headers_mut().insert(
+ resp.headers_mut().insert(
header::CONTENT_SECURITY_POLICY,
"sandbox allow-scripts;".parse().unwrap(),
);
- r
+ resp
})
}