Redirect on invalid UTF-8 in /p/

Previously, uploading a paste with invalid UTF-8 and then viewing it
with the pretty URL would cause a panic.
With this change, it simply redirects to the raw URL.
This commit is contained in:
Leonora Tindall 2022-01-31 20:49:05 -06:00 committed by Leonora Tindall
commit 5aafe2500a
5 changed files with 63 additions and 9 deletions

View file

@ -0,0 +1,31 @@
use rocket::{
request::Request,
response::{Redirect, Responder, Result},
};
use rocket_dyn_templates::Template;
pub enum MaybeRedirect {
Redirect(Box<Redirect>),
Template(Box<Template>),
}
impl From<Redirect> for MaybeRedirect {
fn from(other: Redirect) -> Self {
Self::Redirect(Box::new(other))
}
}
impl From<Template> for MaybeRedirect {
fn from(other: Template) -> Self {
Self::Template(Box::new(other))
}
}
impl<'r, 'o: 'r> Responder<'r, 'o> for MaybeRedirect {
fn respond_to(self, req: &'r Request<'_>) -> Result<'o> {
match self {
Self::Template(t) => t.respond_to(req),
Self::Redirect(r) => r.respond_to(req),
}
}
}

View file

@ -1,3 +1,4 @@
pub mod maybe_redirect;
pub mod paste_id;
pub mod pretty;
pub mod pretty_syntax;

View file

@ -10,16 +10,16 @@ static SYNTAXES: &[u8] =
static THEMES: &[u8] =
include_bytes!("../../resources/themes/ayu_dark.tmTheme");
pub fn get_pretty_body(path: &Path, ext: &str) -> String {
pub fn get_pretty_body(path: &Path, ext: &str) -> std::io::Result<String> {
let ss: SyntaxSet = syntect::dumps::from_binary(SYNTAXES);
let mut theme_cursor = std::io::Cursor::new(THEMES);
let theme = ThemeSet::load_from_reader(&mut theme_cursor).unwrap();
let content = fs::read_to_string(path).unwrap();
let content = fs::read_to_string(path)?;
let syntax = ss
.find_syntax_by_token(ext)
.unwrap_or_else(|| ss.find_syntax_plain_text());
highlighted_html_for_string(&content, &ss, syntax, &theme)
Ok(highlighted_html_for_string(&content, &ss, syntax, &theme))
}