referer_url refactoring

This commit is contained in:
Andriy Rakhnin 2024-08-16 12:04:21 +02:00 committed by Sunshine
parent c10c78a27d
commit ebf96bf1e5
4 changed files with 23 additions and 20 deletions

View File

@ -79,10 +79,10 @@ pub fn parse_data_url(url: &Url) -> (String, String, Vec<u8>) {
(media_type, charset, blob)
}
pub fn referer_url(url: Url) -> Url {
pub fn get_referer_url(url: Url) -> Url {
let mut url = url.clone();
// https://httpwg.org/specs/rfc9110.html#field.referer
// MUST NOT include the fragment and userinfo components of the URI
// Spec: https://httpwg.org/specs/rfc9110.html#field.referer
// Must not include the fragment and userinfo components of the URI
url.set_fragment(None);
url.set_username(&"").unwrap();
url.set_password(None).unwrap();

View File

@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
use url::Url;
use crate::opts::Options;
use crate::url::{clean_url, parse_data_url, referer_url};
use crate::url::{clean_url, get_referer_url, parse_data_url};
const ANSI_COLOR_RED: &'static str = "\x1b[31m";
const ANSI_COLOR_RESET: &'static str = "\x1b[0m";
@ -302,7 +302,7 @@ pub fn retrieve_asset(
if parent_url != url {
headers.insert(
REFERER,
HeaderValue::from_str(referer_url(parent_url.clone()).as_str()).unwrap(),
HeaderValue::from_str(get_referer_url(parent_url.clone()).as_str()).unwrap(),
);
}
match client.get(url.as_str()).headers(headers).send() {

View File

@ -13,18 +13,20 @@ mod passing {
#[test]
fn preserve_original() {
let u: Url = Url::parse("https://somewhere.com/font.eot#iefix").unwrap();
let referer_u: Url = url::referer_url(u.clone());
assert_eq!(referer_u.as_str(), "https://somewhere.com/font.eot");
assert_eq!(u.as_str(), "https://somewhere.com/font.eot#iefix");
let original_url: Url = Url::parse("https://somewhere.com/font.eot#iefix").unwrap();
let referer_url: Url = url::get_referer_url(original_url.clone());
assert_eq!(referer_url.as_str(), "https://somewhere.com/font.eot");
assert_eq!(
original_url.as_str(),
"https://somewhere.com/font.eot#iefix"
);
}
#[test]
fn removes_fragment() {
assert_eq!(
url::referer_url(Url::parse("https://somewhere.com/font.eot#iefix").unwrap()).as_str(),
url::get_referer_url(Url::parse("https://somewhere.com/font.eot#iefix").unwrap())
.as_str(),
"https://somewhere.com/font.eot"
);
}
@ -32,7 +34,7 @@ mod passing {
#[test]
fn removes_empty_fragment() {
assert_eq!(
url::referer_url(Url::parse("https://somewhere.com/font.eot#").unwrap()).as_str(),
url::get_referer_url(Url::parse("https://somewhere.com/font.eot#").unwrap()).as_str(),
"https://somewhere.com/font.eot"
);
}
@ -40,7 +42,7 @@ mod passing {
#[test]
fn removes_empty_fragment_and_keeps_empty_query() {
assert_eq!(
url::referer_url(Url::parse("https://somewhere.com/font.eot?#").unwrap()).as_str(),
url::get_referer_url(Url::parse("https://somewhere.com/font.eot?#").unwrap()).as_str(),
"https://somewhere.com/font.eot?"
);
}
@ -48,7 +50,8 @@ mod passing {
#[test]
fn removes_empty_fragment_and_keeps_query() {
assert_eq!(
url::referer_url(Url::parse("https://somewhere.com/font.eot?a=b&#").unwrap()).as_str(),
url::get_referer_url(Url::parse("https://somewhere.com/font.eot?a=b&#").unwrap())
.as_str(),
"https://somewhere.com/font.eot?a=b&"
);
}
@ -56,7 +59,7 @@ mod passing {
#[test]
fn removes_credentials() {
assert_eq!(
url::referer_url(Url::parse("https://cookie:monster@gibson.lan/path").unwrap())
url::get_referer_url(Url::parse("https://cookie:monster@gibson.lan/path").unwrap())
.as_str(),
"https://gibson.lan/path"
);
@ -65,7 +68,7 @@ mod passing {
#[test]
fn removes_empty_credentials() {
assert_eq!(
url::referer_url(Url::parse("https://@gibson.lan/path").unwrap()).as_str(),
url::get_referer_url(Url::parse("https://@gibson.lan/path").unwrap()).as_str(),
"https://gibson.lan/path"
);
}
@ -73,7 +76,7 @@ mod passing {
#[test]
fn removes_empty_username_credentials() {
assert_eq!(
url::referer_url(Url::parse("https://:monster@gibson.lan/path").unwrap()).as_str(),
url::get_referer_url(Url::parse("https://:monster@gibson.lan/path").unwrap()).as_str(),
"https://gibson.lan/path"
);
}
@ -81,7 +84,7 @@ mod passing {
#[test]
fn removes_empty_password_credentials() {
assert_eq!(
url::referer_url(Url::parse("https://cookie@gibson.lan/path").unwrap()).as_str(),
url::get_referer_url(Url::parse("https://cookie@gibson.lan/path").unwrap()).as_str(),
"https://gibson.lan/path"
);
}

View File

@ -1,6 +1,6 @@
mod clean_url;
mod create_data_url;
mod get_referer_url;
mod is_url_and_has_protocol;
mod parse_data_url;
mod referer_url;
mod resolve_url;