| 1 | /* |
| 2 | RailControl - Model Railway Control Software |
| 3 | |
| 4 | Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 | |
| 6 | RailControl is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 3, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | RailControl is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | See the GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with RailControl; see the file LICENCE. If not see |
| 18 | <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <sstream> |
| 22 | #include <string> |
| 23 | |
| 24 | #include "Server/Web/Response.h" |
| 25 | |
| 26 | namespace Server { namespace Web |
| 27 | { |
| 28 | const Response::responseCodeMap Response::responseTexts = { |
| 29 | { Response::OK, "OK" }, |
| 30 | { Response::NotFound, "Not found"}, |
| 31 | { Response::NotImplemented, "Not Implemented"} |
| 32 | }; |
| 33 | |
| 34 | void Response::AddHeader(const std::string& key, const std::string& value) |
| 35 | { |
| 36 | headers[key] = value; |
| 37 | } |
| 38 | |
| 39 | Response::operator std::string() |
| 40 | { |
| 41 | std::stringstream reply; |
| 42 | reply << *this; |
| 43 | return reply.str(); |
| 44 | } |
| 45 | |
| 46 | std::ostream& operator<<(std::ostream& stream, const Response& response) |
| 47 | { |
| 48 | stream << "HTTP/1.1 " << std::to_string(response.responseCode) << " " << Response::responseTexts.at(response.responseCode) << "\r\n"; |
| 49 | for (auto& header : response.headers) |
| 50 | { |
| 51 | stream << header.first << ": " << header.second << "\r\n"; |
| 52 | } |
| 53 | stream << "\r\n"; |
| 54 | stream << response.content; |
| 55 | return stream; |
| 56 | } |
| 57 | }} // namespace Server::Web |