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

ossia / score / 41341968869

01 Nov 2026 07:29PM UTC coverage: 52.268% (+0.8%) from 51.472%
41341968869

push

github

jcelerier
[http_server] quick fix for crash when setting IP address

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

155 existing lines in 8 files now uncovered.

186108 of 356067 relevant lines covered (52.27%)

310779.08 hits per line

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

12.62
/src/plugins/score-plugin-remotecontrol/RemoteControl/HttpServer/HttpServer.cpp
1
//
2
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3
//
4
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
//
7
// Official repository: https://github.com/boostorg/beast
8
//
9

10
#include "HttpServer.hpp"
11

12
#include <Library/LibrarySettings.hpp>
13

14
#include <cstdlib>
15
#include <ossia/detail/fmt.hpp>
16

17
namespace RemoteControl::HttpServer
18
{
19
HttpServer::HttpServer() { }
1,498 ✔
20

21
HttpServer::~HttpServer()
1,498 ✔
22
{
23
  stop_thread();
1,498 ✔
24
}
1,498 ✔
25

26
// Return a reasonable mime type based on the extension of a file.
27
beast::string_view HttpServer::mime_type(beast::string_view path)
×
28
{
29
  using beast::iequals;
30
  auto const ext = [&path] {
×
31
    auto const pos = path.rfind(".");
×
32
    if(pos == beast::string_view::npos)
×
33
      return beast::string_view{};
×
34
    return path.substr(pos);
×
35
  }();
×
36
  if(iequals(ext, ".htm"))
×
37
    return "text/html";
×
38
  if(iequals(ext, ".html"))
×
39
    return "text/html";
×
40
  if(iequals(ext, ".php"))
×
41
    return "text/html";
×
42
  if(iequals(ext, ".css"))
×
43
    return "text/css";
×
44
  if(iequals(ext, ".txt"))
×
45
    return "text/plain";
×
46
  if(iequals(ext, ".js"))
×
47
    return "application/javascript";
×
48
  if(iequals(ext, ".json"))
×
49
    return "application/json";
×
50
  if(iequals(ext, ".xml"))
×
51
    return "application/xml";
×
52
  if(iequals(ext, ".swf"))
×
53
    return "application/x-shockwave-flash";
×
54
  if(iequals(ext, ".wasm"))
×
55
    return "application/wasm";
×
56
  if(iequals(ext, ".flv"))
×
57
    return "video/x-flv";
×
58
  if(iequals(ext, ".png"))
×
59
    return "image/png";
×
60
  if(iequals(ext, ".jpe"))
×
61
    return "image/jpeg";
×
62
  if(iequals(ext, ".jpeg"))
×
63
    return "image/jpeg";
×
64
  if(iequals(ext, ".jpg"))
×
65
    return "image/jpeg";
×
66
  if(iequals(ext, ".gif"))
×
67
    return "image/gif";
×
68
  if(iequals(ext, ".bmp"))
×
69
    return "image/bmp";
×
70
  if(iequals(ext, ".ico"))
×
71
    return "image/vnd.microsoft.icon";
×
72
  if(iequals(ext, ".tiff"))
×
73
    return "image/tiff";
×
74
  if(iequals(ext, ".tif"))
×
75
    return "image/tiff";
×
76
  if(iequals(ext, ".svg"))
×
77
    return "image/svg+xml";
×
78
  if(iequals(ext, ".svgz"))
×
79
    return "image/svg+xml";
×
80
  return "application/text";
×
81
}
×
82

83
// Append an HTTP rel-path to a local filesystem path.
84
// The returned path is normalized for the platform.
85
std::string HttpServer::path_cat(beast::string_view path)
×
86
{
87
  std::lock_guard<std::mutex> lock{mtx};
×
88
  if(m_buildWasmPath.empty())
×
89
    return std::string(path);
×
90
  std::string result(m_buildWasmPath);
×
91
#ifdef BOOST_MSVC
92
  char constexpr path_separator = '\\';
93
  if(result.back() == path_separator)
94
    result.resize(result.size() - 1);
95
  result.append(path.data(), path.size());
96
  for(auto& c : result)
97
    if(c == '/')
98
      c = path_separator;
99
#else
100
  char constexpr path_separator = '/';
×
101
  if(result.back() == path_separator)
×
102
    result.resize(result.size() - 1);
×
103
  result.append(path.data(), path.size());
×
104
#endif
105
  return result;
×
106
}
×
107

108
// This function produces an HTTP response for the given
109
// request. The type of the response object depends on the
110
// contents of the request, so the interface requires the
111
// caller to pass a generic lambda for receiving the response.
112
template <class Body, class Allocator, class Send>
113
void HttpServer::handle_request(
×
114
    http::request<Body, http::basic_fields<Allocator>>&& req, const Send& send)
115
{
116
  using namespace score;
117

118
  // Returns a bad request response
119
  auto const bad_request = [&req](beast::string_view why) {
×
120
    http::response<http::string_body> res{http::status::bad_request, req.version()};
×
121
    res.set(http::field::server, ossia_score_verison_agent());
×
122
    res.set(http::field::content_type, "text/html");
×
123
    res.keep_alive(req.keep_alive());
×
124
    res.body() = std::string(why);
×
125
    res.prepare_payload();
×
126
    return res;
×
127
  };
×
128

129
  // Returns a not found response
130
  auto const not_found = [&req](beast::string_view target) {
×
131
    http::response<http::string_body> res{http::status::not_found, req.version()};
×
132
    res.set(http::field::server, ossia_score_verison_agent());
×
133
    res.set(http::field::content_type, "text/html");
×
134
    res.keep_alive(req.keep_alive());
×
135
    res.body() = fmt::format("The resource {} {}",
×
136
                             std::string(target),
×
137
                             "' was not found.<br> Go to the following address : http://ip_address:port/ossia_remote.html.");
138
    res.prepare_payload();
×
139
    return res;
×
140
  };
×
141

142
  // Returns a server error response
143
  auto const server_error = [&req](beast::string_view what) {
×
144
    http::response<http::string_body> res{
×
145
        http::status::internal_server_error, req.version()};
×
146
    res.set(http::field::server, ossia_score_verison_agent());
×
147
    res.set(http::field::content_type, "text/html");
×
148
    res.keep_alive(req.keep_alive());
×
149
    res.body() = "An error occurred: '" + std::string(what) + "'";
×
150
    res.prepare_payload();
×
151
    return res;
×
152
  };
×
153

154
  // Make sure we can handle the method
155
  if(req.method() != http::verb::get && req.method() != http::verb::head)
×
156
    return send(bad_request("Unknown HTTP-method"));
×
157

158
  // Request path must be absolute and not contain "..".
159
  if(req.target().empty() || req.target()[0] != '/'
×
160
     || req.target().find("..") != beast::string_view::npos)
×
161
    return send(bad_request("Illegal request-target"));
×
162

163
  // Build the path to the requested file
164
  std::string path = path_cat(req.target());
×
165

166
  // Attempt to open the file
167
  beast::error_code ec;
×
168
  http::file_body::value_type body;
×
169

170
  body.open(path.c_str(), beast::file_mode::scan, ec);
×
171

172
  // Handle the case where the file doesn't exist
173
  if(req.target().back() == '/' ||
×
174
     ec == beast::errc::no_such_file_or_directory)
×
175
    return send(not_found(req.target()));
×
176

177
  // Handle an unknown error
178
  if(ec)
×
179
    return send(server_error(ec.message()));
×
180

181
  // Cache the size since we need it after the move
182
  auto const size = body.size();
×
183

184
  // Respond to HEAD request
185
  if(req.method() == http::verb::head)
×
186
  {
187
    http::response<http::empty_body> res{http::status::ok, req.version()};
×
188
    res.set(http::field::server, ossia_score_verison_agent());
×
189
    res.set(http::field::content_type, mime_type(path));
×
190
    res.content_length(size);
×
191
    res.keep_alive(false);
×
192
    return send(std::move(res));
×
193
  }
×
194

195
  // Respond to GET request
196
  http::response<http::file_body> res{
×
197
      std::piecewise_construct, std::make_tuple(std::move(body)),
×
198
      std::make_tuple(http::status::ok, req.version())};
×
199

200
  // Allow Cross-Origin Resource Sharing (CORS).
201
  res.set(http::field::access_control_allow_headers, "*");
×
202
  res.set(http::field::access_control_allow_origin, "*");
×
203
  res.set(http::field::access_control_allow_methods, "GET");
×
204

205
  res.set(http::field::server, ossia_score_verison_agent());
×
206
  res.set(http::field::content_type, mime_type(path));
×
207
  res.content_length(size);
×
208
  res.keep_alive(false);
×
209
  return send(std::move(res));
×
210
}
×
211

212
// Report a failure
213
void HttpServer::fail(beast::error_code ec, char const* what)
×
214
{
215
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
216
  qDebug() << what << ": " << ec.message();
217
#else
218
  qDebug() << what << ": " << ec.message().c_str();
×
219
#endif
220
}
×
221

222
// Handles an HTTP server connection
223
void HttpServer::do_session(tcp::socket& socket)
×
224
{
225
  bool close = false;
×
226
  beast::error_code ec;
×
227

228
  // This buffer is required to persist across reads
229
  beast::flat_buffer buffer;
×
230

231
  // This lambda is used to send messages
232
  const auto lambda{[&close, &socket, &ec]<bool isRequest, class Body, class Fields>(
×
233
                        http::message<isRequest, Body, Fields>&& msg) {
234
    // Determine if we should close the connection after
235
    close = msg.need_eof();
×
236

237
    // We need the serializer here because the serializer requires
238
    // a non-const file_body, and the message oriented version of
239
    // http::write only works with const messages.
240
    http::serializer<isRequest, Body, Fields> sr{msg};
×
241
    http::write(socket, sr, ec);
×
242
  }};
×
243

244
  for(;;)
×
245
  {
246
    // Read a request
247
    http::request<http::string_body> req;
×
248
    http::read(socket, buffer, req, ec);
×
249
    if(ec == http::error::end_of_stream)
×
250
      break;
×
251
    if(ec)
×
252
      HttpServer::fail(ec, "read");
×
253

254
    // Send the response
255
    HttpServer::handle_request(std::move(req), lambda);
×
256

257
    if(ec)
×
258
      HttpServer::fail(ec, "write");
×
259
    if(close)
×
260
    {
261
      // This means we should close the connection, usually because
262
      // the response indicated the "Connection: close" semantic.
263
      break;
×
264
    }
265
  }
×
266

267
  // Send a TCP shutdown
268
  socket.shutdown(tcp::socket::shutdown_send, ec);
×
269

270
  // At this point the connection is closed gracefully
271
}
×
272

273
// Launch the open_server function in a thread
274
void HttpServer::start_thread()
×
275
{
276
  m_serverThread = std::thread{[this] { open_server(); }};
×
277
}
×
278

279
void HttpServer::stop_thread()
1,498 ✔
280
{
281
  if(!running())
1,498 ✔
282
    return;
1,498 ✔
283

284
  shutdown(m_listenSocket, SHUT_RDWR);
×
285
  m_ioc.stop();
×
286
  m_serverThread.join();
×
287
}
1,498 ✔
288

289
void HttpServer::set_path(const std::string& str)
1,498 ✔
290
{
291
  std::lock_guard<std::mutex> lock{mtx};
1,498 ✔
292
  m_buildWasmPath = str;
1,498 ✔
293
}
1,498 ✔
294

295
void HttpServer::set_address(const std::string& str)
1,498 ✔
296
{
297
  bool is_running{running()};
1,498 ✔
298
  if(is_running)
1,498 ✔
299
    stop_thread();
×
300

301
  try
302
  { m_endpoint.address(net::ip::make_address(str.c_str())); }
1,498 ✔
NEW
303
  catch (...) { }
×
304

305
  if(is_running)
1,498 ✔
306
    start_thread();
×
307
}
1,498 ✔
308

309
void HttpServer::set_port(unsigned short prt)
1,498 ✔
310
{
311
  bool is_running{running()};
1,498 ✔
312
  if(is_running)
1,498 ✔
313
    stop_thread();
×
314
  m_endpoint.port(prt);
1,498 ✔
315
  if(is_running)
1,498 ✔
316
    start_thread();
×
317
}
1,498 ✔
318

319
// Open a server using sockets
320
int HttpServer::open_server()
×
321
{
322
  try
323
  {
324
    // The acceptor receives incoming connections
325
    tcp::acceptor acceptor{m_ioc, m_endpoint};
×
326
    m_listenSocket = acceptor.native_handle();
×
327
    for(;;)
×
328
    {
329
      // This will receive the new connection
330
      tcp::socket socket{m_ioc};
×
331

332
      // Block until we get a connection
333
      acceptor.accept(socket);
×
334

335
      // Launch the session, transferring ownership of the socket
336
      do_session(socket);
×
337
    }
×
338
  }
×
339
  catch(const std::exception& e)
340
  {
341
    qDebug() << "Error: " << e.what();
×
342
    return EXIT_FAILURE;
×
343
  }
×
344
}
×
345

346
bool HttpServer::running()
4,494 ✔
347
{
348
  return m_serverThread.joinable();
4,494 ✔
349
}
350

351
}
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