• 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

81.33
/src/net/socket.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/async/async.hpp>
22
#include <bitcoin/network/config/config.hpp>
23
#include <bitcoin/network/define.hpp>
24
#include <bitcoin/network/log/log.hpp>
25
#include <bitcoin/network/net/deadline.hpp>
26

27
// Async operations not thread safe for same socket object.
28
// Calls stranded to protect socket member.
29

30
namespace libbitcoin {
31
namespace network {
32

33
using namespace system;
34
using namespace std::placeholders;
35

36
// Shared pointers required in handler parameters so closures control lifetime.
37
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
38
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
39
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
40

41
// Bounds query overrun following caller drop, not response latency.
42
constexpr auto watch_interval = milliseconds(10);
43

44
// Construct.
45
// ----------------------------------------------------------------------------
46

47
socket::socket(const logger& log, io::context& service,
444 ✔
48
    const parameters& parameters, bool proxied) NOEXCEPT
444 ✔
49
  : socket(log, service, parameters, {}, {}, proxied, true)
444 ✔
50
{
51
}
444 ✔
52

53
socket::socket(const logger& log, io::context& service,
135 ✔
54
    const parameters& params, const config::address& address,
55
    const config::endpoint& endpoint, bool proxied) NOEXCEPT
135 ✔
56
  : socket(log, service, params, address, endpoint, proxied, false)
135 ✔
57
{
58
}
135 ✔
59

60
// protected
61
socket::socket(const logger& log, io::context& service,
579 ✔
62
    const parameters& params, const config::address& address,
63
    const config::endpoint& endpoint, bool proxied, bool inbound) NOEXCEPT
579 ✔
64
  : reporter(log),
65
    tracker<socket>(log),
66
    inbound_(inbound),
579 ✔
67
    proxied_(proxied),
579 ✔
68
    maximum_(params.maximum_request),
579 ✔
69
    header_(std::min(params.maximum_request, maximum_http_header)),
579 ✔
70
    minimum_buffer_(params.minimum_buffer),
579 ✔
71
    bind_(params.bind),
579 ✔
72
    slot_(params.slot),
579 ✔
73
    strand_(service.get_executor()),
579 ✔
74
    service_(service),
579 ✔
75
    context_(params.context),
579 ✔
76
    role_(params.role),
579 ✔
77
    address_(address),
579 ✔
78
    endpoint_(endpoint),
579 ✔
79
    timer_(emplace_shared<deadline>(log, strand_, params.connect_timeout)),
579 ✔
80
    watch_(emplace_shared<deadline>(log, strand_, watch_interval)),
579 ✔
81
    socket_(std::in_place_type<io::socket>, strand_),
579 ✔
82
    response_(params.maximum_buffer)
2,316 ✔
83
{
84
}
579 ✔
85

86
socket::~socket() NOEXCEPT
579 ✔
87
{
88
    BC_ASSERT_MSG(stopped(), "socket is not stopped");
579 ✔
89
    if (!stopped_.load()) { LOGF("~socket is not stopped."); }
579 ✔
90
}
579 ✔
91

92
// Wait.
93
// ----------------------------------------------------------------------------
94

95
void socket::watch(result_handler&& handler) NOEXCEPT
18 ✔
96
{
97
    io::dispatch(strand_,
18 ✔
98
        std::bind(&socket::do_watch,
36 ✔
99
            shared_from_this(), std::move(handler)));
36 ✔
100
}
18 ✔
101

102
void socket::poll(result_handler&& handler) NOEXCEPT
3 ✔
103
{
104
    io::dispatch(strand_,
3 ✔
105
        std::bind(&socket::do_poll,
6 ✔
106
            shared_from_this(), std::move(handler)));
6 ✔
107
}
3 ✔
108

109
// private
110
void socket::do_watch(const result_handler& handler) NOEXCEPT
21 ✔
111
{
112
    BC_ASSERT(stranded());
21 ✔
113

114
    if (stopped_.load())
21 ✔
115
    {
116
        handler(error::success);
×
117
        return;
×
118
    }
119

120
    watch_->start(std::bind(&socket::handle_watch,
21 ✔
121
        shared_from_this(), _1, handler));
42 ✔
122
}
123

124
// private
125
void socket::do_poll(const result_handler& handler) NOEXCEPT
3 ✔
126
{
127
    BC_ASSERT(stranded());
3 ✔
128

129
    if (stopped_.load())
3 ✔
130
    {
131
        handler(error::success);
1 ✔
132
        return;
1 ✔
133
    }
134

135
    watch_->start(std::bind(&socket::handle_poll,
2 ✔
136
        shared_from_this(), _1, handler));
4 ✔
137
}
138

139
// private
140
void socket::handle_watch(const code& ec,
21 ✔
141
    const result_handler& handler) NOEXCEPT
142
{
143
    BC_ASSERT(stranded());
21 ✔
144

145
    // Only timer stop results in caller not stopping the channel.
146
    if (ec)
21 ✔
147
    {
148
        handler(error::success);
17 ✔
149
        return;
17 ✔
150
    }
151

152
    if (get_base().half_closed())
4 ✔
153
    {
154
        handler(error::peer_disconnect);
1 ✔
155
        return;
1 ✔
156
    }
157

158
    do_watch(handler);
3 ✔
159
}
160

161
// private
162
void socket::handle_poll(const code& ec,
2 ✔
163
    const result_handler& handler) NOEXCEPT
164
{
165
    BC_ASSERT(stranded());
2 ✔
166

167
    // Timer stop (socket stop) results in caller not stopping the channel.
168
    if (!ec && get_base().half_closed())
2 ✔
169
    {
170
        handler(error::peer_disconnect);
1 ✔
171
        return;
1 ✔
172
    }
173

174
    handler(error::success);
1 ✔
175
}
176

177
// Properties.
178
// ----------------------------------------------------------------------------
179

180
io::context& socket::service() const NOEXCEPT
2 ✔
181
{
182
    return service_;
2 ✔
183
}
184

185
io::strand& socket::strand() NOEXCEPT
3,401 ✔
186
{
187
    return strand_;
3,401 ✔
188
}
189

190
bool socket::stranded() const NOEXCEPT
413,021 ✔
191
{
192
    return strand_.running_in_this_thread();
413,021 ✔
193
}
194

195
bool socket::stopped() const NOEXCEPT
83,897 ✔
196
{
197
    return stopped_.load();
83,897 ✔
198
}
199

200
bool socket::inbound() const NOEXCEPT
793 ✔
201
{
202
    return inbound_;
793 ✔
203
}
204

205
bool socket::websocket() const NOEXCEPT
78,281 ✔
206
{
207
    return websocket_.load();
78,281 ✔
208
}
209

210
bool socket::downgraded() const NOEXCEPT
294 ✔
211
{
212
    return downgraded_.load();
294 ✔
213
}
214

215
bool socket::detected() const NOEXCEPT
93 ✔
216
{
217
    return detected_.load();
93 ✔
218
}
219

220
const config::address& socket::address() const NOEXCEPT
304 ✔
221
{
222
    return address_;
304 ✔
223
}
224

225
const config::endpoint& socket::endpoint() const NOEXCEPT
340 ✔
226
{
227
    return endpoint_;
340 ✔
228
}
229

230
const config::endpoint& socket::binding() const NOEXCEPT
9 ✔
231
{
232
    return binding_;
9 ✔
233
}
234

235
size_t socket::slot() const NOEXCEPT
4 ✔
236
{
237
    return slot_;
4 ✔
238
}
239

240
void socket::set_address(const config::address& address) NOEXCEPT
2 ✔
241
{
242
    BC_ASSERT(stranded());
2 ✔
243
    address_ = address;
2 ✔
244
    endpoint_ = address;
2 ✔
245
}
2 ✔
246

247
// Variant state (protected by strand).
248
// ----------------------------------------------------------------------------
249

250
bool socket::secure() const NOEXCEPT
54 ✔
251
{
252
    BC_ASSERT(stranded());
54 ✔
253
    return std::holds_alternative<tls::stream>(socket_);
54 ✔
254
}
255

256
bool socket::encrypted() const NOEXCEPT
1,032 ✔
257
{
258
    BC_ASSERT(stranded());
1,032 ✔
259
    return std::holds_alternative<p2ps::stream>(socket_);
1,032 ✔
260
}
261

262
bool socket::zeromq() const NOEXCEPT
326 ✔
263
{
264
    BC_ASSERT(stranded());
326 ✔
265
    return std::holds_alternative<zmtp::stream>(socket_);
326 ✔
266
}
267

UNCOV
268
bool socket::noised() const NOEXCEPT
×
269
{
UNCOV
270
    BC_ASSERT(stranded());
×
UNCOV
271
    return std::holds_alternative<noise::stream>(socket_);
×
272
}
273

274
// protected
275
bool socket::is_base() const NOEXCEPT
1 ✔
276
{
277
    BC_ASSERT(stranded());
1 ✔
278
    return std::holds_alternative<io::socket>(socket_);
1 ✔
279
}
280

281
// Variant accessors.
282
// ----------------------------------------------------------------------------
283
// protected
284

285
socket::tcp_t socket::get_tcp() NOEXCEPT
77,033 ✔
286
{
287
    BC_ASSERT(stranded());
77,033 ✔
288

289
    return std::visit(overload
154,066 ✔
290
    {
291
        [](io::socket& value) NOEXCEPT -> socket::tcp_t
77,024 ✔
292
        {
293
            return std::ref(value);
77,024 ✔
294
        },
295
        [](tls::stream& value) NOEXCEPT -> socket::tcp_t
9 ✔
296
        {
297
            return std::ref(value);
9 ✔
298
        },
299
        [](p2ps::stream&) NOEXCEPT -> socket::tcp_t
×
300
        {
UNCOV
301
            std::terminate();
×
302
        },
UNCOV
303
        [](zmtp::stream&) NOEXCEPT -> socket::tcp_t
×
304
        {
UNCOV
305
            std::terminate();
×
306
        },
UNCOV
307
        [](noise::stream& value) NOEXCEPT -> socket::tcp_t
×
308
        {
UNCOV
309
            return std::ref(value);
×
310
        }
311
    }, socket_);
77,033 ✔
312
}
313

314
io::socket& socket::get_base() NOEXCEPT
2,857 ✔
315
{
316
    return std::visit(overload
5,714 ✔
317
    {
318
        [](io::socket& value) NOEXCEPT -> io::socket&
2,798 ✔
319
        {
320
            return value;
2,798 ✔
321
        },
322
        [](tls::stream& value) NOEXCEPT -> io::socket&
12 ✔
323
        {
324
            return value.next_layer();
12 ✔
325
        },
326
        [](p2ps::stream& value) NOEXCEPT -> io::socket&
4 ✔
327
        {
328
            return value.next_layer();
4 ✔
329
        },
330
        [](zmtp::stream& value) NOEXCEPT -> io::socket&
43 ✔
331
        {
332
            return value.next_layer();
43 ✔
333
        },
334
        [](noise::stream& value) NOEXCEPT -> io::socket&
×
335
        {
336
            return value.next_layer();
×
337
        }
338
    }, socket_);
2,857 ✔
339
}
340

UNCOV
341
tls::stream& socket::get_tls() NOEXCEPT
×
342
{
UNCOV
343
    BC_ASSERT(stranded());
×
344
    BC_ASSERT(secure());
×
345

346
    return std::visit(overload
×
347
    {
348
        [](io::socket&) NOEXCEPT -> tls::stream&
×
349
        {
UNCOV
350
            std::terminate();
×
351
        },
UNCOV
352
        [](tls::stream& value) NOEXCEPT -> tls::stream&
×
353
        {
UNCOV
354
            return value;
×
355
        },
UNCOV
356
        [](p2ps::stream&) NOEXCEPT -> tls::stream&
×
357
        {
UNCOV
358
            std::terminate();
×
359
        },
360
        [](zmtp::stream&) NOEXCEPT -> tls::stream&
×
361
        {
362
            std::terminate();
×
363
        },
364
        [](noise::stream&) NOEXCEPT -> tls::stream&
×
365
        {
366
            std::terminate();
×
367
        }
UNCOV
368
    }, socket_);
×
369
}
370

371
p2ps::stream& socket::get_p2ps() NOEXCEPT
8 ✔
372
{
373
    BC_ASSERT(stranded());
8 ✔
374
    BC_ASSERT(encrypted());
8 ✔
375

376
    return std::visit(overload
16 ✔
377
    {
UNCOV
378
        [](io::socket&) NOEXCEPT -> p2ps::stream&
×
379
        {
UNCOV
380
            std::terminate();
×
381
        },
UNCOV
382
        [](tls::stream&) NOEXCEPT -> p2ps::stream&
×
383
        {
UNCOV
384
            std::terminate();
×
385
        },
386
        [](p2ps::stream& value) NOEXCEPT -> p2ps::stream&
8 ✔
387
        {
388
            return value;
8 ✔
389
        },
390
        [](zmtp::stream&) NOEXCEPT -> p2ps::stream&
×
391
        {
392
            std::terminate();
×
393
        },
394
        [](noise::stream&) NOEXCEPT -> p2ps::stream&
×
395
        {
396
            std::terminate();
×
397
        }
398
    }, socket_);
8 ✔
399
}
400

401
zmtp::stream& socket::get_zmtp() NOEXCEPT
94 ✔
402
{
403
    BC_ASSERT(stranded());
94 ✔
404
    BC_ASSERT(zeromq());
94 ✔
405

406
    return std::visit(overload
188 ✔
407
    {
UNCOV
408
        [](io::socket&) NOEXCEPT -> zmtp::stream&
×
409
        {
UNCOV
410
            std::terminate();
×
411
        },
UNCOV
412
        [](tls::stream&) NOEXCEPT -> zmtp::stream&
×
413
        {
UNCOV
414
            std::terminate();
×
415
        },
UNCOV
416
        [](p2ps::stream&) NOEXCEPT -> zmtp::stream&
×
417
        {
UNCOV
418
            std::terminate();
×
419
        },
420
        [](zmtp::stream& value) NOEXCEPT -> zmtp::stream&
94 ✔
421
        {
422
            return value;
94 ✔
423
        },
UNCOV
424
        [](noise::stream&) NOEXCEPT -> zmtp::stream&
×
425
        {
UNCOV
426
            std::terminate();
×
427
        }
428
    }, socket_);
94 ✔
429
}
430

431
// Variant (ws vs. tcp)  helpers.
432
// ----------------------------------------------------------------------------
433
// protected
434

435
// write message frame (ws, finish closes message) or unframed fixed size bytes
436
// (tcp). Binary/text setting applies only at start of message, so it is
437
// harmless to pass it on every frame of message.
438
void socket::async_write(const io::const_buffer& buffer, bool binary,
126 ✔
439
    bool finish, const count_handler& handler) NOEXCEPT
440
{
441
    BC_ASSERT(stranded());
126 ✔
442

443
    if (websocket())
126 ✔
444
    {
445
        ws_send_data(buffer, binary, finish, handler);
1 ✔
446
    }
447
    else
448
    {
449
        transmit({ &buffer, one }, finish,
125 ✔
450
            std::bind(&socket::handle_async,
250 ✔
451
                shared_from_this(), _1, _2, handler, "async_write"));
250 ✔
452
    }
453
}
126 ✔
454

455
// Transport (write).
456
// ----------------------------------------------------------------------------
457
// Writes issued one at a time, write issued over
458
// another deferred to its completion. Retained buffer
459
// not prepared while write of its bytes is in progress.
460

461
void socket::hold(bool enable) NOEXCEPT
504 ✔
462
{
463
    BC_ASSERT(stranded());
504 ✔
464
    hold_ = enable;
504 ✔
465
    settling_ = enable;
504 ✔
466
}
504 ✔
467

468
void socket::settle() NOEXCEPT
502 ✔
469
{
470
    BC_ASSERT(stranded());
502 ✔
471
    settling_ = false;
502 ✔
472
    if (settled_)
502 ✔
473
        std::exchange(settled_, {})();
5 ✔
474
}
502 ✔
475

476
bool socket::holding() const NOEXCEPT
477 ✔
477
{
478
    BC_ASSERT(stranded());
477 ✔
479
    return !is_zero(response_.size());
477 ✔
480
}
481

482
void socket::release(count_handler&& handler) NOEXCEPT
2 ✔
483
{
484
    BC_ASSERT(stranded());
2 ✔
485
    hold_ = false;
2 ✔
486
    settling_ = false;
2 ✔
487
    transmit({}, false, handler);
2 ✔
488
}
2 ✔
489

490
// protected
491
// Write is preceded by held writes, or is held (if hold) with them.
492
void socket::transmit(const std::span<const io::const_buffer>& buffers,
791 ✔
493
    bool hold, const count_handler& handler) NOEXCEPT
494
{
495
    BC_ASSERT(stranded());
791 ✔
496

497
    if (sending_)
791 ✔
498
    {
UNCOV
499
        deferred_ = std::bind(&socket::transmit, shared_from_this(),
×
UNCOV
500
            io::to_buffers<io::const_buffer>(buffers), hold, handler);
×
501
        return;
9 ✔
502
    }
503

504
    const auto held = response_.size();
791 ✔
505
    const auto size = io::buffer_size(buffers);
791 ✔
506
    const auto bound = std::min(minimum_buffer_, response_.max_size());
791 ✔
507
    if (hold && hold_ && size <= floored_subtract(bound, held))
791 ✔
508
    {
509
        response_.append(buffers);
9 ✔
510
        if (settling_)
9 ✔
511
        {
512
            settling_ = false;
5 ✔
513
            settled_ = std::bind(handler, error::success, size);
5 ✔
514
            return;
5 ✔
515
        }
516

517
        io::post(strand_, std::bind(handler, error::success, size));
4 ✔
518
        return;
4 ✔
519
    }
520

521
    io::const_buffers gather{};
782 ✔
522
    if (!is_zero(held))
782 ✔
523
        gather.push_back(response_.cdata());
8 ✔
524

525
    gather.insert(gather.end(), buffers.begin(), buffers.end());
782 ✔
526
    response_.clear();
782 ✔
527
    sending_ = true;
782 ✔
528
    VARIANT_DISPATCH_FUNCTION(io::async_write, get_tcp(),
1,564 ✔
529
        gather, std::bind(&socket::handle_transmit,
530
            shared_from_this(), _1, _2, held, handler));
531
}
782 ✔
532

533
// protected
534
// Write is independent of held writes.
535
void socket::interject(const std::span<const io::const_buffer>& buffers,
4 ✔
536
    const count_handler& handler) NOEXCEPT
537
{
538
    BC_ASSERT(stranded());
4 ✔
539

540
    if (sending_)
4 ✔
541
    {
UNCOV
542
        deferred_ = std::bind(&socket::interject, shared_from_this(),
×
UNCOV
543
            io::to_buffers<io::const_buffer>(buffers), handler);
×
UNCOV
544
        return;
×
545
    }
546

547
    sending_ = true;
4 ✔
548
    VARIANT_DISPATCH_FUNCTION(io::async_write, get_tcp(),
8 ✔
549
        buffers, std::bind(&socket::handle_transmit,
550
            shared_from_this(), _1, _2, zero, handler));
551
}
552

553
// private
554
// Size reported excludes that of held writes (reported when held).
555
void socket::handle_transmit(const code& ec, size_t size, size_t held,
786 ✔
556
    const count_handler& handler) NOEXCEPT
557
{
558
    BC_ASSERT(stranded());
786 ✔
559

560
    sending_ = false;
786 ✔
561
    const auto next = std::exchange(deferred_, {});
786 ✔
562
    handler(ec, floored_subtract(size, held));
786 ✔
563

564
    if (next)
786 ✔
UNCOV
565
        next();
×
566
}
786 ✔
567

568
// read arbitrary tcp bytes, iterate (up to buffer capacity).
569
void socket::async_read_some(const io::mutable_buffer& buffer,
133 ✔
570
    const count_handler& handler) NOEXCEPT
571
{
572
    BC_ASSERT(stranded());
133 ✔
573

574
    if (websocket())
133 ✔
575
    {
576
        // Websockets read messages, use async_read(flat_buffer).
UNCOV
577
        handler(error::operation_failed, {});
×
578
    }
579
    else
580
    {
581
        VARIANT_DISPATCH_METHOD(get_tcp(),
266 ✔
582
            async_read_some(buffer, std::bind(&socket::handle_async,
583
                shared_from_this(), _1, _2, handler, "async_read_some")));
584
    }
585
}
133 ✔
586

587
// read arbitrary tcp bytes, iterate (up to buffer capacity).
588
// read whole ws message in any number of frames (up to buffer capacity).
589
void socket::async_read(http::flat_buffer& buffer,
70 ✔
590
    const count_handler& handler) NOEXCEPT
591
{
592
    BC_ASSERT(stranded());
70 ✔
593

594
    try
595
    {
596
        if (websocket())
70 ✔
597
        {
598
            buffer.consume(buffer.size());
30 ✔
599
            do_ws_read_message(std::ref(buffer), handler);
30 ✔
600
        }
601
        else
602
        {
603
            auto remain = floored_subtract(buffer.max_size(), buffer.size());
40 ✔
604
            if (is_zero(remain))
40 ✔
605
            {
606
                handler(error::buffer_overflow, {});
1 ✔
607
                return;
1 ✔
608
            }
609

610
            VARIANT_DISPATCH_METHOD(get_tcp(),
78 ✔
611
                async_read_some(buffer.prepare(remain),
612
                    std::bind(&socket::handle_async, shared_from_this(),
613
                    _1, _2, handler, "async_read_some")));
614
        }
615
    }
UNCOV
616
    catch (const std::exception& e)
×
617
    {
UNCOV
618
        LOGF("Exception @ async_read: " << e.what());
×
UNCOV
619
        handler(error::operation_failed, {});
×
UNCOV
620
    }
×
621
}
622

623
// read tcp fixed count of bytes (waits until mutable buffer is filled).
624
void socket::async_read(const io::mutable_buffer& buffer,
76,002 ✔
625
    const count_handler& handler) NOEXCEPT
626
{
627
    BC_ASSERT(stranded());
76,002 ✔
628

629
    if (websocket())
76,002 ✔
630
    {
631
        // Websockets read frame not size, use async_read(flat_buffer).
632
        handler(error::operation_failed, {});
1 ✔
633
    }
634
    else
635
    {
636
        // Fixed-size semantics.
637
        VARIANT_DISPATCH_FUNCTION(io::async_read, get_tcp(),
152,002 ✔
638
            buffer, std::bind(&socket::handle_async,
639
                shared_from_this(), _1, _2, handler, "async_read"));
640
    }
641
}
76,002 ✔
642

643
void socket::async_read_http(http::flat_buffer& buffer, http::request& request,
43 ✔
644
    const count_handler& handler) NOEXCEPT
645
{
646
    BC_ASSERT(stranded());
43 ✔
647

648
    if (websocket())
43 ✔
649
    {
650
        // Body reader processes websocket read just like
651
        // http read but without headers. Expected body type
652
        // (parser) must be preselected on request object.
653
        body_read(buffer, request, move_copy(handler));
25 ✔
654
    }
655
    else
656
    {
657
        const auto parser = to_shared<http_parser>();
18 ✔
658
        parser->body_limit(maximum_);
18 ✔
659
        parser->header_limit(header_);
18 ✔
660
        do_parse(std::ref(buffer), parser, parse_mode::all, zero,
18 ✔
661
            std::bind(&socket::handle_http_read,
36 ✔
662
                shared_from_this(), _1, _2, std::ref(request), parser,
36 ✔
663
                handler));
664
    }
18 ✔
665
}
43 ✔
666

667
void socket::async_write_http(http::response&& response,
51 ✔
668
    const count_handler& handler) NOEXCEPT
669
{
670
    BC_ASSERT(stranded());
51 ✔
671
    assign_buffer(response.body());
51 ✔
672

673
    if (websocket() || downgraded())
51 ✔
674
    {
675
        // Body writer processes websocket or tcp write just like
676
        // http write but without headers. Expected body type
677
        // (serializer) is determined by caller-assigned body.
678
        body_write(std::move(response), move_copy(handler));
10 ✔
679
    }
680
    else
681
    {
682
        const auto out = emplace_shared<serialize_state<http_serializer>>(
683
            std::move(response));
41 ✔
684

685
        do_serialize(out, zero,
41 ✔
686
            std::bind(&socket::handle_http_write,
82 ✔
687
                shared_from_this(), _1, _2, handler));
82 ✔
688
    }
41 ✔
689
}
51 ✔
690

691
// Handlers.
692
// ----------------------------------------------------------------------------
693

694
// private
695
void socket::handle_async(const code& ec, size_t size,
76,310 ✔
696
    const count_handler& handler, const std::string& operation) NOEXCEPT
697
{
698
    BC_ASSERT(stranded());
76,310 ✔
699

700
    if (ec == error::operation_canceled)
76,310 ✔
701
    {
702
        handler(error::channel_stopped, size);
28 ✔
703
        return;
28 ✔
704
    }
705

706

707
    if (ec == error::unknown)
76,282 ✔
UNCOV
708
        logx(operation, ec);
×
709

710
    handler(ec, size);
76,282 ✔
711
}
712

713
// Logging.
714
// ----------------------------------------------------------------------------
715
// private
716

UNCOV
717
void socket::logx(const std::string& context,
×
718
    const code& ec) const NOEXCEPT
719
{
UNCOV
720
    LOGX("Socket " << context << " error (" << ec.value() << ") "
×
721
        << ec.category().name() << " : " << ec.message());
UNCOV
722
}
×
723

724
BC_POP_WARNING()
725
BC_POP_WARNING()
726
BC_POP_WARNING()
727

728
} // namespace network
729
} // 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