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

libbitcoin / libbitcoin-server / 44691724904

20 Nov 2026 06:59AM UTC coverage: 95.15% (+0.05%) from 95.102%
44691724904

push

github

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

Fix test, disable admin channel inactivity and expiration by default.

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

122 existing lines in 14 files now uncovered.

12792 of 13444 relevant lines covered (95.15%)

445.71 hits per line

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

88.07
/src/protocols/protocol_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/server/protocols/protocol_http.hpp>
20

21
#include <bitcoin/server/define.hpp>
22

23
namespace libbitcoin {
24
namespace server {
25

26
#define CLASS protocol_http
27

28
using namespace network::http;
29
using namespace std::placeholders;
30

31
// Shared pointers required in handler parameters so closures control lifetime.
32
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
33
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
34

35
// Websocket dispatch.
36
// ----------------------------------------------------------------------------
37

38
void protocol_http::handle_receive_websocket(const code& ec,
12,429 ✔
39
    const method::websocket::cptr& websocket) NOEXCEPT
40
{
41
    BC_ASSERT(stranded());
12,429 ✔
42

43
    if (stopped(ec))
12,429 ✔
44
        return;
45

46
    dispatch_websocket(*websocket);
2,444 ✔
47
}
48

49
void protocol_http::handle_receive_stream(const code& ec,
11,367 ✔
50
    const method::stream::cptr& stream) NOEXCEPT
51
{
52
    BC_ASSERT(stranded());
11,367 ✔
53

54
    if (stopped(ec))
11,367 ✔
55
        return;
56

57
    dispatch_websocket(*stream);
1,382 ✔
58
}
59

60
void protocol_http::dispatch_websocket(const request& ) NOEXCEPT
×
61
{
62
    BC_ASSERT(stranded());
×
63
    stop(error::not_implemented);
×
64
}
×
65

66
// Caching for http response abstraction.
67
// ----------------------------------------------------------------------------
68

69
// Cache request for serialization, keeping it out of dispatch.
70
void protocol_http::set_request(const request_cptr& request) NOEXCEPT
1,248 ✔
71
{
72
    ////BC_ASSERT(stranded());
73
    BC_ASSERT(request);
1,248 ✔
74
    request_ = request;
1,248 ✔
75
}
1,248 ✔
76

77
/// Returns default if not set, as ws frames and a tcp downgrade carry no http
78
// envelope (asserts correctness otherwise).
79
request_cptr protocol_http::reset_request() NOEXCEPT
2,130 ✔
80
{
81
    ////BC_ASSERT(stranded());
82
    BC_ASSERT(request_ || websocket() || downgraded());
2,130 ✔
83

84
    if (request_)
2,130 ✔
85
    {
86
        auto copy = request_;
1,238 ✔
87
        request_.reset();
1,238 ✔
88
        return copy;
1,238 ✔
89
    }
1,238 ✔
90

91
    return system::to_shared<request>();
892 ✔
92
}
93

94
const request_cptr& protocol_http::get_request() const NOEXCEPT
730 ✔
95
{
96
    ////BC_ASSERT(stranded());
97
    return request_;
730 ✔
98
}
99

100
response protocol_http::to_response(status status) NOEXCEPT
1,957 ✔
101
{
102
    BC_ASSERT(stranded());
1,957 ✔
103
    const auto request = reset_request();
1,957 ✔
104
    response message{ status, request->version() };
1,957 ✔
105
    add_common_headers(message, *request);
1,957 ✔
106
    add_access_control_headers(message, *request);
1,957 ✔
107
    add_coding_headers(message, *request);
1,957 ✔
108

109
    return message;
1,957 ✔
110
}
1,957 ✔
111

112
// Senders.
113
// ----------------------------------------------------------------------------
114

115
void protocol_http::send_json(system::json::value&& model) NOEXCEPT
187 ✔
116
{
117
    BC_ASSERT(stranded());
187 ✔
118
    auto message = to_response(status::ok);
187 ✔
119
    message.set(field::content_type,
187 ✔
120
        from_media_type(media_type::application_json));
374 ✔
121
    message.body() = json_value{ .model = std::move(model) };
187 ✔
122
    message.prepare_payload();
187 ✔
123
    SEND(std::move(message), handle_complete, _1, error::success);
187 ✔
124
}
187 ✔
125

126
void protocol_http::send_text(std::string&& text) NOEXCEPT
72 ✔
127
{
128
    BC_ASSERT(stranded());
72 ✔
129
    auto message = to_response(status::ok);
72 ✔
130
    message.set(field::content_type, from_media_type(media_type::text_plain));
72 ✔
131
    message.body() = std::move(text);
72 ✔
132
    message.prepare_payload();
72 ✔
133
    SEND(std::move(message), handle_complete, _1, error::success);
72 ✔
134
}
72 ✔
135

136
void protocol_http::send_chunk(system::data_chunk&& bytes) NOEXCEPT
61 ✔
137
{
138
    BC_ASSERT(stranded());
61 ✔
139
    auto message = to_response(status::ok);
61 ✔
140
    message.set(field::content_type,
61 ✔
141
        from_media_type(media_type::application_octet_stream));
122 ✔
142
    message.body() = std::move(bytes);
61 ✔
143
    message.prepare_payload();
61 ✔
144
    SEND(std::move(message), handle_complete, _1, error::success);
61 ✔
145
}
61 ✔
146

147
void protocol_http::send_file(file&& file, media_type type) NOEXCEPT
3 ✔
148
{
149
    BC_ASSERT(stranded());
3 ✔
150
    BC_ASSERT_MSG(file.is_open(), "sending closed file handle");
3 ✔
151
    auto message = to_response(status::ok);
3 ✔
152
    message.set(field::content_type, from_media_type(type));
3 ✔
153
    message.body() = std::move(file);
3 ✔
154
    message.prepare_payload();
3 ✔
155
    SEND(std::move(message), handle_complete, _1, error::success);
3 ✔
156
}
3 ✔
157

158
void protocol_http::send_span(span_body::value_type&& span,
×
159
    media_type type) NOEXCEPT
160
{
161
    BC_ASSERT(stranded());
×
162
    auto message = to_response(status::ok);
×
163
    message.set(field::content_type, from_media_type(type));
×
164
    message.body() = std::move(span);
×
165
    message.prepare_payload();
×
166
    SEND(std::move(message), handle_complete, _1, error::success);
×
167
}
×
168

169
void protocol_http::send_page(span_body::value_type&& span, media_type type,
8 ✔
170
    const std::string& tag) NOEXCEPT
171
{
172
    BC_ASSERT(stranded());
8 ✔
173
    auto message = to_response(status::ok);
8 ✔
174
    message.set(field::content_type, from_media_type(type));
8 ✔
175
    message.set(field::etag, tag);
8 ✔
176
    message.set(field::cache_control, "no-cache");
8 ✔
177
    message.body() = std::move(span);
8 ✔
178
    message.prepare_payload();
8 ✔
179
    SEND(std::move(message), handle_complete, _1, error::success);
8 ✔
180
}
8 ✔
181

182
void protocol_http::send_not_modified(const std::string& tag) NOEXCEPT
5 ✔
183
{
184
    BC_ASSERT(stranded());
5 ✔
185
    auto message = to_response(status::not_modified);
5 ✔
186
    message.set(field::etag, tag);
5 ✔
187
    message.set(field::cache_control, "no-cache");
5 ✔
188
    message.body() = empty_value{};
5 ✔
189
    message.prepare_payload();
5 ✔
190
    SEND(std::move(message), handle_complete, _1, error::success);
5 ✔
191
}
5 ✔
192

193
void protocol_http::send_buffer(buffer_body::value_type&& buffer,
×
194
    media_type type) NOEXCEPT
195
{
196
    BC_ASSERT(stranded());
×
197
    auto message = to_response(status::ok);
×
198
    message.set(field::content_type, from_media_type(type));
×
199
    message.body() = std::move(buffer);
×
200
    message.prepare_payload();
×
201
    SEND(std::move(message), handle_complete, _1, error::success);
×
202
}
×
203

204
void protocol_http::send_empty() NOEXCEPT
6 ✔
205
{
206
    BC_ASSERT(stranded());
6 ✔
207
    auto message = to_response(status::ok);
6 ✔
208
    message.body() = empty_value{};
6 ✔
209
    message.prepare_payload();
6 ✔
210
    SEND(std::move(message), handle_complete, _1, error::success);
6 ✔
211
}
6 ✔
212

213
void protocol_http::send_stream(size_t size, wire_writer&& writer,
20 ✔
214
    media_type type) NOEXCEPT
215
{
216
    BC_ASSERT(stranded());
20 ✔
217
    auto message = to_response(status::ok);
20 ✔
218
    message.set(field::content_type, from_media_type(type));
20 ✔
219
    message.body() = stream_value
20 ✔
220
    {
221
        .write = std::move(writer),
20 ✔
222
        .size = size
223
    };
20 ✔
224
    message.prepare_payload();
20 ✔
225
    SEND(std::move(message), handle_stream, _1);
20 ✔
226
}
20 ✔
227

228
// Error senders.
229
// ----------------------------------------------------------------------------
230

231
void protocol_http::send_bad_request() NOEXCEPT
24 ✔
232
{
233
    send_bad_request(*reset_request());
24 ✔
234
}
24 ✔
235

236
void protocol_http::send_not_found() NOEXCEPT
92 ✔
237
{
238
    send_not_found(*reset_request());
92 ✔
239
}
92 ✔
240

241
void protocol_http::send_not_acceptable() NOEXCEPT
10 ✔
242
{
243
    send_not_acceptable(*reset_request());
10 ✔
244
}
10 ✔
245

246
void protocol_http::send_forbidden() NOEXCEPT
10 ✔
247
{
248
    send_forbidden(*reset_request());
10 ✔
249
}
10 ✔
250

251
void protocol_http::send_not_implemented() NOEXCEPT
13 ✔
252
{
253
    send_not_implemented(*reset_request());
13 ✔
254
}
13 ✔
255

256
void protocol_http::send_method_not_allowed() NOEXCEPT
3 ✔
257
{
258
    send_method_not_allowed(*reset_request());
3 ✔
259
}
3 ✔
260

261
void protocol_http::send_service_unavailable() NOEXCEPT
6 ✔
262
{
263
    send_service_unavailable(*reset_request());
6 ✔
264
}
6 ✔
265

266
void protocol_http::send_internal_server_error(const code& reason) NOEXCEPT
10 ✔
267
{
268
    send_internal_server_error(reason, *reset_request());
10 ✔
269
}
10 ✔
270

271
void protocol_http::send_bad_request(const request& request) NOEXCEPT
28 ✔
272
{
273
    network::protocol_http::send_bad_request(request);
28 ✔
274
}
28 ✔
275

276
void protocol_http::send_not_found(const request& request) NOEXCEPT
93 ✔
277
{
278
    network::protocol_http::send_not_found(request);
93 ✔
279
}
93 ✔
280

281
void protocol_http::send_not_acceptable(const request& request) NOEXCEPT
12 ✔
282
{
283
    network::protocol_http::send_not_acceptable(request);
12 ✔
284
}
12 ✔
285

286
void protocol_http::send_forbidden(const request& request) NOEXCEPT
21 ✔
287
{
288
    network::protocol_http::send_forbidden(request);
21 ✔
289
}
21 ✔
290

291
void protocol_http::send_not_implemented(const request& request) NOEXCEPT
13 ✔
292
{
293
    network::protocol_http::send_not_implemented(request);
13 ✔
294
}
13 ✔
295

296
void protocol_http::send_method_not_allowed(const request& request) NOEXCEPT
5 ✔
297
{
298
    network::protocol_http::send_method_not_allowed(request);
5 ✔
299
}
5 ✔
300

301
void protocol_http::send_service_unavailable(const request& request) NOEXCEPT
6 ✔
302
{
303
    network::protocol_http::send_service_unavailable(request);
6 ✔
304
}
6 ✔
305

306
void protocol_http::send_internal_server_error(const code& reason,
10 ✔
307
    const request& request) NOEXCEPT
308
{
309
    network::protocol_http::send_internal_server_error(reason, request);
10 ✔
310
}
10 ✔
311

312
// A stream body write fails as unexpected only when its writer fails.
313
void protocol_http::handle_stream(const code& ec) NOEXCEPT
20 ✔
314
{
315
    BC_ASSERT(stranded());
20 ✔
316
    if (ec == network::error::unexpected_body)
20 ✔
UNCOV
317
        fault(database::error::integrity);
×
318

319
    handle_complete(ec, network::error::success);
20 ✔
320
}
20 ✔
321

322
BC_POP_WARNING()
323
BC_POP_WARNING()
324

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