From e8f5a3b44ec1eeafb49d0adb59165c06ec365634 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 9 Jul 2023 14:12:08 +0800 Subject: [PATCH] Fix response for HTTP HEAD method Closes #19288. --- src/base/http/connection.cpp | 26 +++++++++++++++++++++----- src/base/http/responsegenerator.cpp | 5 +++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/base/http/connection.cpp b/src/base/http/connection.cpp index 617b64163..ff39cccbc 100644 --- a/src/base/http/connection.cpp +++ b/src/base/http/connection.cpp @@ -123,14 +123,30 @@ void Connection::read() { const Environment env {m_socket->localAddress(), m_socket->localPort(), m_socket->peerAddress(), m_socket->peerPort()}; - Response resp = m_requestHandler->processRequest(result.request, env); + if (result.request.method == HEADER_REQUEST_METHOD_HEAD) + { + Request getRequest = result.request; + getRequest.method = HEADER_REQUEST_METHOD_GET; - if (acceptsGzipEncoding(result.request.headers[u"accept-encoding"_s])) - resp.headers[HEADER_CONTENT_ENCODING] = u"gzip"_s; + Response resp = m_requestHandler->processRequest(getRequest, env); - resp.headers[HEADER_CONNECTION] = u"keep-alive"_s; + resp.headers[HEADER_CONNECTION] = u"keep-alive"_s; + resp.headers[HEADER_CONTENT_LENGTH] = QString::number(resp.content.length()); + resp.content.clear(); + + sendResponse(resp); + } + else + { + Response resp = m_requestHandler->processRequest(result.request, env); + + if (acceptsGzipEncoding(result.request.headers.value(u"accept-encoding"_s))) + resp.headers[HEADER_CONTENT_ENCODING] = u"gzip"_s; + resp.headers[HEADER_CONNECTION] = u"keep-alive"_s; + + sendResponse(resp); + } - sendResponse(resp); m_receivedData.remove(0, result.frameSize); } break; diff --git a/src/base/http/responsegenerator.cpp b/src/base/http/responsegenerator.cpp index a152e7fe2..b34b973db 100644 --- a/src/base/http/responsegenerator.cpp +++ b/src/base/http/responsegenerator.cpp @@ -38,8 +38,9 @@ QByteArray Http::toByteArray(Response response) { compressContent(response); - response.headers[HEADER_CONTENT_LENGTH] = QString::number(response.content.length()); response.headers[HEADER_DATE] = httpDate(); + if (QString &value = response.headers[HEADER_CONTENT_LENGTH]; value.isEmpty()) + value = QString::number(response.content.length()); QByteArray buf; buf.reserve(1024 + response.content.length()); @@ -63,7 +64,7 @@ QByteArray Http::toByteArray(Response response) // the first empty line buf += CRLF; - // message body // TODO: support HEAD request + // message body buf += response.content; return buf;