wsd: http: use named HTTP status-code instead of naked int
Self-documenting, less error-prone, and searchable. Change-Id: Ifc21d7cf5512d9a63c5d5bc10ebc003fcccc93c1 Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>pull/7529/head
parent
f978b0146c
commit
0c59eda780
|
@ -23,8 +23,8 @@
|
|||
|
||||
namespace HttpHelper
|
||||
{
|
||||
void sendError(int errorCode, const std::shared_ptr<StreamSocket>& socket, const std::string& body,
|
||||
const std::string& extraHeader)
|
||||
void sendError(http::StatusCode errorCode, const std::shared_ptr<StreamSocket>& socket,
|
||||
const std::string& body, const std::string& extraHeader)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "HTTP/1.1 " << errorCode << "\r\n"
|
||||
|
@ -36,7 +36,7 @@ void sendError(int errorCode, const std::shared_ptr<StreamSocket>& socket, const
|
|||
socket->send(oss.str());
|
||||
}
|
||||
|
||||
void sendErrorAndShutdown(int errorCode, const std::shared_ptr<StreamSocket>& socket,
|
||||
void sendErrorAndShutdown(http::StatusCode errorCode, const std::shared_ptr<StreamSocket>& socket,
|
||||
const std::string& body, const std::string& extraHeader)
|
||||
{
|
||||
sendError(errorCode, socket, body, extraHeader + "Connection: close\r\n");
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <HttpRequest.hpp>
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace Net
|
||||
|
@ -23,12 +25,12 @@ class StreamSocket;
|
|||
namespace HttpHelper
|
||||
{
|
||||
/// Write headers and body for an error response.
|
||||
void sendError(int errorCode, const std::shared_ptr<StreamSocket>& socket,
|
||||
void sendError(http::StatusCode errorCode, const std::shared_ptr<StreamSocket>& socket,
|
||||
const std::string& body = std::string(),
|
||||
const std::string& extraHeader = std::string());
|
||||
|
||||
/// Write headers and body for an error response. Afterwards, shutdown the socket.
|
||||
void sendErrorAndShutdown(int errorCode, const std::shared_ptr<StreamSocket>& socket,
|
||||
void sendErrorAndShutdown(http::StatusCode errorCode, const std::shared_ptr<StreamSocket>& socket,
|
||||
const std::string& body = std::string(),
|
||||
const std::string& extraHeader = std::string());
|
||||
|
||||
|
|
|
@ -158,7 +158,7 @@ private:
|
|||
catch (const std::exception& exc)
|
||||
{
|
||||
// Bad request.
|
||||
HttpHelper::sendErrorAndShutdown(400, socket);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
||||
|
||||
// NOTE: Check _wsState to choose between HTTP response or WebSocket (app-level) error.
|
||||
LOG_INF('#' << socket->getFD() << " Exception while processing incoming request: [" <<
|
||||
|
|
|
@ -4043,7 +4043,7 @@ private:
|
|||
if (!COOLWSD::isSSLEnabled() && socket->sniffSSL())
|
||||
{
|
||||
LOG_ERR("Looks like SSL/TLS traffic on plain http port");
|
||||
HttpHelper::sendErrorAndShutdown(400, socket);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -4238,7 +4238,7 @@ private:
|
|||
LOG_ERR("Unknown resource: " << requestDetails.toString());
|
||||
|
||||
// Bad request.
|
||||
HttpHelper::sendErrorAndShutdown(400, socket);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -4249,7 +4249,7 @@ private:
|
|||
<< "]: " << ex.what());
|
||||
|
||||
// Bad request.
|
||||
HttpHelper::sendErrorAndShutdown(400, socket);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
||||
return;
|
||||
}
|
||||
catch (const std::exception& exc)
|
||||
|
@ -4500,7 +4500,7 @@ private:
|
|||
std::string errMsg = "Empty clipboard item / session tag " + tag;
|
||||
|
||||
// Bad request.
|
||||
HttpHelper::sendErrorAndShutdown(400, socket, errMsg);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket, errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5119,7 +5119,7 @@ private:
|
|||
<< ". Terminating connection. Error: " << exc.what());
|
||||
}
|
||||
// badness occurred:
|
||||
HttpHelper::sendErrorAndShutdown(400, streamSocket);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, streamSocket);
|
||||
});
|
||||
}
|
||||
else
|
||||
|
@ -5127,7 +5127,7 @@ private:
|
|||
auto streamSocket = std::static_pointer_cast<StreamSocket>(disposition.getSocket());
|
||||
LOG_ERR("Failed to find document");
|
||||
// badness occurred:
|
||||
HttpHelper::sendErrorAndShutdown(400, streamSocket);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, streamSocket);
|
||||
// FIXME: send docunloading & re-try on client ?
|
||||
}
|
||||
}
|
||||
|
|
|
@ -259,9 +259,9 @@ void ClientSession::handleClipboardRequest(DocumentBroker::ClipboardRequest
|
|||
return; // the getclipboard already completed.
|
||||
if (type == DocumentBroker::CLIP_REQUEST_SET)
|
||||
{
|
||||
#if !MOBILEAPP
|
||||
HttpHelper::sendErrorAndShutdown(400, socket);
|
||||
#endif
|
||||
#if !MOBILEAPP
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
||||
#endif
|
||||
}
|
||||
else // will be handled during shutdown
|
||||
{
|
||||
|
@ -320,9 +320,9 @@ void ClientSession::handleClipboardRequest(DocumentBroker::ClipboardRequest
|
|||
}
|
||||
else
|
||||
{
|
||||
#if !MOBILEAPP
|
||||
HttpHelper::sendErrorAndShutdown(400, socket);
|
||||
#endif
|
||||
#if !MOBILEAPP
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3247,7 +3247,8 @@ bool DocumentBroker::lookupSendClipboardTag(const std::shared_ptr<StreamSocket>
|
|||
|
||||
#if !MOBILEAPP
|
||||
// Bad request.
|
||||
HttpHelper::sendError(400, socket, "Failed to find this clipboard", "Connection: close\r\n");
|
||||
HttpHelper::sendError(http::StatusCode::BadRequest, socket, "Failed to find this clipboard",
|
||||
"Connection: close\r\n");
|
||||
#endif
|
||||
socket->shutdown();
|
||||
socket->ignoreInput();
|
||||
|
|
|
@ -621,7 +621,8 @@ void FileServerRequestHandler::handleRequest(const HTTPRequest& request,
|
|||
"Expires: " + Poco::DateTimeFormatter::format(
|
||||
later, Poco::DateTimeFormat::HTTP_FORMAT) + "\r\n" +
|
||||
"Cache-Control: max-age=11059200\r\n";
|
||||
HttpHelper::sendErrorAndShutdown(304, socket, std::string(), extraHeaders);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::NotModified, socket,
|
||||
std::string(), extraHeaders);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -671,31 +672,35 @@ void FileServerRequestHandler::handleRequest(const HTTPRequest& request,
|
|||
catch (const Poco::Net::NotAuthenticatedException& exc)
|
||||
{
|
||||
LOG_ERR("FileServerRequestHandler::NotAuthenticated: " << exc.displayText());
|
||||
sendError(401, request, socket, "", "", "WWW-authenticate: Basic realm=\"online\"\r\n");
|
||||
sendError(http::StatusCode::Unauthorized, request, socket, "", "",
|
||||
"WWW-authenticate: Basic realm=\"online\"\r\n");
|
||||
}
|
||||
catch (const Poco::FileAccessDeniedException& exc)
|
||||
{
|
||||
LOG_ERR("FileServerRequestHandler: " << exc.displayText());
|
||||
sendError(403, request, socket, "403 - Access denied!",
|
||||
sendError(http::StatusCode::Forbidden, request, socket, "403 - Access denied!",
|
||||
"You are unable to access");
|
||||
}
|
||||
catch (const Poco::FileNotFoundException& exc)
|
||||
{
|
||||
LOG_ERR("FileServerRequestHandler: " << exc.displayText());
|
||||
sendError(404, request, socket, "404 - file not found!",
|
||||
sendError(http::StatusCode::NotFound, request, socket, "404 - file not found!",
|
||||
"There seems to be a problem locating");
|
||||
}
|
||||
catch (Poco::SyntaxException& exc)
|
||||
{
|
||||
LOG_ERR("Incorrect config value: " << exc.displayText());
|
||||
sendError(500, request, socket, "500 - Internal Server Error!",
|
||||
sendError(http::StatusCode::InternalServerError, request, socket,
|
||||
"500 - Internal Server Error!",
|
||||
"Cannot process the request - " + exc.displayText());
|
||||
}
|
||||
}
|
||||
|
||||
void FileServerRequestHandler::sendError(int errorCode, const Poco::Net::HTTPRequest& request,
|
||||
void FileServerRequestHandler::sendError(http::StatusCode errorCode,
|
||||
const Poco::Net::HTTPRequest& request,
|
||||
const std::shared_ptr<StreamSocket>& socket,
|
||||
const std::string& shortMessage, const std::string& longMessage,
|
||||
const std::string& shortMessage,
|
||||
const std::string& longMessage,
|
||||
const std::string& extraHeader)
|
||||
{
|
||||
std::string body;
|
||||
|
|
|
@ -8,7 +8,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "Socket.hpp"
|
||||
|
||||
#include <HttpRequest.hpp>
|
||||
#include <Socket.hpp>
|
||||
|
||||
#include <Poco/MemoryStream.h>
|
||||
#include <Poco/Util/LayeredConfiguration.h>
|
||||
|
@ -67,9 +69,10 @@ public:
|
|||
|
||||
private:
|
||||
static std::map<std::string, std::pair<std::string, std::string>> FileHash;
|
||||
static void sendError(int errorCode, const Poco::Net::HTTPRequest& request,
|
||||
const std::shared_ptr<StreamSocket>& socket, const std::string& shortMessage,
|
||||
const std::string& longMessage, const std::string& extraHeader = "");
|
||||
static void sendError(http::StatusCode errorCode, const Poco::Net::HTTPRequest& request,
|
||||
const std::shared_ptr<StreamSocket>& socket,
|
||||
const std::string& shortMessage, const std::string& longMessage,
|
||||
const std::string& extraHeader = std::string());
|
||||
};
|
||||
|
||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
||||
|
|
|
@ -67,25 +67,25 @@ void ProxyRequestHandler::handleRequest(const std::string& relPath,
|
|||
}
|
||||
else
|
||||
{
|
||||
HttpHelper::sendErrorAndShutdown(400, socket);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
||||
}
|
||||
}
|
||||
catch(std::exception& exc)
|
||||
{
|
||||
LOG_ERR("ProxyCallback: " << exc.what());
|
||||
HttpHelper::sendErrorAndShutdown(400, socket);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
LOG_ERR("ProxyCallback: Unknown exception");
|
||||
HttpHelper::sendErrorAndShutdown(400, socket);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
||||
}
|
||||
};
|
||||
|
||||
sessionProxy->setFinishedHandler(proxyCallback);
|
||||
if (!sessionProxy->asyncRequest(requestProxy, *COOLWSD::getWebServerPoll()))
|
||||
{
|
||||
HttpHelper::sendErrorAndShutdown(400, socket);
|
||||
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue