-
-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add HTTP referer header for resource requests #400
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,17 @@ pub fn parse_data_url(url: &Url) -> (String, String, Vec<u8>) { | |
(media_type, charset, blob) | ||
} | ||
|
||
pub fn referer_url(url: Url) -> Url { | ||
let mut url = url.clone(); | ||
// https://httpwg.org/specs/rfc9110.html#field.referer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think putting "Spec: " or "Reference: " before the URL could be a good way to make it more readable, otherwise it's just a commented out URL. I try not to put them myself, since URLs tend to become 404/301 sometimes, and then it'll have to be updated. There's this file here https://github.com/Y2Z/monolith/blob/master/docs/references.md that I created to keep all used references, maybe we could move it there, what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just add a It's nice to have internal documentation and refer to it, but in case when ref leads just to another/external ref, in my vision, it doesn't make sense. Anyway in case external link "died" one have to update it no matter its place in the source code. |
||
// MUST NOT include the fragment and userinfo components of the URI | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "MUST NOT" sounds a bit too commanding, as if somebody's shouting at you. "Must not" would be nicer and easier to read. |
||
url.set_fragment(None); | ||
url.set_username(&"").unwrap(); | ||
url.set_password(None).unwrap(); | ||
|
||
url | ||
} | ||
|
||
pub fn resolve_url(from: &Url, to: &str) -> Url { | ||
match Url::parse(&to) { | ||
Ok(parsed_url) => parsed_url, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ mod passing { | |
} | ||
|
||
#[test] | ||
fn removesempty_fragment_and_keeps_empty_query() { | ||
fn removes_empty_fragment_and_keeps_query() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Embarrassing, but typos aren't as shameful when they're in tests 😄 |
||
assert_eq!( | ||
url::clean_url(Url::parse("https://somewhere.com/font.eot?a=b&#").unwrap()).as_str(), | ||
"https://somewhere.com/font.eot?a=b&" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗ | ||
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝ | ||
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗ | ||
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║ | ||
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝ | ||
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ | ||
|
||
#[cfg(test)] | ||
mod passing { | ||
use reqwest::Url; | ||
|
||
use monolith::url; | ||
|
||
#[test] | ||
fn preserve_original() { | ||
let u: Url = Url::parse("https://somewhere.com/font.eot#iefix").unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: naming it |
||
|
||
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"); | ||
} | ||
|
||
#[test] | ||
fn removes_fragment() { | ||
assert_eq!( | ||
url::referer_url(Url::parse("https://somewhere.com/font.eot#iefix").unwrap()).as_str(), | ||
"https://somewhere.com/font.eot" | ||
); | ||
} | ||
|
||
#[test] | ||
fn removes_empty_fragment() { | ||
assert_eq!( | ||
url::referer_url(Url::parse("https://somewhere.com/font.eot#").unwrap()).as_str(), | ||
"https://somewhere.com/font.eot" | ||
); | ||
} | ||
|
||
#[test] | ||
fn removes_empty_fragment_and_keeps_empty_query() { | ||
assert_eq!( | ||
url::referer_url(Url::parse("https://somewhere.com/font.eot?#").unwrap()).as_str(), | ||
"https://somewhere.com/font.eot?" | ||
); | ||
} | ||
|
||
#[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(), | ||
"https://somewhere.com/font.eot?a=b&" | ||
); | ||
} | ||
|
||
#[test] | ||
fn removes_credentials() { | ||
assert_eq!( | ||
url::referer_url(Url::parse("https://cookie:monster@gibson.lan/path").unwrap()) | ||
.as_str(), | ||
"https://gibson.lan/path" | ||
); | ||
} | ||
|
||
#[test] | ||
fn removes_empty_credentials() { | ||
assert_eq!( | ||
url::referer_url(Url::parse("https://@gibson.lan/path").unwrap()).as_str(), | ||
"https://gibson.lan/path" | ||
); | ||
} | ||
|
||
#[test] | ||
fn removes_empty_username_credentials() { | ||
assert_eq!( | ||
url::referer_url(Url::parse("https://:monster@gibson.lan/path").unwrap()).as_str(), | ||
"https://gibson.lan/path" | ||
); | ||
} | ||
|
||
#[test] | ||
fn removes_empty_password_credentials() { | ||
assert_eq!( | ||
url::referer_url(Url::parse("https://cookie@gibson.lan/path").unwrap()).as_str(), | ||
"https://gibson.lan/path" | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like
get_referer_url
,format_referer_url
, orurl_to_referer_url
would be more descriptive? Just to be more consistent with function names in that file.