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

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

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

84.5
/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(
11,377✔
36
    tx: *mut std::os::raw::c_void, direction: u8, ctx: *const std::os::raw::c_void,
11,377✔
37
) -> std::os::raw::c_int {
11,377✔
38
    let tx = cast_pointer!(tx, HTTP2Transaction);
11,377✔
39
    let ctx = cast_pointer!(ctx, DetectUintArrayData<u8>);
11,377✔
40
    let frames = if direction & Direction::ToServer as u8 != 0 {
11,377✔
41
        &tx.frames_ts
6,064✔
42
    } else {
43
        &tx.frames_tc
5,313✔
44
    };
45
    return detect_uint_match_at_index::<HTTP2Frame, u8>(
11,377✔
46
        frames,
11,377✔
47
        ctx,
11,377✔
48
        |f| Some(f.header.ftype),
11,377✔
49
        tx.state >= HTTP2TransactionState::HTTP2StateClosed,
11,377✔
50
    );
11,377✔
51
}
11,377✔
52

53
#[no_mangle]
54
pub unsafe extern "C" fn SCHttp2ParseFrametype(
4,454✔
55
    str: *const std::os::raw::c_char,
4,454✔
56
) -> *mut std::os::raw::c_void {
4,454✔
57
    let ft_name: &CStr = CStr::from_ptr(str); //unsafe
4,454✔
58
    if let Ok(s) = ft_name.to_str() {
4,454✔
59
        if let Some(ctx) = detect_parse_array_uint_enum::<u8, parser::HTTP2FrameType>(s) {
4,454✔
60
            let boxed = Box::new(ctx);
3,557✔
61
            // DetectUintArrayData<u8> cannot be cbindgend
3,557✔
62
            return Box::into_raw(boxed) as *mut c_void;
3,557✔
63
        }
897✔
64
    }
×
65
    return std::ptr::null_mut();
897✔
66
}
4,454✔
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(
60✔
97
    str: *const std::os::raw::c_char,
60✔
98
) -> *mut std::os::raw::c_void {
60✔
99
    let ft_name: &CStr = CStr::from_ptr(str); //unsafe
60✔
100
    if let Ok(s) = ft_name.to_str() {
60✔
101
        // special case for backward compatibility, now parsed as HTTP11_REQUIRED
102
        if s.to_uppercase() == "HTTP_1_1_REQUIRED" {
60✔
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
        }
59✔
116
        if let Some(ctx) = detect_parse_array_uint_enum::<u32, parser::HTTP2ErrorCode>(s) {
59✔
117
            let boxed = Box::new(ctx);
18✔
118
            return Box::into_raw(boxed) as *mut c_void;
18✔
119
        }
41✔
120
    }
×
121
    return std::ptr::null_mut();
41✔
122
}
60✔
123

124
fn get_http2_priority(frame: &HTTP2Frame) -> Option<u8> {
13✔
125
    return match &frame.data {
13✔
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,
9✔
134
    };
135
}
13✔
136

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

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

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

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

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

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

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

206
fn http2_detect_settings_match(
113✔
207
    set: &[parser::HTTP2FrameSettings], ctx: &parser::DetectHTTP2settingsSigCtx,
113✔
208
) -> std::os::raw::c_int {
113✔
209
    for e in set {
361✔
210
        if e.id == ctx.id {
272✔
211
            match &ctx.value {
34✔
212
                None => {
213
                    return 1;
11✔
214
                }
215
                Some(x) => {
23✔
216
                    if detect_match_uint(x, e.value) {
23✔
217
                        return 1;
13✔
218
                    }
10✔
219
                }
220
            }
221
        }
238✔
222
    }
223
    return 0;
89✔
224
}
113✔
225

