💾 Archived View for lists.flounder.online › patches › threads › 20210328161130.1161-3-johann@qwertqw… captured on 2022-04-28 at 19:24:16. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

[PATCH mygit 2/3] format, add catchall error pages

[PATCH mygit 2/3] format, add catchall error pages

From: johann@qwertqwefsday.eu

Date: Sun, 28 Mar 2021 18:11:29 +0200

Message-Id: 20210328161130.1161-3-johann@qwertqwefsday.eu

To: <~aw/patches@lists.sr.ht>

In-Reply-To: 20210328161130.1161-1-johann@qwertqwefsday.eu

Cc: "Johann150" <johann@qwertqwefsday.eu>

Reply

Export

--------------------------------------

From: Johann150 <johann@qwertqwefsday.eu>

---

src/errorpage.rs | 8 ++++++++

src/main.rs | 29 +++++++++++++++++++++--------

2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/src/errorpage.rs b/src/errorpage.rs

index 3df280f..a1436d3 100644

--- a/src/errorpage.rs

+++ b/src/errorpage.rs

@@ -24,6 +24,14 @@ impl<State: Clone + Send + Sync + 'static> Middleware<State> for ErrorToErrorpag

message: err.into_inner().to_string(),

}

.into();

+ if status == 405 {

+ // The origin server MUST generate an Allow header field in

+ // a 405 response containing a list of the target

+ // resource's currently supported methods. - RFC 7231§6.5.5

+ //

+ // We only ever support GET requests.

+ response.insert_header("Allow", "GET");

+ }

response.set_status(status);

}

diff --git a/src/main.rs b/src/main.rs

index 9964bfd..2ba9f68 100644

--- a/src/main.rs

+++ b/src/main.rs

@@ -7,7 +7,7 @@ use std::fs;

use std::path::Path;

use std::str;

use syntect::parsing::SyntaxSet;

-use tide::Request;

+use tide::{Request, Response};

mod errorpage;

