Skip to main content

opendal_service_http/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::HttpBuilder;
24
25/// Config for Http service support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct HttpConfig {
30    /// endpoint of this backend
31    pub endpoint: Option<String>,
32    /// username of this backend
33    pub username: Option<String>,
34    /// password of this backend
35    pub password: Option<String>,
36    /// token of this backend
37    pub token: Option<String>,
38    /// root of this backend
39    pub root: Option<String>,
40}
41
42impl Debug for HttpConfig {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        f.debug_struct("HttpConfig")
45            .field("endpoint", &self.endpoint)
46            .field("root", &self.root)
47            .finish_non_exhaustive()
48    }
49}
50
51impl opendal_core::Configurator for HttpConfig {
52    type Builder = HttpBuilder;
53
54    fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
55        let mut map = uri.options().clone();
56        if let Some(authority) = uri.authority() {
57            map.insert(
58                "endpoint".to_string(),
59                format!("{}://{}", uri.scheme(), authority),
60            );
61        }
62
63        if let Some(root) = uri.root() {
64            map.insert("root".to_string(), root.to_string());
65        }
66
67        Self::from_iter(map)
68    }
69
70    fn into_builder(self) -> Self::Builder {
71        HttpBuilder { config: self }
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78    use opendal_core::Configurator;
79    use opendal_core::Operator;
80    use opendal_core::OperatorUri;
81
82    fn register_http() {
83        let once = std::sync::Once::new();
84        once.call_once(|| {
85            let registry = opendal_core::OperatorRegistry::get();
86            crate::register_http_service(registry);
87        });
88    }
89
90    #[test]
91    fn from_uri_sets_endpoint_and_root() {
92        let uri = OperatorUri::new(
93            "http://example.com/static/assets",
94            Vec::<(String, String)>::new(),
95        )
96        .unwrap();
97
98        let cfg = HttpConfig::from_uri(&uri).unwrap();
99        assert_eq!(cfg.endpoint.as_deref(), Some("http://example.com"));
100        assert_eq!(cfg.root.as_deref(), Some("static/assets"));
101    }
102
103    #[test]
104    fn from_uri_allows_missing_authority() {
105        let uri = OperatorUri::new("http", Vec::<(String, String)>::new()).unwrap();
106
107        let cfg = HttpConfig::from_uri(&uri).unwrap();
108        assert!(cfg.endpoint.is_none());
109    }
110
111    #[test]
112    fn from_uri_preserves_query_options() {
113        let uri = OperatorUri::new(
114            "http://cdn.example.com/data?token=abc123",
115            Vec::<(String, String)>::new(),
116        )
117        .unwrap();
118        let cfg = HttpConfig::from_uri(&uri).unwrap();
119
120        assert_eq!(cfg.endpoint.as_deref(), Some("http://cdn.example.com"));
121        assert_eq!(cfg.token.as_deref(), Some("abc123"));
122    }
123
124    #[test]
125    fn from_uri_ignores_endpoint_override() {
126        let uri = OperatorUri::new(
127            "http://example.com/data",
128            vec![(
129                "endpoint".to_string(),
130                "https://cdn.example.com".to_string(),
131            )],
132        )
133        .unwrap();
134        let cfg = HttpConfig::from_uri(&uri).unwrap();
135
136        assert_eq!(cfg.endpoint.as_deref(), Some("http://example.com"));
137    }
138
139    #[test]
140    fn operator_from_uri_http() {
141        register_http();
142        let op = Operator::from_uri("http://example.com").unwrap();
143        assert_eq!(op.info().scheme(), "http");
144    }
145
146    #[test]
147    fn operator_from_uri_https() {
148        register_http();
149        let op = Operator::from_uri("https://example.com").unwrap();
150        assert_eq!(op.info().scheme(), "http");
151    }
152
153    #[test]
154    fn from_uri_with_https_scheme() {
155        // "https" is an alias for "http" to support standard https:// URIs
156        let uri = OperatorUri::new("https://example.com", Vec::<(String, String)>::new()).unwrap();
157        let cfg = HttpConfig::from_uri(&uri).unwrap();
158        assert_eq!(cfg.endpoint.as_deref(), Some("https://example.com"));
159    }
160}