-
-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always use
http::Uri
instead of url::Url
- Loading branch information
Showing
8 changed files
with
58 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,56 @@ | ||
use http::Request; | ||
use http::{uri, Request}; | ||
use hyper::Body; | ||
|
||
/// Set cluster URL. | ||
pub fn set_cluster_url(req: Request<Body>, url: &url::Url) -> Request<Body> { | ||
pub fn set_cluster_url(req: Request<Body>, base_uri: &http::Uri) -> Request<Body> { | ||
let (mut parts, body) = req.into_parts(); | ||
let pandq = parts.uri.path_and_query().expect("valid path+query from kube"); | ||
parts.uri = finalize_url(url, &pandq).parse().expect("valid URL"); | ||
let request_pandq = parts.uri.path_and_query().expect("nonempty path+query"); | ||
parts.uri = finalize_url(base_uri, request_pandq); | ||
Request::from_parts(parts, body) | ||
} | ||
|
||
/// An internal url joiner to deal with the two different interfaces | ||
/// | ||
/// - api module produces a http::Uri which we can turn into a PathAndQuery (has a leading slash by construction) | ||
/// - config module produces a url::Url from user input (sometimes contains path segments) | ||
/// | ||
/// This deals with that in a pretty easy way (tested below) | ||
fn finalize_url(cluster_url: &url::Url, request_pandq: &http::uri::PathAndQuery) -> String { | ||
let base = cluster_url.as_str().trim_end_matches('/'); // pandq always starts with a slash | ||
format!("{}{}", base, request_pandq) | ||
// Join base URI and Path+Query, preserving any path in the base. | ||
fn finalize_url(base_uri: &http::Uri, request_pandq: &uri::PathAndQuery) -> http::Uri { | ||
let mut builder = uri::Builder::new(); | ||
if let Some(scheme) = base_uri.scheme() { | ||
builder = builder.scheme(scheme.as_str()); | ||
} | ||
if let Some(authority) = base_uri.authority() { | ||
builder = builder.authority(authority.as_str()); | ||
} | ||
if let Some(pandq) = base_uri.path_and_query() { | ||
// If `base_uri` has path, remove any trailing space and join. | ||
// `PathAndQuery` always starts with a slash. | ||
let base_path = pandq.path().trim_end_matches('/'); | ||
builder = builder.path_and_query(format!("{}{}", base_path, request_pandq)); | ||
} else { | ||
builder = builder.path_and_query(request_pandq.as_str()); | ||
} | ||
builder.build().expect("valid URI") | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn normal_host() { | ||
let minikube_host = "https://192.168.1.65:8443"; | ||
let cluster_url = url::Url::parse(minikube_host).unwrap(); | ||
let apipath: http::Uri = "/api/v1/nodes?hi=yes".parse().unwrap(); | ||
let base_uri = http::Uri::from_static("https://192.168.1.65:8443"); | ||
let apipath = http::Uri::from_static("/api/v1/nodes?hi=yes"); | ||
let pandq = apipath.path_and_query().expect("could pandq apipath"); | ||
let final_url = super::finalize_url(&cluster_url, &pandq); | ||
assert_eq!( | ||
final_url.as_str(), | ||
super::finalize_url(&base_uri, &pandq), | ||
"https://192.168.1.65:8443/api/v1/nodes?hi=yes" | ||
); | ||
} | ||
|
||
#[test] | ||
fn rancher_host() { | ||
// in rancher, kubernetes server names are not hostnames, but a host with a path: | ||
let rancher_host = "https://hostname/foo/bar"; | ||
let cluster_url = url::Url::parse(rancher_host).unwrap(); | ||
assert_eq!(cluster_url.host_str().unwrap(), "hostname"); | ||
assert_eq!(cluster_url.path(), "/foo/bar"); | ||
// we must be careful when using Url::join on our http::Uri result | ||
// as a straight two Uri::join would trim away rancher's initial path | ||
// case in point (discards original path): | ||
assert_eq!(cluster_url.join("/api/v1/nodes").unwrap().path(), "/api/v1/nodes"); | ||
|
||
let apipath: http::Uri = "/api/v1/nodes?hi=yes".parse().unwrap(); | ||
let pandq = apipath.path_and_query().expect("could pandq apipath"); | ||
|
||
let final_url = super::finalize_url(&cluster_url, &pandq); | ||
assert_eq!(final_url.as_str(), "https://hostname/foo/bar/api/v1/nodes?hi=yes"); | ||
let base_uri = http::Uri::from_static("https://example.com/foo/bar"); | ||
let api_path = http::Uri::from_static("/api/v1/nodes?hi=yes"); | ||
let pandq = api_path.path_and_query().unwrap(); | ||
assert_eq!( | ||
super::finalize_url(&base_uri, &pandq), | ||
"https://example.com/foo/bar/api/v1/nodes?hi=yes" | ||
); | ||
} | ||
} |