@@ -400,7 +400,7 @@ async fn repo_file(req: Request<()>) -> tide::Result {

.find_syntax_by_extension(extension)

.unwrap_or_else(|| syntax_set.find_syntax_plain_text());

let tmpl = match tree_entry.to_object(&repo)?.into_tree() {

- // this is a subtree

+ // this is a subtree

Ok(tree) => RepoTreeTemplate {

repo: &repo,

tree,

@@ -410,19 +410,28 @@ async fn repo_file(req: Request<()>) -> tide::Result {

.into(),

// this is not a subtree, so it should be a blob i.e. file

Err(tree_obj) => {

- use syntect::{html::{ClassedHTMLGenerator, ClassStyle}, util::LinesWithEndings};

+ use syntect::{

+ html::{ClassStyle, ClassedHTMLGenerator},

+ util::LinesWithEndings,

+ };

// get file contents from git object

let file_string = str::from_utf8(tree_obj.as_blob().unwrap().content())?;

// create a highlighter that uses CSS classes so we can use prefers-color-scheme

- let mut highlighter = ClassedHTMLGenerator::new_with_class_style(&syntax, &syntax_set, ClassStyle::SpacedPrefixed{prefix:"code"});

- LinesWithEndings::from(file_string).for_each(|line| highlighter.parse_html_for_line_which_includes_newline(line));

+ let mut highlighter = ClassedHTMLGenerator::new_with_class_style(

+ &syntax,

+ &syntax_set,

+ ClassStyle::SpacedPrefixed { prefix: "code" },

+ );

+ LinesWithEndings::from(file_string)

+ .for_each(|line| highlighter.parse_html_for_line_which_includes_newline(line));

let mut output = String::from("<pre>\n");

for (n, line) in highlighter.finalize().lines().enumerate() {

output.push_str(&format!(

"<a href='#L{0}' id='L{0}' class='line'>{0}</a>{1}\n",

- n + 1, line

+ n + 1,

+ line

));

}

output.push_str("</pre>\n");

@@ -551,10 +560,12 @@ async fn main() -> Result<(), std::io::Error> {

.serve_file("templates/static/style.css")?; // TODO configurable

app.at("/:repo_name").get(repo_home);

app.at("/:repo_name/").get(repo_home);

- // git clone stuff -- handle thse urls

+

+ // git clone stuff

app.at("/:repo_name/info/refs").get(git_data);

app.at("/:repo_name/HEAD").get(git_data);

app.at("/:repo_name/objects/*obj").get(git_data);

+

app.at("/:repo_name/commit/:commit").get(repo_commit);

app.at("/:repo_name/refs").get(repo_refs);

app.at("/:repo_name/log").get(repo_log);

@@ -563,8 +574,10 @@ async fn main() -> Result<(), std::io::Error> {

app.at("/:repo_name/tree/:ref").get(repo_tree);

app.at("/:repo_name/tree/:ref/item/*object_name")

.get(repo_file);

+ app.at("*")

+ .get(|_| async { Result::<Response, tide::Error>::Err(tide::Error::from_str(404, "This page does not exist.")) })

+ .all(|_| async { Result::<Response, tide::Error>::Err(tide::Error::from_str(405, "This method is not allowed.")) });

// Raw files, patch files

app.listen(format!("[::]:{}", CONFIG.port)).await?;

- // app.all 404

Ok(())

}

--

2.20.1

[PATCH mygit v2 2/3] format, add catchall error pages

From: johann@qwertqwefsday.eu

Date: Sun, 28 Mar 2021 23:45:28 +0200

Message-Id: 20210328214529.1369-2-johann@qwertqwefsday.eu

To: <~aw/patches@lists.sr.ht>

In-Reply-To: 20210328214529.1369-1-johann@qwertqwefsday.eu

Cc: "Johann150" <johann@qwertqwefsday.eu>

Reply

Export

--------------------------------------

From: Johann150 <johann@qwertqwefsday.eu>

---

src/errorpage.rs | 8 ++++++++

src/main.rs | 29 +++++++++++++++++++++--------

2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/src/errorpage.rs b/src/errorpage.rs

index 3df280f..a1436d3 100644

--- a/src/errorpage.rs

+++ b/src/errorpage.rs

@@ -24,6 +24,14 @@ impl<State: Clone + Send + Sync + 'static> Middleware<State> for ErrorToErrorpag

message: err.into_inner().to_string(),

}

.into();

+ if status == 405 {

+ // The origin server MUST generate an Allow header field in

+ // a 405 response containing a list of the target

+ // resource's currently supported methods. - RFC 7231§6.5.5

+ //

+ // We only ever support GET requests.

+ response.insert_header("Allow", "GET");

+ }

response.set_status(status);

}

diff --git a/src/main.rs b/src/main.rs

index 9964bfd..2ba9f68 100644

--- a/src/main.rs

+++ b/src/main.rs

@@ -7,7 +7,7 @@ use std::fs;

use std::path::Path;

use std::str;

use syntect::parsing::SyntaxSet;

-use tide::Request;

+use tide::{Request, Response};

mod errorpage;

@@ -400,7 +400,7 @@ async fn repo_file(req: Request<()>) -> tide::Result {

.find_syntax_by_extension(extension)

.unwrap_or_else(|| syntax_set.find_syntax_plain_text());

let tmpl = match tree_entry.to_object(&repo)?.into_tree() {

- // this is a subtree

+ // this is a subtree

Ok(tree) => RepoTreeTemplate {

repo: &repo,

tree,

@@ -410,19 +410,28 @@ async fn repo_file(req: Request<()>) -> tide::Result {

.into(),

// this is not a subtree, so it should be a blob i.e. file

Err(tree_obj) => {

- use syntect::{html::{ClassedHTMLGenerator, ClassStyle}, util::LinesWithEndings};

+ use syntect::{

+ html::{ClassStyle, ClassedHTMLGenerator},

+ util::LinesWithEndings,

+ };

// get file contents from git object

let file_string = str::from_utf8(tree_obj.as_blob().unwrap().content())?;

// create a highlighter that uses CSS classes so we can use prefers-color-scheme

- let mut highlighter = ClassedHTMLGenerator::new_with_class_style(&syntax, &syntax_set, ClassStyle::SpacedPrefixed{prefix:"code"});

- LinesWithEndings::from(file_string).for_each(|line| highlighter.parse_html_for_line_which_includes_newline(line));

+ let mut highlighter = ClassedHTMLGenerator::new_with_class_style(

+ &syntax,

+ &syntax_set,

+ ClassStyle::SpacedPrefixed { prefix: "code" },

+ );

+ LinesWithEndings::from(file_string)

+ .for_each(|line| highlighter.parse_html_for_line_which_includes_newline(line));

let mut output = String::from("<pre>\n");

for (n, line) in highlighter.finalize().lines().enumerate() {

output.push_str(&format!(

"<a href='#L{0}' id='L{0}' class='line'>{0}</a>{1}\n",

- n + 1, line

+ n + 1,

+ line

));

}

output.push_str("</pre>\n");

@@ -551,10 +560,12 @@ async fn main() -> Result<(), std::io::Error> {

.serve_file("templates/static/style.css")?; // TODO configurable

app.at("/:repo_name").get(repo_home);

app.at("/:repo_name/").get(repo_home);

- // git clone stuff -- handle thse urls

+

+ // git clone stuff

app.at("/:repo_name/info/refs").get(git_data);

app.at("/:repo_name/HEAD").get(git_data);

app.at("/:repo_name/objects/*obj").get(git_data);

+

app.at("/:repo_name/commit/:commit").get(repo_commit);

app.at("/:repo_name/refs").get(repo_refs);

app.at("/:repo_name/log").get(repo_log);

@@ -563,8 +574,10 @@ async fn main() -> Result<(), std::io::Error> {

app.at("/:repo_name/tree/:ref").get(repo_tree);

app.at("/:repo_name/tree/:ref/item/*object_name")

.get(repo_file);

+ app.at("*")

+ .get(|_| async { Result::<Response, tide::Error>::Err(tide::Error::from_str(404, "This page does not exist.")) })

+ .all(|_| async { Result::<Response, tide::Error>::Err(tide::Error::from_str(405, "This method is not allowed.")) });

// Raw files, patch files

app.listen(format!("[::]:{}", CONFIG.port)).await?;

- // app.all 404

Ok(())

}

--

2.20.1