• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23350122333

20 Mar 2026 03:33PM UTC coverage: 76.492% (-2.8%) from 79.315%
23350122333

Pull #15053

github

web-flow
Merge f5bf69f97 into 6587e363a
Pull Request #15053: Flow queue/v3

113 of 129 new or added lines in 9 files covered. (87.6%)

9534 existing lines in 453 files now uncovered.

256601 of 335461 relevant lines covered (76.49%)

4680806.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.3
/rust/src/http2/detect.rs
1
/* Copyright (C) 2020-2024 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
use super::http2::{
19
    HTTP2Event, HTTP2Frame, HTTP2FrameTypeData, HTTP2State, HTTP2Transaction, HTTP2TransactionState,
20
};
21
use super::parser;
22
use crate::detect::uint::{
23
    detect_match_uint, detect_parse_array_uint_enum, detect_uint_match_at_index,
24
    DetectUintArrayData, DetectUintData, DetectUintIndex, DetectUintMode,
25
};
26
use crate::detect::EnumString;
27
use crate::direction::Direction;
28
use base64::{engine::general_purpose::STANDARD, Engine};
29
use std::ffi::CStr;
30
use std::os::raw::c_void;
31
use std::rc::Rc;
32
use suricata_sys::sys::DetectEngineThreadCtx;
33

34
#[no_mangle]
35
pub unsafe extern "C" fn SCHttp2TxHasFrametype(
178✔
36
    tx: *mut std::os::raw::c_void, direction: u8, ctx: *const std::os::raw::c_void,
178✔
37
) -> std::os::raw::c_int {
178✔
38
    let tx = cast_pointer!(tx, HTTP2Transaction);
178✔
39
    let ctx = cast_pointer!(ctx, DetectUintArrayData<u8>);
178✔
40
    let frames = if direction & Direction::ToServer as u8 != 0 {
178✔
41
        &tx.frames_ts
89✔
42
    } else {
43
        &tx.frames_tc
89✔
44
    };
45
    return detect_uint_match_at_index::<HTTP2Frame, u8>(
178✔
46
        frames,
178✔
47
        ctx,
178✔
48
        |f| Some(f.header.ftype),
178✔
49
        tx.state >= HTTP2TransactionState::HTTP2StateClosed,
178✔
50
    );
178✔
51
}
178✔
52

53
#[no_mangle]
54
pub unsafe extern "C" fn SCHttp2ParseFrametype(
6✔
55
    str: *const std::os::raw::c_char,
6✔
56
) -> *mut std::os::raw::c_void {
6✔
57
    let ft_name: &CStr = CStr::from_ptr(str); //unsafe
6✔
58
    if let Ok(s) = ft_name.to_str() {
6✔
59
        if let Some(ctx) = detect_parse_array_uint_enum::<u8, parser::HTTP2FrameType>(s) {
6✔
60
            let boxed = Box::new(ctx);
6✔
61
            // DetectUintArrayData<u8> cannot be cbindgend
6✔
62
            return Box::into_raw(boxed) as *mut c_void;
6✔
UNCOV
63
        }
×
64
    }
×
UNCOV
65
    return std::ptr::null_mut();
×
66
}
6✔
67

68
fn http2_tx_get_errorcode(f: &HTTP2Frame) -> Option<u32> {
48✔
69
    match &f.data {
48✔
70
        HTTP2FrameTypeData::GOAWAY(goaway) => Some(goaway.errorcode),
16✔
71
        HTTP2FrameTypeData::RSTSTREAM(rst) => Some(rst.errorcode),
×
72
        _ => None,
32✔
73
    }
74
}
48✔
75

76
#[no_mangle]
77
pub unsafe extern "C" fn SCHttp2TxHasErrorCode(
48✔
78
    tx: *mut std::os::raw::c_void, direction: u8, ctx: *const std::os::raw::c_void,
48✔
79
) -> std::os::raw::c_int {
48✔
80
    let tx = cast_pointer!(tx, HTTP2Transaction);
48✔
81
    let ctx = cast_pointer!(ctx, DetectUintArrayData<u32>);
48✔
82
    let frames = if direction & Direction::ToServer as u8 != 0 {
48✔
83
        &tx.frames_ts
24✔
84
    } else {
85
        &tx.frames_tc
24✔
86
    };
87
    return detect_uint_match_at_index::<HTTP2Frame, u32>(
48✔
88
        frames,
48✔
89
        ctx,
48✔
90
        http2_tx_get_errorcode,
48✔
91
        tx.state >= HTTP2TransactionState::HTTP2StateClosed,
48✔
92
    );
48✔
93
}
48✔
94

95
#[no_mangle]
96
pub unsafe extern "C" fn SCHttp2ParseErrorCode(
5✔
97
    str: *const std::os::raw::c_char,
5✔
98
) -> *mut std::os::raw::c_void {
5✔
99
    let ft_name: &CStr = CStr::from_ptr(str); //unsafe
5✔
100
    if let Ok(s) = ft_name.to_str() {
5✔
101
        // special case for backward compatibility, now parsed as HTTP11_REQUIRED
102
        if s.to_uppercase() == "HTTP_1_1_REQUIRED" {
5✔
103
            let ctx = DetectUintArrayData::<u32> {
1✔
104
                du: DetectUintData {
1✔
105
                    arg1: parser::HTTP2ErrorCode::Http11Required.into_u(),
1✔
106
                    arg2: 0,
1✔
107
                    mode: DetectUintMode::DetectUintModeEqual,
1✔
108
                },
1✔
109
                index: DetectUintIndex::Any,
1✔
110
                start: 0,
1✔
111
                end: 0,
1✔
112
            };
1✔
113
            let boxed = Box::new(ctx);
1✔
114
            return Box::into_raw(boxed) as *mut c_void;
1✔
115
        }
4✔
116
        if let Some(ctx) = detect_parse_array_uint_enum::<u32, parser::HTTP2ErrorCode>(s) {
4✔
117
            let boxed = Box::new(ctx);
4✔
118
            return Box::into_raw(boxed) as *mut c_void;
4✔
UNCOV
119
        }
×
120
    }
×
UNCOV
121
    return std::ptr::null_mut();
×
122
}
5✔
123

124
fn get_http2_priority(frame: &HTTP2Frame) -> Option<u8> {
15✔
125
    return match &frame.data {
15✔
126
        HTTP2FrameTypeData::PRIORITY(prio) => Some(prio.weight),
×
127
        HTTP2FrameTypeData::HEADERS(hd) => {
4✔
128
            if let Some(prio) = hd.priority {
4✔
129
                return Some(prio.weight);
4✔
130
            }
×
131
            None
×
132
        }
133
        _ => None,
11✔
134
    };
135
}
15✔
136

137
fn http2_match_priority(
28✔
138
    tx: &HTTP2Transaction, direction: Direction, ctx: &DetectUintArrayData<u8>,
28✔
139
) -> std::os::raw::c_int {
28✔
140
    let frames = if direction == Direction::ToServer {
28✔
141
        &tx.frames_ts
14✔
142
    } else {
143
        &tx.frames_tc
14✔
144
    };
145
    let eof = tx.state >= HTTP2TransactionState::HTTP2StateClosed;
28✔
146
    return detect_uint_match_at_index::<HTTP2Frame, u8>(frames, ctx, get_http2_priority, eof);
28✔
147
}
28✔
148

149
#[no_mangle]
150
pub unsafe extern "C" fn SCHttp2PriorityMatch(
28✔
151
    tx: *mut std::os::raw::c_void, direction: u8, ctx: *const std::os::raw::c_void,
28✔
152
) -> std::os::raw::c_int {
28✔
153
    let tx = cast_pointer!(tx, HTTP2Transaction);
28✔
154
    let ctx = cast_pointer!(ctx, DetectUintArrayData<u8>);
28✔
155
    return http2_match_priority(tx, direction.into(), ctx);
28✔
156
}
28✔
157

158
fn get_http2_window(frame: &HTTP2Frame) -> Option<u32> {
1,605✔
159
    if let HTTP2FrameTypeData::WINDOWUPDATE(wu) = &frame.data {
1,605✔
160
        return Some(wu.sizeinc);
191✔
161
    }
1,414✔
162
    return None;
1,414✔
163
}
1,605✔
164

165
fn http2_match_window(
504✔
166
    tx: &HTTP2Transaction, direction: Direction, ctx: &DetectUintArrayData<u32>,
504✔
167
) -> std::os::raw::c_int {
504✔
168
    let frames = if direction == Direction::ToServer {
504✔
169
        &tx.frames_ts
252✔
170
    } else {
171
        &tx.frames_tc
252✔
172
    };
173
    let eof = tx.state >= HTTP2TransactionState::HTTP2StateClosed;
504✔
174
    return detect_uint_match_at_index::<HTTP2Frame, u32>(frames, ctx, get_http2_window, eof);
504✔
175
}
504✔
176

177
#[no_mangle]
178
pub unsafe extern "C" fn SCHttp2WindowMatch(
504✔
179
    tx: *mut std::os::raw::c_void, direction: u8, ctx: *const std::os::raw::c_void,
504✔
180
) -> std::os::raw::c_int {
504✔
181
    let tx = cast_pointer!(tx, HTTP2Transaction);
504✔
182
    let ctx = cast_pointer!(ctx, DetectUintArrayData<u32>);
504✔
183
    return http2_match_window(tx, direction.into(), ctx);
504✔
184
}
504✔
185

186
#[no_mangle]
187
pub unsafe extern "C" fn SCHttp2DetectSettingsCtxParse(
4✔
188
    str: *const std::os::raw::c_char,
4✔
189
) -> *mut std::os::raw::c_void {
4✔
190
    let ft_name: &CStr = CStr::from_ptr(str); //unsafe
4✔
191
    if let Ok(s) = ft_name.to_str() {
4✔
192
        if let Ok((_, ctx)) = parser::http2_parse_settingsctx(s) {
4✔
193
            let boxed = Box::new(ctx);
4✔
194
            return Box::into_raw(boxed) as *mut _;
4✔
UNCOV
195
        }
×
196
    }
×
UNCOV
197
    return std::ptr::null_mut();
×
198
}
4✔
199

200
#[no_mangle]
201
pub unsafe extern "C" fn SCHttp2DetectSettingsCtxFree(ctx: *mut std::os::raw::c_void) {
4✔
202
    // Just unbox...
4✔
203
    std::mem::drop(Box::from_raw(ctx as *mut parser::DetectHTTP2settingsSigCtx));
4✔
204
}
4✔
205

206
fn http2_detect_settings_match(
9✔
207
    set: &[parser::HTTP2FrameSettings], ctx: &parser::DetectHTTP2settingsSigCtx,
9✔
208
) -> std::os::raw::c_int {
9✔
209
    for e in set {
21✔
210
        if e.id == ctx.id {
15✔
211
            match &ctx.value {
6✔
212
                None => {
UNCOV
213
                    return 1;
×
214
                }
215
                Some(x) => {
6✔
216
                    if detect_match_uint(x, e.value) {
6✔
217
                        return 1;
3✔
218
                    }
3✔
219
                }
220
            }
221
        }
9✔
222
    }
223
    return 0;
6✔
224
}
9✔
225

226
fn http2_detect_settingsctx_match(
126✔
227
    ctx: &parser::DetectHTTP2settingsSigCtx, tx: &HTTP2Transaction, direction: Direction,
126✔
228
) -> std::os::raw::c_int {
126✔
229
    if direction == Direction::ToServer {
126✔
230
        for i in 0..tx.frames_ts.len() {
63✔
231
            if let HTTP2FrameTypeData::SETTINGS(set) = &tx.frames_ts[i].data {
60✔
232
                if http2_detect_settings_match(set, ctx) != 0 {
6✔
233
                    return 1;
3✔
234
                }
3✔
235
            }
54✔
236
        }
237
    } else {
238
        for i in 0..tx.frames_tc.len() {
63✔
239
            if let HTTP2FrameTypeData::SETTINGS(set) = &tx.frames_tc[i].data {
3✔
240
                if http2_detect_settings_match(set, ctx) != 0 {
3✔
UNCOV
241
                    return 1;
×
242
                }
3✔
UNCOV
243
            }
×
244
        }
245
    }
246
    return 0;
123✔
247
}
126✔
248

249
#[no_mangle]
250
pub unsafe extern "C" fn SCHttp2DetectSettingsCtxMatch(
126✔
251
    ctx: *const std::os::raw::c_void, tx: *mut std::os::raw::c_void, direction: u8,
126✔
252
) -> std::os::raw::c_int {
126✔
253
    let ctx = cast_pointer!(ctx, parser::DetectHTTP2settingsSigCtx);
126✔
254
    let tx = cast_pointer!(tx, HTTP2Transaction);
126✔
255
    return http2_detect_settingsctx_match(ctx, tx, direction.into());
126✔
256
}
126✔
257

258
fn http2_detect_sizeupdate_match(
×
259
    blocks: &[parser::HTTP2FrameHeaderBlock], ctx: &DetectUintData<u64>,
×
260
) -> std::os::raw::c_int {
×
261
    for block in blocks.iter() {
×
262
        if block.error == parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeSizeUpdate
×
263
            && detect_match_uint(ctx, block.sizeupdate)
×
264
        {
265
            return 1;
×
266
        }
×
267
    }
268
    return 0;
×
269
}
×
270

271
fn http2_header_blocks(frame: &HTTP2Frame) -> Option<&[parser::HTTP2FrameHeaderBlock]> {
10,994✔
272
    match &frame.data {
10,994✔
273
        HTTP2FrameTypeData::HEADERS(hd) => {
3,177✔
274
            return Some(&hd.blocks);
3,177✔
275
        }
276
        HTTP2FrameTypeData::CONTINUATION(hd) => {
36✔
277
            return Some(&hd.blocks);
36✔
278
        }
279
        HTTP2FrameTypeData::PUSHPROMISE(hd) => {
×
280
            return Some(&hd.blocks);
×
281
        }
282
        _ => {}
7,781✔
283
    }
7,781✔
284
    return None;
7,781✔
285
}
10,994✔
286

287
fn http2_detect_sizeupdatectx_match(
×
288
    ctx: &DetectUintData<u64>, tx: &HTTP2Transaction, direction: Direction,
×
289
) -> std::os::raw::c_int {
×
290
    if direction == Direction::ToServer {
×
291
        for i in 0..tx.frames_ts.len() {
×
292
            if let Some(blocks) = http2_header_blocks(&tx.frames_ts[i]) {
×
293
                if http2_detect_sizeupdate_match(blocks, ctx) != 0 {
×
294
                    return 1;
×
295
                }
×
296
            }
×
297
        }
298
    } else {
299
        for i in 0..tx.frames_tc.len() {
×
300
            if let Some(blocks) = http2_header_blocks(&tx.frames_tc[i]) {
×
301
                if http2_detect_sizeupdate_match(blocks, ctx) != 0 {
×
302
                    return 1;
×
303
                }
×
304
            }
×
305
        }
306
    }
307
    return 0;
×
308
}
×
309

310
#[no_mangle]
311
pub unsafe extern "C" fn SCHttp2DetectSizeUpdateCtxMatch(
×
312
    ctx: *const std::os::raw::c_void, tx: *mut std::os::raw::c_void, direction: u8,
×
313
) -> std::os::raw::c_int {
×
314
    let ctx = cast_pointer!(ctx, DetectUintData<u64>);
×
315
    let tx = cast_pointer!(tx, HTTP2Transaction);
×
316
    return http2_detect_sizeupdatectx_match(ctx, tx, direction.into());
×
317
}
×
318

319
//TODOask better syntax between SCHttp2TxGetHeaderName in argument
320
// and SCHttp2DetectSizeUpdateCtxMatch explicitly casting
321
#[no_mangle]
322
pub unsafe extern "C" fn SCHttp2TxGetHeaderName(
×
323
    _de: *mut DetectEngineThreadCtx, tx: *const c_void, direction: u8, nb: u32,
×
324
    buffer: *mut *const u8, buffer_len: *mut u32,
×
325
) -> bool {
×
326
    let tx = cast_pointer!(tx, HTTP2Transaction);
×
327
    let mut pos = 0_u32;
×
328
    match direction.into() {
×
329
        Direction::ToServer => {
330
            for i in 0..tx.frames_ts.len() {
×
331
                if let Some(blocks) = http2_header_blocks(&tx.frames_ts[i]) {
×
332
                    if nb < pos + blocks.len() as u32 {
×
333
                        let value = &blocks[(nb - pos) as usize].name;
×
334
                        *buffer = value.as_ptr(); //unsafe
×
335
                        *buffer_len = value.len() as u32;
×
336
                        return true;
×
337
                    } else {
×
338
                        pos += blocks.len() as u32;
×
339
                    }
×
340
                }
×
341
            }
342
        }
343
        Direction::ToClient => {
344
            for i in 0..tx.frames_tc.len() {
×
345
                if let Some(blocks) = http2_header_blocks(&tx.frames_tc[i]) {
×
346
                    if nb < pos + blocks.len() as u32 {
×
347
                        let value = &blocks[(nb - pos) as usize].name;
×
348
                        *buffer = value.as_ptr(); //unsafe
×
349
                        *buffer_len = value.len() as u32;
×
350
                        return true;
×
351
                    } else {
×
352
                        pos += blocks.len() as u32;
×
353
                    }
×
354
                }
×
355
            }
356
        }
357
    }
358
    return false;
×
359
}
×
360

361
fn http2_frames_get_header_firstvalue<'a>(
1,258✔
362
    tx: &'a mut HTTP2Transaction, direction: Direction, name: &str,
1,258✔
363
) -> Result<&'a [u8], ()> {
1,258✔
364
    let frames = if direction == Direction::ToServer {
1,258✔
365
        &tx.frames_ts
895✔
366
    } else {
367
        &tx.frames_tc
363✔
368
    };
369
    for frame in frames {
1,682✔
370
        if let Some(blocks) = http2_header_blocks(frame) {
935✔
371
            for block in blocks.iter() {
910✔
372
                if block.name.as_ref() == name.as_bytes() {
910✔
373
                    return Ok(&block.value);
511✔
374
                }
399✔
375
            }
376
        }
397✔
377
    }
378
    return Err(());
747✔
379
}
1,258✔
380

381
// same as http2_frames_get_header_value but returns a new Vec
382
// instead of using the transaction to store the result slice
383
pub fn http2_frames_get_header_value_vec(
199✔
384
    tx: &HTTP2Transaction, direction: Direction, name: &str,
199✔
385
) -> Result<Vec<u8>, ()> {
199✔
386
    let mut found = 0;
199✔
387
    let mut vec = Vec::new();
199✔
388
    let frames = if direction == Direction::ToServer {
199✔
389
        &tx.frames_ts
24✔
390
    } else {
391
        &tx.frames_tc
175✔
392
    };
393
    for frame in frames {
741✔
394
        if let Some(blocks) = http2_header_blocks(frame) {
542✔
395
            for block in blocks.iter() {
1,832✔
396
                if block.name.as_ref() == name.as_bytes() {
1,832✔
397
                    if found == 0 {
36✔
398
                        vec.extend_from_slice(&block.value);
36✔
399
                        found = 1;
36✔
400
                    } else if found == 1 && Rc::strong_count(&block.name) <= 2 {
36✔
401
                        vec.extend_from_slice(b", ");
×
402
                        vec.extend_from_slice(&block.value);
×
403
                        found = 2;
×
404
                    } else if Rc::strong_count(&block.name) <= 2 {
×
405
                        vec.extend_from_slice(b", ");
×
406
                        vec.extend_from_slice(&block.value);
×
407
                    }
×
408
                }
1,796✔
409
            }
410
        }
175✔
411
    }
412
    if found == 0 {
199✔
413
        return Err(());
163✔
414
    } else {
415
        return Ok(vec);
36✔
416
    }
417
}
199✔
418

419
#[derive(Debug, PartialEq)]
420
enum Http2Header<'a> {
421
    Single(&'a [u8]),
422
    Multiple(Vec<u8>),
423
}
424

425
fn http2_frames_get_header_value<'a>(
2,710✔
426
    tx: &'a HTTP2Transaction, direction: Direction, name: &str,
2,710✔
427
) -> Option<Http2Header<'a>> {
2,710✔
428
    let mut found = 0;
2,710✔
429
    let mut vec = Vec::new();
2,710✔
430
    let mut single = None;
2,710✔
431
    let frames = if direction == Direction::ToServer {
2,710✔
432
        &tx.frames_ts
1,750✔
433
    } else {
434
        &tx.frames_tc
960✔
435
    };
436
    for frame in frames {
8,928✔
437
        if let Some(blocks) = http2_header_blocks(frame) {
6,218✔
438
            for block in blocks.iter() {
12,304✔
439
                if block.name.as_ref() == name.as_bytes() {
12,304✔
440
                    if found == 0 {
753✔
441
                        single = Some(Http2Header::Single(&block.value));
728✔
442
                        found = 1;
728✔
443
                    } else if found == 1 && Rc::strong_count(&block.name) <= 2 {
728✔
444
                        if let Some(Http2Header::Single(s)) = single {
10✔
445
                            vec.extend_from_slice(s);
10✔
446
                        }
10✔
447
                        vec.extend_from_slice(b", ");
10✔
448
                        vec.extend_from_slice(&block.value);
10✔
449
                        found = 2;
10✔
450
                    } else if Rc::strong_count(&block.name) <= 2 {
15✔
451
                        vec.extend_from_slice(b", ");
3✔
452
                        vec.extend_from_slice(&block.value);
3✔
453
                    }
12✔
454
                }
11,551✔
455
            }
456
        }
4,687✔
457
    }
458
    if found == 0 {
2,710✔
459
        return None;
1,982✔
460
    } else if found == 1 {
728✔
461
        return single;
718✔
462
    } else {
463
        return Some(Http2Header::Multiple(vec));
10✔
464
    }
465
}
2,710✔
466

467
// we mutate the tx to cache req_line
468
fn http2_tx_get_req_line(tx: &mut HTTP2Transaction) {
174✔
469
    if !tx.req_line.is_empty() {
174✔
470
        return;
5✔
471
    }
169✔
472
    let empty = Vec::new();
169✔
473
    let mut req_line = Vec::new();
169✔
474
    let method =
169✔
475
        if let Ok(value) = http2_frames_get_header_firstvalue(tx, Direction::ToServer, ":method") {
169✔
476
            value
66✔
477
        } else {
478
            &empty
103✔
479
        };
480
    req_line.extend(method);
169✔
481
    req_line.push(b' ');
169✔
482

483
    let uri =
169✔
484
        if let Ok(value) = http2_frames_get_header_firstvalue(tx, Direction::ToServer, ":path") {
169✔
485
            value
66✔
486
        } else {
487
            &empty
103✔
488
        };
489
    req_line.extend(uri);
169✔
490
    req_line.extend(b" HTTP/2\r\n");
169✔
491
    tx.req_line.extend(req_line)
169✔
492
}
174✔
493

494
#[no_mangle]
495
pub unsafe extern "C" fn SCHttp2TxGetRequestLine(
174✔
496
    tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32,
174✔
497
) -> u8 {
174✔
498
    http2_tx_get_req_line(tx);
174✔
499
    *buffer = tx.req_line.as_ptr(); //unsafe
174✔
500
    *buffer_len = tx.req_line.len() as u32;
174✔
501
    return 1;
174✔
502
}
174✔
503

504
fn http2_tx_get_resp_line(tx: &mut HTTP2Transaction) {
194✔
505
    if !tx.resp_line.is_empty() {
194✔
506
        return;
25✔
507
    }
169✔
508
    let empty = Vec::new();
169✔
509
    let mut resp_line: Vec<u8> = Vec::new();
169✔
510

511
    let status =
169✔
512
        if let Ok(value) = http2_frames_get_header_firstvalue(tx, Direction::ToClient, ":status") {
169✔
513
            value
66✔
514
        } else {
515
            &empty
103✔
516
        };
517
    resp_line.extend(b"HTTP/2 ");
169✔
518
    resp_line.extend(status);
169✔
519
    resp_line.extend(b"\r\n");
169✔
520
    tx.resp_line.extend(resp_line)
169✔
521
}
194✔
522

523
#[no_mangle]
524
pub unsafe extern "C" fn SCHttp2TxGetResponseLine(
194✔
525
    tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32,
194✔
526
) -> u8 {
194✔
527
    http2_tx_get_resp_line(tx);
194✔
528
    *buffer = tx.resp_line.as_ptr(); //unsafe
194✔
529
    *buffer_len = tx.resp_line.len() as u32;
194✔
530
    return 1;
194✔
531
}
194✔
532

533
#[no_mangle]
534
pub unsafe extern "C" fn SCHttp2TxGetUri(
383✔
535
    tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32,
383✔
536
) -> u8 {
383✔
537
    if let Ok(value) = http2_frames_get_header_firstvalue(tx, Direction::ToServer, ":path") {
383✔
538
        *buffer = value.as_ptr(); //unsafe
151✔
539
        *buffer_len = value.len() as u32;
151✔
540
        return 1;
151✔
541
    }
232✔
542
    return 0;
232✔
543
}
383✔
544

545
#[no_mangle]
546
pub unsafe extern "C" fn SCHttp2TxGetMethod(
174✔
547
    tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32,
174✔
548
) -> u8 {
174✔
549
    if let Ok(value) = http2_frames_get_header_firstvalue(tx, Direction::ToServer, ":method") {
174✔
550
        *buffer = value.as_ptr(); //unsafe
71✔
551
        *buffer_len = value.len() as u32;
71✔
552
        return 1;
71✔
553
    }
103✔
554
    return 0;
103✔
555
}
174✔
556

557
#[no_mangle]
558
pub unsafe extern "C" fn SCHttp2TxGetHost(
34✔
559
    tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void,
34✔
560
) -> u8 {
34✔
561
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
34✔
562
    if let Some(value) = http2_frames_get_header_value(tx, Direction::ToServer, ":authority") {
34✔
563
        match value {
11✔
564
            Http2Header::Single(v) => {
10✔
565
                *buffer = v.as_ptr(); //unsafe
10✔
566
                *buffer_len = v.len() as u32;
10✔
567
            }
10✔
568
            Http2Header::Multiple(v) => {
1✔
569
                tbuf.data = v;
1✔
570
                *buffer = tbuf.data.as_ptr(); //unsafe
1✔
571
                *buffer_len = tbuf.data.len() as u32;
1✔
572
            }
1✔
573
        }
574
        return 1;
11✔
575
    }
23✔
576
    return 0;
23✔
577
}
34✔
578

579
// returns a tuple with the value and its size
580
fn http2_normalize_host(ve: Http2Header) -> Http2Header {
148✔
581
    let vs = match &ve {
148✔
582
        Http2Header::Single(v) => v,
146✔
583
        Http2Header::Multiple(v) => v.as_slice(),
2✔
584
    };
585
    let (start, end) = match vs.iter().position(|&x| x == b'@') {
2,870✔
586
        Some(i) => match &vs[i + 1..].iter().position(|&x| x == b':') {
39✔
587
            Some(j) => (i + 1, i + 1 + j),
1✔
588
            None => (i + 1, vs.len()),
3✔
589
        },
590
        None => match vs.iter().position(|&x| x == b':') {
2,618✔
591
            Some(i) => (0, i),
41✔
592
            None => (0, vs.len()),
103✔
593
        },
594
    };
595
    return match ve {
148✔
596
        Http2Header::Single(v) => {
146✔
597
            let mut need_transform = false;
146✔
598
            for c in v.iter().take(end).skip(start) {
2,592✔
599
                if c.is_ascii_uppercase() {
2,592✔
600
                    need_transform = true;
1✔
601
                    break;
1✔
602
                }
2,591✔
603
            }
604
            if need_transform {
146✔
605
                let mut vec: Vec<u8> = Vec::with_capacity(end - start);
1✔
606
                for c in v.iter().take(end).skip(start) {
7✔
607
                    vec.push(c.to_ascii_lowercase());
7✔
608
                }
7✔
609
                Http2Header::Multiple(vec)
1✔
610
            } else {
611
                Http2Header::Single(&v[start..end])
145✔
612
            }
613
        }
614
        Http2Header::Multiple(mut v) => {
2✔
615
            if end < v.len() {
2✔
616
                v.truncate(end);
2✔
617
            }
2✔
618
            if start > 0 {
2✔
619
                v.drain(0..start);
×
620
            }
2✔
621
            for e in &mut v {
20✔
622
                e.make_ascii_lowercase();
18✔
623
            }
18✔
624
            Http2Header::Multiple(v)
2✔
625
        }
626
    };
627
}
148✔
628

629
#[no_mangle]
630
pub unsafe extern "C" fn SCHttp2TxGetHostNorm(
350✔
631
    tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void,
350✔
632
) -> u8 {
350✔
633
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
350✔
634
    if let Some(value) = http2_frames_get_header_value(tx, Direction::ToServer, ":authority") {
350✔
635
        match http2_normalize_host(value) {
143✔
636
            Http2Header::Single(v) => {
141✔
637
                *buffer = v.as_ptr(); //unsafe
141✔
638
                *buffer_len = v.len() as u32;
141✔
639
            }
141✔
640
            Http2Header::Multiple(v) => {
2✔
641
                tbuf.data = v;
2✔
642
                *buffer = tbuf.data.as_ptr(); //unsafe
2✔
643
                *buffer_len = tbuf.data.len() as u32;
2✔
644
            }
2✔
645
        }
646
        return 1;
143✔
647
    }
207✔
648
    return 0;
207✔
649
}
350✔
650

651
#[no_mangle]
652
pub unsafe extern "C" fn SCHttp2TxGetUserAgent(
174✔
653
    tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void,
174✔
654
) -> u8 {
174✔
655
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
174✔
656
    if let Some(value) = http2_frames_get_header_value(tx, Direction::ToServer, "user-agent") {
174✔
657
        match value {
66✔
658
            Http2Header::Single(v) => {
66✔
659
                *buffer = v.as_ptr(); //unsafe
66✔
660
                *buffer_len = v.len() as u32;
66✔
661
            }
66✔
662
            Http2Header::Multiple(v) => {
×
663
                tbuf.data = v;
×
664
                *buffer = tbuf.data.as_ptr(); //unsafe
×
665
                *buffer_len = tbuf.data.len() as u32;
×
666
            }
×
667
        }
668
        return 1;
66✔
669
    }
108✔
670
    return 0;
108✔
671
}
174✔
672

673
#[no_mangle]
674
pub unsafe extern "C" fn SCHttp2TxGetStatus(
194✔
675
    tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32,
194✔
676
) -> u8 {
194✔
677
    if let Ok(value) = http2_frames_get_header_firstvalue(tx, Direction::ToClient, ":status") {
194✔
678
        *buffer = value.as_ptr(); //unsafe
91✔
679
        *buffer_len = value.len() as u32;
91✔
680
        return 1;
91✔
681
    }
103✔
682
    return 0;
103✔
683
}
194✔
684

685
#[no_mangle]
686
pub unsafe extern "C" fn SCHttp2TxGetCookie(
384✔
687
    tx: &mut HTTP2Transaction, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32,
384✔
688
    tbuf: *mut c_void,
384✔
689
) -> u8 {
384✔
690
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
384✔
691
    if direction == u8::from(Direction::ToServer) {
384✔
692
        if let Some(value) = http2_frames_get_header_value(tx, Direction::ToServer, "cookie") {
182✔
693
            match value {
13✔
694
                Http2Header::Single(v) => {
7✔
695
                    *buffer = v.as_ptr(); //unsafe
7✔
696
                    *buffer_len = v.len() as u32;
7✔
697
                }
7✔
698
                Http2Header::Multiple(v) => {
6✔
699
                    tbuf.data = v;
6✔
700
                    *buffer = tbuf.data.as_ptr(); //unsafe
6✔
701
                    *buffer_len = tbuf.data.len() as u32;
6✔
702
                }
6✔
703
            }
704
            return 1;
13✔
705
        }
169✔
706
    } else if let Some(value) = http2_frames_get_header_value(tx, Direction::ToClient, "set-cookie")
202✔
707
    {
708
        match value {
1✔
709
            Http2Header::Single(v) => {
1✔
710
                *buffer = v.as_ptr(); //unsafe
1✔
711
                *buffer_len = v.len() as u32;
1✔
712
            }
1✔
713
            Http2Header::Multiple(v) => {
×
714
                tbuf.data = v;
×
715
                *buffer = tbuf.data.as_ptr(); //unsafe
×
716
                *buffer_len = tbuf.data.len() as u32;
×
717
            }
×
718
        }
719
        return 1;
1✔
720
    }
201✔
721
    return 0;
370✔
722
}
384✔
723

724
#[no_mangle]
725
pub unsafe extern "C" fn SCHttp2TxGetHeaderValue(
1,767✔
726
    tx: &mut HTTP2Transaction, direction: u8, strname: *const std::os::raw::c_char,
1,767✔
727
    buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void,
1,767✔
728
) -> u8 {
1,767✔
729
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
1,767✔
730
    let hname: &CStr = CStr::from_ptr(strname); //unsafe
1,767✔
731
    if let Ok(s) = hname.to_str() {
1,767✔
732
        if let Some(value) = http2_frames_get_header_value(tx, direction.into(), &s.to_lowercase())
1,767✔
733
        {
734
            match value {
493✔
735
                Http2Header::Single(v) => {
493✔
736
                    *buffer = v.as_ptr(); //unsafe
493✔
737
                    *buffer_len = v.len() as u32;
493✔
738
                }
493✔
739
                Http2Header::Multiple(v) => {
×
740
                    tbuf.data = v;
×
741
                    *buffer = tbuf.data.as_ptr(); //unsafe
×
742
                    *buffer_len = tbuf.data.len() as u32;
×
743
                }
×
744
            }
745
            return 1;
493✔
746
        }
1,274✔
747
    }
×
748
    return 0;
1,274✔
749
}
1,767✔
750

751
fn http2_escape_header(blocks: &[parser::HTTP2FrameHeaderBlock], i: u32) -> Vec<u8> {
59✔
752
    //minimum size + 2 for escapes
59✔
753
    let normalsize = blocks[i as usize].value.len() + 2 + blocks[i as usize].name.len();
59✔
754
    let mut vec = Vec::with_capacity(normalsize);
59✔
755
    vec.extend_from_slice(&blocks[i as usize].name);
59✔
756
    vec.extend_from_slice(b": ");
59✔
757
    vec.extend_from_slice(&blocks[i as usize].value);
59✔
758
    return vec;
59✔
759
}
59✔
760

761
#[derive(Default)]
762
struct Http2ThreadBuf {
763
    data: Vec<u8>,
764
}
765

766
#[no_mangle]
767
pub unsafe extern "C" fn SCHttp2ThreadBufDataInit(_cfg: *mut c_void) -> *mut c_void {
208,432✔
768
    let boxed = Box::new(Http2ThreadBuf::default());
208,432✔
769
    return Box::into_raw(boxed) as *mut c_void;
208,432✔
770
}
208,432✔
771

772
#[no_mangle]
773
pub unsafe extern "C" fn SCHttp2ThreadBufDataFree(ctx: *mut c_void) {
208,432✔
774
    std::mem::drop(Box::from_raw(ctx as *mut Http2ThreadBuf));
208,432✔
775
}
208,432✔
776

777
#[no_mangle]
778
pub unsafe extern "C" fn SCHttp2TxGetHeaderNames(
440✔
779
    tx: &mut HTTP2Transaction, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32,
440✔
780
    tbuf: *mut c_void,
440✔
781
) -> u8 {
440✔
782
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
440✔
783
    tbuf.data.clear();
440✔
784
    tbuf.data.extend_from_slice(b"\r\n");
440✔
785
    let frames = if direction & Direction::ToServer as u8 != 0 {
440✔
786
        &tx.frames_ts
210✔
787
    } else {
788
        &tx.frames_tc
230✔
789
    };
790
    for frame in frames {
1,549✔
791
        if let Some(blocks) = http2_header_blocks(frame) {
1,109✔
792
            for block in blocks.iter() {
1,729✔
793
                // we do not escape linefeeds in headers names
1,729✔
794
                tbuf.data.extend_from_slice(&block.name);
1,729✔
795
                tbuf.data.extend_from_slice(b"\r\n");
1,729✔
796
            }
1,729✔
797
        }
851✔
798
    }
799
    if tbuf.data.len() > 2 {
440✔
800
        tbuf.data.extend_from_slice(b"\r\n");
174✔
801
        *buffer = tbuf.data.as_ptr(); //unsafe
174✔
802
        *buffer_len = tbuf.data.len() as u32;
174✔
803
        return 1;
174✔
804
    }
266✔
805
    return 0;
266✔
806
}
440✔
807

808
fn http2_header_iscookie(direction: Direction, hname: &[u8]) -> bool {
1,729✔
809
    if let Ok(s) = std::str::from_utf8(hname) {
1,729✔
810
        if direction == Direction::ToServer {
1,729✔
811
            if s.to_lowercase() == "cookie" {
860✔
812
                return true;
26✔
813
            }
834✔
814
        } else if s.to_lowercase() == "set-cookie" {
869✔
815
            return true;
1✔
816
        }
868✔
UNCOV
817
    }
×
818
    return false;
1,702✔
819
}
1,729✔
820

821
fn http2_header_trimspaces(value: &[u8]) -> &[u8] {
1,705✔
822
    let mut start = 0;
1,705✔
823
    let mut end = value.len();
1,705✔
824
    while start < value.len() {
1,708✔
825
        if value[start] == b' ' || value[start] == b'\t' {
1,686✔
826
            start += 1;
3✔
827
        } else {
3✔
828
            break;
1,683✔
829
        }
830
    }
831
    while end > start {
1,706✔
832
        if value[end - 1] == b' ' || value[end - 1] == b'\t' {
1,684✔
833
            end -= 1;
1✔
834
        } else {
1✔
835
            break;
1,683✔
836
        }
837
    }
838
    return &value[start..end];
1,705✔
839
}
1,705✔
840

841
#[no_mangle]
842
pub unsafe extern "C" fn SCHttp2TxGetHeaders(
440✔
843
    tx: &mut HTTP2Transaction, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32,
440✔
844
    tbuf: *mut c_void,
440✔
845
) -> u8 {
440✔
846
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
440✔
847
    tbuf.data.clear();
440✔
848
    let frames = if direction & Direction::ToServer as u8 != 0 {
440✔
849
        &tx.frames_ts
210✔
850
    } else {
851
        &tx.frames_tc
230✔
852
    };
853
    for frame in frames {
1,549✔
854
        if let Some(blocks) = http2_header_blocks(frame) {
1,109✔
855
            for block in blocks.iter() {
1,729✔
856
                if !http2_header_iscookie(direction.into(), &block.name) {
1,729✔
857
                    // we do not escape linefeeds nor : in headers names
1,702✔
858
                    tbuf.data.extend_from_slice(&block.name);
1,702✔
859
                    tbuf.data.extend_from_slice(b": ");
1,702✔
860
                    tbuf.data
1,702✔
861
                        .extend_from_slice(http2_header_trimspaces(&block.value));
1,702✔
862
                    tbuf.data.extend_from_slice(b"\r\n");
1,702✔
863
                }
1,702✔
864
            }
865
        }
851✔
866
    }
867
    if !tbuf.data.is_empty() {
440✔
868
        *buffer = tbuf.data.as_ptr(); //unsafe
174✔
869
        *buffer_len = tbuf.data.len() as u32;
174✔
870
        return 1;
174✔
871
    }
266✔
872
    return 0;
266✔
873
}
440✔
874

875
#[no_mangle]
876
pub unsafe extern "C" fn SCHttp2TxGetHeadersRaw(
356✔
877
    tx: &mut HTTP2Transaction, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32,
356✔
878
    tbuf: *mut c_void,
356✔
879
) -> u8 {
356✔
880
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
356✔
881
    tbuf.data.clear();
356✔
882
    let frames = if direction & Direction::ToServer as u8 != 0 {
356✔
883
        &tx.frames_ts
168✔
884
    } else {
885
        &tx.frames_tc
188✔
886
    };
887
    for frame in frames {
1,353✔
888
        if let Some(blocks) = http2_header_blocks(frame) {
997✔
889
            for block in blocks.iter() {
1,602✔
890
                // we do not escape linefeeds nor : in headers names
1,602✔
891
                tbuf.data.extend_from_slice(&block.name);
1,602✔
892
                tbuf.data.extend_from_slice(b": ");
1,602✔
893
                tbuf.data.extend_from_slice(&block.value);
1,602✔
894
                tbuf.data.extend_from_slice(b"\r\n");
1,602✔
895
            }
1,602✔
896
        }
803✔
897
    }
898
    if !tbuf.data.is_empty() {
356✔
899
        *buffer = tbuf.data.as_ptr(); //unsafe
158✔
900
        *buffer_len = tbuf.data.len() as u32;
158✔
901
        return 1;
158✔
902
    }
198✔
903
    return 0;
198✔
904
}
356✔
905

906
#[derive(Default)]
907
struct Http2ThreadMultiBuf {
908
    data: Vec<Vec<u8>>,
909
}
910

911
#[no_mangle]
912
pub unsafe extern "C" fn SCHttp2ThreadMultiBufDataInit(_cfg: *mut c_void) -> *mut c_void {
26,054✔
913
    let boxed = Box::new(Http2ThreadMultiBuf::default());
26,054✔
914
    return Box::into_raw(boxed) as *mut c_void;
26,054✔
915
}
26,054✔
916

917
#[no_mangle]
918
pub unsafe extern "C" fn SCHttp2ThreadMultiBufDataFree(ctx: *mut c_void) {
26,054✔
919
    std::mem::drop(Box::from_raw(ctx as *mut Http2ThreadMultiBuf));
26,054✔
920
}
26,054✔
921

922
#[no_mangle]
923
pub unsafe extern "C" fn SCHttp2TxGetHeader(
89✔
924
    tbuf: *mut c_void, tx: *const c_void, direction: u8, nb: u32, buffer: *mut *const u8,
89✔
925
    buffer_len: *mut u32,
89✔
926
) -> bool {
89✔
927
    let tbuf = cast_pointer!(tbuf, Http2ThreadMultiBuf);
89✔
928
    let tx = cast_pointer!(tx, HTTP2Transaction);
89✔
929
    let mut pos = 0_u32;
89✔
930
    if nb == 0 {
89✔
931
        tbuf.data.clear();
30✔
932
    }
59✔
933
    match direction.into() {
89✔
934
        Direction::ToServer => {
935
            for i in 0..tx.frames_ts.len() {
89✔
936
                if let Some(blocks) = http2_header_blocks(&tx.frames_ts[i]) {
84✔
937
                    if nb < pos + blocks.len() as u32 {
67✔
938
                        let ehdr = http2_escape_header(blocks, nb - pos);
59✔
939
                        tbuf.data.push(ehdr);
59✔
940
                        let idx = tbuf.data.len() - 1;
59✔
941
                        let value = &tbuf.data[idx];
59✔
942
                        *buffer = value.as_ptr(); //unsafe
59✔
943
                        *buffer_len = value.len() as u32;
59✔
944
                        return true;
59✔
945
                    } else {
8✔
946
                        pos += blocks.len() as u32;
8✔
947
                    }
8✔
948
                }
17✔
949
            }
950
        }
951
        Direction::ToClient => {
UNCOV
952
            for i in 0..tx.frames_tc.len() {
×
UNCOV
953
                if let Some(blocks) = http2_header_blocks(&tx.frames_tc[i]) {
×
954
                    if nb < pos + blocks.len() as u32 {
×
955
                        let ehdr = http2_escape_header(blocks, nb - pos);
×
956
                        tbuf.data.push(ehdr);
×
957
                        let idx = tbuf.data.len() - 1;
×
958
                        let value = &tbuf.data[idx];
×
959
                        *buffer = value.as_ptr(); //unsafe
×
960
                        *buffer_len = value.len() as u32;
×
961
                        return true;
×
962
                    } else {
×
963
                        pos += blocks.len() as u32;
×
964
                    }
×
UNCOV
965
                }
×
966
            }
967
        }
968
    }
969
    return false;
30✔
970
}
89✔
971

972
fn http2_tx_set_header(state: &mut HTTP2State, name: &[u8], input: &[u8]) {
155✔
973
    let head = parser::HTTP2FrameHeader {
155✔
974
        length: 0,
155✔
975
        ftype: parser::HTTP2FrameType::Headers as u8,
155✔
976
        flags: 0,
155✔
977
        reserved: 0,
155✔
978
        stream_id: 1,
155✔
979
    };
155✔
980
    let mut blocks = Vec::new();
155✔
981
    let b = parser::HTTP2FrameHeaderBlock {
155✔
982
        name: Rc::new(name.to_vec()),
155✔
983
        value: Rc::new(input.to_vec()),
155✔
984
        error: parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeSuccess,
155✔
985
        sizeupdate: 0,
155✔
986
    };
155✔
987
    blocks.push(b);
155✔
988
    let hs = parser::HTTP2FrameHeaders {
155✔
989
        padlength: None,
155✔
990
        priority: None,
155✔
991
        blocks,
155✔
992
    };
155✔
993
    let txdata = HTTP2FrameTypeData::HEADERS(hs);
155✔
994
    let tx = state
155✔
995
        .find_or_create_tx(&head, &txdata, Direction::ToServer)
155✔
996
        .unwrap();
155✔
997
    tx.frames_ts.push(HTTP2Frame {
155✔
998
        header: head,
155✔
999
        data: txdata,
155✔
1000
    });
155✔
1001
    //we do not expect more data from client
155✔
1002
    tx.state = HTTP2TransactionState::HTTP2StateHalfClosedClient;
155✔
1003
}
155✔
1004

1005
#[no_mangle]
1006
pub unsafe extern "C" fn SCHttp2TxSetMethod(
21✔
1007
    state: &mut HTTP2State, buffer: *const u8, buffer_len: u32,
21✔
1008
) {
21✔
1009
    let slice = build_slice!(buffer, buffer_len as usize);
21✔
1010
    http2_tx_set_header(state, ":method".as_bytes(), slice)
21✔
1011
}
21✔
1012

1013
#[no_mangle]
1014
pub unsafe extern "C" fn SCHttp2TxSetUri(
21✔
1015
    state: &mut HTTP2State, buffer: *const u8, buffer_len: u32,
21✔
1016
) {
21✔
1017
    let slice = build_slice!(buffer, buffer_len as usize);
21✔
1018
    http2_tx_set_header(state, ":path".as_bytes(), slice)
21✔
1019
}
21✔
1020

1021
fn http2_tx_set_settings(state: &mut HTTP2State, input: &[u8]) {
19✔
1022
    match STANDARD.decode(input) {
19✔
1023
        Ok(dec) => {
19✔
1024
            if dec.len() % 6 != 0 {
19✔
1025
                state.set_event(HTTP2Event::InvalidHttp1Settings);
×
1026
            }
19✔
1027

1028
            let head = parser::HTTP2FrameHeader {
19✔
1029
                length: dec.len() as u32,
19✔
1030
                ftype: parser::HTTP2FrameType::Settings as u8,
19✔
1031
                flags: 0,
19✔
1032
                reserved: 0,
19✔
1033
                stream_id: 0,
19✔
1034
            };
19✔
1035

19✔
1036
            match parser::http2_parse_frame_settings(&dec) {
19✔
1037
                Ok((_, set)) => {
19✔
1038
                    let txdata = HTTP2FrameTypeData::SETTINGS(set);
19✔
1039
                    let tx = state
19✔
1040
                        .find_or_create_tx(&head, &txdata, Direction::ToServer)
19✔
1041
                        .unwrap();
19✔
1042
                    tx.frames_ts.push(HTTP2Frame {
19✔
1043
                        header: head,
19✔
1044
                        data: txdata,
19✔
1045
                    });
19✔
1046
                }
19✔
1047
                Err(_) => {
×
1048
                    state.set_event(HTTP2Event::InvalidHttp1Settings);
×
1049
                }
×
1050
            }
1051
        }
UNCOV
1052
        Err(_) => {
×
UNCOV
1053
            state.set_event(HTTP2Event::InvalidHttp1Settings);
×
UNCOV
1054
        }
×
1055
    }
1056
}
19✔
1057

1058
fn http2_caseinsensitive_cmp(s1: &[u8], s2: &str) -> bool {
113✔
1059
    if let Ok(s) = std::str::from_utf8(s1) {
113✔
1060
        return s.to_lowercase() == s2;
113✔
UNCOV
1061
    }
×
UNCOV
1062
    return false;
×
1063
}
113✔
1064

1065
#[no_mangle]
1066
pub unsafe extern "C" fn SCHttp2TxAddHeader(
132✔
1067
    state: &mut HTTP2State, name: *const u8, name_len: u32, value: *const u8, value_len: u32,
132✔
1068
) {
132✔
1069
    let slice_name = build_slice!(name, name_len as usize);
132✔
1070
    let slice_value = build_slice!(value, value_len as usize);
132✔
1071
    if slice_name == "HTTP2-Settings".as_bytes() {
132✔
1072
        http2_tx_set_settings(state, slice_value)
19✔
1073
    } else if http2_caseinsensitive_cmp(slice_name, "host") {
113✔
1074
        http2_tx_set_header(state, ":authority".as_bytes(), slice_value)
21✔
1075
    } else {
1076
        http2_tx_set_header(state, slice_name, slice_value)
92✔
1077
    }
1078
}
132✔
1079

1080
#[cfg(test)]
1081
mod tests {
1082

1083
    use super::*;
1084

1085
    #[test]
1086
    fn test_http2_normalize_host() {
1✔
1087
        let buf0 = "aBC.com:1234".as_bytes();
1✔
1088
        let r0 = http2_normalize_host(Http2Header::Single(buf0));
1✔
1089
        assert_eq!(r0, Http2Header::Multiple("abc.com".as_bytes().to_vec()));
1✔
1090
        let buf1 = "oisf.net".as_bytes();
1✔
1091
        let r1 = http2_normalize_host(Http2Header::Single(buf1));
1✔
1092
        assert_eq!(r1, Http2Header::Single("oisf.net".as_bytes()));
1✔
1093
        let buf2 = "localhost:3000".as_bytes();
1✔
1094
        let r2 = http2_normalize_host(Http2Header::Single(buf2));
1✔
1095
        assert_eq!(r2, Http2Header::Single("localhost".as_bytes()));
1✔
1096
        let buf3 = "user:pass@localhost".as_bytes();
1✔
1097
        let r3 = http2_normalize_host(Http2Header::Single(buf3));
1✔
1098
        assert_eq!(r3, Http2Header::Single("localhost".as_bytes()));
1✔
1099
        let buf4 = "user:pass@localhost:123".as_bytes();
1✔
1100
        let r4 = http2_normalize_host(Http2Header::Single(buf4));
1✔
1101
        assert_eq!(r4, Http2Header::Single("localhost".as_bytes()));
1✔
1102
    }
1✔
1103

1104
    #[test]
1105
    fn test_http2_header_trimspaces() {
1✔
1106
        let buf0 = "nospaces".as_bytes();
1✔
1107
        let r0 = http2_header_trimspaces(buf0);
1✔
1108
        assert_eq!(r0, "nospaces".as_bytes());
1✔
1109
        let buf1 = " spaces\t".as_bytes();
1✔
1110
        let r1 = http2_header_trimspaces(buf1);
1✔
1111
        assert_eq!(r1, "spaces".as_bytes());
1✔
1112
        let buf2 = " \t".as_bytes();
1✔
1113
        let r2 = http2_header_trimspaces(buf2);
1✔
1114
        assert_eq!(r2, "".as_bytes());
1✔
1115
    }
1✔
1116

1117
    #[test]
1118
    fn test_http2_frames_get_header_value() {
1✔
1119
        let mut tx = HTTP2Transaction::new();
1✔
1120
        let head = parser::HTTP2FrameHeader {
1✔
1121
            length: 0,
1✔
1122
            ftype: parser::HTTP2FrameType::Headers as u8,
1✔
1123
            flags: 0,
1✔
1124
            reserved: 0,
1✔
1125
            stream_id: 1,
1✔
1126
        };
1✔
1127
        let mut blocks = Vec::new();
1✔
1128
        let b = parser::HTTP2FrameHeaderBlock {
1✔
1129
            name: "Host".as_bytes().to_vec().into(),
1✔
1130
            value: "abc.com".as_bytes().to_vec().into(),
1✔
1131
            error: parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeSuccess,
1✔
1132
            sizeupdate: 0,
1✔
1133
        };
1✔
1134
        blocks.push(b);
1✔
1135
        let b2 = parser::HTTP2FrameHeaderBlock {
1✔
1136
            name: "Host".as_bytes().to_vec().into(),
1✔
1137
            value: "efg.net".as_bytes().to_vec().into(),
1✔
1138
            error: parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeSuccess,
1✔
1139
            sizeupdate: 0,
1✔
1140
        };
1✔
1141
        blocks.push(b2);
1✔
1142
        let hs = parser::HTTP2FrameHeaders {
1✔
1143
            padlength: None,
1✔
1144
            priority: None,
1✔
1145
            blocks,
1✔
1146
        };
1✔
1147
        let txdata = HTTP2FrameTypeData::HEADERS(hs);
1✔
1148
        tx.frames_ts.push(HTTP2Frame {
1✔
1149
            header: head,
1✔
1150
            data: txdata,
1✔
1151
        });
1✔
1152
        match http2_frames_get_header_value(&tx, Direction::ToServer, "Host") {
1✔
1153
            Some(Http2Header::Multiple(x)) => {
1✔
1154
                assert_eq!(x, "abc.com, efg.net".as_bytes());
1✔
1155
            }
1156
            _ => {
1157
                panic!("Result should have been a multiple header value");
1158
            }
1159
        }
1160
    }
1✔
1161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc