asupersync 0.3.1

Spec-first, cancel-correct, capability-secure async runtime for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! HTTP/2 connection settings.
//!
//! Manages HTTP/2 settings as defined in RFC 7540 Section 6.5.

use super::error::{ErrorCode, H2Error};
use super::frame::Setting;

/// Default header table size (4 KB).
pub const DEFAULT_HEADER_TABLE_SIZE: u32 = 4096;

/// Default enable push (true for servers, false for clients).
pub const DEFAULT_ENABLE_PUSH: bool = true;

/// Default max concurrent streams (256).
///
/// A reasonable default that balances concurrency with resource protection.
/// Servers should configure this based on their capacity requirements.
pub const DEFAULT_MAX_CONCURRENT_STREAMS: u32 = 256;

/// Default initial window size (64 KB - 1).
pub const DEFAULT_INITIAL_WINDOW_SIZE: u32 = 65535;

/// Default max frame size (16 KB).
pub const DEFAULT_MAX_FRAME_SIZE: u32 = 16384;

/// Default max header list size (64 KB).
///
/// Protects against memory exhaustion attacks via oversized headers.
/// Most legitimate requests have headers well under this limit.
pub const DEFAULT_MAX_HEADER_LIST_SIZE: u32 = 65536;

/// Default continuation timeout (5 seconds).
///
/// Maximum time allowed for a CONTINUATION frame sequence to complete.
/// Protects against DoS attacks where a peer sends HEADERS without END_HEADERS
/// and never sends the CONTINUATION frames.
pub const DEFAULT_CONTINUATION_TIMEOUT_MS: u64 = 5000;

/// Maximum allowed initial window size.
pub const MAX_INITIAL_WINDOW_SIZE: u32 = 0x7fff_ffff;

/// Maximum allowed frame size.
pub const MAX_MAX_FRAME_SIZE: u32 = 0x00ff_ffff;

/// Minimum allowed frame size.
pub const MIN_MAX_FRAME_SIZE: u32 = 16384;

/// HTTP/2 connection settings.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Settings {
    /// Maximum size of the header compression table.
    pub header_table_size: u32,
    /// Whether server push is enabled.
    pub enable_push: bool,
    /// Maximum number of concurrent streams.
    pub max_concurrent_streams: u32,
    /// Initial window size for stream-level flow control.
    pub initial_window_size: u32,
    /// Maximum frame payload size.
    pub max_frame_size: u32,
    /// Maximum size of header list.
    pub max_header_list_size: u32,
    /// Continuation sequence timeout in milliseconds.
    ///
    /// Maximum time allowed for a HEADERS/PUSH_PROMISE CONTINUATION sequence
    /// to complete. If the peer doesn't send END_HEADERS within this time,
    /// the connection returns a protocol error.
    ///
    /// This protects against DoS attacks where a malicious peer sends a
    /// HEADERS frame without END_HEADERS and never sends CONTINUATION.
    pub continuation_timeout_ms: u64,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            header_table_size: DEFAULT_HEADER_TABLE_SIZE,
            enable_push: DEFAULT_ENABLE_PUSH,
            max_concurrent_streams: DEFAULT_MAX_CONCURRENT_STREAMS,
            initial_window_size: DEFAULT_INITIAL_WINDOW_SIZE,
            max_frame_size: DEFAULT_MAX_FRAME_SIZE,
            max_header_list_size: DEFAULT_MAX_HEADER_LIST_SIZE,
            continuation_timeout_ms: DEFAULT_CONTINUATION_TIMEOUT_MS,
        }
    }
}

impl Settings {
    /// Create new default settings.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create client-side default settings (push disabled).
    #[must_use]
    pub fn client() -> Self {
        Self {
            enable_push: false,
            ..Self::default()
        }
    }

    /// Create server-side default settings.
    #[must_use]
    pub fn server() -> Self {
        Self::default()
    }

