Browse Source

Response proper error status for invalid request methods

adaptive-webui-19844
Chocobo1 1 year ago
parent
commit
cb0c09769f
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 13
      src/base/http/connection.cpp
  2. 4
      src/base/http/requestparser.cpp
  3. 1
      src/base/http/requestparser.h

13
src/base/http/connection.cpp

@ -106,6 +106,19 @@ void Connection::read() @@ -106,6 +106,19 @@ void Connection::read()
}
return;
case RequestParser::ParseStatus::BadMethod:
{
LogMsg(tr("Bad Http request method, closing socket. IP: %1. Method: \"%2\"")
.arg(m_socket->peerAddress().toString(), result.request.method), Log::WARNING);
Response resp(501, u"Not Implemented"_s);
resp.headers[HEADER_CONNECTION] = u"close"_s;
sendResponse(resp);
m_socket->close();
}
return;
case RequestParser::ParseStatus::BadRequest:
{
LogMsg(tr("Bad Http request, closing socket. IP: %1")

4
src/base/http/requestparser.cpp

@ -103,6 +103,7 @@ RequestParser::ParseResult RequestParser::doParse(const QByteArray &data) @@ -103,6 +103,7 @@ RequestParser::ParseResult RequestParser::doParse(const QByteArray &data)
// handle supported methods
if ((m_request.method == HEADER_REQUEST_METHOD_GET) || (m_request.method == HEADER_REQUEST_METHOD_HEAD))
return {ParseStatus::OK, m_request, headerLength};
if (m_request.method == HEADER_REQUEST_METHOD_POST)
{
const auto parseContentLength = [this]() -> int
@ -146,8 +147,7 @@ RequestParser::ParseResult RequestParser::doParse(const QByteArray &data) @@ -146,8 +147,7 @@ RequestParser::ParseResult RequestParser::doParse(const QByteArray &data)
return {ParseStatus::OK, m_request, (headerLength + contentLength)};
}
qWarning() << Q_FUNC_INFO << "unsupported request method: " << m_request.method;
return {ParseStatus::BadRequest, Request(), 0}; // TODO: SHOULD respond "501 Not Implemented"
return {ParseStatus::BadMethod, m_request, 0};
}
bool RequestParser::parseStartLines(const QStringView data)

1
src/base/http/requestparser.h

@ -41,6 +41,7 @@ namespace Http @@ -41,6 +41,7 @@ namespace Http
{
OK,
Incomplete,
BadMethod,
BadRequest
};

Loading…
Cancel
Save