#include "Test.h"
#include "../utils/Utils.h"
namespace Application
{
bool test(
HttpServer::Request &request,
HttpServer::Response &response
) {
// Output incoming headers
std::string s = R"(
| Incoming headers |
| Header name |
Header value |
)";
for (auto const &pair : request.headers)
{
s += R"(
| )" + pair.first + R"( |
)" + pair.second + R"( |
)";
}
// Output incoming url parameters
s += R"(
| Incoming url parameters |
| Parameter name |
Parameter value |
)";
for (auto const &pair : request.params)
{
s += R"(
| )" + pair.first + R"( |
)" + pair.second + R"( |
)";
}
// Output incoming form data
s += R"(
| Incoming form data |
| Form field |
Field value |
)";
for (auto const &pair : request.data)
{
s += R"(
| )" + pair.first + R"( |
)" + pair.second + R"( |
)";
}
// Output info about incoming files
s += R"(
| Incoming files |
| Form field |
Original name |
File type |
File size |
Uploaded name |
)";
for (auto const &pair : request.files)
{
const std::string &field_name = pair.first;
const Transfer::FileIncoming &file = pair.second;
s += R"(
| )" + field_name + R"( |
)" + file.getName() + R"( |
)" + file.getType() + R"( |
)" + std::to_string(file.getSize() ) + R"( |
)" + file.getTmpName() + R"( |
)";
}
s += R"(
)";
std::unordered_map &headers = response.headers;
// Set outgoing headers
response.setStatusCode(Http::StatusCode::OK);
headers["content-type"] = "text/html; charset=utf-8";
headers["accept-ranges"] = "bytes";
headers["content-length"] = std::to_string(s.size() );
headers["connection"] = "keep-alive";
headers["date"] = Utils::getDatetimeAsString();
// Additional headers
// In additional headers may be placed values of cookies
std::vector > additional;
// additional.emplace_back("set-cookie", "key=value; expires=; path=/; domain=*");
// Send headers and data
const std::chrono::milliseconds timeout(5000);
if (response.sendHeaders(additional, timeout, s.empty() ) ) {
if (s.empty() == false) {
response.sendData(s.data(), s.size(), timeout, true);
}
}
return EXIT_SUCCESS;
}
}