    /// Apply a setting value.
    ///
    /// Returns an error if the value is invalid per RFC 7540 Section 6.5.2:
    /// - `SETTINGS_INITIAL_WINDOW_SIZE` above 2^31-1: FLOW_CONTROL_ERROR
    /// - `SETTINGS_MAX_FRAME_SIZE` outside 16384..16777215: PROTOCOL_ERROR
    pub fn apply(&mut self, setting: Setting) -> Result<(), H2Error> {
        match setting {
            Setting::HeaderTableSize(v) => {
                self.header_table_size = v;
                Ok(())
            }
            Setting::EnablePush(v) => {
                self.enable_push = v;
                Ok(())
            }
            Setting::MaxConcurrentStreams(v) => {
                self.max_concurrent_streams = v;
                Ok(())
            }
            Setting::InitialWindowSize(v) => {
                if v > MAX_INITIAL_WINDOW_SIZE {
                    // RFC 7540 Section 6.5.2: Values above 2^31-1 MUST be treated
                    // as a connection error of type FLOW_CONTROL_ERROR
                    Err(H2Error::connection(
                        ErrorCode::FlowControlError,
                        "initial window size exceeds maximum (2^31-1)",
                    ))
                } else {
                    self.initial_window_size = v;
                    Ok(())
                }
            }
            Setting::MaxFrameSize(v) => {
                if (MIN_MAX_FRAME_SIZE..=MAX_MAX_FRAME_SIZE).contains(&v) {
                    self.max_frame_size = v;
                    Ok(())
                } else {
                    // RFC 7540 Section 6.5.2: Values outside 16384..16777215
                    // MUST be treated as a connection error of type PROTOCOL_ERROR
                    Err(H2Error::protocol("max frame size out of valid range"))
                }
            }
            Setting::MaxHeaderListSize(v) => {
                self.max_header_list_size = v;
                Ok(())
            }
        }
    }

    /// Convert settings to a list of Setting values for encoding.
    #[must_use]
    pub fn to_settings(&self) -> Vec<Setting> {
        self.to_settings_for_role(true)
    }

    /// Convert settings to a list of Setting values for encoding, with role rules.
    ///
    /// Per RFC 7540 §6.5.2, servers MUST NOT send `SETTINGS_ENABLE_PUSH`.
    /// Set `is_client` to `false` when serializing server settings.
    #[must_use]
    pub fn to_settings_for_role(&self, is_client: bool) -> Vec<Setting> {
        let mut settings = Vec::with_capacity(if is_client { 6 } else { 5 });
        settings.push(Setting::HeaderTableSize(self.header_table_size));
        if is_client {
            settings.push(Setting::EnablePush(self.enable_push));
        }
        settings.push(Setting::MaxConcurrentStreams(self.max_concurrent_streams));
        settings.push(Setting::InitialWindowSize(self.initial_window_size));
        settings.push(Setting::MaxFrameSize(self.max_frame_size));
        settings.push(Setting::MaxHeaderListSize(self.max_header_list_size));
        settings
    }

    /// Convert settings to a minimal list (only non-default values).
    #[must_use]
    pub fn to_settings_minimal(&self) -> Vec<Setting> {
        self.to_settings_minimal_for_role(true)
    }

    /// Convert settings to a minimal list (only non-default values), with role rules.
    ///
    /// Per RFC 7540 §6.5.2, servers MUST NOT send `SETTINGS_ENABLE_PUSH`.
    /// Set `is_client` to `false` when serializing server settings.
    #[must_use]
    pub fn to_settings_minimal_for_role(&self, is_client: bool) -> Vec<Setting> {
        let mut settings = Vec::new();

        if self.header_table_size != 4096 {
            settings.push(Setting::HeaderTableSize(self.header_table_size));
        }
        if is_client && !self.enable_push {
            settings.push(Setting::EnablePush(self.enable_push));
        }
        // RFC default is unlimited. Since our default is 256, we should always send it unless it's unlimited (u32::MAX).
        if self.max_concurrent_streams != u32::MAX {
            settings.push(Setting::MaxConcurrentStreams(self.max_concurrent_streams));
        }
        if self.initial_window_size != 65535 {
            settings.push(Setting::InitialWindowSize(self.initial_window_size));
        }
        if self.max_frame_size != 16384 {
            settings.push(Setting::MaxFrameSize(self.max_frame_size));
        }
        // RFC default is unlimited. Since our default is 65536, we should always send it unless it's unlimited (u32::MAX).
        if self.max_header_list_size != u32::MAX {
            settings.push(Setting::MaxHeaderListSize(self.max_header_list_size));
        }

        settings
    }
}

