Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/common/location.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use http::Uri;
use HeaderValue;

/// `Location` header, defined in
Expand Down Expand Up @@ -28,6 +29,18 @@ derive_header! {
name: LOCATION
}

impl Location {
/// Creates a `Location` header from a uri
pub fn uri(uri: Uri) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda wonder if we would want this function name as an accessor at some point... Not sure.

A From impl would work too.

let uri = uri.to_string();
// cf. https://www.rfc-editor.org/rfc/rfc3986#section-2
Self(
HeaderValue::from_str(&uri)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use try_from and it won't need to copy to a new buffer.

.expect("All URI characters should be valid HTTP header value characters"),
)
}
}

#[cfg(test)]
mod tests {
use super::super::test_decode;
Expand All @@ -48,4 +61,20 @@ mod tests {

assert_eq!(loc, Location(HeaderValue::from_static(s)));
}

#[test]
fn uri_constructor() {
let s = "https://www.rust-lang.org/tools";
let uri: Uri = s.parse().unwrap();
let loc = Location::uri(uri);

assert_eq!(loc, Location(HeaderValue::from_static(s)));
}

#[test]
fn uri_constructor_invalid_chars() {
let s = "https://www.rust-lang.org/hélas";
let uri: Result<Uri, _> = s.parse();
assert!(uri.is_err());
}
}