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

libbitcoin / libbitcoin-network / 46042104491

27 Nov 2026 05:28PM UTC coverage: 93.826% (-0.7%) from 94.551%
46042104491

push

github

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

Allow an opaque origin when no origins are configured.

2 of 2 new or added lines in 1 file covered. (100.0%)

196 existing lines in 13 files now uncovered.

17841 of 19015 relevant lines covered (93.83%)

1918.12 hits per line

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

93.03
/src/net/socket_http.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/net/socket.hpp>
20

21
#include <bitcoin/network/config/config.hpp>
22
#include <bitcoin/network/define.hpp>
23
#include <bitcoin/network/log/log.hpp>
24
#include <bitcoin/network/messages/messages.hpp>
25
#include <bitcoin/network/settings.hpp>
26

27
namespace libbitcoin {
28
namespace network {
29
    
30
using namespace system;
31
using namespace std::placeholders;
32

33
// Shared pointers required in handler parameters so closures control lifetime.
34
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
35
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
36
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
37

38
// DETECT (first read).
39
// ----------------------------------------------------------------------------
40

41
// local
42
constexpr bool is_json_open(char byte) NOEXCEPT
23 ✔
43
{
44
    // Json value opens with object or array, and neither is valid http method
45
    // token character (rfc9110 tchar).
46
    return byte == '{' || byte == '[';
23 ✔
47
}
48

49
// local
50
constexpr bool is_line_break(char byte) NOEXCEPT
39 ✔
51
{
52
    // A server tolerates empty lines preceding a request line (rfc9112).
53
    return byte == '\r' || byte == '\n';
39 ✔
54
}
55

56
// local
57
constexpr bool is_token_char(char byte) NOEXCEPT
13 ✔
58
{
59
    // A request line opens with its method token (rfc9110 tchar).
60
    return (byte >= '0' && byte <= '9') || (byte >= 'A' && byte <= 'Z') ||
14 ✔
61
        (byte >= 'a' && byte <= 'z') ||
14 ✔
62
        std::string_view{ "!#$%&'*+-.^_`|~" }.find(byte) !=
1 ✔
63
            std::string_view::npos;
13 ✔
64
}
65

66
// local
67
// False implies inconclusive (buffer holds only empty lines).
68
inline bool detect_tcp(bool& tcp, const http::flat_buffer& buffer,
74 ✔
69
    bool binary) NOEXCEPT
70
{
71
    const auto data = buffer.data();
74 ✔
72
    const auto chars = static_cast<const char*>(data.data());
74 ✔
73
    const std::string_view bytes{ chars, data.size() };
74 ✔
74

75
    for (const auto byte: bytes)
77 ✔
76
    {
77
        if (is_line_break(byte))
39 ✔
78
            continue;
79

80
        tcp = binary ? !is_token_char(byte) : is_json_open(byte);
36 ✔
81
        return true;
36 ✔
82
    }
83

84
    return false;
85
}
86

87
void socket::detect(http::flat_buffer& buffer, bool binary,
38 ✔
88
    count_handler&& handler) NOEXCEPT
89
{
90
    io::dispatch(strand_,
38 ✔
91
        std::bind(&socket::do_detect, shared_from_this(),
76 ✔
92
            std::ref(buffer), binary, std::move(handler)));
38 ✔
93
}
38 ✔
94

95
// private
96
void socket::do_detect(const ref<http::flat_buffer>& buffer, bool binary,
74 ✔
97
    const count_handler& handler) NOEXCEPT
98
{
99
    BC_ASSERT(stranded());
74 ✔
100

101
    bool tcp{};
74 ✔
102
    if (detect_tcp(tcp, buffer.get(), binary))
74 ✔
103
    {
104
        detected_.store(true);
36 ✔
105
        downgraded_.store(tcp);
36 ✔
106
        handler(error::success, zero);
36 ✔
107
        return;
36 ✔
108
    }
109

110
    // Tolerated empty lines only, so read more (bounded by the buffer).
111
    async_read(buffer.get(),
38 ✔
112
        std::bind(&socket::handle_detect,
76 ✔
113
            shared_from_this(), _1, _2, buffer, binary, handler));
76 ✔
114
}
115

116
// private
117
void socket::handle_detect(const code& ec, size_t size,
38 ✔
118
    const ref<http::flat_buffer>& buffer, bool binary,
119
    const count_handler& handler) NOEXCEPT
120
{
121
    BC_ASSERT(stranded());
38 ✔
122

123
    if (ec)
38 ✔
124
    {
125
        handler(ec, size);
2 ✔
126
        return;
2 ✔
127
    }
128

129
    // async_read prepares, the caller commits (as does handle_body_read).
130
    buffer.get().commit(size);
36 ✔
131
    do_detect(buffer, binary, handler);
36 ✔
132
}
133

134
// HTTP/WS (read).
135
// ----------------------------------------------------------------------------
136

137
void socket::http_read(http::flat_buffer& buffer,
43 ✔
138
    http::request& request, count_handler&& handler) NOEXCEPT
139
{
140
    io::dispatch(strand_,
43 ✔
141
        std::bind(&socket::do_http_read, shared_from_this(),
86 ✔
142
            std::ref(buffer), std::ref(request), std::move(handler)));
43 ✔
143
}
43 ✔
144

145
// private
146
void socket::do_http_read(ref<http::flat_buffer> buffer,
43 ✔
147
    const ref<http::request>& request,
148
    const count_handler& handler) NOEXCEPT
149
{
150
    BC_ASSERT(stranded());
43 ✔
151
    async_read_http(buffer.get(), request.get(), handler);
43 ✔
152
}
43 ✔
153

154
// private
155
void socket::handle_http_read(const code& ec, size_t size,
18 ✔
156
    const ref<http::request>& request, const http_parser_ptr& parser,
157
    const count_handler& handler) NOEXCEPT
158
{
159
    BC_ASSERT(stranded());
18 ✔
160

161
    if (!ec && http::is_upgrade(parser->get()))
18 ✔
162
    {
163
        request.get() = parser->release();
12 ✔
164
        handler(error::upgrade, size);
12 ✔
165
        return;
12 ✔
166
    }
167

168
    if (!ec)
6 ✔
169
    {
170
        request.get() = parser->release();
3 ✔
171
        if (request.get().method() == http::verb::unknown)
3 ✔
172
        {
173
            handler(error::bad_method, size);
1 ✔
174
            return;
1 ✔
175
        }
176
    }
177

178
    handler(ec, size);
5 ✔
179
}
180

181
// HTTP/WS (write).
182
// ----------------------------------------------------------------------------
183

184
void socket::http_write(http::response&& response,
51 ✔
185
    count_handler&& handler) NOEXCEPT
186
{
187
    const auto out = move_shared(std::move(response));
51 ✔
188
    io::dispatch(strand_,
51 ✔
189
        std::bind(&socket::do_http_write, shared_from_this(),
102 ✔
190
            out, std::move(handler)));
51 ✔
191
}
51 ✔
192

193
// private
194
void socket::do_http_write(const http::response_ptr& response,
51 ✔
195
    const count_handler& handler) NOEXCEPT
196
{
197
    BC_ASSERT(stranded());
51 ✔
198
    async_write_http(std::move(*response), handler);
51 ✔
199
}
51 ✔
200

201
// private
202
void socket::handle_http_write(const code& ec, size_t size,
478 ✔
203
    const count_handler& handler) NOEXCEPT
204
{
205
    BC_ASSERT(stranded());
478 ✔
206
    reclaim_buffer();
478 ✔
207
    handler(ec, size);
478 ✔
208
}
478 ✔
209

210
// Codec (parse).
211
// ----------------------------------------------------------------------------
212
// Buffered bytes parsed before reading more. Header parse returns
213
// once header is complete, some parse once it progresses, and full
214
// parse once message is complete. Body reader may pause any parse
215
// (need_buffer), which is returned to caller. Websocket reads each
216
// message whole, and other transports read bytes available.
217

218
// private
219
template <class Parser>
220
void socket::do_parse(const ref<http::flat_buffer>& buffer,
375 ✔
221
    const std::shared_ptr<Parser>& parser, parse_mode mode, size_t total,
222
    const count_handler& handler) NOEXCEPT
223
{
224
    BC_ASSERT(stranded());
375 ✔
225
    auto& in = buffer.get();
375 ✔
226

UNCOV
227
    while (true)
×
228
    {
229
        code ec{};
375 ✔
230
        const auto used = parser->put(in.data(), ec);
375 ✔
231
        in.consume(used);
375 ✔
232
        total = ceilinged_add(total, used);
375 ✔
233

234
        if (ec == error::http_error_t::need_more)
375 ✔
235
            break;
236

237
        if (ec)
216 ✔
238
        {
239
            const auto code = error::rpc_to_error_code(ec);
72 ✔
240
            if (code == error::unknown) logx("parse", ec);
72 ✔
241
            handler(code, total);
72 ✔
242
            return;
243
        }
244

245
        if (parser->is_done() || (mode == parse_mode::header &&
25 ✔
246
            parser->is_header_done()) || (mode == parse_mode::some &&
169 ✔
UNCOV
247
            !is_zero(used)))
×
248
        {
249
            handler(error::success, total);
144 ✔
250
            return;
144 ✔
251
        }
252

UNCOV
253
        if (is_zero(used))
×
254
            break;
255
    }
256

257
    if (websocket())
159 ✔
258
    {
259
        async_read(in,
54 ✔
260
            std::bind(&socket::handle_parse<Parser>,
54 ✔
261
                shared_from_this(), _1, _2, buffer, parser, mode, total,
262
                handler));
263
        return;
27 ✔
264
    }
265

266
    // Read into available capacity, growing by up to the header limit.
267
    const auto limit = floored_subtract(in.max_size(), in.size());
132 ✔
268
    const auto size = std::min(limit, std::max(in.capacity() - in.size(),
132 ✔
269
        header_));
132 ✔
270

271
    if (is_zero(size))
132 ✔
272
    {
UNCOV
273
        handler(error::buffer_overflow, total);
×
UNCOV
274
        return;
×
275
    }
276

277
    async_read_some(in.prepare(size),
396 ✔
278
        std::bind(&socket::handle_parse<Parser>,
264 ✔
279
            shared_from_this(), _1, _2, buffer, parser, mode, total,
280
            handler));
281
}
282

283
// private
284
template <class Parser>
285
void socket::handle_parse(const code& ec, size_t size,
159 ✔
286
    const ref<http::flat_buffer>& buffer,
287
    const std::shared_ptr<Parser>& parser, parse_mode mode, size_t total,
288
    const count_handler& handler) NOEXCEPT
289
{
290
    BC_ASSERT(stranded());
159 ✔
291

292
    // The end of the stream completes a body so delimited, or is premature.
293
    if (ec == error::peer_disconnect)
159 ✔
294
    {
295
        code eof{};
17 ✔
296
        parser->put_eof(eof);
17 ✔
297
        if (eof)
17 ✔
298
            handler(error::http_to_error_code(eof), total);
1 ✔
299
        else
300
            handler(parser->is_done() ? error::success : ec, total);
16 ✔
301

302
        return;
303
    }
304

305
    if (ec)
142 ✔
306
    {
307
        handler(ec, total);
13 ✔
308
        return;
13 ✔
309
    }
310

311
    if (!websocket())
129 ✔
312
        buffer.get().commit(size);
109 ✔
313

314
    do_parse(buffer, parser, mode, total, handler);
129 ✔
315
}
316

317
// Codec (serialize).
318
// ----------------------------------------------------------------------------
319
// Each set of buffers written in turn. Websocket writes each buffer as frame of
320
// one message, which last buffer closes.
321

322
// private
323
template <class Serializer>
324
void socket::do_serialize(
1,101 ✔
325
    const std::shared_ptr<serialize_state<Serializer>>& out, size_t total,
326
    const count_handler& handler) NOEXCEPT
327
{
328
    BC_ASSERT(stranded());
1,101 ✔
329

330
    // Held writes that fill buffer are sent before serializing behind them, as
331
    // writer requires room beyond byte it reserves.
332
    const auto room = floored_subtract(response_.max_size(), response_.size());
1,101 ✔
333
    if (room <= one && holding())
1,101 ✔
334
    {
335
        transmit({}, false,
2 ✔
336
            std::bind(&socket::handle_serialize<Serializer>,
2 ✔
337
                shared_from_this(), _1, _2, out, total, handler));
338
        return;
1 ✔
339
    }
340

UNCOV
341
    while (true)
×
342
    {
343
        code ec{};
1,100 ✔
344
        out->buffers = out->serializer.next(ec);
1,100 ✔
345
        if (ec)
1,100 ✔
346
        {
UNCOV
347
            const auto code = error::http_to_error_code(ec);
×
UNCOV
348
            if (code == error::unknown) logx("serialize", ec);
×
UNCOV
349
            handler(code, total);
×
350
            return;
351
        }
352

353
        if (!out->buffers.empty())
1,100 ✔
354
            break;
355

356
        if (out->serializer.is_done())
473 ✔
357
        {
358
            handler(error::success, total);
473 ✔
359
            return;
473 ✔
360
        }
361
    }
362

363
    if (websocket())
627 ✔
364
    {
365
        BC_ASSERT(is_one(out->buffers.size()));
136 ✔
366
        ws_send_data(out->buffers.front(), out->serializer.binary(),
408 ✔
367
            out->serializer.is_done(),
136 ✔
368
            std::bind(&socket::handle_serialize<Serializer>,
272 ✔
369
                shared_from_this(), _1, _2, out, total, handler));
370
        return;
136 ✔
371
    }
372

373
    transmit(out->buffers, out->serializer.is_done(),
1,473 ✔
374
        std::bind(&socket::handle_serialize<Serializer>,
982 ✔
375
            shared_from_this(), _1, _2, out, total, handler));
376
}
377

378
// private
379
template <class Serializer>
380
void socket::handle_serialize(const code& ec, size_t size,
628 ✔
381
    const std::shared_ptr<serialize_state<Serializer>>& out, size_t total,
382
    const count_handler& handler) NOEXCEPT
383
{
384
    BC_ASSERT(stranded());
628 ✔
385

386
    total = ceilinged_add(total, size);
628 ✔
387
    if (ec == error::operation_canceled)
628 ✔
388
    {
UNCOV
389
        handler(error::channel_stopped, total);
×
UNCOV
390
        return;
×
391
    }
392

393
    if (ec)
628 ✔
394
    {
395
        if (ec == error::unknown) logx("write", ec);
5 ✔
396
        handler(ec, total);
5 ✔
397
        return;
5 ✔
398
    }
399

400
    do_serialize(out, total, handler);
623 ✔
401
}
402

403
template void socket::do_parse<socket::http_parser>(
404
    const ref<http::flat_buffer>&, const http_parser_ptr&, parse_mode, size_t,
405
    const count_handler&) NOEXCEPT;
406
template void socket::do_parse<socket::body_parser>(
407
    const ref<http::flat_buffer>&, const body_parser_ptr&, parse_mode, size_t,
408
    const count_handler&) NOEXCEPT;
409
template void socket::do_serialize<socket::http_serializer>(
410
    const std::shared_ptr<serialize_state<http_serializer>>&, size_t,
411
    const count_handler&) NOEXCEPT;
412
template void socket::do_serialize<socket::body_serializer>(
413
    const std::shared_ptr<serialize_state<body_serializer>>&, size_t,
414
    const count_handler&) NOEXCEPT;
415
template void socket::do_serialize<socket::notify_serializer>(
416
    const std::shared_ptr<serialize_state<notify_serializer>>&, size_t,
417
    const count_handler&) NOEXCEPT;
418

419
// HTTP (progressive read).
420
// ----------------------------------------------------------------------------
421
// Caller owns parser, which owns http framing (content length and chunk
422
// decoding) across message. Batched json-rpc body delivers each message from
423
// paused parse (need_buffer), mapped to success here.
424

425
void socket::http_read_header(http::flat_buffer& buffer,
80 ✔
426
    const http_parser_ptr& parser, count_handler&& handler) NOEXCEPT
427
{
428
    io::dispatch(strand_,
80 ✔
429
        std::bind(&socket::do_http_read_header, shared_from_this(),
160 ✔
430
            std::ref(buffer), parser, std::move(handler)));
80 ✔
431
}
80 ✔
432

433
// private
434
void socket::do_http_read_header(ref<http::flat_buffer> buffer,
80 ✔
435
    const http_parser_ptr& parser, const count_handler& handler) NOEXCEPT
436
{
437
    BC_ASSERT(stranded());
80 ✔
438

439
    // Websocket messages have no http headers.
440
    if (websocket())
80 ✔
441
    {
442
        handler(error::operation_failed, {});
1 ✔
443
        return;
1 ✔
444
    }
445

446
    parser->body_limit(maximum_);
79 ✔
447
    parser->header_limit(header_);
79 ✔
448
    do_parse(buffer, parser, parse_mode::header, zero,
79 ✔
449
        std::bind(&socket::handle_http_read_header,
158 ✔
450
            shared_from_this(), _1, _2, parser, handler));
158 ✔
451
}
452

453
// private
454
void socket::handle_http_read_header(const code& ec, size_t size,
79 ✔
455
    const http_parser_ptr& parser, const count_handler& handler) NOEXCEPT
456
{
457
    BC_ASSERT(stranded());
79 ✔
458

459
    if (!ec && http::is_upgrade(parser->get()))
79 ✔
460
    {
461
        handler(error::upgrade, size);
37 ✔
462
        return;
37 ✔
463
    }
464

465
    if (!ec && parser->get().method() == http::verb::unknown)
42 ✔
466
    {
467
        handler(error::bad_method, size);
2 ✔
468
        return;
2 ✔
469
    }
470

471
    handler(ec, size);
40 ✔
472
}
473

474
void socket::http_read_some(http::flat_buffer& buffer,
15 ✔
475
    const http_parser_ptr& parser, count_handler&& handler) NOEXCEPT
476
{
477
    io::dispatch(strand_,
15 ✔
478
        std::bind(&socket::do_http_read_body, shared_from_this(),
30 ✔
479
            std::ref(buffer), parser, parse_mode::some,
30 ✔
480
            std::move(handler)));
15 ✔
481
}
15 ✔
482

483
void socket::http_read_body(http::flat_buffer& buffer,
32 ✔
484
    const http_parser_ptr& parser, count_handler&& handler) NOEXCEPT
485
{
486
    io::dispatch(strand_,
32 ✔
487
        std::bind(&socket::do_http_read_body, shared_from_this(),
64 ✔
488
            std::ref(buffer), parser, parse_mode::all, std::move(handler)));
32 ✔
489
}
32 ✔
490

491
// private
492
void socket::do_http_read_body(ref<http::flat_buffer> buffer,
47 ✔
493
    const http_parser_ptr& parser, parse_mode mode,
494
    const count_handler& handler) NOEXCEPT
495
{
496
    BC_ASSERT(stranded());
47 ✔
497

498
    // Websocket messages have no http framing.
499
    if (websocket())
47 ✔
500
    {
501
        handler(error::operation_failed, {});
2 ✔
502
        return;
2 ✔
503
    }
504

505
    do_parse(buffer, parser, mode, zero, handler);
45 ✔
506
}
507

508
// HTTP (progressive write).
509
// ----------------------------------------------------------------------------
510
// Header written once (chunked, no content length), and each response part then
511
// written as chunk data, batch close part terminating chunked body. Mirrors
512
// progressive read (nothing materialized).
513

514
void socket::http_write_header(http::response&& response,
8 ✔
515
    count_handler&& handler) NOEXCEPT
516
{
517
    const auto out = emplace_shared<header_state>(
518
        http::serialize_header(response.base()));
8 ✔
519

520
    io::dispatch(strand_,
8 ✔
521
        std::bind(&socket::do_http_write_header, shared_from_this(),
16 ✔
522
            out, std::move(handler)));
8 ✔
523
}
8 ✔
524

525
// private
526
void socket::do_http_write_header(const header_state::ptr& out,
8 ✔
527
    const count_handler& handler) NOEXCEPT
528
{
529
    BC_ASSERT(stranded());
8 ✔
530

531
    // Websocket messages have no http headers.
532
    if (websocket())
8 ✔
533
    {
534
        handler(error::operation_failed, {});
1 ✔
535
        return;
1 ✔
536
    }
537

538
    const io::const_buffer data{ out->header.data(),
7 ✔
539
        out->header.size() };
7 ✔
540

541
    transmit({ &data, one }, true,
7 ✔
542
        std::bind(&socket::handle_http_write_header,
14 ✔
543
            shared_from_this(), _1, _2, out, handler));
14 ✔
544
}
545

546
// private
547
void socket::handle_http_write_header(const code& ec, size_t size,
7 ✔
548
    const header_state::ptr&, const count_handler& handler) NOEXCEPT
549
{
550
    BC_ASSERT(stranded());
7 ✔
551

552
    if (ec == error::operation_canceled)
7 ✔
553
    {
UNCOV
554
        handler(error::channel_stopped, size);
×
UNCOV
555
        return;
×
556
    }
557

558
    if (ec == error::unknown) logx("http-write-header", ec);
7 ✔
559
    handler(ec, size);
7 ✔
560
}
561

562
void socket::rpc_write_chunk(rpc::response&& response,
18 ✔
563
    count_handler&& handler) NOEXCEPT
564
{
565
    http::response wrapper{};
18 ✔
566
    wrapper.body() = std::move(response);
18 ✔
567
    assign_buffer(wrapper.body());
18 ✔
568

569
    code ec{};
18 ✔
570
    const auto out = emplace_shared<chunk_state>(std::move(wrapper));
18 ✔
571
    out->writer.init(ec);
18 ✔
572

573
    io::dispatch(strand_,
18 ✔
574
        std::bind(&socket::do_chunk_write,
36 ✔
575
            shared_from_this(), ec, zero, out, std::move(handler)));
36 ✔
576
}
18 ✔
577

578
// private
579
void socket::do_chunk_write(code ec, size_t total,
26 ✔
580
    const chunk_state::ptr& out, const count_handler& handler) NOEXCEPT
581
{
582
    BC_ASSERT(stranded());
26 ✔
583

584
    // Websocket messages have no http framing.
585
    if (websocket())
26 ✔
586
    {
587
        handler(error::operation_failed, total);
1 ✔
588
        return;
4 ✔
589
    }
590

591
    const auto buffer = ec ? chunk_state::out_buffer{} : out->writer.get(ec);
25 ✔
592
    if (ec)
25 ✔
593
    {
UNCOV
594
        const auto code = error::http_to_error_code(ec);
×
UNCOV
595
        if (code == error::unknown) logx("chunk-write", ec);
×
UNCOV
596
        handler(code, total);
×
597
        return;
598
    }
599

600
    if (!buffer.has_value())
25 ✔
601
    {
602
        out->more = false;
3 ✔
603
        handle_chunk_write({}, zero, total, out, handler);
3 ✔
604
        return;
3 ✔
605
    }
606

607
    out->more = buffer.value().second;
22 ✔
608
    const auto& data = buffer.value().first;
22 ✔
609

610
    // Frame the part buffer as http chunk data (chunk_state lifetime).
611
    out->prefix = http::chunk_header(data.size());
22 ✔
612
    out->buffers =
22 ✔
613
    {
614
        io::const_buffer{ out->prefix.data(), out->prefix.size() },
22 ✔
615
        data,
616
        io::const_buffer{ http::chunk_end.data(), http::chunk_end.size() }
617
    };
22 ✔
618

619
    transmit(out->buffers, !out->more && !out->last(),
27 ✔
620
        std::bind(&socket::handle_chunk_write,
44 ✔
621
            shared_from_this(), _1, _2, total, out, handler));
44 ✔
622
}
623

624
// private
625
void socket::handle_chunk_write(const code& ec, size_t size,
25 ✔
626
    size_t total, const chunk_state::ptr& out,
627
    const count_handler& handler) NOEXCEPT
628
{
629
    BC_ASSERT(stranded());
25 ✔
630

631
    total = ceilinged_add(total, size);
25 ✔
632
    if (ec == error::operation_canceled)
25 ✔
633
    {
UNCOV
634
        reclaim_buffer();
×
UNCOV
635
        handler(error::channel_stopped, total);
×
636
        return;
19 ✔
637
    }
638

639
    if (ec)
25 ✔
640
    {
641
        reclaim_buffer();
1 ✔
642
        if (ec == error::unknown) logx("chunk-write", ec);
1 ✔
643
        handler(ec, total);
1 ✔
644
        return;
1 ✔
645
    }
646

647
    if (out->more)
24 ✔
648
    {
649
        do_chunk_write({}, total, out, handler);
8 ✔
650
        return;
8 ✔
651
    }
652

653
    reclaim_buffer();
16 ✔
654

655
    // Only the batch close part terminates the chunked body.
656
    if (!out->last())
16 ✔
657
    {
658
        handler(error::success, total);
10 ✔
659
        return;
10 ✔
660
    }
661

662
    const io::const_buffer last
6 ✔
663
    {
664
        http::last_chunk.data(), http::last_chunk.size()
665
    };
666

667
    transmit({ &last, one }, true,
6 ✔
668
        std::bind(&socket::handle_chunk_last,
12 ✔
669
            shared_from_this(), _1, _2, total, handler));
12 ✔
670
}
671

672
// private
673
void socket::handle_chunk_last(const code& ec, size_t size,
6 ✔
674
    size_t total, const count_handler& handler) NOEXCEPT
675
{
676
    BC_ASSERT(stranded());
6 ✔
677

678
    total = ceilinged_add(total, size);
6 ✔
679
    if (ec == error::operation_canceled)
6 ✔
680
    {
UNCOV
681
        handler(error::channel_stopped, total);
×
UNCOV
682
        return;
×
683
    }
684

685
    if (ec == error::unknown) logx("chunk-last", ec);
6 ✔
686
    handler(ec, total);
6 ✔
687
}
688

689
BC_POP_WARNING()
690
BC_POP_WARNING()
691
BC_POP_WARNING()
692

693
} // namespace network
694
} // 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