summaryrefslogtreecommitdiff
path: root/src/lib/http_server.cc
blob: 4955327043c22e4a814221d19ea6278e96ccf0d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
    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 "config.h"
#include "cross.h"
#include "dcpomatic_log.h"
#include "dcpomatic_socket.h"
#include "http_server.h"
#include "show_playlist_content_store.h"
#include "show_playlist_list.h"
#include "util.h"
#include "variant.h"
#include <dcp/raw_convert.h>
#include <nlohmann/json.hpp>
#include <boost/algorithm/string.hpp>
#include <stdexcept>
#include <utility>


using std::make_pair;
using std::make_shared;
using std::runtime_error;
using std::shared_ptr;
using std::string;
using std::vector;
using boost::optional;


Response Response::ERROR_404 = { 404, "<html><head><title>Error 404</title></head><body><h1>Error 404</h1></body></html>"};


HTTPServer::HTTPServer(int port, int timeout)
	: Server(port, timeout)
{

}



Response::Response(int code)
	: _code(code)
{

}


Response::Response(int code, string payload)
	: _code(code)
	, _payload(payload)
{

}


void
Response::add_header(string key, string value)
{
	_headers.push_back(make_pair(key, value));
}


void
Response::send(shared_ptr<Socket> socket)
{
	socket->write(String::compose("HTTP/1.1 %1 OK\r\n", _code));
	switch (_type) {
	case Type::HTML:
		socket->write("Content-Type: text/html; charset=utf-8\r\n");
		break;
	case Type::JSON:
		socket->write("Content-Type: text/json; charset=utf-8\r\n");
		break;
	}
	socket->write(String::compose("Content-Length: %1\r\n", _payload.length()));
	for (auto const& header: _headers) {
		socket->write(String::compose("%1: %2\r\n", header.first, header.second));
	}
	socket->write("\r\n");
	socket->write(_payload);
}


Response
HTTPServer::get(string const& url)
{
	if (url == "/") {
		return Response(200, String::compose(dcp::file_to_string(resources_path() / "web" / "index.html"), variant::dcpomatic_player()));
	} else if (url == "/playlists") {
		return Response(200, String::compose(dcp::file_to_string(resources_path() / "web" / "playlists.html"), variant::dcpomatic_player()));
	} else if (url == "/api/v1/status") {
		nlohmann::json json;
		{
			boost::mutex::scoped_lock lm(_mutex);
			json["playing"] = _playing;
			json["position"] = seconds_to_hms(_position.seconds());
			json["dcp_name"] = _dcp_name;
		}
		auto response = Response(200, json.dump());
		response.set_type(Response::Type::JSON);
		return response;
	} else if (url == "/api/v1/playlists") {
		ShowPlaylistList spl_list;
		nlohmann::json json;
		for (auto const& spl: spl_list.show_playlists()) {
			json.push_back(spl.second.as_json());
		}
		auto response = Response(200, json.dump());
		response.set_type(Response::Type::JSON);
		return response;
	} else if (boost::algorithm::starts_with(url, "/api/v1/playlist/")) {
		vector<string> parts;
		boost::algorithm::split(parts, url, boost::is_any_of("/"));
		if (parts.size() != 5) {
			return Response::ERROR_404;
		}
		ShowPlaylistList spl_list;
		for (auto const& spl: spl_list.show_playlists()) {
			if (spl.second.uuid() == parts[4]) {
				// XXX
				// auto response = Response(200, spl->as_json_with_content().dump());
				// response.set_type(Response::Type::JSON);
				// return response;
			}
		}
		return Response::ERROR_404;
	} else if (url == "/api/v1/content") {
		nlohmann::json json;
		for (auto i: ShowPlaylistContentStore::instance()->all()) {
			json.push_back(i.as_json());
		}
		auto response = Response(200, json.dump());
		response.set_type(Response::Type::JSON);
		return response;
	} else if (boost::algorithm::starts_with(url, "/api/v1/content/")) {
		vector<string> parts;
		boost::algorithm::split(parts, url, boost::is_any_of("/"));
		if (parts.size() != 5) {
			return Response::ERROR_404;
		}
		auto content = ShowPlaylistContentStore::instance()->get(parts[4]);
		if (!content) {
			return Response::ERROR_404;
		}
		auto json = content->as_json();
		auto response = Response(200, json.dump());
		response.set_type(Response::Type::JSON);
		return response;
	} else {
		LOG_HTTP("404 %1", url);
		return Response::ERROR_404;
	}
}


Response
HTTPServer::post(string const& url, vector<uint8_t> const& body)
{
	if (url == "/api/v1/play") {
		emit(boost::bind(boost::ref(Play)));
		auto response = Response(303);
		response.add_header("Location", "/");
		return response;
	} else if (url == "/api/v1/stop") {
		emit(boost::bind(boost::ref(Stop)));
		auto response = Response(303);
		response.add_header("Location", "/");
		return response;
	} else if (boost::algorithm::starts_with(url, "/api/v1/playlist/")) {
		vector<string> parts;
		boost::algorithm::split(parts, url, boost::is_any_of("/"));
		if (parts.size() != 6 || parts[5] != "insert") {
			return Response::ERROR_404;
		}
		bool found = false;
		ShowPlaylistList spl_list;
		for (auto const& spl: spl_list.show_playlists()) {
			if (spl.second.uuid() == parts[4]) {
				nlohmann::json details = nlohmann::json::parse(body);
				// XXX
				// spl->insert(
				// 	SPLEntry(ContentStore::instance()->get(details["digest"])),
				// 	details["before"].is_null() ? optional<string>() : details["before"].get<string>()
				// );
				// if (auto dir = Config::instance()->player_playlist_directory()) {
				// 	spl->write(*dir / (spl->id() + ".xml"));
				// }
				found = true;
			}
		}
		if (!found) {
			return Response::ERROR_404;
		}
		return Response(201);
	} else {
		return Response::ERROR_404;
	}
}


Response
HTTPServer::request(vector<string> const& request, vector<uint8_t> const& body)
{
	vector<string> parts;
	boost::split(parts, request[0], boost::is_any_of(" "));
	if (parts.size() != 3) {
		return Response::ERROR_404;
	}

	try {
		if (parts[0] == "GET") {
			LOG_HTTP("GET %1", parts[1]);
			return get(parts[1]);
		} else if (parts[0] == "POST") {
			LOG_HTTP("POST %1", parts[1]);
			return post(parts[1], body);
		}
	} catch (std::exception& e) {
		LOG_ERROR("Error while handling HTTP request: %1", e.what());
	} catch (...) {
		LOG_ERROR_NC("Unknown exception while handling HTTP request");
	}

	LOG_HTTP("404 %1", parts[0]);
	return Response::ERROR_404;
}


void
HTTPServer::handle(shared_ptr<Socket> socket)
{
	class Reader
	{
	public:
		void read_block(boost::system::error_code const& ec, uint8_t* data, std::size_t size)
		{
			if (ec.value() != boost::system::errc::success) {
				_close = true;
				_error_code = ec;
				return;
			}

			for (std::size_t i = 0; i < size; ++i) {
				switch (_state) {
				case State::HEADER:
					if (_line.length() >= 1024) {
						_close = true;
						return;
					}
					_line += data[i];
					if (_line.length() >= 2 && _line.substr(_line.length() - 2) == "\r\n") {
						/* Got a line */
						if (_line.length() == 2) {
							_state = BODY;
						} else if (_request.size() > 64) {
							_close = true;
							return;
						} else if (_line.size() >= 2) {
							_line = _line.substr(0, _line.length() - 2);
							LOG_HTTP("Receive: %1", _line);
							if (_request.empty()) {
								_method = _line.substr(0, _line.find(' '));
							}
							string lower_case = _line;
							transform(lower_case.begin(), lower_case.end(), lower_case.begin(), ::tolower);
							auto const colon = lower_case.find(':');
							string const key = lower_case.substr(0, colon);
							string value = colon != std::string::npos ? lower_case.substr(colon + 1): "";
							boost::trim(value);
							if (key == "content-length") {
								_content_length = dcp::raw_convert<int>(value);
							}
							_request.push_back(_line);
							_line = "";
						}
					}
					break;
				case State::BODY:
					_body.push_back(data[i]);
					if (_body.size() > 65536) {
						_close = true;
						return;
					}
					break;
				}
			}
		}

		bool completed() const {
			return _state == State::BODY && (_method == "GET" || (_method == "POST" && static_cast<int>(_body.size()) == _content_length));
		}

		bool close() const {
			return _close;
		}

		std::vector<uint8_t> body() const {
			return _body;
		}

		boost::system::error_code error_code() const {
			return _error_code;
		}

		std::string method() const {
			return _method;
		}

		vector<std::string> const& request() const {
			return _request;
		}

	private:
		enum State {
			HEADER,
			BODY
		} _state = HEADER;
		std::string _method;
		std::string _line;
		vector<std::string> _request;
		std::vector<uint8_t> _body;
		int _content_length = 0;
		bool _close = false;
		boost::system::error_code _error_code;
	};

	Reader reader;

	vector<uint8_t> buffer(2048);
	socket->set_deadline_from_now(2);
	socket->socket().async_read_some(
		boost::asio::buffer(buffer.data(), buffer.size()),
		[&reader, &buffer, socket](boost::system::error_code const& ec, std::size_t bytes_transferred) {
			reader.read_block(ec, buffer.data(), bytes_transferred);
		});

	while (!reader.completed() && !reader.close() && socket->is_open()) {
		socket->run();
	}

	if (reader.completed() && !reader.close()) {
		try {
			auto response = request(reader.request(), reader.body());
			response.send(socket);
		} catch (runtime_error& e) {
			LOG_ERROR_NC(e.what());
		}
	}

	/* I think we should keep the socket open if the client requested keep-alive, but some browsers
	 * send keep-alive then don't re-use the connection.  Since we can only accept one request at once,
	 * this blocks until our request read (above) times out.  We probably should accept multiple
	 * requests in parallel, but it's easier for not to use close the socket.
	 */
	socket->close();
}