// SPDX-FileCopyrightText: 2023 Yury Gubich // SPDX-License-Identifier: GPL-3.0-or-later #include "response.h" constexpr std::array(Response::Status::__size)> statusCodes = { "Status: 200 OK", "Status: 404 Not Found", "Status: 405 Method Not Allowed", "Status: 500 Internal Error" }; constexpr std::array(Response::ContentType::__size)> contentTypes = { "Content-type: text/plain", "Content-type: application/json" }; Response::Response(Request& request): request(request), status(Status::ok), type(ContentType::text), body() {} Response::Response(Request& request, Status status): request(request), status(status), type(ContentType::text), body() {} void Response::send() const { // OStream out = status == Status::ok ? // request.getOutputStream() : // request.getErrorStream(); OStream out = request.getOutputStream(this); out << statusCodes[static_cast(status)]; if (!body.empty()) out << '\n' << contentTypes[static_cast(type)] << '\n' << '\n' << body; request.responseIsComplete(this); } void Response::setBody(const std::string& body) { type = ContentType::text; Response::body = body; } void Response::setBody(const nlohmann::json& body) { type = ContentType::json; Response::body = body.dump(); }