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

libbitcoin / libbitcoin-server / 45096939718

23 Nov 2026 01:51AM UTC coverage: 93.468% (+0.008%) from 93.46%
45096939718

push

github

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

Updated settings, embed page fix, help text.

40 of 41 new or added lines in 7 files covered. (97.56%)

122 existing lines in 14 files now uncovered.

13436 of 14375 relevant lines covered (93.47%)

461.19 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,
13,413 ✔
39
    const method::websocket::cptr& websocket) NOEXCEPT
40
{
41
    BC_ASSERT(stranded());
13,413 ✔
42

43
    if (stopped(ec))
13,413 ✔
44
        return;
45

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

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

54
    if (stopped(ec))
12,093 ✔
55
        return;
56

57
    dispatch_websocket(*stream);
1,407 ✔
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,326 ✔
71
{
72
    BC_ASSERT(request);
1,326 ✔
73
    request_ = request;
1,326 ✔
74
}
1,326 ✔
75

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

82
    if (request_)
2,223 ✔
83
    {
84
        auto copy = request_;
1,316 ✔
85
        request_.reset();
1,316 ✔
86
        return copy;
1,316 ✔
87
    }
1,316 ✔
88

89
    return system::to_shared<request>();
907 ✔
90
}
91

92
const request_cptr& protocol_http::get_request() const NOEXCEPT
760 ✔
93
{
94
    return request_;
760 ✔
95
}
96

97
response protocol_http::to_response(status status) NOEXCEPT
2,021 ✔
98
{
99
    BC_ASSERT(stranded());
2,021 ✔
100
    const auto request = reset_request();
2,021 ✔
101
    response message{ status, request->version() };
2,021 ✔
102
    add_common_headers(message, *request);
2,021 ✔
103
    add_access_control_headers(message, *request);
2,021 ✔
104
    add_coding_headers(message, *request);
2,021 ✔
105

106
    return message;
2,021 ✔
107
}
2,021 ✔
108

109
// Senders.
110
// ----------------------------------------------------------------------------
111

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

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

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

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

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

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

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

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

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

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

225
// Error senders.
226
// ----------------------------------------------------------------------------
227

228
void protocol_http::send_bad_request() NOEXCEPT
24 ✔
229
{
230
    send_bad_request(*reset_request());
24 ✔
231
}
24 ✔
232

233
void protocol_http::send_not_found() NOEXCEPT
116 ✔
234
{
235
    send_not_found(*reset_request());
116 ✔
236
}
116 ✔
237

238
void protocol_http::send_not_acceptable() NOEXCEPT
10 ✔
239
{
240
    send_not_acceptable(*reset_request());
10 ✔
241
}
10 ✔
242

243
void protocol_http::send_forbidden() NOEXCEPT
10 ✔
244
{
245
    send_forbidden(*reset_request());
10 ✔
246
}
10 ✔
247

248
void protocol_http::send_not_implemented() NOEXCEPT
13 ✔
249
{
250
    send_not_implemented(*reset_request());
13 ✔
251
}
13 ✔
252

253
void protocol_http::send_method_not_allowed() NOEXCEPT
4 ✔
254
{
255
    send_method_not_allowed(*reset_request());
4 ✔
256
}
4 ✔
257

258
void protocol_http::send_service_unavailable() NOEXCEPT
6 ✔
259
{
260
    send_service_unavailable(*reset_request());
6 ✔
261
}
6 ✔
262

263
void protocol_http::send_internal_server_error(const code& reason) NOEXCEPT
10 ✔
264
{
265
    send_internal_server_error(reason, *reset_request());
10 ✔
266
}
10 ✔
267

268
void protocol_http::send_bad_request(const request& request) NOEXCEPT
28 ✔
269
{
270
    network::protocol_http::send_bad_request(request);
28 ✔
271
}
28 ✔
272

273
void protocol_http::send_not_found(const request& request) NOEXCEPT
117 ✔
274
{
275
    network::protocol_http::send_not_found(request);
117 ✔
276
}
117 ✔
277

278
void protocol_http::send_not_acceptable(const request& request) NOEXCEPT
12 ✔
279
{
280
    network::protocol_http::send_not_acceptable(request);
12 ✔
281
}
12 ✔
282

283
void protocol_http::send_forbidden(const request& request) NOEXCEPT
21 ✔
284
{
285
    network::protocol_http::send_forbidden(request);
21 ✔
286
}
21 ✔
287

288
void protocol_http::send_not_implemented(const request& request) NOEXCEPT
13 ✔
289
{
290
    network::protocol_http::send_not_implemented(request);
13 ✔
291
}
13 ✔
292

293
void protocol_http::send_method_not_allowed(const request& request) NOEXCEPT
6 ✔
294
{
295
    network::protocol_http::send_method_not_allowed(request);
6 ✔
296
}
6 ✔
297

298
void protocol_http::send_service_unavailable(const request& request) NOEXCEPT
6 ✔
299
{
300
    network::protocol_http::send_service_unavailable(request);
6 ✔
301
}
6 ✔
302

303
void protocol_http::send_internal_server_error(const code& reason,
10 ✔
304
    const request& request) NOEXCEPT
305
{
306
    network::protocol_http::send_internal_server_error(reason, request);
10 ✔
307
}
10 ✔
308

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

316
    handle_complete(ec, network::error::success);
24 ✔
317
}
24 ✔
318

319
BC_POP_WARNING()
320
BC_POP_WARNING()
321

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