226
fn http2_detect_settingsctx_match(
8,122✔
227
    ctx: &parser::DetectHTTP2settingsSigCtx, tx: &HTTP2Transaction, direction: Direction,
8,122✔
228
) -> std::os::raw::c_int {
8,122✔
229
    if direction == Direction::ToServer {
8,122✔
230
        for i in 0..tx.frames_ts.len() {
4,496✔
231
            if let HTTP2FrameTypeData::SETTINGS(set) = &tx.frames_ts[i].data {
491✔
232
                if http2_detect_settings_match(set, ctx) != 0 {
71✔
233
                    return 1;
23✔
234
                }
48✔
235
            }
420✔
236
        }
237
    } else {
238
        for i in 0..tx.frames_tc.len() {
3,626✔
239
            if let HTTP2FrameTypeData::SETTINGS(set) = &tx.frames_tc[i].data {
3,312✔
240
                if http2_detect_settings_match(set, ctx) != 0 {
42✔
241
                    return 1;
1✔
242
                }
41✔
243
            }
3,270✔
244
        }
245
    }
246
    return 0;
8,098✔
247
}
8,122✔
248

249
#[no_mangle]
250
pub unsafe extern "C" fn SCHttp2DetectSettingsCtxMatch(
8,122✔
251
    ctx: *const std::os::raw::c_void, tx: *mut std::os::raw::c_void, direction: u8,
8,122✔
252
) -> std::os::raw::c_int {
8,122✔
253
    let ctx = cast_pointer!(ctx, parser::DetectHTTP2settingsSigCtx);
8,122✔
254
    let tx = cast_pointer!(tx, HTTP2Transaction);
8,122✔
255
    return http2_detect_settingsctx_match(ctx, tx, direction.into());
8,122✔
256
}
8,122✔
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]> {
15,930✔
272
    match &frame.data {
15,930✔
273
        HTTP2FrameTypeData::HEADERS(hd) => {
8,584✔
274
            return Some(&hd.blocks);
8,584✔
275
        }
276
        HTTP2FrameTypeData::CONTINUATION(hd) => {
4✔
277
            return Some(&hd.blocks);
4✔
278
        }
279
        HTTP2FrameTypeData::PUSHPROMISE(hd) => {
×
280
            return Some(&hd.blocks);
×
281
        }
282
        _ => {}
7,342✔
283
    }
7,342✔
284
    return None;
7,342✔
285
}
15,930✔
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>(
3,460✔
362
    tx: &'a mut HTTP2Transaction, direction: Direction, name: &str,
3,460✔
363
) -> Result<&'a [u8], ()> {
3,460✔
364
    let frames = if direction == Direction::ToServer {
3,460✔
365
        &tx.frames_ts
3,018✔
366
    } else {
367
        &tx.frames_tc
442✔
368
    };
369
    for frame in frames {
4,613✔
370
        if let Some(blocks) = http2_header_blocks(frame) {
2,443✔
371
            for block in blocks.iter() {
2,513✔
372
                if block.name.as_ref() == name.as_bytes() {
2,513✔
373
                    return Ok(&block.value);
1,290✔
374
                }
1,223✔
375
            }
376
        }
1,036✔
377
    }
378
    return Err(());
2,170✔
379
}
3,460✔
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(
2,373✔
384
    tx: &HTTP2Transaction, direction: Direction, name: &str,
2,373✔
385
) -> Result<Vec<u8>, ()> {
2,373✔
386
    let mut found = 0;
2,373✔
387
    let mut vec = Vec::new();
2,373✔
388
    let frames = if direction == Direction::ToServer {
2,373✔
389
        &tx.frames_ts
122✔
390
    } else {
391
        &tx.frames_tc
2,251✔
392
    };
393
    for frame in frames {
7,769✔
394
        if let Some(blocks) = http2_header_blocks(frame) {
5,396✔
395
            for block in blocks.iter() {
22,483✔
396
                if block.name.as_ref() == name.as_bytes() {
22,483✔
397
                    if found == 0 {
185✔
398
                        vec.extend_from_slice(&block.value);
185✔
399
                        found = 1;
185✔
400
                    } else if found == 1 && Rc::strong_count(&block.name) <= 2 {
185✔
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
                }
22,298✔
409
            }
410
        }
2,362✔
411
    }
412
    if found == 0 {
2,373✔
413
        return Err(());
2,188✔
414
    } else {
415
        return Ok(vec);
185✔
416
    }
417
}
2,373✔
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>(
5,072✔
426
    tx: &'a HTTP2Transaction, direction: Direction, name: &str,
5,072✔
427
) -> Option<Http2Header<'a>> {
5,072✔
428
    let mut found = 0;
5,072✔
429
    let mut vec = Vec::new();
5,072✔
430
    let mut single = None;
5,072✔
431
    let frames = if direction == Direction::ToServer {
5,072✔
432
        &tx.frames_ts
3,298✔
433
    } else {
434
        &tx.frames_tc
1,774✔
435
    };
436
    for frame in frames {
9,477✔
437
        if let Some(blocks) = http2_header_blocks(frame) {
4,405✔
438
            for block in blocks.iter() {
13,536✔
439
                if block.name.as_ref() == name.as_bytes() {
13,536✔
440
                    if found == 0 {
748✔
441
                        single = Some(Http2Header::Single(&block.value));
632✔
442
                        found = 1;
632✔
443
                    } else if found == 1 && Rc::strong_count(&block.name) <= 2 {
632✔
444
                        if let Some(Http2Header::Single(s)) = single {
53✔
445
                            vec.extend_from_slice(s);
53✔
446
                        }
53✔
447
                        vec.extend_from_slice(b", ");
53✔
448
                        vec.extend_from_slice(&block.value);
53✔
449
                        found = 2;
53✔
450
                    } else if Rc::strong_count(&block.name) <= 2 {
63✔
451
                        vec.extend_from_slice(b", ");
3✔
452
                        vec.extend_from_slice(&block.value);
3✔
453
                    }
60✔
454
                }
12,788✔
455
            }
456
        }
2,789✔
457
    }
458
    if found == 0 {
5,072✔
459
        return None;
4,440✔
460
    } else if found == 1 {
632✔
461
        return single;
579✔
462
    } else {
463
        return Some(Http2Header::Multiple(vec));
53✔
464
    }
465
}
5,072✔
466

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

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

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

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

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

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

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

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

