#include "Test.h"
bool test(HttpServer::ServerRequest &request, HttpServer::ServerResponse &response)
{
// Output incoming headers
std::string s = R"(
| Incoming headers |
)";
for (auto const &pair : request.headers)
{
s += R"(
| )" + pair.first + R"( |
)" + pair.second + R"( |
)";
}
// Output incoming data
s += R"(
| Incoming data |
)";
for (auto const &pair : request.data)
{
s += R"(
| )" + pair.first + R"( |
)" + pair.second + R"( |
)";
}
// Output incoming url parameters
s += R"(
| Incoming url parameters |
)";
for (auto const &pair : request.params)
{
s += R"(
| )" + pair.first + R"( |
)" + pair.second + R"( |
)";
}
// Output info about incoming files
s += R"(
| Incoming files |
)";
for (auto const &pair : request.files)
{
const HttpServer::FileIncoming &file = pair.second;
s += R"(
| )" + file.getName() + R"( |
)" + std::to_string(file.getSize() ) + R"( |
)";
}
s += R"(
)";
HttpServer::SocketAdapter &socket = response.socket;
std::map &headers_outgoing = response.headers;
// Set outgoing headers
headers_outgoing[""] = "HTTP/1.1 200 OK";
headers_outgoing["Content-Type"] = "text/html; charset=utf-8";
headers_outgoing["Accept-Ranges"] = "bytes";
headers_outgoing["Content-Length"] = std::to_string(s.length() );
headers_outgoing["Connection"] = "Keep-Alive";
headers_outgoing["Date"] = Utils::getDatetimeAsString();
std::string headers;
for (auto const &h : headers_outgoing)
{
if (h.first.length() )
{
headers += h.first + ": ";
}
headers += h.second + "\r\n";
}
headers += "\r\n";
// Send headers and page
const std::chrono::milliseconds timeout(5000);
socket.nonblock_send(headers, timeout);
socket.nonblock_send(s, timeout);
return EXIT_SUCCESS;
}