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

libbitcoin / libbitcoin-network / 44679191865

20 Nov 2026 05:10AM UTC coverage: 94.093% (+0.009%) from 94.084%
44679191865

push

github

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

Send held writes that fill the buffer before serializing behind them.

10 of 10 new or added lines in 3 files covered. (100.0%)

1 existing line in 1 file now uncovered.

16136 of 17149 relevant lines covered (94.09%)

2260.58 hits per line

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

92.91
/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
22 ✔
43
{
44
    // A json value opens with an object or array, and neither is a valid
45
    // http method token character (rfc9110 tchar).
46
    return byte == '{' || byte == '[';
22 ✔
47
}
48

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

56
// local
57
// False implies inconclusive (the buffer holds only empty lines).
58
inline bool detect_json(bool& json, const http::flat_buffer& buffer) NOEXCEPT
46 ✔
59
{
60
    const auto data = buffer.data();
46 ✔
61
    const std::string_view bytes{ static_cast<const char*>(data.data()),
46 ✔
62
        data.size() };
46 ✔
63

64
    for (const auto byte: bytes)
49 ✔
65
    {
66
        if (is_line_break(byte))
25 ✔
67
            continue;
68

69
        json = is_json_open(byte);
22 ✔
70
        return true;
22 ✔
71
    }
72

73
    return false;
74
}
75

76
void socket::detect(http::flat_buffer& buffer,
24 ✔
77
    count_handler&& handler) NOEXCEPT
78
{
79
    io::dispatch(strand_,
24 ✔
80
        std::bind(&socket::do_detect, shared_from_this(),
48 ✔
81
            std::ref(buffer), std::move(handler)));
24 ✔
82
}
24 ✔
83

84
// private
85
void socket::do_detect(const ref<http::flat_buffer>& buffer,
46 ✔
86
    const count_handler& handler) NOEXCEPT
87
{
88
    BC_ASSERT(stranded());
46 ✔
89

90
    bool json{};
46 ✔
91
    if (detect_json(json, buffer.get()))
46 ✔
92
    {
93
        detected_.store(true);
22 ✔
94
        downgraded_.store(json);
22 ✔
95
        handler(error::success, zero);
22 ✔
96
        return;
22 ✔
97
    }
98

99
    // Tolerated empty lines only, so read more (bounded by the buffer).
100
    async_read(buffer.get(),
24 ✔
101
        std::bind(&socket::handle_detect,
48 ✔
102
            shared_from_this(), _1, _2, buffer, handler));
48 ✔
103
}
104

105
// private
106
void socket::handle_detect(const code& ec, size_t size,
24 ✔
107
    const ref<http::flat_buffer>& buffer,
108
    const count_handler& handler) NOEXCEPT
109
{
110
    BC_ASSERT(stranded());
24 ✔
111

112
    if (ec)
24 ✔
113
    {
114
        handler(ec, size);
2 ✔
115
        return;
2 ✔
116
    }
117

118
    // async_read prepares, the caller commits (as does handle_body_read).
119
    buffer.get().commit(size);
22 ✔
120
    do_detect(buffer, handler);
22 ✔
121
}
122

123
// HTTP/WS (read).
124
// ----------------------------------------------------------------------------
125

126
void socket::http_read(http::flat_buffer& buffer,
25 ✔
127
    http::request& request, count_handler&& handler) NOEXCEPT
128
{
129
    io::dispatch(strand_,
25 ✔
130
        std::bind(&socket::do_http_read, shared_from_this(),
50 ✔
131
            std::ref(buffer), std::ref(request), std::move(handler)));
25 ✔
132
}
25 ✔
133

134
// private
135
void socket::do_http_read(ref<http::flat_buffer> buffer,
25 ✔
136
    const ref<http::request>& request,
137
    const count_handler& handler) NOEXCEPT
138
{
139
    BC_ASSERT(stranded());
25 ✔
140
    async_read_http(buffer.get(), request.get(), handler);
25 ✔
141
}
25 ✔
142

143
// private
144
void socket::handle_http_read(const code& ec, size_t size,
18 ✔
145
    const ref<http::request>& request, const http_parser_ptr& parser,
146
    const count_handler& handler) NOEXCEPT
147
{
148
    BC_ASSERT(stranded());
18 ✔
149

150
    if (!ec && http::is_upgrade(parser->get()))
18 ✔
151
    {
152
        request.get() = parser->release();
12 ✔
153
        handler(error::upgrade, size);
12 ✔
154
        return;
12 ✔
155
    }
156

157
    if (!ec)
6 ✔
158
    {
159
        request.get() = parser->release();
3 ✔
160
        if (request.get().method() == http::verb::unknown)
3 ✔
161
        {
162
            handler(error::bad_method, size);
1 ✔
163
            return;
1 ✔
164
        }
165
    }
166

167
    handler(ec, size);
5 ✔
168
}
169

170
// HTTP/WS (write).
171
// ----------------------------------------------------------------------------
172

173
void socket::http_write(http::response&& response,
41 ✔
174
    count_handler&& handler) NOEXCEPT
175
{
176
    const auto out = move_shared(std::move(response));
41 ✔
177
    io::dispatch(strand_,
41 ✔
178
        std::bind(&socket::do_http_write, shared_from_this(),
82 ✔
179
            out, std::move(handler)));
41 ✔
180
}
41 ✔
181

182
// private
183
void socket::do_http_write(const http::response_ptr& response,
41 ✔
184
    const count_handler& handler) NOEXCEPT
185
{
186
    BC_ASSERT(stranded());
41 ✔
187
    async_write_http(std::move(*response), handler);
41 ✔
188
}
41 ✔
189

190
// private
191
void socket::handle_http_write(const code& ec, size_t size,
468 ✔
192
    const count_handler& handler) NOEXCEPT
193
{
194
    BC_ASSERT(stranded());
468 ✔
195
    reclaim_buffer();
468 ✔
196
    handler(ec, size);
468 ✔
197
}
468 ✔
198

199
// Codec (parse).
200
// ----------------------------------------------------------------------------
201
// The buffered bytes are parsed before reading more. The header parse returns
202
// once the header is complete, the some parse once it progresses, and the
203
// full parse once the message is complete. A body reader may pause any parse
204
// (need_buffer), which is returned to the caller. A websocket reads each
205
// message whole, and other transports read the bytes available.
206

207
// private
208
template <class Parser>
209
void socket::do_parse(const ref<http::flat_buffer>& buffer,
310 ✔
210
    const std::shared_ptr<Parser>& parser, parse_mode mode, size_t total,
211
    const count_handler& handler) NOEXCEPT
212
{
213
    BC_ASSERT(stranded());
310 ✔
214
    auto& in = buffer.get();
310 ✔
215

216
    while (true)
×
217
    {
218
        code ec{};
310 ✔
219
        const auto used = parser->put(in.data(), ec);
310 ✔
220
        in.consume(used);
310 ✔
221
        total = ceilinged_add(total, used);
310 ✔
222

223
        if (ec == error::http_error_t::need_more)
310 ✔
224
            break;
225

226
        if (ec)
180 ✔
227
        {
228
            const auto code = error::rpc_to_error_code(ec);
69 ✔
229
            if (code == error::unknown) logx("parse", ec);
69 ✔
230
            handler(code, total);
69 ✔
231
            return;
232
        }
233

234
        if (parser->is_done() || (mode == parse_mode::header &&
24 ✔
235
            parser->is_header_done()) || (mode == parse_mode::some &&
135 ✔
236
            !is_zero(used)))
×
237
        {
238
            handler(error::success, total);
111 ✔
239
            return;
111 ✔
240
        }
241

242
        if (is_zero(used))
×
243
            break;
244
    }
245

246
    if (websocket())
130 ✔
247
    {
248
        async_read(in,
18 ✔
249
            std::bind(&socket::handle_parse<Parser>,
18 ✔
250
                shared_from_this(), _1, _2, buffer, parser, mode, total,
251
                handler));
252
        return;
9 ✔
253
    }
254

255
    // Read into available capacity, growing by up to the header limit.
256
    const auto limit = floored_subtract(in.max_size(), in.size());
121 ✔
257
    const auto size = std::min(limit, std::max(in.capacity() - in.size(),
121 ✔
258
        header_));
121 ✔
259

260
    if (is_zero(size))
121 ✔
261
    {
262
        handler(error::buffer_overflow, total);
×
263
        return;
×
264
    }
265

266
    async_read_some(in.prepare(size),
363 ✔
267
        std::bind(&socket::handle_parse<Parser>,
242 ✔
268
            shared_from_this(), _1, _2, buffer, parser, mode, total,
269
            handler));
270
}
271

272
// private
273
template <class Parser>
274
void socket::handle_parse(const code& ec, size_t size,
130 ✔
275
    const ref<http::flat_buffer>& buffer,
276
    const std::shared_ptr<Parser>& parser, parse_mode mode, size_t total,
277
    const count_handler& handler) NOEXCEPT
278
{
279
    BC_ASSERT(stranded());
130 ✔
280

281
    // The end of the stream completes a body so delimited, or is premature.
282
    if (ec == error::peer_disconnect)
130 ✔
283
    {
284
        code eof{};
11 ✔
285
        parser->put_eof(eof);
11 ✔
286
        if (eof)
11 ✔
287
            handler(error::http_to_error_code(eof), total);
2 ✔
288
        else
289
            handler(parser->is_done() ? error::success : ec, total);
9 ✔
290

291
        return;
292
    }
293

294
    if (ec)
119 ✔
295
    {
296
        handler(ec, total);
13 ✔
297
        return;
13 ✔
298
    }
299

300
    if (!websocket())
106 ✔
301
        buffer.get().commit(size);
98 ✔
302

303
    do_parse(buffer, parser, mode, total, handler);
106 ✔
304
}
305

306
// Codec (serialize).
307
// ----------------------------------------------------------------------------
308
// Each set of buffers is written in turn. A websocket writes each buffer as a
309
// frame of one message, which the last buffer closes.
310

311
// private
312
template <class Serializer>
313
void socket::do_serialize(
1,081 ✔
314
    const std::shared_ptr<serialize_state<Serializer>>& out, size_t total,
315
    const count_handler& handler) NOEXCEPT
316
{
317
    BC_ASSERT(stranded());
1,081 ✔
318

319
    // Held writes that fill the buffer are sent before serializing behind
320
    // them, as a writer requires room beyond the byte it reserves.
321
    const auto room = floored_subtract(response_.max_size(), response_.size());
1,081 ✔
322
    if (room <= one && holding())
1,081 ✔
323
    {
324
        transmit({}, false,
2 ✔
325
            std::bind(&socket::handle_serialize<Serializer>,
2 ✔
326
                shared_from_this(), _1, _2, out, total, handler));
327
        return;
1 ✔
328
    }
329

UNCOV
330
    while (true)
×
331
    {
332
        code ec{};
1,080 ✔
333
        out->buffers = out->serializer.next(ec);
1,080 ✔
334
        if (ec)
1,080 ✔
335
        {
336
            const auto code = error::http_to_error_code(ec);
×
337
            if (code == error::unknown) logx("serialize", ec);
×
338
            handler(code, total);
×
339
            return;
340
        }
341

342
        if (!out->buffers.empty())
1,080 ✔
343
            break;
344

345
        if (out->serializer.is_done())
463 ✔
346
        {
347
            handler(error::success, total);
463 ✔
348
            return;
463 ✔
349
        }
350
    }
351

352
    if (websocket())
617 ✔
353
    {
354
        BC_ASSERT(is_one(out->buffers.size()));
132 ✔
355
        ws_send_data(out->buffers.front(), out->serializer.binary(),
396 ✔
356
            out->serializer.is_done(),
132 ✔
357
            std::bind(&socket::handle_serialize<Serializer>,
264 ✔
358
                shared_from_this(), _1, _2, out, total, handler));
359
        return;
132 ✔
360
    }
361

362
    transmit(out->buffers, out->serializer.is_done(),
1,455 ✔
363
        std::bind(&socket::handle_serialize<Serializer>,
970 ✔
364
            shared_from_this(), _1, _2, out, total, handler));
365
}
366

367
// private
368
template <class Serializer>
369
void socket::handle_serialize(const code& ec, size_t size,
618 ✔
370
    const std::shared_ptr<serialize_state<Serializer>>& out, size_t total,
371
    const count_handler& handler) NOEXCEPT
372
{
373
    BC_ASSERT(stranded());
618 ✔
374

375
    total = ceilinged_add(total, size);
618 ✔
376
    if (ec == error::operation_canceled)
618 ✔
377
    {
378
        handler(error::channel_stopped, total);
×
379
        return;
×
380
    }
381

382
    if (ec)
618 ✔
383
    {
384
        if (ec == error::unknown) logx("write", ec);
5 ✔
385
        handler(ec, total);
5 ✔
386
        return;
5 ✔
387
    }
388

389
    do_serialize(out, total, handler);
613 ✔
390
}
391

392
template void socket::do_parse<socket::http_parser>(
393
    const ref<http::flat_buffer>&, const http_parser_ptr&, parse_mode, size_t,
394
    const count_handler&) NOEXCEPT;
395
template void socket::do_parse<socket::body_parser>(
396
    const ref<http::flat_buffer>&, const body_parser_ptr&, parse_mode, size_t,
397
    const count_handler&) NOEXCEPT;
398
template void socket::do_serialize<socket::http_serializer>(
399
    const std::shared_ptr<serialize_state<http_serializer>>&, size_t,
400
    const count_handler&) NOEXCEPT;
401
template void socket::do_serialize<socket::body_serializer>(
402
    const std::shared_ptr<serialize_state<body_serializer>>&, size_t,
403
    const count_handler&) NOEXCEPT;
404
template void socket::do_serialize<socket::notify_serializer>(
405
    const std::shared_ptr<serialize_state<notify_serializer>>&, size_t,
406
    const count_handler&) NOEXCEPT;
407

408
// HTTP (progressive read).
409
// ----------------------------------------------------------------------------
410
// The caller owns the parser, which owns http framing (content length and
411
// chunk decoding) across the message. A batched json-rpc body delivers each
412
// message from the paused parse (need_buffer), mapped to success here.
413

414
void socket::http_read_header(http::flat_buffer& buffer,
59 ✔
415
    const http_parser_ptr& parser, count_handler&& handler) NOEXCEPT
416
{
417
    io::dispatch(strand_,
59 ✔
418
        std::bind(&socket::do_http_read_header, shared_from_this(),
118 ✔
419
            std::ref(buffer), parser, std::move(handler)));
59 ✔
420
}
59 ✔
421

422
// private
423
void socket::do_http_read_header(ref<http::flat_buffer> buffer,
59 ✔
424
    const http_parser_ptr& parser, const count_handler& handler) NOEXCEPT
425
{
426
    BC_ASSERT(stranded());
59 ✔
427

428
    // Websocket messages have no http headers.
429
    if (websocket())
59 ✔
430
    {
431
        handler(error::operation_failed, {});
1 ✔
432
        return;
1 ✔
433
    }
434

435
    parser->body_limit(maximum_);
58 ✔
436
    parser->header_limit(header_);
58 ✔
437
    do_parse(buffer, parser, parse_mode::header, zero,
58 ✔
438
        std::bind(&socket::handle_http_read_header,
116 ✔
439
            shared_from_this(), _1, _2, parser, handler));
116 ✔
440
}
441

442
// private
443
void socket::handle_http_read_header(const code& ec, size_t size,
58 ✔
444
    const http_parser_ptr& parser, const count_handler& handler) NOEXCEPT
445
{
446
    BC_ASSERT(stranded());
58 ✔
447

448
    if (!ec && http::is_upgrade(parser->get()))
58 ✔
449
    {
450
        handler(error::upgrade, size);
17 ✔
451
        return;
17 ✔
452
    }
453

454
    if (!ec && parser->get().method() == http::verb::unknown)
41 ✔
455
    {
456
        handler(error::bad_method, size);
2 ✔
457
        return;
2 ✔
458
    }
459

460
    handler(ec, size);
39 ✔
461
}
462

463
void socket::http_read_some(http::flat_buffer& buffer,
15 ✔
464
    const http_parser_ptr& parser, count_handler&& handler) NOEXCEPT
