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

libbitcoin / libbitcoin-network / 45096769114

23 Nov 2026 01:49AM UTC coverage: 94.187% (-0.03%) from 94.216%
45096769114

push

github

web-flow
Merge pull request #1058 from evoskuil/master

http compliance fixes.

39 of 54 new or added lines in 5 files covered. (72.22%)

165 existing lines in 20 files now uncovered.

16204 of 17204 relevant lines covered (94.19%)

2171.08 hits per line

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

89.54
/src/messages/http/parser.cpp
1
/**
2
 * Copyright (c) 2011-2026 libbitcoin developers
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <bitcoin/network/messages/http/parser.hpp>
20

21
#include <algorithm>
22
#include <charconv>
23
#include <bitcoin/network/define.hpp>
24
#include <bitcoin/network/messages/http/fields.hpp>
25
#include <bitcoin/network/messages/http/header.hpp>
26

27
namespace libbitcoin {
28
namespace network {
29
namespace http {
30

31
using namespace system;
32
using namespace network::error;
33

34
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
35

36
// Each parse function consumes from front of its text, returning number of
37
// characters consumed, or zero with need_more if text ends before element does.
38
// Validation proceeds character by character, so invalid element is reported
39
// once its first invalid character is present.
40

41
// Character classes (rfc9110).
42
// ----------------------------------------------------------------------------
43

44
constexpr bool is_digit(char character) NOEXCEPT
8,648 ✔
45
{
46
    return character >= '0' && character <= '9';
8,648 ✔
47
}
48

49
// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
50
//         "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
51
constexpr bool is_tchar(char character) NOEXCEPT
7,882 ✔
52
{
53
    constexpr std::string_view others{ "!#$%&'*+-.^_`|~" };
7,882 ✔
54
    return is_digit(character) ||
15,619 ✔
55
        (character >= 'a' && character <= 'z') ||
7,737 ✔
56
        (character >= 'A' && character <= 'Z') ||
10,493 ✔
57
        others.find(character) != std::string_view::npos;
512 ✔
58
}
59

60
// Visible characters and obs-text.
61
constexpr bool is_target_char(char character) NOEXCEPT
309 ✔
62
{
63
    const auto byte = static_cast<uint8_t>(character);
309 ✔
64
    return byte > 0x20 && byte != 0x7f;
309 ✔
65
}
66

67
// Control characters other than horizontal tab.
68
constexpr bool is_control(char character) NOEXCEPT
47,929 ✔
69
{
70
    const auto byte = static_cast<uint8_t>(character);
47,929 ✔
71
    return (byte < 0x20 && byte != '\t') || byte == 0x7f;
47,929 ✔
72
}
73

74
constexpr bool is_whitespace(char character) NOEXCEPT
2,207 ✔
75
{
76
    return character == ' ' || character == '\t';
2,207 ✔
77
}
78

79
constexpr int8_t to_nibble(char character) NOEXCEPT
127 ✔
80
{
81
    if (is_digit(character))
127 ✔
82
        return character - '0';
114 ✔
83

84
    if (character >= 'a' && character <= 'f')
13 ✔
85
        return 10 + (character - 'a');
10 ✔
86

87
    if (character >= 'A' && character <= 'F')
3 ✔
88
        return 10 + (character - 'A');
×
89

90
    return -1;
91
}
92

93
static void set_need_more(code& ec) NOEXCEPT
154 ✔
94
{
95
    ec = to_http_code(http_error_t::need_more);
154 ✔
96
}
154 ✔
97

98
// Find line end (CRLF) of text without control characters, setting text to
99
// index of CR and end to index following LF. False with ec clear on control
100
// character, false with ec set if line incomplete or CR does not precede LF.
101
static bool find_line(size_t& text, size_t& end, const std::string_view& in,
568 ✔
102
    size_t start, code& ec) NOEXCEPT
103
{
104
    for (auto index = start; index < in.size(); ++index)
47,953 ✔
105
    {
106
        const auto character = in[index];
47,929 ✔
107
        if (!is_control(character))
47,929 ✔
108
            continue;
109

110
        if (character != '\r')
544 ✔
111
        {
112
            ec.clear();
1 ✔
113
            return false;
1 ✔
114
        }
115

116
        if (add1(index) == in.size())
543 ✔
117
        {
118
            set_need_more(ec);
3 ✔
119
            return false;
3 ✔
120
        }
121

122
        if (in[add1(index)] != '\n')
540 ✔
123
        {
124
            ec = to_http_code(http_error_t::bad_line_ending);
1 ✔
125
            return false;
1 ✔
126
        }
127

128
        text = index;
539 ✔
129
        end = index + two;
539 ✔
130
        ec.clear();
539 ✔
131
        return true;
539 ✔
132
    }
133

134
    set_need_more(ec);
24 ✔
135
    return false;
24 ✔
136
}
137

138
static size_t trim_back(const std::string_view& in, size_t first,
465 ✔
139
    size_t last) NOEXCEPT
140
{
141
    while (last > first && is_whitespace(in[sub1(last)]))
466 ✔
142
        --last;
1 ✔
143

144
    return last;
465 ✔
145
}
146

147
// HTTP-version = "HTTP/" DIGIT "." DIGIT, limited to 1.0 and 1.1.
148
static bool parse_version(uint32_t& version, const std::string_view& in,
200 ✔
149
    size_t& index, code& ec) NOEXCEPT
150
{
151
    constexpr auto size = 8u;
152
    if (in.size() - index < size)
200 ✔
153
    {
154
        set_need_more(ec);
16 ✔
155
        return false;
16 ✔
156
    }
157

158
    const auto text = in.substr(index, size);
184 ✔
159
    if (!text.starts_with("HTTP/") || !is_digit(text[5]) || text[6] != '.' ||
184 ✔
160
        !is_digit(text[7]))
184 ✔
161
    {
UNCOV
162
        ec = to_http_code(http_error_t::bad_version);
×
163
        return false;
×
164
    }
165

166
    version = (text[5] - '0') * 10u + (text[7] - '0');
184 ✔
167
    if (version < version_1_0 || version > version_1_1)
184 ✔
168
    {
169
        ec = to_http_code(http_error_t::bad_version);
1 ✔
170
        return false;
1 ✔
171
    }
172

173
    index += size;
183 ✔
174
    return true;
183 ✔
175
}
176

177
// Start lines.
178
// ----------------------------------------------------------------------------
179

180
// request-line = method SP request-target SP HTTP-version CRLF
181
size_t parse_start_line(request_header& out, const std::string_view& in,
142 ✔
182
    code& ec) NOEXCEPT
183
{
184
    size_t index{};
142 ✔
185
    while (index < in.size() && is_tchar(in[index]))
630 ✔
186
        ++index;
488 ✔
187

188
    if (index == in.size())
142 ✔
189
    {
190
        set_need_more(ec);
8 ✔
191
        return {};
8 ✔
192
    }
193

194
    if (in[index] != ' ' || is_zero(index))
134 ✔
195
    {
196
        ec = to_http_code(http_error_t::bad_method);
1 ✔
197
        return {};
1 ✔
198
    }
199

200
    const auto method = in.substr(zero, index++);
133 ✔
201
    const auto first = index;
133 ✔
202
    while (index < in.size() && is_target_char(in[index]))
313 ✔
203
        ++index;
180 ✔
204

205
    if (index == in.size())
133 ✔
206
    {
207
        set_need_more(ec);
4 ✔
208
        return {};
4 ✔
209
    }
210

211
    if (in[index] != ' ' || index == first)
129 ✔
212
    {
213
        ec = to_http_code(http_error_t::bad_target);
1 ✔
214
        return {};
1 ✔
215
    }
216

217
    const auto target = in.substr(first, index++ - first);
128 ✔
218
    uint32_t version{};
128 ✔
219
    if (!parse_version(version, in, index, ec))
128 ✔
220
        return {};
221

222
    if (in.size() - index < two)
111 ✔
223
    {
224
        set_need_more(ec);
4 ✔
225
        return {};
4 ✔
226
    }
227

228
    if (in[index] != '\r' || in[add1(index)] != '\n')
107 ✔
229
    {
UNCOV
230
        ec = to_http_code(http_error_t::bad_version);
×
231
        return {};
×
232
    }
233

234
    out.method_string(method);
107 ✔
235
    out.target(target);
107 ✔
236
    out.version(version);
107 ✔
237
    ec.clear();
107 ✔
238
    return index + two;
107 ✔
239
}
240

241
// status-line = HTTP-version SP status-code SP reason-phrase CRLF
242
size_t parse_start_line(response_header& out, const std::string_view& in,
72 ✔
243
    code& ec) NOEXCEPT
244
{
245
    size_t index{};
72 ✔
246
    uint32_t version{};
72 ✔
247
    if (!parse_version(version, in, index, ec))
72 ✔
248
        return {};
249

250
    if (index == in.size())
72 ✔
251
    {
UNCOV
252
        set_need_more(ec);
×
253
        return {};
×
254
    }
255

256
    if (in[index++] != ' ')
72 ✔
257
    {
UNCOV
258
        ec = to_http_code(http_error_t::bad_version);
×
259
        return {};
×
260
    }
261

262
    constexpr auto status_size = 4u;
263
    if (in.size() - index < status_size)
72 ✔
264
    {
UNCOV
265
        set_need_more(ec);
×
266
        return {};
×
267
    }
268

269
    if (!is_digit(in[index]) || !is_digit(in[add1(index)]) ||
144 ✔
270
        !is_digit(in[index + two]) || in[index + 3u] != ' ')
144 ✔
271
    {
272
        ec = to_http_code(http_error_t::bad_status);
1 ✔
273
        return {};
1 ✔
274
    }
275

276
    const auto code = (in[index] - '0') * 100u + (in[add1(index)] - '0') *
71 ✔
277
        10u + (in[index + two] - '0');
71 ✔
278

279
    index += status_size;
71 ✔
280
    size_t text{};
71 ✔
281
    size_t end{};
71 ✔
282
    if (!find_line(text, end, in, index, ec))
71 ✔
283
    {
UNCOV
284
        if (!ec)
×
285
            ec = to_http_code(http_error_t::bad_reason);
×
286

UNCOV
287
        return {};
×
288
    }
289

290
    out.version(version);
71 ✔
291
    out.result(code);
71 ✔
292
    out.reason(in.substr(index, text - index));
71 ✔
293
    ec.clear();
71 ✔
294
    return end;
71 ✔
295
}
296

297
// Fields.
298
// ----------------------------------------------------------------------------
299

300
// #token = [ ( "," / token ) *( OWS "," [ OWS token ] ) ]
301
// Leading tokens of list, up to first invalid element (valid false if any).
302
static string_list token_list(const std::string_view& value,
168 ✔
303
    bool& valid) NOEXCEPT
304
{
305
    string_list out{};
168 ✔
306
    size_t index{};
307
    bool need_comma{};
308
    valid = true;
168 ✔
309
    while (true)
310
    {
311
        while (index < value.size() && is_whitespace(value[index]))
349 ✔
312
            ++index;
5 ✔
313

314
        if (index == value.size())
344 ✔
315
            return out;
316

317
        const auto character = value[index];
177 ✔
318
        if (is_tchar(character))
177 ✔
319
        {
320
            if (need_comma)
173 ✔
321
            {
322
                valid = false;
1 ✔
323
                return out;
1 ✔
324
            }
325

326
            const auto first = index;
327
            while (index < value.size() && is_tchar(value[index]))
1,011 ✔
328
                ++index;
839 ✔
329

330
            out.emplace_back(value.substr(first, index - first));
172 ✔
331
            need_comma = true;
332
            continue;
172 ✔
333
        }
172 ✔
334

335
        if (character != ',')
4 ✔
336
        {
UNCOV
337
            valid = false;
×
UNCOV
338
            return out;
×
339
        }
340

341
        need_comma = false;
342
        ++index;
4 ✔
343
    }
UNCOV
344
}
×
345

346
// Connection must be valid token list.
347
// Content-Length may be list of equal values, and conflicts with chunked.
348
// Transfer-Encoding conflicts with Content-Length and may not follow chunked.
349
// Chunked framing requires first chunked coding be last coding.
350
static void frame_field(framing& frame, field name,
464 ✔
351
    const std::string_view& value, code& ec) NOEXCEPT
352
{
353
    ec.clear();
464 ✔
354
    if (name == field::connection)
464 ✔
355
    {
356
        bool valid{};
66 ✔
357
        token_list(value, valid);
66 ✔
358
        if (!valid)
66 ✔
359
            ec = to_http_code(http_error_t::bad_value);
1 ✔
360

361
        return;
362
    }
363

364
    if (name == field::content_length)
398 ✔
365
    {
366
        bool valid{};
57 ✔
367
        token_list(value, valid);
57 ✔
368
        if (frame.chunked || frame.encoded || !valid)
57 ✔
369
        {
370
            ec = to_http_code(http_error_t::bad_content_length);
2 ✔
371
            return;
2 ✔
372
        }
373

374
        const auto items = split(value, { "," }, http_whitespace);
55 ✔
375
        for (const auto& item: items)
107 ✔
376
        {
377
            uint64_t number{};
55 ✔
378
            const auto last = std::next(item.data(), item.size());
55 ✔
379
            const auto [next, error] = std::from_chars(item.data(), last,
55 ✔
380
                number);
381

382
            if (item.empty() || !is_digit(item.front()) ||
55 ✔
383
                error != std::errc{} || next != last)
109 ✔
384
            {
385
                ec = to_http_code(http_error_t::bad_content_length);
1 ✔
386
                return;
3 ✔
387
            }
388

389
            if (frame.length.has_value() && number != frame.length.value())
54 ✔
390
            {
391
                ec = to_http_code(http_error_t::multiple_content_length);
2 ✔
392
                return;
393
            }
394

395
            frame.length = number;
52 ✔
396
        }
397

398
        return;
52 ✔
399
    }
55 ✔
400

401
    if (name == field::transfer_encoding)
341 ✔
402
    {
403
        if (frame.chunked || frame.encoded || frame.length.has_value())
47 ✔
404
        {
405
            ec = to_http_code(http_error_t::bad_transfer_encoding);
2 ✔
406
            return;
2 ✔
407
        }
408

409
        bool valid{};
45 ✔
410
        const auto codings = token_list(value, valid);
45 ✔
411
        if (!valid || codings.empty())
45 ✔
412
        {
UNCOV
413
            ec = to_http_code(http_error_t::bad_transfer_encoding);
×
414
            return;
415
        }
416

417
        const auto it = std::ranges::find_if(codings,
45 ✔
418
            [](const auto& coding) NOEXCEPT
48 ✔
419
            {
420
                return iequals(coding, "chunked");
48 ✔
421
            });
422

423
        frame.chunked = it != codings.end() && std::next(it) == codings.end();
86 ✔
424
        frame.encoded = !frame.chunked;
45 ✔
425
    }
45 ✔
426
}
427

428
// header-field = field-name ":" OWS field-value OWS, where folded lines
429
// (obs-fold) are joined by a space.
430
size_t parse_field(fields* out, framing& frame, bool& end,
752 ✔
431
    const std::string_view& in, code& ec) NOEXCEPT
432
{
433
    end = false;
752 ✔
434
    if (in.size() < two)
752 ✔
435
    {
436
        set_need_more(ec);
11 ✔
437
        return {};
11 ✔
438
    }
439

440
    if (in.front() == '\r')
741 ✔
441
    {
442
        if (in[one] != '\n')
198 ✔
443
        {
UNCOV
444
            ec = to_http_code(http_error_t::bad_line_ending);
×
UNCOV
445
            return {};
×
446
        }
447

448
        end = true;
198 ✔
449
        ec.clear();
198 ✔
450
        return two;
198 ✔
451
    }
452

453
    size_t index{};
454
    while (true)
455
    {
456
        if (index == in.size())
6,778 ✔
457
        {
458
            set_need_more(ec);
40 ✔
459
            return {};
40 ✔
460
        }
461

462
        if (in[index] == ':')
6,738 ✔
463
            break;
464

465
        if (!is_tchar(in[index]))
6,236 ✔
466
        {
467
            ec = to_http_code(http_error_t::bad_field);
1 ✔
468
            return {};
1 ✔
469
        }
470

471
        ++index;
6,235 ✔
472
    }
473

474
    if (is_zero(index))
502 ✔
475
    {
UNCOV
476
        ec = to_http_code(http_error_t::bad_field);
×
UNCOV
477
        return {};
×
478
    }
479

480
    const auto name = in.substr(zero, index++);
502 ✔
481
    std::string value{};
502 ✔
482
    while (true)
483
    {
484
        while (true)
485
        {
486
            if (index == in.size())
1,003 ✔
487
            {
488
                set_need_more(ec);
6 ✔
489
                return {};
38 ✔
490
            }
491

492
            if (!is_whitespace(in[index]))
997 ✔
493
                break;
494

495
            ++index;
500 ✔
496
        }
497

498
        size_t text{};
497 ✔
499
        size_t line{};
497 ✔
500
        if (!find_line(text, line, in, index, ec))
497 ✔
501
        {
502
            if (!ec)
29 ✔
503
                ec = to_http_code(http_error_t::bad_value);
1 ✔
504

505
            return {};
29 ✔
506
        }
507

508
        // The character following the line end determines folding.
509
        if (line == in.size())
468 ✔
510
        {
511
            set_need_more(ec);
3 ✔
512
            return {};
513
        }
514

515
        const auto last = trim_back(in, index, text);
465 ✔
516
        if (last != index)
465 ✔
517
        {
518
            if (!value.empty())
465 ✔
519
                value += ' ';
1 ✔
520

521
            value.append(in.substr(index, last - index));
465 ✔
522
        }
523

524
        index = line;
465 ✔
525
        if (!is_whitespace(in[index]))
465 ✔
526
            break;
527

528
        ++index;
1 ✔
529
    }
1 ✔
530

531
    // Proxy-Connection (not enumerated) is validated as Connection.
532
    const auto id = iequals(name, "Proxy-Connection") ? field::connection :
464 ✔
533
        to_field(name);
464 ✔
534

535
    frame_field(frame, id, value, ec);
464 ✔
536
    if (ec)
464 ✔
537
        return {};
538

539
    if (!is_null(out))
456 ✔
540
        out->insert(name, value);
455 ✔
541

542
    return index;
543
}
502 ✔
544

545
// Chunks.
546
// ----------------------------------------------------------------------------
547

548
size_t parse_chunk_size(uint64_t& size, const std::string_view& line,
90 ✔
549
    code& ec) NOEXCEPT
550
{
551
    size = zero;
90 ✔
552
    size_t index{};
553
    for (; index < line.size(); ++index)
214 ✔
554
    {
555
        const auto nibble = to_nibble(line[index]);
127 ✔
556
        if (is_negative(nibble))
127 ✔
557
            break;
558

559
        if (size > (max_uint64 >> 4))
124 ✔
560
        {
UNCOV
561
            ec = to_http_code(http_error_t::bad_chunk);
×
UNCOV
562
            return {};
×
563
        }
564

565
        size = (size << 4) | to_unsigned(nibble);
124 ✔
566
    }
567

568
    if (is_zero(index))
90 ✔
569
    {
570
        ec = to_http_code(http_error_t::bad_chunk);
1 ✔
571
        return {};
1 ✔
572
    }
573

574
    ec.clear();
89 ✔
575
    return index;
89 ✔
576
}
577

578
// chunk-ext = *( BWS ";" BWS chunk-ext-name [ BWS "=" BWS chunk-ext-val ] ),
579
// chunk-ext-val = token / quoted-string. Valid if they extend to the line end.
580
bool valid_chunk_extensions(const std::string_view& line,
89 ✔
581
    size_t index) NOEXCEPT
582
{
583
    // The line end (CR) follows the line.
584
    const auto at = [&](size_t position) NOEXCEPT
194 ✔
585
    {
586
        return position < line.size() ? line[position] : '\r';
194 ✔
587
    };
89 ✔
588

589
    const auto skip = [&]() NOEXCEPT
5 ✔
590
    {
591
        while (is_whitespace(at(index)))
6 ✔
592
            ++index;
1 ✔
593
    };
89 ✔
594

595
    while (true)
596
    {
597
        if (!is_whitespace(at(index)) && at(index) != ';')
90 ✔
598
            return index == line.size();
88 ✔
599

600
        skip();
2 ✔
601
        if (at(index) != ';')
2 ✔
602
            return false;
603

604
        while (true)
605
        {
606
            ++index;
1 ✔
607
            skip();
1 ✔
608
            if (!is_tchar(at(index)))
1 ✔
609
                return false;
610

611
            while (is_tchar(at(index)))
2 ✔
612
                ++index;
1 ✔
613

614
            const auto spaced = is_whitespace(at(index));
1 ✔
615
            skip();
1 ✔
616
            if (at(index) == ';')
1 ✔
617
                continue;
618

619
            if (at(index) != '=')
1 ✔
620
            {
UNCOV
621
                if (spaced)
×
622
                    return false;
623

624
                break;
625
            }
626

627
            ++index;
1 ✔
628
            skip();
1 ✔
629
            if (at(index) != '"')
1 ✔
630
            {
UNCOV
631
                if (!is_tchar(at(index)))
×
632
                    return false;
633

UNCOV
634
                while (is_tchar(at(index)))
×
UNCOV
635
                    ++index;
×
636
            }
637
            else
638
            {
639
                while (true)
640
                {
641
                    ++index;
2 ✔
642
                    if (index >= line.size())
2 ✔
643
                        return false;
644

645
                    if (line[index] == '"')
2 ✔
646
                        break;
647

648
                    if (line[index] == '\\')
1 ✔
UNCOV
649
                        ++index;
×
650
                }
651

652
                ++index;
1 ✔
653
            }
654

655
            break;
UNCOV
656
        }
×
657
    }
658
}
659

660
// Line end (CRLF) of chunk line, which may contain any characters but CR that
661
// does not precede LF.
662
size_t find_chunk_line(size_t& text, const std::string_view& in,
125 ✔
663
    code& ec) NOEXCEPT
664
{
665
    for (size_t index{}; index < in.size(); ++index)
303 ✔
666
    {
667
        if (in[index] != '\r')
274 ✔
668
            continue;
669

670
        if (add1(index) == in.size())
96 ✔
671
            break;
672

673
        if (in[add1(index)] != '\n')
90 ✔
674
        {
UNCOV
675
            ec = to_http_code(http_error_t::bad_line_ending);
×
UNCOV
676
            return {};
×
677
        }
678

679
        text = index;
90 ✔
680
        ec.clear();
90 ✔
681
        return index + two;
90 ✔
682
    }
683

684
    set_need_more(ec);
35 ✔
685
    return {};
35 ✔
686
}
687

688
BC_POP_WARNING()
689

690
} // namespace http
691
} // namespace network
692
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc