mirror of
https://github.com/TeamPiped/piped-proxy.git
synced 2024-08-14 23:50:45 +00:00
Escape URL when rewriting DASH responses.
This commit is contained in:
parent
7586ae314b
commit
19dca8bdd0
1 changed files with 23 additions and 1 deletions
24
src/main.rs
24
src/main.rs
|
@ -410,7 +410,8 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
|||
for capture in captures {
|
||||
let url = capture.get(1).unwrap().as_str();
|
||||
let new_url = localize_url(url, host.as_str());
|
||||
new_resp = new_resp.replace(url, new_url.as_str());
|
||||
let new_url = escape_xml(new_url.as_str());
|
||||
new_resp = new_resp.replace(url, new_url.as_ref());
|
||||
}
|
||||
return Ok(response.body(new_resp));
|
||||
}
|
||||
|
@ -635,6 +636,27 @@ fn finalize_url(path: &str, query: BTreeMap<String, String>) -> String {
|
|||
format!("{}?{}", path, query)
|
||||
}
|
||||
|
||||
pub fn escape_xml(raw: &str) -> Cow<'_, str> {
|
||||
if !raw.contains(&['<', '>', '&', '\'', '"'][..]) {
|
||||
// If there are no characters to escape, return the original string.
|
||||
Cow::Borrowed(raw)
|
||||
} else {
|
||||
// If there are characters to escape, build a new string with the replacements.
|
||||
let mut escaped = String::with_capacity(raw.len());
|
||||
for c in raw.chars() {
|
||||
match c {
|
||||
'<' => escaped.push_str("<"),
|
||||
'>' => escaped.push_str(">"),
|
||||
'&' => escaped.push_str("&"),
|
||||
'\'' => escaped.push_str("'"),
|
||||
'"' => escaped.push_str("""),
|
||||
_ => escaped.push(c),
|
||||
}
|
||||
}
|
||||
Cow::Owned(escaped)
|
||||
}
|
||||
}
|
||||
|
||||
fn localize_url(url: &str, host: &str) -> String {
|
||||
if url.starts_with("https://") {
|
||||
let url = Url::parse(url).unwrap();
|
||||
|
|
Loading…
Reference in a new issue