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

libbitcoin / libbitcoin-server / 44866815011

21 Nov 2026 04:34AM UTC coverage: 95.068% (+0.02%) from 95.044%
44866815011

push

github

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

Ignore SIGPIPE so a closed output reader does not end the process.

69 of 69 new or added lines in 4 files covered. (100.0%)

1 existing line in 1 file now uncovered.

12895 of 13564 relevant lines covered (95.07%)

449.98 hits per line

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

93.81
/src/protocols/admin/protocol_admin.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/server/protocols/protocol_admin.hpp>
20

21
#include <atomic>
22
#include <chrono>
23
#include <bitcoin/server/define.hpp>
24
#include <bitcoin/server/parsers/parsers.hpp>
25
#include <bitcoin/server/serializers/serializers.hpp>
26
#include <bitcoin/server/utilities/utilities.hpp>
27

28
namespace libbitcoin {
29
namespace server {
30

31
using namespace system;
32
using namespace network;
33
using namespace network::http;
34
using namespace std::placeholders;
35
constexpr auto relaxed = std::memory_order_relaxed;
36

37
#define CLASS protocol_admin
38
#define SUBSCRIBE_ADMIN(method, ...) \
39
    subscribe<CLASS>(&CLASS::method, __VA_ARGS__)
40

41
// Limited by json number domain and sentinel bit.
42
static_assert(node::events::unknown < 53u);
43
static_assert(network::levels::verbose < 53u);
44

45
// Start.
46
// ----------------------------------------------------------------------------
47

48
void protocol_admin::start() NOEXCEPT
34 ✔
49
{
50
    BC_ASSERT(stranded());
34 ✔
51

52
    if (started())
34 ✔
53
        return;
54

55
    // Subscription methods.
56
    SUBSCRIBE_ADMIN(handle_get_log_subscribe, _1, _2, _3, _4);
34 ✔
57
    SUBSCRIBE_ADMIN(handle_get_event_subscribe, _1, _2, _3, _4);
34 ✔
58
    protocol_native::start();
34 ✔
59
}
60

61
void protocol_admin::stopping(const code& ec) NOEXCEPT
34 ✔
62
{
63
    BC_ASSERT(stranded());
34 ✔
64
    log_interest_->set(zero);
34 ✔
65
    admin_dispatcher_.stop(ec);
34 ✔
66
    protocol_native::stopping(ec);
34 ✔
67
}
34 ✔
68

69
// Dispatch.
70
// ----------------------------------------------------------------------------
71

72
bool protocol_admin::try_dispatch_object(const http::request& request) NOEXCEPT
22 ✔
73
{
74
    BC_ASSERT(stranded());
22 ✔
75

76
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
77
    const auto target = request.target();
22 ✔
78
    BC_POP_WARNING()
79

80
    rpc::request_t model{};
22 ✔
81
    if (const auto ec = admin_target(model, target))
22 ✔
82
    {
83
        // An unrecognized admin target may be a native interface request.
84
        if (ec == error::invalid_target)
6 ✔
85
            return protocol_native::try_dispatch_object(request);
6 ✔
86

87
        // A target without a version or requesting html is a page request.
88
        if (ec == error::missing_version || ec == error::empty_path ||
2 ✔
89
            is_page_request(request))
1 ✔
UNCOV
90
            return false;
×
91

92
        send_bad_target(ec, *reset_request());
1 ✔
93
        return true;
1 ✔
94
    }
95

96
    // No media defaults injected for an http request.
97
    if (!admin_query(model, request))
16 ✔
98
    {
99
        send_bad_request();
4 ✔
100
        return true;
101
    }
102

103
    const auto media = strip_media(model);
12 ✔
104
    if (media == media_type::unknown)
12 ✔
105
    {
106
        send_not_acceptable();
1 ✔
107
        return true;
108
    }
109

110
    // Falls through to html page dispatch.
111
    if (media == media_type::text_html)
11 ✔
112
        return false;
113

114
    // The credential may be restricted to a subset of interface methods.
115
    if (!permitted(model.method))
10 ✔
116
    {
117
        send_forbidden();
1 ✔
118
        return true;
119
    }
120

121
    if (admin_dispatcher_.notify(model))
9 ✔
122
        send_bad_request();
×
123

124
    return true;
125
}
22 ✔
126

127
void protocol_admin::dispatch_websocket(const http::request& request) NOEXCEPT
20 ✔
128
{
129
    BC_ASSERT(stranded());
20 ✔
130

131
    // ws frames carry no headers, so ws authorization is enforced here, not
132
    // by the channel (established by basic auth on the upgrade request).
133
    if (!authorized())
20 ✔
134
    {
135
        send_unauthorized(request);
×
136
        return;
6 ✔
137
    }
138

139
    // Admin websocket interface supports only string requests.
140
    if (!request.body().contains<http::string_value>())
20 ✔
141
    {
142
        send_not_acceptable(request);
×
143
        return;
×
144
    }
145

146
    // Target with query string is passed via websocket string body.
147
    const auto target = request.body().get<http::string_value>();
20 ✔
148

149
    rpc::request_t model{};
20 ✔
150
    if (const auto ec = admin_target(model, target))
20 ✔
151
    {
152
        // An unrecognized admin target may be a native interface request.
153
        if (ec == error::invalid_target)
4 ✔
154
            protocol_native::dispatch_websocket(request);
3 ✔
155
        else
156
            send_bad_target(ec, request);
1 ✔
157

158
        return;
4 ✔
159
    }
160

161
    // Default to json by simulating a json accept header (format overrides).
162
    if (!admin_query(model, target, { media_type::application_json }))
16 ✔
163
    {
164
        send_bad_request(request);
1 ✔
165
        return;
166
    }
167

168
    if (strip_media(model) != media_type::application_json)
15 ✔
169
    {
170
        send_not_acceptable(request);
1 ✔
171
        return;
172
    }
173

174
    // The credential may be restricted to a subset of interface methods.
175
    if (!permitted(model.method))
14 ✔
176
    {
177
        send_forbidden(request);
×
178
        return;
179
    }
180

181
    if (admin_dispatcher_.notify(model))
14 ✔
182
        send_bad_request(request);
×
183
}
20 ✔
184

185
// Interface handlers.
186
// ----------------------------------------------------------------------------
187

188
bool protocol_admin::handle_get_log_subscribe(const code& ec,
49 ✔
189
    admin_interface::log_subscribe, uint8_t /*version*/,
190
    uint64_t filter) NOEXCEPT
191
{
192
    BC_ASSERT(stranded());
49 ✔
193

194
    if (stopped(ec))
49 ✔
195
        return false;
196

197
    // log outlives node, so cannot hold shared_ptr to the protocol.
198
    const auto previous = log_interest_->set(filter);
15 ✔
199
    if (!is_zero(filter) && !log_subscribed_)
15 ✔
200
    {
201
        auto handler = BIND_WEAK(handle_log, _1, _2, _3, _4);
10 ✔
202
        log.subscribe_messages(log_interest_, std::move(handler));
10 ✔
203
        log_subscribed_ = true;
10 ✔
204
    }
10 ✔
205

206
    send_json({ { "previous", previous } });
15 ✔
207
    return true;
15 ✔
208
}
209

210
bool protocol_admin::handle_get_event_subscribe(const code& ec,
42 ✔
211
    admin_interface::event_subscribe, uint8_t /*version*/,
212
    uint64_t filter) NOEXCEPT
213
{
214
    BC_ASSERT(stranded());
42 ✔
215

216
    if (stopped(ec))
42 ✔
217
        return false;
218

219
    // log outlives node, so cannot hold shared_ptr to the protocol.
220
    uint64_t previous{};
8 ✔
221
    if (update_filter(previous, filter, event_state_))
8 ✔
222
        log.subscribe_events(BIND_WEAK(handle_event, _1, _2, _3, _4));
6 ✔
223

224
    send_json({ { "previous", previous } });
8 ✔
225
    return true;
8 ✔
226
}
227

228
// Event handlers.
229
// ----------------------------------------------------------------------------
230

231
bool protocol_admin::handle_log(const code& ec, uint8_t level, time_t zulu,
5 ✔
232
    const std::string& message) NOEXCEPT
233
{
234
    if (stopped(ec))
5 ✔
235
        return false;
236

237
    if (websocket())
5 ✔
238
    {
239
        POST(do_log, level, zulu * 1'000_u64, message);
4 ✔
240
    }
241

242
    return true;
243
}
244

245
bool protocol_admin::handle_event(const code& ec, uint8_t event_,
4 ✔
246
    uint64_t value, const logger::time& point) NOEXCEPT
247
{
248
    bool filtered{};
4 ✔
249
    if (stopped(ec) || !get_filter(filtered, event_, event_state_))
4 ✔
250
        return false;
1 ✔
251

252
    if (!filtered || !websocket())
3 ✔
253
        return true;
1 ✔
254

255
    POST(do_event, event_, to_zulu(point), value);
2 ✔
256
    return true;
2 ✔
257
}
258

259
// Notification event handlers.
260
// ----------------------------------------------------------------------------
261
// Posted to the protocol strand.
262

263
void protocol_admin::do_log(uint8_t level, uint64_t zulu,
4 ✔
264
    const std::string& message) NOEXCEPT
265
{
266
    BC_ASSERT(stranded() && websocket());
4 ✔
267

268
    if (stopped())
4 ✔
269
        return;
270

271
    notify_json(
4 ✔
272
    {
273
        { "level", level },
274
        { "time", zulu },
275
        { "message", message }
276
    });
277
}
278

279
void protocol_admin::do_event(uint8_t event_, uint64_t zulu,
2 ✔
280
    uint64_t value_) NOEXCEPT
281
{
282
    BC_ASSERT(stranded() && websocket());
2 ✔
283

284
    if (stopped())
2 ✔
285
        return;
286

287
    notify_json(
2 ✔
288
    {
289
        { "event", event_ },
290
        { "value", value_ },
291
        { "time", zulu }
292
    });
293
}
294

295
// Utils.
296
// ----------------------------------------------------------------------------
297
// static/private
298

299
// zulu time in milliseconds since epoch (native js time format).
300
uint64_t protocol_admin::to_zulu(const logger::time& point) NOEXCEPT
2 ✔
301
{
302
    using namespace std::chrono;
303
    const auto now = logger::now();
2 ✔
304
    const auto delta = duration_cast<system_clock::duration>(now - point);
2 ✔
305
    const auto time = (system_clock::now() - delta).time_since_epoch();
2 ✔
306
    return duration_cast<milliseconds>(time).count();
2 ✔
307
}
308

309
// Unsubscribe if false.
310
bool protocol_admin::get_filter(bool& filtered, size_t bit,
4 ✔
311
    filter_t& filter) NOEXCEPT
312
{
313
    auto state = filter.load(relaxed);
314
    const auto next = set_left(state, zero, false);
4 ✔
315

316
    // Empty filter unregisters, failure implies a new filter arrived.
317
    if (is_zero(next) && filter.compare_exchange_strong(state, next, relaxed))
5 ✔
318
        return false;
319

320
    filtered = get_right(state, bit);
3 ✔
321
    return true;
3 ✔
322
}
323

324
// Subscribe if true.
325
bool protocol_admin::update_filter(uint64_t& prior, uint64_t value,
8 ✔
326
    filter_t& filter) NOEXCEPT
327
{
328
    const auto next = set_left(value, zero, true);
8 ✔
329
    const auto state = is_zero(value) ? filter.fetch_and(next, relaxed) :
8 ✔
330
        filter.exchange(next, relaxed);
331

332
    prior = set_left(state, zero, false);
8 ✔
333
    return !is_zero(value) && !get_left(state);
8 ✔
334
}
335

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