465
{
466
    io::dispatch(strand_,
15 ✔
467
        std::bind(&socket::do_http_read_body, shared_from_this(),
30 ✔
468
            std::ref(buffer), parser, parse_mode::some,
30 ✔
469
            std::move(handler)));
15 ✔
470
}
15 ✔
471

472
void socket::http_read_body(http::flat_buffer& buffer,
30 ✔
473
    const http_parser_ptr& parser, count_handler&& handler) NOEXCEPT
474
{
475
    io::dispatch(strand_,
30 ✔
476
        std::bind(&socket::do_http_read_body, shared_from_this(),
60 ✔
477
            std::ref(buffer), parser, parse_mode::all, std::move(handler)));
30 ✔
478
}
30 ✔
479

480
// private
481
void socket::do_http_read_body(ref<http::flat_buffer> buffer,
45 ✔
482
    const http_parser_ptr& parser, parse_mode mode,
483
    const count_handler& handler) NOEXCEPT
484
{
485
    BC_ASSERT(stranded());
45 ✔
486

487
    // Websocket messages have no http framing.
488
    if (websocket())
45 ✔
489
    {
490
        handler(error::operation_failed, {});
2 ✔
491
        return;
2 ✔
492
    }
493

494
    do_parse(buffer, parser, mode, zero, handler);
43 ✔
495
}
496

497
// HTTP (progressive write).
498
// ----------------------------------------------------------------------------
499
// The header is written once (chunked, no content length), and each response
500
// part is then written as chunk data, the batch close part terminating the
501
// chunked body. This mirrors the progressive read (nothing is materialized).
502

503
void socket::http_write_header(http::response&& response,
8 ✔
504
    count_handler&& handler) NOEXCEPT
505
{
506
    const auto out = emplace_shared<header_state>(
507
        http::serialize_header(response.base()));
8 ✔
508

509
    io::dispatch(strand_,
8 ✔
510
        std::bind(&socket::do_http_write_header, shared_from_this(),
16 ✔
511
            out, std::move(handler)));
8 ✔
512
}
8 ✔
513

514
// private
515
void socket::do_http_write_header(const header_state::ptr& out,
8 ✔
516
    const count_handler& handler) NOEXCEPT
517
{
518
    BC_ASSERT(stranded());
8 ✔
519

520
    // Websocket messages have no http headers.
521
    if (websocket())
8 ✔
522
    {
523
        handler(error::operation_failed, {});
1 ✔
524
        return;
1 ✔
525
    }
526

527
    const io::const_buffer data{ out->header.data(),
7 ✔
528
        out->header.size() };
7 ✔
529

530
    transmit({ &data, one }, true,
7 ✔
531
        std::bind(&socket::handle_http_write_header,
14 ✔
532
            shared_from_this(), _1, _2, out, handler));
14 ✔
533
}
534

535
// private
536
void socket::handle_http_write_header(const code& ec, size_t size,
7 ✔
537
    const header_state::ptr&, const count_handler& handler) NOEXCEPT
538
{
539
    BC_ASSERT(stranded());
7 ✔
540

541
    if (ec == error::operation_canceled)
7 ✔
542
    {
543
        handler(error::channel_stopped, size);
×
544
        return;
×
545
    }
546

547
    if (ec == error::unknown) logx("http-write-header", ec);
7 ✔
548
    handler(ec, size);
7 ✔
549
}
550

551
void socket::rpc_write_chunk(rpc::response&& response,
18 ✔
552
    count_handler&& handler) NOEXCEPT
553
{
554
    http::response wrapper{};
18 ✔
555
    wrapper.body() = std::move(response);
18 ✔
556
    assign_buffer(wrapper.body());
18 ✔
557

558
    code ec{};
18 ✔
559
    const auto out = emplace_shared<chunk_state>(std::move(wrapper));
18 ✔
560
    out->writer.init(ec);
18 ✔
561

562
    io::dispatch(strand_,
18 ✔
563
        std::bind(&socket::do_chunk_write,
36 ✔
564
            shared_from_this(), ec, zero, out, std::move(handler)));
36 ✔
565
}
18 ✔
566

