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::Negotiate, 30 31 #[cfg(feature = "http2")] 32 http2_config: http2::H2Config::new(), 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` or `HTTP/1.0` requests. 47 Http1, 48 49 #[cfg(feature = "http2")] 50 /// Enforce `HTTP/2.0` requests without `HTTP/1.1` Upgrade or ALPN. 51 Http2, 52 53 /// Negotiate the protocol version through the ALPN. 54 Negotiate, 55 } 56 57 #[cfg(feature = "http2")] 58 pub(crate) mod http2 { 59 const DEFAULT_MAX_FRAME_SIZE: u32 = 16 * 1024; 60 const DEFAULT_HEADER_TABLE_SIZE: u32 = 4096; 61 const DEFAULT_MAX_HEADER_LIST_SIZE: u32 = 16 * 1024; 62 // window size at the client connection level 63 // The initial value specified in rfc9113 is 64kb, 64 // but the default value is 1mb for performance purposes and is synchronized 65 // using WINDOW_UPDATE after sending SETTINGS. 66 const DEFAULT_CONN_WINDOW_SIZE: u32 = 10 * 1024 * 1024; 67 const DEFAULT_STREAM_WINDOW_SIZE: u32 = 2 * 1024 * 1024; 68 69 /// Settings which can be used to configure a http2 connection. 70 #[derive(Clone)] 71 pub(crate) struct H2Config { 72 max_frame_size: u32, 73 max_header_list_size: u32, 74 header_table_size: u32, 75 init_conn_window_size: u32, 76 init_stream_window_size: u32, 77 enable_push: bool, 78 allowed_cache_frame_size: usize, 79 use_huffman: bool, 80 } 81 82 impl H2Config { 83 /// `H2Config` constructor. new() -> Self84 pub(crate) fn new() -> Self { 85 Self::default() 86 } 87 88 /// Sets the SETTINGS_MAX_FRAME_SIZE. set_max_frame_size(&mut self, size: u32)89 pub(crate) fn set_max_frame_size(&mut self, size: u32) { 90 self.max_frame_size = size; 91 } 92 93 /// Sets the SETTINGS_MAX_HEADER_LIST_SIZE. set_max_header_list_size(&mut self, size: u32)94 pub(crate) fn set_max_header_list_size(&mut self, size: u32) { 95 self.max_header_list_size = size; 96 } 97 98 /// Sets the SETTINGS_HEADER_TABLE_SIZE. set_header_table_size(&mut self, size: u32)99 pub(crate) fn set_header_table_size(&mut self, size: u32) { 100 self.header_table_size = size; 101 } 102 set_conn_window_size(&mut self, size: u32)103 pub(crate) fn set_conn_window_size(&mut self, size: u32) { 104 self.init_conn_window_size = size; 105 } 106 set_stream_window_size(&mut self, size: u32)107 pub(crate) fn set_stream_window_size(&mut self, size: u32) { 108 self.init_stream_window_size = size; 109 } 110 set_allowed_cache_frame_size(&mut self, size: usize)111 pub(crate) fn set_allowed_cache_frame_size(&mut self, size: usize) { 112 self.allowed_cache_frame_size = size; 113 } 114 set_use_huffman_coding(&mut self, use_huffman: bool)115 pub(crate) fn set_use_huffman_coding(&mut self, use_huffman: bool) { 116 self.use_huffman = use_huffman; 117 } 118 119 /// Gets the SETTINGS_MAX_FRAME_SIZE. max_frame_size(&self) -> u32120 pub(crate) fn max_frame_size(&self) -> u32 { 121 self.max_frame_size 122 } 123 124 /// Gets the SETTINGS_MAX_HEADER_LIST_SIZE. max_header_list_size(&self) -> u32125 pub(crate) fn max_header_list_size(&self) -> u32 { 126 self.max_header_list_size 127 } 128 129 /// Gets the SETTINGS_MAX_FRAME_SIZE. header_table_size(&self) -> u32130 pub(crate) fn header_table_size(&self) -> u32 { 131 self.header_table_size 132 } 133 enable_push(&self) -> bool134 pub(crate) fn enable_push(&self) -> bool { 135 self.enable_push 136 } 137 conn_window_size(&self) -> u32138 pub(crate) fn conn_window_size(&self) -> u32 { 139 self.init_conn_window_size 140 } 141 stream_window_size(&self) -> u32142 pub(crate) fn stream_window_size(&self) -> u32 { 143 self.init_stream_window_size 144 } 145 allowed_cache_frame_size(&self) -> usize146 pub(crate) fn allowed_cache_frame_size(&self) -> usize { 147 self.allowed_cache_frame_size 148 } 149 use_huffman_coding(&self) -> bool150 pub(crate) fn use_huffman_coding(&self) -> bool { 151 self.use_huffman 152 } 153 } 154 155 impl Default for H2Config { default() -> Self156 fn default() -> Self { 157 Self { 158 max_frame_size: DEFAULT_MAX_FRAME_SIZE, 159 max_header_list_size: DEFAULT_MAX_HEADER_LIST_SIZE, 160 header_table_size: DEFAULT_HEADER_TABLE_SIZE, 161 init_conn_window_size: DEFAULT_CONN_WINDOW_SIZE, 162 init_stream_window_size: DEFAULT_STREAM_WINDOW_SIZE, 163 enable_push: false, 164 allowed_cache_frame_size: 5, 165 use_huffman: true, 166 } 167 } 168 } 169 } 170