1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

Fix unnecessary owned string and change htmlescape in favor of askama_escape (#584)

This commit is contained in:
imaperson 2018-11-09 01:08:06 +01:00 committed by Nikolay Kim
parent 9ab586e24e
commit 1a0bf32ec7
3 changed files with 21 additions and 10 deletions

View File

@ -64,11 +64,11 @@ cell = ["actix-net/cell"]
actix = "^0.7.5" actix = "^0.7.5"
actix-net = "0.2.0" actix-net = "0.2.0"
askama_escape = "0.1.0"
base64 = "0.10" base64 = "0.10"
bitflags = "1.0" bitflags = "1.0"
failure = "^0.1.2" failure = "^0.1.2"
h2 = "0.1" h2 = "0.1"
htmlescape = "0.3"
http = "^0.1.8" http = "^0.1.8"
httparse = "1.3" httparse = "1.3"
log = "0.4" log = "0.4"

View File

@ -11,10 +11,10 @@ use std::{cmp, io};
#[cfg(unix)] #[cfg(unix)]
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
use askama_escape::{escape as escape_html_entity};
use bytes::Bytes; use bytes::Bytes;
use futures::{Async, Future, Poll, Stream}; use futures::{Async, Future, Poll, Stream};
use futures_cpupool::{CpuFuture, CpuPool}; use futures_cpupool::{CpuFuture, CpuPool};
use htmlescape::encode_minimal as escape_html_entity;
use mime; use mime;
use mime_guess::{get_mime_type, guess_mime_type}; use mime_guess::{get_mime_type, guess_mime_type};
use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET}; use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET};
@ -561,6 +561,20 @@ impl Directory {
} }
} }
// show file url as relative to static path
macro_rules! encode_file_url {
($path:ident) => {
utf8_percent_encode(&$path.to_string_lossy(), DEFAULT_ENCODE_SET)
};
}
// " -- &quot; & -- &amp; ' -- &#x27; < -- &lt; > -- &gt; / -- &#x2f;
macro_rules! encode_file_name {
($entry:ident) => {
escape_html_entity(&$entry.file_name().to_string_lossy())
};
}
fn directory_listing<S>( fn directory_listing<S>(
dir: &Directory, req: &HttpRequest<S>, dir: &Directory, req: &HttpRequest<S>,
) -> Result<HttpResponse, io::Error> { ) -> Result<HttpResponse, io::Error> {
@ -575,11 +589,6 @@ fn directory_listing<S>(
Ok(p) => base.join(p), Ok(p) => base.join(p),
Err(_) => continue, Err(_) => continue,
}; };
// show file url as relative to static path
let file_url = utf8_percent_encode(&p.to_string_lossy(), DEFAULT_ENCODE_SET)
.to_string();
// " -- &quot; & -- &amp; ' -- &#x27; < -- &lt; > -- &gt;
let file_name = escape_html_entity(&entry.file_name().to_string_lossy());
// if file is a directory, add '/' to the end of the name // if file is a directory, add '/' to the end of the name
if let Ok(metadata) = entry.metadata() { if let Ok(metadata) = entry.metadata() {
@ -587,13 +596,15 @@ fn directory_listing<S>(
let _ = write!( let _ = write!(
body, body,
"<li><a href=\"{}\">{}/</a></li>", "<li><a href=\"{}\">{}/</a></li>",
file_url, file_name encode_file_url!(p),
encode_file_name!(entry),
); );
} else { } else {
let _ = write!( let _ = write!(
body, body,
"<li><a href=\"{}\">{}</a></li>", "<li><a href=\"{}\">{}</a></li>",
file_url, file_name encode_file_url!(p),
encode_file_name!(entry),
); );
} }
} else { } else {

View File

@ -100,9 +100,9 @@ extern crate failure;
extern crate lazy_static; extern crate lazy_static;
#[macro_use] #[macro_use]
extern crate futures; extern crate futures;
extern crate askama_escape;
extern crate cookie; extern crate cookie;
extern crate futures_cpupool; extern crate futures_cpupool;
extern crate htmlescape;
extern crate http as modhttp; extern crate http as modhttp;
extern crate httparse; extern crate httparse;
extern crate language_tags; extern crate language_tags;