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

libbitcoin / libbitcoin-server / 43765399729

15 Nov 2026 09:44PM UTC coverage: 95.2% (-0.04%) from 95.241%
43765399729

push

github

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

http compression updates, embedded page cache tagging.

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

9 existing lines in 1 file now uncovered.

12476 of 13105 relevant lines covered (95.2%)

433.19 hits per line

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

87.65
/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,024 ✔
39
    const method::websocket::cptr& websocket) NOEXCEPT
40
{
41
    BC_ASSERT(stranded());
12,024 ✔
42

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

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

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

54
    if (stopped(ec))
10,957 ✔
55
        return;
56

57
    dispatch_websocket(*stream);
1,376 ✔
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,167 ✔
71
{
72
    ////BC_ASSERT(stranded());
73
    BC_ASSERT(request);
1,167 ✔
74
    request_ = request;
1,167 ✔
75
}
1,167 ✔
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,044 ✔
80
{
81
    ////BC_ASSERT(stranded());
82
    BC_ASSERT(request_ || websocket() || downgraded());
2,044 ✔
83

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

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

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

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

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

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

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

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

136
void protocol_http::send_chunk(system::data_chunk&& bytes) NOEXCEPT
52 ✔
137
{
138
    BC_ASSERT(stranded());
52 ✔
139
    auto message = to_response(status::ok);
52 ✔
140
    message.set(field::content_type,
52 ✔
141
        from_media_type(media_type::application_octet_stream));
104 ✔
142
    message.body() = std::move(bytes);
52 ✔
143
    message.prepare_payload();
52 ✔
144
    SEND(std::move(message), handle_complete, _1, error::success);
52 ✔
145
}
52 ✔
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

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

UNCOV
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,
19 ✔
214
    media_type type) NOEXCEPT
215
{
216
    BC_ASSERT(stranded());
19 ✔
217
    auto message = to_response(status::ok);
19 ✔
218
    message.set(field::content_type, from_media_type(type));
19 ✔
219
    message.body() = stream_value
19 ✔
220
    {
221
        .write = std::move(writer),
19 ✔
222
        .size = size
223
    };
19 ✔
224
    message.prepare_payload();
19 ✔
225
    SEND(std::move(message), handle_stream, _1);
19 ✔
226
}
19 ✔
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
80 ✔
237
{
238
    send_not_found(*reset_request());
80 ✔
239
}
80 ✔
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
10 ✔
252
{
253
    send_not_implemented(*reset_request());
10 ✔
254
}
10 ✔
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_internal_server_error(const code& reason) NOEXCEPT
10 ✔
262
{
263
    send_internal_server_error(reason, *reset_request());
10 ✔
264
}
10 ✔
265

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

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

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

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

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

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

296
void protocol_http::send_internal_server_error(const code& reason,
10 ✔
297
    const request& request) NOEXCEPT
298
{
299
    network::protocol_http::send_internal_server_error(reason, request);
10 ✔
300
}
10 ✔
301

302
// A stream body write fails as unexpected only when its writer fails.
303
void protocol_http::handle_stream(const code& ec) NOEXCEPT
19 ✔
304
{
305
    BC_ASSERT(stranded());
19 ✔
306
    if (ec == network::error::unexpected_body)
19 ✔
307
        fault(database::error::integrity);
×
308

309
    handle_complete(ec, network::error::success);
19 ✔
310
}
19 ✔
311

312
BC_POP_WARNING()
313
BC_POP_WARNING()
314

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