Fix response for HTTP HEAD method

Closes #19288.
This commit is contained in:
Chocobo1 2023-07-09 14:12:08 +08:00
parent fff7b1dcbd
commit e8f5a3b44e
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
2 changed files with 24 additions and 7 deletions

View file

@ -123,14 +123,30 @@ void Connection::read()
{ {
const Environment env {m_socket->localAddress(), m_socket->localPort(), m_socket->peerAddress(), m_socket->peerPort()}; const Environment env {m_socket->localAddress(), m_socket->localPort(), m_socket->peerAddress(), m_socket->peerPort()};
if (result.request.method == HEADER_REQUEST_METHOD_HEAD)
{
Request getRequest = result.request;
getRequest.method = HEADER_REQUEST_METHOD_GET;
Response resp = m_requestHandler->processRequest(getRequest, env);
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); Response resp = m_requestHandler->processRequest(result.request, env);
if (acceptsGzipEncoding(result.request.headers[u"accept-encoding"_s])) if (acceptsGzipEncoding(result.request.headers.value(u"accept-encoding"_s)))
resp.headers[HEADER_CONTENT_ENCODING] = u"gzip"_s; resp.headers[HEADER_CONTENT_ENCODING] = u"gzip"_s;
resp.headers[HEADER_CONNECTION] = u"keep-alive"_s; resp.headers[HEADER_CONNECTION] = u"keep-alive"_s;
sendResponse(resp); sendResponse(resp);
}
m_receivedData.remove(0, result.frameSize); m_receivedData.remove(0, result.frameSize);
} }
break; break;

View file

@ -38,8 +38,9 @@ QByteArray Http::toByteArray(Response response)
{ {
compressContent(response); compressContent(response);
response.headers[HEADER_CONTENT_LENGTH] = QString::number(response.content.length());
response.headers[HEADER_DATE] = httpDate(); response.headers[HEADER_DATE] = httpDate();
if (QString &value = response.headers[HEADER_CONTENT_LENGTH]; value.isEmpty())
value = QString::number(response.content.length());
QByteArray buf; QByteArray buf;
buf.reserve(1024 + response.content.length()); buf.reserve(1024 + response.content.length());
@ -63,7 +64,7 @@ QByteArray Http::toByteArray(Response response)
// the first empty line // the first empty line
buf += CRLF; buf += CRLF;
// message body // TODO: support HEAD request // message body
buf += response.content; buf += response.content;
return buf; return buf;