567
// private
568
void socket::do_chunk_write(code ec, size_t total,
26 ✔
569
    const chunk_state::ptr& out, const count_handler& handler) NOEXCEPT
570
{
571
    BC_ASSERT(stranded());
26 ✔
572

573
    // Websocket messages have no http framing.
574
    if (websocket())
26 ✔
575
    {
576
        handler(error::operation_failed, total);
1 ✔
577
        return;
4 ✔
578
    }
579

580
    const auto buffer = ec ? chunk_state::out_buffer{} : out->writer.get(ec);
25 ✔
581
    if (ec)
25 ✔
582
    {
583
        const auto code = error::http_to_error_code(ec);
×
584
        if (code == error::unknown) logx("chunk-write", ec);
×
585
        handler(code, total);
×
586
        return;
587
    }
588

589
    if (!buffer.has_value())
25 ✔
590
    {
591
        out->more = false;
3 ✔
592
        handle_chunk_write({}, zero, total, out, handler);
3 ✔
593
        return;
3 ✔
594
    }
595

596
    out->more = buffer.value().second;
22 ✔
597
    const auto& data = buffer.value().first;
22 ✔
598

599
    // Frame the part buffer as http chunk data (chunk_state lifetime).
600
    out->prefix = http::chunk_header(data.size());
22 ✔
601
    out->buffers =
22 ✔
602
    {
603
        io::const_buffer{ out->prefix.data(), out->prefix.size() },
22 ✔
604
        data,
605
        io::const_buffer{ http::chunk_end.data(), http::chunk_end.size() }
606
    };
22 ✔
607

608
    transmit(out->buffers, !out->more && !out->last(),
27 ✔
609
        std::bind(&socket::handle_chunk_write,
44 ✔
610
            shared_from_this(), _1, _2, total, out, handler));
44 ✔
611
}
612

613
// private
614
void socket::handle_chunk_write(const code& ec, size_t size,
25 ✔
615
    size_t total, const chunk_state::ptr& out,
616
    const count_handler& handler) NOEXCEPT
617
{
618
    BC_ASSERT(stranded());
25 ✔
619

620
    total = ceilinged_add(total, size);
25 ✔
621
    if (ec == error::operation_canceled)
25 ✔
622
    {
623
        reclaim_buffer();
×
624
        handler(error::channel_stopped, total);
×
625
        return;
19 ✔
626
    }
627

628
    if (ec)
25 ✔
629
    {
630
        reclaim_buffer();
1 ✔
631
        if (ec == error::unknown) logx("chunk-write", ec);
1 ✔
632
        handler(ec, total);
1 ✔
633
        return;
1 ✔
634
    }
635

636
    if (out->more)
24 ✔
637
    {
638
        do_chunk_write({}, total, out, handler);
8 ✔
639
        return;
8 ✔
640
    }
641

642
    reclaim_buffer();
16 ✔
643

644
    // Only the batch close part terminates the chunked body.
645
    if (!out->last())
16 ✔
646
    {
647
        handler(error::success, total);
10 ✔
648
        return;
10 ✔
649
    }
650

651
    const io::const_buffer last{ http::last_chunk.data(),
6 ✔
652
        http::last_chunk.size() };
653

654
    transmit({ &last, one }, true,
6 ✔
655
        std::bind(&socket::handle_chunk_last,
12 ✔
656
            shared_from_this(), _1, _2, total, handler));
12 ✔
657
}
658

659
// private
660
void socket::handle_chunk_last(const code& ec, size_t size,
6 ✔
661
    size_t total, const count_handler& handler) NOEXCEPT
662
{
663
    BC_ASSERT(stranded());
6 ✔
664

665
    total = ceilinged_add(total, size);
6 ✔
666
    if (ec == error::operation_canceled)
6 ✔
667
    {
668
        handler(error::channel_stopped, total);
×
669
        return;
×
670
    }
671

672
    if (ec == error::unknown) logx("chunk-last", ec);
6 ✔
673
    handler(ec, total);
6 ✔
674
}
675

676
BC_POP_WARNING()
677
BC_POP_WARNING()
678
BC_POP_WARNING()
679

680
} // namespace network
681
} // 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