/// Builder for configuring HTTP/2 settings.
#[derive(Debug, Clone)]
pub struct SettingsBuilder {
    settings: Settings,
}

impl SettingsBuilder {
    /// Create a new settings builder with default values.
    #[must_use]
    pub fn new() -> Self {
        Self {
            settings: Settings::default(),
        }
    }

    /// Create a builder for client settings.
    #[must_use]
    pub fn client() -> Self {
        Self {
            settings: Settings::client(),
        }
    }

    /// Create a builder for server settings.
    #[must_use]
    pub fn server() -> Self {
        Self {
            settings: Settings::server(),
        }
    }

    /// Set the header table size.
    #[must_use]
    pub fn header_table_size(mut self, size: u32) -> Self {
        self.settings.header_table_size = size;
        self
    }

    /// Enable or disable server push.
    #[must_use]
    pub fn enable_push(mut self, enable: bool) -> Self {
        self.settings.enable_push = enable;
        self
    }

    /// Set the maximum concurrent streams.
    #[must_use]
    pub fn max_concurrent_streams(mut self, max: u32) -> Self {
        self.settings.max_concurrent_streams = max;
        self
    }

    /// Set the initial window size.
    #[must_use]
    pub fn initial_window_size(mut self, size: u32) -> Self {
        self.settings.initial_window_size = size.min(MAX_INITIAL_WINDOW_SIZE);
        self
    }

    /// Set the maximum frame size.
    #[must_use]
    pub fn max_frame_size(mut self, size: u32) -> Self {
        self.settings.max_frame_size = size.clamp(MIN_MAX_FRAME_SIZE, MAX_MAX_FRAME_SIZE);
        self
    }

    /// Set the maximum header list size.
    #[must_use]
    pub fn max_header_list_size(mut self, size: u32) -> Self {
        self.settings.max_header_list_size = size;
        self
    }

    /// Set the continuation sequence timeout in milliseconds.
    ///
    /// This controls how long a HEADERS/PUSH_PROMISE CONTINUATION sequence
    /// can remain incomplete before the connection times out.
    #[must_use]
    pub fn continuation_timeout_ms(mut self, timeout_ms: u64) -> Self {
        self.settings.continuation_timeout_ms = timeout_ms;
        self
    }

    /// Build the settings.
    #[must_use]
    pub fn build(self) -> Settings {
        self.settings
    }
}

