diff options
| author | Carl Hetherington <cth@carlh.net> | 2026-01-24 21:21:46 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2026-02-16 01:20:38 +0100 |
| commit | 43181cb008813ecbf85804ee66029afb3fbf5b82 (patch) | |
| tree | 74649d6f38d0c9a279d26f1512d0d43373551a71 | |
| parent | 5d3be01a220403cc821f255445ee70dbcaa12ba6 (diff) | |
Tidy Response initialization and support ::CSS.
| -rw-r--r-- | src/lib/http_server.cc | 30 | ||||
| -rw-r--r-- | src/lib/http_server.h | 9 |
2 files changed, 16 insertions, 23 deletions
diff --git a/src/lib/http_server.cc b/src/lib/http_server.cc index 17f7d3cbe..110165547 100644 --- a/src/lib/http_server.cc +++ b/src/lib/http_server.cc @@ -60,8 +60,9 @@ Response::Response(int code) } -Response::Response(int code, string payload) +Response::Response(int code, string payload, Type type) : _code(code) + , _type(type) , _payload(payload) { @@ -86,6 +87,9 @@ Response::send(shared_ptr<Socket> socket) case Type::JSON: socket->write("Content-Type: text/json; charset=utf-8\r\n"); break; + case Type::CSS: + socket->write("Content-Type: text/css; charset=utf-8\r\n"); + break; } socket->write(fmt::format("Content-Length: {}\r\n", _payload.length())); for (auto const& header: _headers) { @@ -117,18 +121,14 @@ HTTPServer::get_request(string const& url) 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; + return Response(200, json.dump(), Response::Type::JSON); } else if (url == "/api/v1/playlists") { ShowPlaylistList list; nlohmann::json json; for (auto 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; + return Response(200, json.dump(), Response::Type::JSON); } else if (boost::algorithm::starts_with(url, "/api/v1/content/")) { vector<string> parts; boost::algorithm::split(parts, url, boost::is_any_of("/")); @@ -141,9 +141,7 @@ HTTPServer::get_request(string const& url) } /* XXX: converting to JSON this way feels a bit grotty */ auto json = ShowPlaylistEntry(content, {}).as_json(); - auto response = Response(200, json.dump()); - response.set_type(Response::Type::JSON); - return response; + return Response(200, json.dump(), Response::Type::JSON); } else if (boost::algorithm::starts_with(url, "/api/v1/playlist/")) { vector<string> parts; boost::algorithm::split(parts, url, boost::is_any_of("/")); @@ -165,18 +163,14 @@ HTTPServer::get_request(string const& url) for (auto entry: list.entries(parts[4])) { json["content"].push_back(entry.as_json()); } - auto response = Response(200, json.dump()); - response.set_type(Response::Type::JSON); - return response; + return Response(200, json.dump(), Response::Type::JSON); } else if (url == "/api/v1/content") { nlohmann::json json; for (auto i: ShowPlaylistContentStore::instance()->all()) { /* XXX: converting to JSON this way feels a bit grotty */ json.push_back(ShowPlaylistEntry(i, {}).as_json()); } - auto response = Response(200, json.dump()); - response.set_type(Response::Type::JSON); - return response; + return Response(200, json.dump(), Response::Type::JSON); } else if (boost::algorithm::starts_with(url, "/api/v1/content/")) { vector<string> parts; boost::algorithm::split(parts, url, boost::is_any_of("/")); @@ -189,9 +183,7 @@ HTTPServer::get_request(string const& url) } /* XXX: converting to JSON this way feels a bit grotty */ auto json = ShowPlaylistEntry(content, {}).as_json(); - auto response = Response(200, json.dump()); - response.set_type(Response::Type::JSON); - return response; + return Response(200, json.dump(), Response::Type::JSON); } else { LOG_HTTP("404 {}", url); return Response::ERROR_404; diff --git a/src/lib/http_server.h b/src/lib/http_server.h index 982fb4d9a..56f75047c 100644 --- a/src/lib/http_server.h +++ b/src/lib/http_server.h @@ -30,14 +30,15 @@ LIBDCP_ENABLE_WARNINGS class Response { public: - Response(int code); - Response(int code, std::string payload); - enum class Type { HTML, - JSON + JSON, + CSS }; + Response(int code); + Response(int code, std::string payload, Type type = Type::HTML); + void add_header(std::string key, std::string value); void set_type(Type type) { _type = type; |
