WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Changes from all commits
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
37 changes: 37 additions & 0 deletions src/common/location.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::convert::TryFrom;

use http::Uri;
use HeaderValue;

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

impl Location {
/// Accesses the header's value
pub fn value(&self) -> &HeaderValue {
&self.0
}
}

impl From<Uri> for Location {
fn from(uri: Uri) -> Self {
Self(
HeaderValue::try_from(uri.to_string())
// cf. https://www.rfc-editor.org/rfc/rfc3986#section-2
.expect("All URI characters should be valid HTTP header value characters"),
)
}
}

#[cfg(test)]
mod tests {
use super::super::test_decode;
Expand All @@ -48,4 +68,21 @@ 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::from(uri);

assert_eq!(loc, Location(HeaderValue::from_static(s)));
assert_eq!(loc.value().to_str().unwrap(), 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());
}
}