rustfs_signer/
utils.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use http::request;
16
17use s3s::Body;
18
19pub fn get_host_addr(req: &request::Request<Body>) -> String {
20    let host = req.headers().get("host");
21    let uri = req.uri();
22    let req_host;
23    if let Some(port) = uri.port() {
24        req_host = format!("{}:{}", uri.host().unwrap(), port);
25    } else {
26        req_host = uri.host().unwrap().to_string();
27    }
28    if let Some(host) = host {
29        if req_host != *host.to_str().unwrap() {
30            return (*host.to_str().unwrap()).to_string();
31        }
32    }
33    /*if req.uri_ref().unwrap().host().is_some() {
34        return req.uri_ref().unwrap().host().unwrap();
35    }*/
36    req_host
37}
38
39pub fn sign_v4_trim_all(input: &str) -> String {
40    let ss = input.split_whitespace().collect::<Vec<_>>();
41    ss.join(" ")
42}
43
44pub fn stable_sort_by_first<T>(v: &mut [(T, T)])
45where
46    T: Ord,
47{
48    v.sort_by(|lhs, rhs| lhs.0.cmp(&rhs.0));
49}