impl Default for SettingsBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_settings() {
        let settings = Settings::default();
        assert_eq!(settings.header_table_size, 4096);
        assert!(settings.enable_push);
        assert_eq!(
            settings.max_concurrent_streams,
            DEFAULT_MAX_CONCURRENT_STREAMS
        );
        assert_eq!(settings.initial_window_size, 65535);
        assert_eq!(settings.max_frame_size, 16384);
        assert_eq!(settings.max_header_list_size, DEFAULT_MAX_HEADER_LIST_SIZE);
    }

    #[test]
    fn test_client_settings() {
        let settings = Settings::client();
        assert!(!settings.enable_push);
    }

    #[test]
    fn test_apply_valid_settings() {
        let mut settings = Settings::default();
        assert!(settings.apply(Setting::InitialWindowSize(32768)).is_ok());
        assert_eq!(settings.initial_window_size, 32768);
    }

    #[test]
    fn test_apply_invalid_initial_window_size() {
        let mut settings = Settings::default();
        // Value too large
        let err = settings
            .apply(Setting::InitialWindowSize(0x8000_0000))
            .unwrap_err();
        assert_eq!(err.code, ErrorCode::FlowControlError);
    }

    #[test]
    fn test_apply_invalid_max_frame_size() {
        let mut settings = Settings::default();
        // Value too small
        let err = settings.apply(Setting::MaxFrameSize(1000)).unwrap_err();
        assert_eq!(err.code, ErrorCode::ProtocolError);
        // Value too large
        let err = settings
            .apply(Setting::MaxFrameSize(0x0100_0000))
            .unwrap_err();
        assert_eq!(err.code, ErrorCode::ProtocolError);
    }

    #[test]
    fn test_apply_max_frame_size_bounds() {
        let mut settings = Settings::default();
        assert!(
            settings
                .apply(Setting::MaxFrameSize(MIN_MAX_FRAME_SIZE))
                .is_ok()
        );
        assert!(
            settings
                .apply(Setting::MaxFrameSize(MAX_MAX_FRAME_SIZE))
                .is_ok()
        );
    }

    #[test]
    fn test_settings_builder() {
        let settings = SettingsBuilder::new()
            .header_table_size(8192)
            .enable_push(false)
            .max_concurrent_streams(100)
            .initial_window_size(131_072)
            .max_frame_size(32768)
            .continuation_timeout_ms(2500)
            .build();

        assert_eq!(settings.header_table_size, 8192);
        assert!(!settings.enable_push);
        assert_eq!(settings.max_concurrent_streams, 100);
        assert_eq!(settings.initial_window_size, 131_072);
        assert_eq!(settings.max_frame_size, 32768);
        assert_eq!(settings.continuation_timeout_ms, 2500);
    }

    #[test]
    fn test_to_settings_minimal() {
        let settings = SettingsBuilder::new()
            .enable_push(false)
            .max_concurrent_streams(100)
            .build();

        let minimal = settings.to_settings_minimal();
        // EnablePush (false != true), MaxConcurrentStreams (100 != MAX), MaxHeaderListSize (65536 != MAX)
        assert_eq!(minimal.len(), 3);
        assert!(minimal.contains(&Setting::EnablePush(false)));
        assert!(minimal.contains(&Setting::MaxConcurrentStreams(100)));
        assert!(minimal.contains(&Setting::MaxHeaderListSize(DEFAULT_MAX_HEADER_LIST_SIZE)));
    }

    #[test]
    fn test_to_settings_for_server_omits_enable_push() {
        let settings = SettingsBuilder::server().enable_push(false).build();
        let serialized = settings.to_settings_for_role(false);
        assert!(
            !serialized
                .iter()
                .any(|setting| matches!(setting, Setting::EnablePush(_)))
        );
    }

    #[test]
    fn test_to_settings_minimal_for_server_omits_enable_push() {
        let settings = SettingsBuilder::server().enable_push(false).build();
        let minimal = settings.to_settings_minimal_for_role(false);
        assert!(
            !minimal
                .iter()
                .any(|setting| matches!(setting, Setting::EnablePush(_)))
        );
    }

    // --- wave 77 trait coverage ---

    #[test]
    fn settings_debug_clone_eq_default() {
        let s = Settings::default();
        let s2 = s.clone();
        assert_eq!(s, s2);
        let modified = Settings {
            max_frame_size: 32768,
            ..Settings::default()
        };
        assert_ne!(s, modified);
        let dbg = format!("{s:?}");
        assert!(dbg.contains("Settings"));
    }

    #[test]
    fn settings_builder_debug_clone() {
        let b = SettingsBuilder::new();
        let b2 = b.clone();
        let dbg = format!("{b:?}");
        assert!(dbg.contains("SettingsBuilder"));
        assert_eq!(b.build(), b2.build());
    }
}