diff options
| author | Carl Hetherington <cth@carlh.net> | 2024-06-14 01:45:18 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2024-06-23 19:51:28 +0200 |
| commit | 117f6bd199479fdaeff665acbea109e967500308 (patch) | |
| tree | b3da204320e58c55cdde2538921e1cc51eb4ba38 /src/lib/http_server.h | |
| parent | d04355507baefd5fa42629341ed422f7402772f4 (diff) | |
Add minimal player HTTP server (#2830).
Diffstat (limited to 'src/lib/http_server.h')
| -rw-r--r-- | src/lib/http_server.h | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/lib/http_server.h b/src/lib/http_server.h new file mode 100644 index 000000000..f0ee50ef5 --- /dev/null +++ b/src/lib/http_server.h @@ -0,0 +1,91 @@ +/* + Copyright (C) 2024 Carl Hetherington <cth@carlh.net> + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>. + +*/ + + +#include "dcpomatic_time.h" +#include "server.h" +#include "signaller.h" +#include <boost/signals2.hpp> + + +class Response +{ +public: + Response(int code); + Response(int code, std::string payload); + + enum class Type { + HTML, + JSON + }; + + void add_header(std::string key, std::string value); + void set_type(Type type) { + _type = type; + } + + void send(std::shared_ptr<Socket> socket); + + static Response ERROR_404; + +private: + int _code; + + Type _type = Type::HTML; + std::string _payload; + std::vector<std::pair<std::string, std::string>> _headers; +}; + + +class HTTPServer : public Server, public Signaller +{ +public: + explicit HTTPServer(int port, int timeout = 30); + + boost::signals2::signal<void ()> Play; + boost::signals2::signal<void ()> Stop; + + void set_playing(bool playing) { + boost::mutex::scoped_lock lm(_mutex); + _playing = playing; + } + + void set_position(dcpomatic::DCPTime position) { + boost::mutex::scoped_lock lm(_mutex); + _position = position; + } + + void set_dcp_name(std::string name) { + boost::mutex::scoped_lock lm(_mutex); + _dcp_name = name; + } + +private: + void handle(std::shared_ptr<Socket> socket) override; + Response request(std::vector<std::string> const& request); + Response get(std::string const& url); + Response post(std::string const& url); + + boost::mutex _mutex; + bool _playing = false; + dcpomatic::DCPTime _position; + std::string _dcp_name; +}; + |
