aws_smithy_http/
endpoint.rs1use aws_smithy_runtime_api::client::endpoint::{error::InvalidEndpointError, EndpointPrefix};
9use std::borrow::Cow;
10use std::result::Result as StdResult;
11use std::str::FromStr;
12
13pub mod error;
14pub use error::ResolveEndpointError;
15
16#[deprecated(since = "0.60.1", note = "Was never used.")]
18pub type Result = std::result::Result<aws_smithy_types::endpoint::Endpoint, ResolveEndpointError>;
19
20pub fn apply_endpoint(
24 uri: &mut http_1x::Uri,
25 endpoint: &http_1x::Uri,
26 prefix: Option<&EndpointPrefix>,
27) -> StdResult<(), InvalidEndpointError> {
28 let prefix = prefix.map(EndpointPrefix::as_str).unwrap_or("");
29 let authority = endpoint
30 .authority()
31 .as_ref()
32 .map(|auth| auth.as_str())
33 .unwrap_or("");
34 let authority = if !prefix.is_empty() {
35 Cow::Owned(format!("{}{}", prefix, authority))
36 } else {
37 Cow::Borrowed(authority)
38 };
39 let authority = http_1x::uri::Authority::from_str(&authority).map_err(|err| {
40 InvalidEndpointError::failed_to_construct_authority(authority.into_owned(), err)
41 })?;
42 let scheme = *endpoint
43 .scheme()
44 .as_ref()
45 .ok_or_else(InvalidEndpointError::endpoint_must_have_scheme)?;
46 let new_uri = http_1x::Uri::builder()
47 .authority(authority)
48 .scheme(scheme.clone())
49 .path_and_query(merge_paths(endpoint, uri).as_ref())
50 .build()
51 .map_err(InvalidEndpointError::failed_to_construct_uri)?;
52 *uri = new_uri;
53 Ok(())
54}
55
56fn merge_paths<'a>(endpoint: &'a http_1x::Uri, uri: &'a http_1x::Uri) -> Cow<'a, str> {
57 if let Some(query) = endpoint.path_and_query().and_then(|pq| pq.query()) {
58 tracing::warn!(query = %query, "query specified in endpoint will be ignored during endpoint resolution");
59 }
60 let endpoint_path = endpoint.path();
61 let uri_path_and_query = uri.path_and_query().map(|pq| pq.as_str()).unwrap_or("");
62 if endpoint_path.is_empty() {
63 Cow::Borrowed(uri_path_and_query)
64 } else {
65 let ep_no_slash = endpoint_path.strip_suffix('/').unwrap_or(endpoint_path);
66 let uri_path_no_slash = uri_path_and_query
67 .strip_prefix('/')
68 .unwrap_or(uri_path_and_query);
69 Cow::Owned(format!("{}/{}", ep_no_slash, uri_path_no_slash))
70 }
71}