我想在子类createRequestHandler(HTTPServerRequest& request)
的{{1}}函数中处理无效的URI客户端请求。
我想通过错误代码
发送URI无效的回复HTTP_NOT_FOUND(404)。
HTTPRequestHandlerFactory
答案 0 :(得分:-1)
void sendErrorResponse(HTTPResponse::HTTPStatus status, const std::string& message)
{
response().setStatusAndReason(status);
std::string statusAndReason(NumberFormatter::format(static_cast<int>(response().getStatus())));
statusAndReason += " - ";
statusAndReason += response().getReason();
std::string page("<HTML><HEAD><TITLE>");
page += statusAndReason;
page += "</TITLE></HEAD><BODY><H1>";
page += statusAndReason;
page += "</H1>";
page += "<P>";
page += message;
page += "</P></BODY></HTML>";
response().sendBuffer(page.data(), page.size());
}
if (request.getURI() != ExpectedURI)
{
sendErrorResponse(HTTPResponse::HTTP_NOT_FOUND, request.getURI());
}