557
#[no_mangle]
558
pub unsafe extern "C" fn SCHttp2TxGetHost(
362✔
559
    tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void,
362✔
560
) -> u8 {
362✔
561
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
362✔
562
    if let Some(value) = http2_frames_get_header_value(tx, Direction::ToServer, ":authority") {
362✔
563
        match value {
160✔
564
            Http2Header::Single(v) => {
159✔
565
                *buffer = v.as_ptr(); //unsafe
159✔
566
                *buffer_len = v.len() as u32;
159✔
567
            }
159✔
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;
160✔
575
    }
202✔
576
    return 0;
202✔
577
}
362✔
578

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

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

651
#[no_mangle]
652
pub unsafe extern "C" fn SCHttp2TxGetUserAgent(
316✔
653
    tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void,
316✔
654
) -> u8 {
316✔
655
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
316✔
656
    if let Some(value) = http2_frames_get_header_value(tx, Direction::ToServer, "user-agent") {
316✔
657
        match value {
57✔
658
            Http2Header::Single(v) => {
57✔
659
                *buffer = v.as_ptr(); //unsafe
57✔
660
                *buffer_len = v.len() as u32;
57✔
661
            }
57✔
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;
57✔
669
    }
259✔
670
    return 0;
259✔
671
}
316✔
672

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

685
#[no_mangle]
686
pub unsafe extern "C" fn SCHttp2TxGetCookie(
1,208✔
687
    tx: &mut HTTP2Transaction, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32,
1,208✔
688
    tbuf: *mut c_void,
1,208✔
689
) -> u8 {
1,208✔
690
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
1,208✔
691
    if direction == u8::from(Direction::ToServer) {
1,208✔
692
        if let Some(value) = http2_frames_get_header_value(tx, Direction::ToServer, "cookie") {
706✔
693
            match value {
109✔
694
                Http2Header::Single(v) => {
58✔
695
                    *buffer = v.as_ptr(); //unsafe
58✔
696
                    *buffer_len = v.len() as u32;
58✔
697
                }
58✔
698
                Http2Header::Multiple(v) => {
51✔
699
                    tbuf.data = v;
51✔
700
                    *buffer = tbuf.data.as_ptr(); //unsafe
51✔
701
                    *buffer_len = tbuf.data.len() as u32;
51✔
702
                }
51✔
703
            }
704
            return 1;
109✔
705
        }
597✔
706
    } else if let Some(value) = http2_frames_get_header_value(tx, Direction::ToClient, "set-cookie")
502✔
707
    {
UNCOV
708
        match value {
×
UNCOV
709
            Http2Header::Single(v) => {
×
UNCOV
710
                *buffer = v.as_ptr(); //unsafe
×
UNCOV
711
                *buffer_len = v.len() as u32;
×
UNCOV
712
            }
×
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
        }
UNCOV
719
        return 1;
×
720
    }
502✔
721
    return 0;
1,099✔
722
}
1,208✔
723

724
#[no_mangle]
725
pub unsafe extern "C" fn SCHttp2TxGetHeaderValue(
2,241✔
726
    tx: &mut HTTP2Transaction, direction: u8, strname: *const std::os::raw::c_char,
2,241✔
727
    buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void,
2,241✔
728
) -> u8 {
2,241✔
729
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
2,241✔
730
    let hname: &CStr = CStr::from_ptr(strname); //unsafe
2,241✔
731
    if let Ok(s) = hname.to_str() {
2,241✔
732
        if let Some(value) = http2_frames_get_header_value(tx, direction.into(), &s.to_lowercase())
2,241✔
733
        {
734
            match value {
117✔
735
                Http2Header::Single(v) => {
117✔
736
                    *buffer = v.as_ptr(); //unsafe
117✔
737
                    *buffer_len = v.len() as u32;
117✔
738
                }
117✔
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;
117✔
746
        }
2,124✔
747
    }
×
748
    return 0;
2,124✔
749
}
2,241✔
750

751
fn http2_escape_header(blocks: &[parser::HTTP2FrameHeaderBlock], i: u32) -> Vec<u8> {
1,209✔
752
    //minimum size + 2 for escapes
1,209✔
753
    let normalsize = blocks[i as usize].value.len() + 2 + blocks[i as usize].name.len();
1,209✔
754
    let mut vec = Vec::with_capacity(normalsize);
1,209✔
755
    vec.extend_from_slice(&blocks[i as usize].name);
1,209✔
756
    vec.extend_from_slice(b": ");
1,209✔
757
    vec.extend_from_slice(&blocks[i as usize].value);
1,209✔
758
    return vec;
1,209✔
759
}
1,209✔
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 {
567,008✔
768
    let boxed = Box::new(Http2ThreadBuf::default());
567,008✔
769
    return Box::into_raw(boxed) as *mut c_void;
567,008✔
770
}
567,008✔
771

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

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

808
fn http2_header_iscookie(direction: Direction, hname: &[u8]) -> bool {
3,481✔
809
    if let Ok(s) = std::str::from_utf8(hname) {
3,481✔
810
        if direction == Direction::ToServer {
3,479✔
811
            if s.to_lowercase() == "cookie" {
2,469✔
812
                return true;
9✔
813
            }
2,460✔
814
        } else if s.to_lowercase() == "set-cookie" {
1,010✔
UNCOV
815
            return true;
×
816
        }
1,010✔
817
    }
2✔
818
    return false;
3,472✔
819
}
3,481✔
820

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

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

875
#[no_mangle]
876
pub unsafe extern "C" fn SCHttp2TxGetHeadersRaw(
136✔
877
    tx: &mut HTTP2Transaction, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32,
136✔
878
    tbuf: *mut c_void,
136✔
879
) -> u8 {
136✔
880
    let tbuf = cast_pointer!(tbuf, Http2ThreadBuf);
136✔
881
    tbuf.data.clear();
136✔
882
    let frames = if direction & Direction::ToServer as u8 != 0 {
136✔
883
        &tx.frames_ts
136✔
884
    } else {
UNCOV
885
        &tx.frames_tc
×
886
    };
887
    for frame in frames {
311✔
888
        if let Some(blocks) = http2_header_blocks(frame) {
175✔
889
            for block in blocks.iter() {
1,138✔
890
                // we do not escape linefeeds nor : in headers names
1,138✔
891
                tbuf.data.extend_from_slice(&block.name);
1,138✔
892
                tbuf.data.extend_from_slice(b": ");
1,138✔
893
                tbuf.data.extend_from_slice(&block.value);
1,138✔
894
                tbuf.data.extend_from_slice(b"\r\n");
1,138✔
895
            }
1,138✔
896
        }
77✔
897
    }
898
    if !tbuf.data.is_empty() {
136✔
899
        *buffer = tbuf.data.as_ptr(); //unsafe
48✔
900
        *buffer_len = tbuf.data.len() as u32;
48✔
901
        return 1;
48✔
902
    }
88✔
903
    return 0;
88✔
904
}
136✔
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 {
70,876✔
913
    let boxed = Box::new(Http2ThreadMultiBuf::default());
70,876✔
914
    return Box::into_raw(boxed) as *mut c_void;
70,876✔
915
}
70,876✔
916

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

922
#[no_mangle]
923
pub unsafe extern "C" fn SCHttp2TxGetHeader(
1,450✔
924
    tbuf: *mut c_void, tx: *const c_void, direction: u8, nb: u32, buffer: *mut *const u8,
1,450✔
925
    buffer_len: *mut u32,
1,450✔
926
) -> bool {
1,450✔
927
    let tbuf = cast_pointer!(tbuf, Http2ThreadMultiBuf);
1,450✔
928
    let tx = cast_pointer!(tx, HTTP2Transaction);
1,450✔
929
    let mut pos = 0_u32;
1,450✔
930
    if nb == 0 {
1,450✔
931
        tbuf.data.clear();
241✔
932
    }
1,209✔
933
    match direction.into() {
1,450✔
934
        Direction::ToServer => {
935
            for i in 0..tx.frames_ts.len() {
1,583✔
936
                if let Some(blocks) = http2_header_blocks(&tx.frames_ts[i]) {
1,583✔
937
                    if nb < pos + blocks.len() as u32 {
1,428✔
938
                        let ehdr = http2_escape_header(blocks, nb - pos);
1,209✔
939
                        tbuf.data.push(ehdr);
1,209✔
940
                        let idx = tbuf.data.len() - 1;
1,209✔
941
                        let value = &tbuf.data[idx];
1,209✔
942
                        *buffer = value.as_ptr(); //unsafe
1,209✔
943
                        *buffer_len = value.len() as u32;
1,209✔
944
                        return true;
1,209✔
945
                    } else {
219✔
946
                        pos += blocks.len() as u32;
219✔
947
                    }
219✔
948
                }
155✔
949
            }
950
        }
951
        Direction::ToClient => {
952
            for i in 0..tx.frames_tc.len() {
4✔
953
                if let Some(blocks) = http2_header_blocks(&tx.frames_tc[i]) {
2✔
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
                    }
×
965
                }
2✔
966
            }
967
        }
968
    }
969
    return false;
241✔
970
}
1,450✔
971

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

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

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

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

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

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

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

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

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

1083
    use super::*;
1084

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

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

1117
    #[test]
1118
    fn test_http2_frames_get_header_value() {
1119
        let mut tx = HTTP2Transaction::new();
1120
        let head = parser::HTTP2FrameHeader {
1121
            length: 0,
1122
            ftype: parser::HTTP2FrameType::Headers as u8,
1123
            flags: 0,
1124
            reserved: 0,
1125
            stream_id: 1,
1126
        };
1127
        let mut blocks = Vec::new();
1128
        let b = parser::HTTP2FrameHeaderBlock {
1129
            name: "Host".as_bytes().to_vec().into(),
1130
            value: "abc.com".as_bytes().to_vec().into(),
1131
            error: parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeSuccess,
1132
            sizeupdate: 0,
1133
        };
1134
        blocks.push(b);
1135
        let b2 = parser::HTTP2FrameHeaderBlock {
1136
            name: "Host".as_bytes().to_vec().into(),
1137
            value: "efg.net".as_bytes().to_vec().into(),
1138
            error: parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeSuccess,
1139
            sizeupdate: 0,
1140
        };
1141
        blocks.push(b2);
1142
        let hs = parser::HTTP2FrameHeaders {
1143
            padlength: None,
1144
            priority: None,
1145
            blocks,
1146
        };
1147
        let txdata = HTTP2FrameTypeData::HEADERS(hs);
1148
        tx.frames_ts.push(HTTP2Frame {
1149
            header: head,
1150
            data: txdata,
1151
        });
1152
        match http2_frames_get_header_value(&tx, Direction::ToServer, "Host") {
1153
            Some(Http2Header::Multiple(x)) => {
1154
                assert_eq!(x, "abc.com, efg.net".as_bytes());
1155
            }
1156
            _ => {
1157
                panic!("Result should have been a multiple header value");
1158
            }
1159
        }
1160
    }
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