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

libbitcoin / libbitcoin-network / 45941322671

27 Nov 2026 04:40AM UTC coverage: 94.333% (+0.02%) from 94.31%
45941322671

push

github

web-flow
Merge pull request #1066 from echennells/fix/upgrade-policy

Apply host and origin policy to websocket upgrade.

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

1 existing line in 1 file now uncovered.

17396 of 18441 relevant lines covered (94.33%)

1978.44 hits per line

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

99.25
/src/net/proxy.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/proxy.hpp>
20

21
#include <bitcoin/network/define.hpp>
22
#include <bitcoin/network/log/log.hpp>
23
#include <bitcoin/network/net/socket.hpp>
24

25
namespace libbitcoin {
26
namespace network {
27

28
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
29

30
// Factory for variable deadline timer pointer construction (or null).
31
inline deadline::ptr make_throttle(const logger& log, io::strand& strand,
338 ✔
32
    uint32_t rate_limit) NOEXCEPT
33
{
34
    return to_bool(rate_limit) ?
338 ✔
35
        system::emplace_shared<deadline>(log, strand) : nullptr;
338 ✔
36
}
37

38
// Created in started state and must be stopped, as subscribers assert if not
39
// stopped. Subscribers may hold protocols even if service not started.
40
proxy::proxy(const socket::ptr& socket, uint32_t rate_limit,
338 ✔
41
    size_t maximum_backlog) NOEXCEPT
338 ✔
42
  : reporter(socket->log),
338 ✔
43
    rate_limit_(rate_limit),
338 ✔
44
    maximum_backlog_(maximum_backlog),
338 ✔
45
    socket_(socket),
338 ✔
46
    throttle_(make_throttle(socket->log, socket->strand(), rate_limit))
676 ✔
47
{
48
}
338 ✔
49

50
proxy::~proxy() NOEXCEPT
338 ✔
51
{
52
    BC_ASSERT_MSG(stopped(), "proxy is not stopped");
338 ✔
53
    if (!stopped()) { LOGF("~proxy is not stopped."); }
338 ✔
54
}
338 ✔
55

56
// Stop (socket/proxy started upon create).
57
// ----------------------------------------------------------------------------
58
// Proxy does not (must not) stop itself.
59

60
void proxy::stop(const code& ec) NOEXCEPT
501 ✔
61
{
62
    if (stopped())
501 ✔
63
    {
64
        // Hard stop ends outstanding lazy stop, otherwise nothing.
65
        if (!lazy(ec))
163 ✔
66
            socket_->stop();
159 ✔
67

68
        return;
163 ✔
69
    }
70

71
    // Open batch response closed (written) before socket stops. Batch state
72
    // protected by strand, so only stranded stop can close gracefully
73
    // (unstranded stop, e.g. shutdown, is immediate).
74
    if (stranded() && batched_ && parted_)
338 ✔
75
    {
76
        do_stop(ec);
3 ✔
77
        return;
3 ✔
78
    }
79

80
    finish_stop(ec);
335 ✔
81
}
82

83
// private
84
// Close open batch response, then stop (batch open rides on first part, so
85
// nothing to close unless parted).
86
void proxy::do_stop(const code& ec) NOEXCEPT
3 ✔
87
{
88
    BC_ASSERT(stranded());
3 ✔
89
    using namespace std::placeholders;
90

91
    // Socket not yet stopped, so deferred send still holds queue. Release it
92
    // here so close part is not delayed by throttle.
93
    if (throttle_) throttle_->stop();
3 ✔
94

95
    // This is the final message of the channel.
96
    queue_close(std::bind(&proxy::handle_stop_write,
3 ✔
97
        shared_from_this(), _1, _2, ec));
6 ✔
98
}
3 ✔
99

100
// private
101
void proxy::handle_stop_write(const code&, size_t,
3 ✔
102
    const code& reason) NOEXCEPT
103
{
104
    BC_ASSERT(stranded());
3 ✔
105
    finish_stop(reason);
3 ✔
106
}
3 ✔
107

108
// private
109
// Thread safe (called directly from stop or from the strand).
110
void proxy::finish_stop(const code& ec) NOEXCEPT
338 ✔
111
{
112
    // Stop read loop, stop accepting new work, cancel pending work.
113
    // Graceful close issues follow-on iocontext jobs, so it is not performed
114
    // within socket::stop(), and subsequent socket::stop() terminates it.
115
    if (lazy(ec))
338 ✔
116
        socket_->lazy_stop();
83 ✔
117
    else
118
        socket_->stop();
255 ✔
119

120
    io::post(strand(),
338 ✔
121
        std::bind(&proxy::stopping, shared_from_this(), ec));
676 ✔
122
}
338 ✔
123

124
// A stop for a failed or canceled transport has no peer to close with.
125
bool proxy::lazy(const code& ec) const NOEXCEPT
241 ✔
126
{
127
    return !(ec == error::service_stopped
241 ✔
128
        || ec == error::channel_stopped
106 ✔
129
        || ec == error::operation_canceled
95 ✔
130
        || ec == error::operation_failed
95 ✔
131
        || ec == error::operation_timeout
94 ✔
132
        || ec == error::peer_disconnect
94 ✔
133
        || ec == error::bad_stream
92 ✔
134
        || ec == error::end_of_stream
89 ✔
135
        || ec == error::partial_message
89 ✔
136
        || ec == error::short_read
89 ✔
137
        || ec == error::tls_stream_truncated);
89 ✔
138
}
139

140
// protected
141
// Should not be called internally (invoked by stop()).
142
void proxy::stopping(const code& ec) NOEXCEPT
338 ✔
143
{
144
    BC_ASSERT(stranded());
338 ✔
145

146
    // Release any deferred send (fires pending charge with canceled).
147
    if (throttle_) throttle_->stop();
338 ✔
148

149
    // Release any http message parse in progress.
150
    parser_.reset();
338 ✔
151

152
    // Release queued writes, handlers are not invoked after stop.
153
    queue_.clear();
338 ✔
154
    deferred_.clear();
338 ✔
155

156
    // Post stop handlers to strand and clear/stop accepting subscriptions.
157
    // Code provides information on reason channel stopped.
158
    stop_subscriber_.stop(ec);
338 ✔
159
}
338 ✔
160

161
proxy::object_key proxy::subscribe_stop(result_handler&& handler) NOEXCEPT
452 ✔
162
{
163
    BC_ASSERT(stranded());
452 ✔
164
    using namespace std::placeholders;
165

166
    const auto key = create_key();
452 ✔
167
    stop_subscriber_.subscribe(
452 ✔
168
        std::bind(&proxy::handle_stop, _1, std::move(handler)), key);
904 ✔
169

170
    return key;
452 ✔
171
}
172

173
// private
174
bool proxy::handle_stop(const code& ec,
452 ✔
175
    const result_handler& handler) NOEXCEPT
176
{
177
    handler(ec);
452 ✔
178
    return false;
452 ✔
179
}
180

181
bool proxy::unsubscribe_stop(object_key key) NOEXCEPT
2 ✔
182
{
183
    BC_ASSERT(stranded());
2 ✔
184
    return stop_subscriber_.notify_one(key, error::desubscribed);
2 ✔
185
}
186

187
void proxy::subscribe_stop(result_handler&& handler,
183 ✔
188
    result_handler&& complete) NOEXCEPT
189
{
190
    if (stopped())
183 ✔
191
    {
192
        complete(error::channel_stopped);
5 ✔
193
        handler(error::channel_stopped);
5 ✔
194
        return;
5 ✔
195
    }
196

197
    io::dispatch(strand(),
178 ✔
198
        std::bind(&proxy::do_subscribe_stop,
356 ✔
199
            shared_from_this(), std::move(handler), std::move(complete)));
356 ✔
200
}
201

202
// private
203
void proxy::do_subscribe_stop(const result_handler& handler,
178 ✔
204
    const result_handler& complete) NOEXCEPT
205
{
206
    BC_ASSERT(stranded());
178 ✔
207
    subscribe_stop(move_copy(handler));
178 ✔
208
    complete(error::success);
178 ✔
209
}
178 ✔
210

211
// private
212
proxy::object_key proxy::create_key() NOEXCEPT
452 ✔
213
{
214
    BC_ASSERT(stranded());
452 ✔
215

216
    if (is_zero(++keys_))
452 ✔
217
    {
UNCOV
218
        BC_ASSERT_MSG(false, "overflow");
×
219
        LOGF("Stop subscriber key overflow.");
220
    }
221

222
    return keys_;
452 ✔
223
}
224

225
// Signal activity.
226
// ----------------------------------------------------------------------------
227
// override reading() to update timers.
228

229
// protected
230
void proxy::reading() NOEXCEPT
54 ✔
231
{
232
}
54 ✔
233

234
// protected
235
void proxy::writing() NOEXCEPT
50 ✔
236
{
237
}
50 ✔
238

239
// private
240
void proxy::do_reading() NOEXCEPT
546 ✔
241
{
242
    io::dispatch(strand(),
546 ✔
243
        std::bind(&proxy::reading, shared_from_this()));
1,092 ✔
244
}
546 ✔
245

246
// Properties.
247
// ----------------------------------------------------------------------------
248

249
io::context& proxy::service() const NOEXCEPT
1 ✔
250
{
251
    return socket_->service();
1 ✔
252
}
253

254
io::strand& proxy::strand() NOEXCEPT
2,527 ✔
255
{
256
    return socket_->strand();
2,527 ✔
257
}
258

259
bool proxy::stranded() const NOEXCEPT
20,253 ✔
260
{
261
    return socket_->stranded();
20,253 ✔
262
}
263

264
bool proxy::stopped() const NOEXCEPT
7,461 ✔
265
{
266
    return socket_->stopped();
7,461 ✔
267
}
268

269
bool proxy::inbound() const NOEXCEPT
792 ✔
270
{
271
    return socket_->inbound();
792 ✔
272
}
273

274
bool proxy::secure() const NOEXCEPT
50 ✔
275
{
276
    return socket_->secure();
50 ✔
277
}
278

279
bool proxy::encrypted() const NOEXCEPT
106 ✔
280
{
281
    return socket_->encrypted();
106 ✔
282
}
283

284
bool proxy::websocket() const NOEXCEPT
272 ✔
285
{
286
    return socket_->websocket();
272 ✔
287
}
288

289
bool proxy::downgraded() const NOEXCEPT
48 ✔
290
{
291
    return socket_->downgraded();
48 ✔
292
}
293

294
bool proxy::detected() const NOEXCEPT
3 ✔
295
{
296
    return socket_->detected();
3 ✔
297
}
298

299
code proxy::accept_websocket(const http::request& request) NOEXCEPT
29 ✔
300
{
301
    return socket_->accept_websocket(request);
29 ✔
302
}
303

304
uint64_t proxy::received() const NOEXCEPT
84 ✔
305
{
306
    return received_.load();
84 ✔
307
}
308

309
uint64_t proxy::sent() const NOEXCEPT
84 ✔
310
{
311
    return sent_.load();
84 ✔
312
}
313

314
const config::address& proxy::address() const NOEXCEPT
123 ✔
315
{
316
    return socket_->address();
123 ✔
317
}
318

319
const config::endpoint& proxy::endpoint() const NOEXCEPT
321 ✔
320
{
321
    return socket_->endpoint();
321 ✔
322
}
323

324
const config::endpoint& proxy::binding() const NOEXCEPT
4 ✔
325
{
326
    return socket_->binding();
4 ✔
327
}
328

329
size_t proxy::slot() const NOEXCEPT
3 ✔
330
{
331
    return socket_->slot();
3 ✔
332
}
333

334
BC_POP_WARNING()
335

336
} // namespace network
337
} // 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