245 lines
6.7 KiB
C++
245 lines
6.7 KiB
C++
#include "HttpReply.h"
|
|
|
|
|
|
namespace Http {
|
|
|
|
namespace StatusStrings {
|
|
|
|
const std::string Ok =
|
|
"HTTP/1.0 200 OK\r\n";
|
|
const std::string Created =
|
|
"HTTP/1.0 201 Created\r\n";
|
|
const std::string Accepted =
|
|
"HTTP/1.0 202 Accepted\r\n";
|
|
const std::string NoContent =
|
|
"HTTP/1.0 204 No Content\r\n";
|
|
const std::string MultipleChoices =
|
|
"HTTP/1.0 300 Multiple Choices\r\n";
|
|
const std::string MovedPermanently =
|
|
"HTTP/1.0 301 Moved Permanently\r\n";
|
|
const std::string MovedTemporarily =
|
|
"HTTP/1.0 302 Moved Temporarily\r\n";
|
|
const std::string NotModified =
|
|
"HTTP/1.0 304 Not Modified\r\n";
|
|
const std::string BadRequest =
|
|
"HTTP/1.0 400 Bad Request\r\n";
|
|
const std::string Unauthorized =
|
|
"HTTP/1.0 401 Unauthorized\r\n";
|
|
const std::string Forbidden =
|
|
"HTTP/1.0 403 Forbidden\r\n";
|
|
const std::string NotFound =
|
|
"HTTP/1.0 404 Not Found\r\n";
|
|
const std::string InternalServerError =
|
|
"HTTP/1.0 500 Internal Server Error\r\n";
|
|
const std::string NotImplemented =
|
|
"HTTP/1.0 501 Not Implemented\r\n";
|
|
const std::string BadGateway =
|
|
"HTTP/1.0 502 Bad Gateway\r\n";
|
|
const std::string ServiceUnavailable =
|
|
"HTTP/1.0 503 Service Unavailable\r\n";
|
|
|
|
asio::const_buffer ToBuffer(HttpReply::Status::type status)
|
|
{
|
|
switch (status)
|
|
{
|
|
case HttpReply::Status::Ok:
|
|
return asio::buffer(Ok);
|
|
case HttpReply::Status::Created:
|
|
return asio::buffer(Created);
|
|
case HttpReply::Status::Accepted:
|
|
return asio::buffer(Accepted);
|
|
case HttpReply::Status::NoContent:
|
|
return asio::buffer(NoContent);
|
|
case HttpReply::Status::MultipleChoices:
|
|
return asio::buffer(MultipleChoices);
|
|
case HttpReply::Status::MovedPermanently:
|
|
return asio::buffer(MovedPermanently);
|
|
case HttpReply::Status::MovedTemporarily:
|
|
return asio::buffer(MovedTemporarily);
|
|
case HttpReply::Status::NotModified:
|
|
return asio::buffer(NotModified);
|
|
case HttpReply::Status::BadRequest:
|
|
return asio::buffer(BadRequest);
|
|
case HttpReply::Status::Unauthorized:
|
|
return asio::buffer(Unauthorized);
|
|
case HttpReply::Status::Forbidden:
|
|
return asio::buffer(Forbidden);
|
|
case HttpReply::Status::NotFound:
|
|
return asio::buffer(NotFound);
|
|
case HttpReply::Status::InternalServerError:
|
|
return asio::buffer(InternalServerError);
|
|
case HttpReply::Status::NotImplemented:
|
|
return asio::buffer(NotImplemented);
|
|
case HttpReply::Status::BadGateway:
|
|
return asio::buffer(BadGateway);
|
|
case HttpReply::Status::ServiceUnavailable:
|
|
return asio::buffer(ServiceUnavailable);
|
|
default:
|
|
return asio::buffer(InternalServerError);
|
|
}
|
|
}
|
|
|
|
} // namespace StatusStrings
|
|
|
|
namespace miscellaneousStrings {
|
|
|
|
const char nameValueSeparator[] = { ':', ' ' };
|
|
const char crlf[] = { '\r', '\n' };
|
|
|
|
} // namespace miscellaneousStrings
|
|
|
|
std::vector<asio::const_buffer> HttpReply::ToBuffers()
|
|
{
|
|
std::vector<asio::const_buffer> buffers;
|
|
buffers.push_back(StatusStrings::ToBuffer(status));
|
|
for (std::size_t i = 0; i < headers.size(); ++i)
|
|
{
|
|
HttpHeader& h = headers[i];
|
|
buffers.push_back(asio::buffer(h.name));
|
|
buffers.push_back(asio::buffer(miscellaneousStrings::nameValueSeparator));
|
|
buffers.push_back(asio::buffer(h.value));
|
|
buffers.push_back(asio::buffer(miscellaneousStrings::crlf));
|
|
}
|
|
buffers.push_back(asio::buffer(miscellaneousStrings::crlf));
|
|
buffers.push_back(asio::buffer(content));
|
|
|
|
return buffers;
|
|
}
|
|
|
|
namespace StockReplies {
|
|
|
|
const char Ok[] = "";
|
|
const char Created[] =
|
|
"<html>"
|
|
"<head><title>Created</title></head>"
|
|
"<body><h1>201 Created</h1></body>"
|
|
"</html>";
|
|
const char Accepted[] =
|
|
"<html>"
|
|
"<head><title>Accepted</title></head>"
|
|
"<body><h1>202 Accepted</h1></body>"
|
|
"</html>";
|
|
const char NoContent[] =
|
|
"<html>"
|
|
"<head><title>No Content</title></head>"
|
|
"<body><h1>204 Content</h1></body>"
|
|
"</html>";
|
|
const char MultipleChoices[] =
|
|
"<html>"
|
|
"<head><title>Multiple Choices</title></head>"
|
|
"<body><h1>300 Multiple Choices</h1></body>"
|
|
"</html>";
|
|
const char MovedPermanently[] =
|
|
"<html>"
|
|
"<head><title>Moved Permanently</title></head>"
|
|
"<body><h1>301 Moved Permanently</h1></body>"
|
|
"</html>";
|
|
const char MovedTemporarily[] =
|
|
"<html>"
|
|
"<head><title>Moved Temporarily</title></head>"
|
|
"<body><h1>302 Moved Temporarily</h1></body>"
|
|
"</html>";
|
|
const char NotModified[] =
|
|
"<html>"
|
|
"<head><title>Not Modified</title></head>"
|
|
"<body><h1>304 Not Modified</h1></body>"
|
|
"</html>";
|
|
const char BadRequest[] =
|
|
"<html>"
|
|
"<head><title>Bad Request</title></head>"
|
|
"<body><h1>400 Bad Request</h1></body>"
|
|
"</html>";
|
|
const char Unauthorized[] =
|
|
"<html>"
|
|
"<head><title>Unauthorized</title></head>"
|
|
"<body><h1>401 Unauthorized</h1></body>"
|
|
"</html>";
|
|
const char Forbidden[] =
|
|
"<html>"
|
|
"<head><title>Forbidden</title></head>"
|
|
"<body><h1>403 Forbidden</h1></body>"
|
|
"</html>";
|
|
const char NotFound[] =
|
|
"<html>"
|
|
"<head><title>Not Found</title></head>"
|
|
"<body><h1>404 Not Found</h1></body>"
|
|
"</html>";
|
|
const char InternalServerError[] =
|
|
"<html>"
|
|
"<head><title>Internal Server Error</title></head>"
|
|
"<body><h1>500 Internal Server Error</h1></body>"
|
|
"</html>";
|
|
const char NotImplemented[] =
|
|
"<html>"
|
|
"<head><title>Not Implemented</title></head>"
|
|
"<body><h1>501 Not Implemented</h1></body>"
|
|
"</html>";
|
|
const char BadGateway[] =
|
|
"<html>"
|
|
"<head><title>Bad Gateway</title></head>"
|
|
"<body><h1>502 Bad Gateway</h1></body>"
|
|
"</html>";
|
|
const char ServiceUnavailable[] =
|
|
"<html>"
|
|
"<head><title>Service Unavailable</title></head>"
|
|
"<body><h1>503 Service Unavailable</h1></body>"
|
|
"</html>";
|
|
|
|
std::string ToString(HttpReply::Status::type status)
|
|
{
|
|
switch (status)
|
|
{
|
|
case HttpReply::Status::Ok:
|
|
return Ok;
|
|
case HttpReply::Status::Created:
|
|
return Created;
|
|
case HttpReply::Status::Accepted:
|
|
return Accepted;
|
|
case HttpReply::Status::NoContent:
|
|
return NoContent;
|
|
case HttpReply::Status::MultipleChoices:
|
|
return MultipleChoices;
|
|
case HttpReply::Status::MovedPermanently:
|
|
return MovedPermanently;
|
|
case HttpReply::Status::MovedTemporarily:
|
|
return MovedTemporarily;
|
|
case HttpReply::Status::NotModified:
|
|
return NotModified;
|
|
case HttpReply::Status::BadRequest:
|
|
return BadRequest;
|
|
case HttpReply::Status::Unauthorized:
|
|
return Unauthorized;
|
|
case HttpReply::Status::Forbidden:
|
|
return Forbidden;
|
|
case HttpReply::Status::NotFound:
|
|
return NotFound;
|
|
case HttpReply::Status::InternalServerError:
|
|
return InternalServerError;
|
|
case HttpReply::Status::NotImplemented:
|
|
return NotImplemented;
|
|
case HttpReply::Status::BadGateway:
|
|
return BadGateway;
|
|
case HttpReply::Status::ServiceUnavailable:
|
|
return ServiceUnavailable;
|
|
default:
|
|
return InternalServerError;
|
|
}
|
|
}
|
|
|
|
} // namespace StockReplies
|
|
|
|
HttpReply HttpReply::StockReply(HttpReply::Status::type status)
|
|
{
|
|
HttpReply reply;
|
|
reply.status = status;
|
|
reply.content = StockReplies::ToString(status);
|
|
reply.headers.resize(2);
|
|
reply.headers[0].name = "Content-Length";
|
|
reply.headers[0].value = std::to_string(reply.content.size());
|
|
reply.headers[1].name = "Content-Type";
|
|
reply.headers[1].value = "text/html";
|
|
return reply;
|
|
}
|
|
|
|
} // namespace Http
|