Loading...
Searching...
No Matches
HttpServer.hpp
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#pragma once
11#include <score_git_info.hpp>
12
13#define BOOST_DATE_TIME_NO_LIB 1
14
15#include <boost/asio/ip/tcp.hpp>
16#include <boost/beast/core.hpp>
17#include <boost/beast/http.hpp>
18#include <boost/beast/version.hpp>
19#include <boost/config.hpp>
20
21#include <string>
22#include <thread>
23
24#ifdef _WIN32
25#define SHUT_RDWR 2
26#endif
27
28#include <QApplication>
29
30namespace score
31{
32static std::string ossia_score_verison_agent()
33{
34 static QString agent{
35 QCoreApplication::organizationDomain() + '.' + QCoreApplication::applicationName()
36 + '/' + SCORE_TAG_NO_V}; // the release only: no branch name leaks to clients
37
38 return agent.toStdString();
39};
40}
41
42namespace beast = boost::beast; // from <boost/beast.hpp>
43namespace http = beast::http; // from <boost/beast/http.hpp>
44namespace net = boost::asio; // from <boost/asio.hpp>
45using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
46
47namespace RemoteControl::HttpServer
48{
50{
51public:
52 HttpServer();
54
55 void start_thread();
56 void stop_thread();
57 void set_path(const std::string& str);
58 void set_address(const std::string& str);
59 void set_port(unsigned short prt);
60
61private:
62 // Return a reasonable mime type based on the extension of a file.
63 beast::string_view mime_type(beast::string_view path);
64
65 // Append an HTTP rel-path to a local filesystem path.
66 // The returned path is normalized for the platform.
67 std::string path_cat(beast::string_view path);
68
69 // This function produces an HTTP response for the given
70 // request. The type of the response object depends on the
71 // contents of the request, so the interface requires the
72 // caller to pass a generic lambda for receiving the response.
73 template <class Body, class Allocator, class Send>
74 void handle_request(
75 http::request<Body, http::basic_fields<Allocator>>&& req, const Send& send);
76
77 // Report a failure
78 void fail(beast::error_code ec, char const* what);
79
80 // Handles an HTTP server connection
81 void do_session(tcp::socket& socket);
82
83 // Open a server using sockets
84 int open_server();
85 bool running();
86
87 net::io_context m_ioc;
88 tcp::endpoint m_endpoint{};
89 std::thread m_serverThread;
90 std::string m_buildWasmPath;
91 int m_listenSocket{};
92 std::mutex mtx{};
93};
94}
Definition HttpServer.hpp:50
Base toolkit upon which the software is built.
Definition Application.cpp:117