• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! HTTP configure module.
15 
16 /// Options and flags which can be used to configure `HTTP` related logic.
17 #[derive(Clone)]
18 pub(crate) struct HttpConfig {
19     pub(crate) version: HttpVersion,
20 
21     #[cfg(feature = "http2")]
22     pub(crate) http2_config: http2::H2Config,
23 }
24 
25 impl HttpConfig {
26     /// Creates a new, default `HttpConfig`.
new() -> Self27     pub(crate) fn new() -> Self {
28         Self {
29             version: HttpVersion::Http11,
30 
31             #[cfg(feature = "http2")]
32             http2_config: http2::H2Config::default(),
33         }
34     }
35 }
36 
37 impl Default for HttpConfig {
default() -> Self38     fn default() -> Self {
39         Self::new()
40     }
41 }
42 
43 /// `HTTP` version to use.
44 #[derive(PartialEq, Eq, Clone)]
45 pub(crate) enum HttpVersion {
46     /// Enforces `HTTP/1.1` requests.
47     Http11,
48 
49     #[cfg(feature = "http2")]
50     /// Enforce `HTTP/2.0` requests without `HTTP/1.1` Upgrade.
51     Http2PriorKnowledge,
52 }
53 
54 #[cfg(feature = "http2")]
55 pub(crate) mod http2 {
56     const DEFAULT_MAX_FRAME_SIZE: u32 = 2 << 13;
57     const DEFAULT_HEADER_TABLE_SIZE: u32 = 4096;
58     const DEFAULT_MAX_HEADER_LIST_SIZE: u32 = 16 << 20;
59 
60     /// Settings which can be used to configure a http2 connection.
61     ///
62     /// # Examples
63     ///
64     /// ```
65     /// use ylong_http_client::util::H2Config;
66     ///
67     /// let config = H2Config::new()
68     ///     .set_header_table_size(4096)
69     ///     .set_max_header_list_size(16 << 20)
70     ///     .set_max_frame_size(2 << 13);
71     /// ```
72     #[derive(Clone)]
73     pub struct H2Config {
74         max_frame_size: u32,
75         max_header_list_size: u32,
76         header_table_size: u32,
77     }
78 
79     impl H2Config {
80         /// `H2Config` constructor.
81         ///
82         /// # Examples
83         ///
84         /// ```
85         /// use ylong_http_client::util::H2Config;
86         ///
87         /// let config = H2Config::new();
88         /// ```
new() -> Self89         pub fn new() -> Self {
90             Self::default()
91         }
92 
93         /// Sets the SETTINGS_MAX_FRAME_SIZE.
94         ///
95         /// # Examples
96         ///
97         /// ```
98         /// use ylong_http_client::util::H2Config;
99         ///
100         /// let config = H2Config::new().set_max_frame_size(2 << 13);
101         /// ```
set_max_frame_size(mut self, size: u32) -> Self102         pub fn set_max_frame_size(mut self, size: u32) -> Self {
103             self.max_frame_size = size;
104             self
105         }
106 
107         /// Sets the SETTINGS_MAX_HEADER_LIST_SIZE.
108         ///
109         /// # Examples
110         ///
111         /// ```
112         /// use ylong_http_client::util::H2Config;
113         ///
114         /// let config = H2Config::new().set_max_header_list_size(16 << 20);
115         /// ```
set_max_header_list_size(mut self, size: u32) -> Self116         pub fn set_max_header_list_size(mut self, size: u32) -> Self {
117             self.max_header_list_size = size;
118             self
119         }
120 
121         /// Sets the SETTINGS_HEADER_TABLE_SIZE.
122         ///
123         /// # Examples
124         ///
125         /// ```
126         /// use ylong_http_client::util::H2Config;
127         ///
128         /// let config = H2Config::new().set_max_header_list_size(4096);
129         /// ```
set_header_table_size(mut self, size: u32) -> Self130         pub fn set_header_table_size(mut self, size: u32) -> Self {
131             self.header_table_size = size;
132             self
133         }
134 
135         /// Gets the SETTINGS_MAX_FRAME_SIZE.
136         ///
137         /// # Examples
138         ///
139         /// ```
140         /// use ylong_http_client::util::H2Config;
141         ///
142         /// let config = H2Config::new().set_max_frame_size(2 << 13);
143         /// assert_eq!(config.max_frame_size(), 2 << 13);
144         /// ```
max_frame_size(&self) -> u32145         pub fn max_frame_size(&self) -> u32 {
146             self.max_frame_size
147         }
148 
149         /// Gets the SETTINGS_MAX_HEADER_LIST_SIZE.
150         ///
151         /// # Examples
152         ///
153         /// ```
154         /// use ylong_http_client::util::H2Config;
155         ///
156         /// let config = H2Config::new().set_max_header_list_size(16 << 20);
157         /// assert_eq!(config.max_header_list_size(), 16 << 20);
158         /// ```
max_header_list_size(&self) -> u32159         pub fn max_header_list_size(&self) -> u32 {
160             self.max_header_list_size
161         }
162 
163         /// Gets the SETTINGS_MAX_FRAME_SIZE.
164         ///
165         /// # Examples
166         ///
167         /// ```
168         /// use ylong_http_client::util::H2Config;
169         ///
170         /// let config = H2Config::new().set_header_table_size(4096);
171         /// assert_eq!(config.header_table_size(), 4096);
172         /// ```
header_table_size(&self) -> u32173         pub fn header_table_size(&self) -> u32 {
174             self.header_table_size
175         }
176     }
177 
178     impl Default for H2Config {
default() -> Self179         fn default() -> Self {
180             Self {
181                 max_frame_size: DEFAULT_MAX_FRAME_SIZE,
182                 max_header_list_size: DEFAULT_MAX_HEADER_LIST_SIZE,
183                 header_table_size: DEFAULT_HEADER_TABLE_SIZE,
184             }
185         }
186     }
187 }
188