mirror of
https://github.com/QuasarApp/qthttpserver.git
synced 2025-05-18 20:39:40 +00:00
Small refactoring of QHttpServerRequest and QAbstractHttpServer
1. Move initialization of httpParser from QAbstractHttpServer to QHttpServerRequest. 2. Simplify access to an upgrade header value from a request. httpParser sets an upgrade flag only if the request has two headers or the request is HTTP CONNECT method. Change-Id: I39c2325558a58679e40d38e46706cd61ef36d327 Reviewed-by: Mikhail Svetkin <mikhail.svetkin@gmail.com>
This commit is contained in:
parent
a96d975ea8
commit
65ba5db9e0
@ -58,8 +58,6 @@ void QAbstractHttpServerPrivate::handleNewConnections()
|
|||||||
Q_ASSERT(tcpServer);
|
Q_ASSERT(tcpServer);
|
||||||
while (auto socket = tcpServer->nextPendingConnection()) {
|
while (auto socket = tcpServer->nextPendingConnection()) {
|
||||||
auto request = new QHttpServerRequest(socket->peerAddress()); // TODO own tcp server could pre-allocate it
|
auto request = new QHttpServerRequest(socket->peerAddress()); // TODO own tcp server could pre-allocate it
|
||||||
http_parser_init(&request->d->httpParser, HTTP_REQUEST);
|
|
||||||
|
|
||||||
QObject::connect(socket, &QTcpSocket::readyRead, q_ptr,
|
QObject::connect(socket, &QTcpSocket::readyRead, q_ptr,
|
||||||
[this, request, socket] () {
|
[this, request, socket] () {
|
||||||
handleReadyRead(socket, request);
|
handleReadyRead(socket, request);
|
||||||
@ -94,13 +92,11 @@ void QAbstractHttpServerPrivate::handleReadyRead(QTcpSocket *socket,
|
|||||||
request->d->state != QHttpServerRequestPrivate::State::OnMessageComplete)
|
request->d->state != QHttpServerRequestPrivate::State::OnMessageComplete)
|
||||||
return; // Partial read
|
return; // Partial read
|
||||||
|
|
||||||
if (request->d->httpParser.upgrade) { // Upgrade
|
if (request->d->httpParser.upgrade &&
|
||||||
const auto &headers = request->d->headers;
|
request->d->httpParser.method != HTTP_CONNECT) { // Upgrade
|
||||||
const auto upgradeHash = request->d->headerHash(QByteArrayLiteral("upgrade"));
|
const auto &upgradeValue = request->value(QByteArrayLiteral("upgrade"));
|
||||||
const auto it = headers.find(upgradeHash);
|
|
||||||
if (it != headers.end()) {
|
|
||||||
#if defined(QT_WEBSOCKETS_LIB)
|
#if defined(QT_WEBSOCKETS_LIB)
|
||||||
if (it.value().second.compare(QByteArrayLiteral("websocket"), Qt::CaseInsensitive) == 0) {
|
if (upgradeValue.compare(QByteArrayLiteral("websocket"), Qt::CaseInsensitive) == 0) {
|
||||||
static const auto signal = QMetaMethod::fromSignal(
|
static const auto signal = QMetaMethod::fromSignal(
|
||||||
&QAbstractHttpServer::newWebSocketConnection);
|
&QAbstractHttpServer::newWebSocketConnection);
|
||||||
if (q->isSignalConnected(signal)) {
|
if (q->isSignalConnected(signal)) {
|
||||||
@ -116,8 +112,7 @@ void QAbstractHttpServerPrivate::handleReadyRead(QTcpSocket *socket,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
qCWarning(lcHttpServer, "Upgrade to %s not supported", it.value().second.constData());
|
qCWarning(lcHttpServer, "Upgrade to %s not supported", upgradeValue.constData());
|
||||||
}
|
|
||||||
socket->disconnectFromHost();
|
socket->disconnectFromHost();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -94,6 +94,7 @@ QHttpServerRequestPrivate::QHttpServerRequestPrivate(const QHostAddress &remoteA
|
|||||||
: remoteAddress(remoteAddress)
|
: remoteAddress(remoteAddress)
|
||||||
{
|
{
|
||||||
httpParser.data = this;
|
httpParser.data = this;
|
||||||
|
http_parser_init(&httpParser, HTTP_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray QHttpServerRequestPrivate::header(const QByteArray &key) const
|
QByteArray QHttpServerRequestPrivate::header(const QByteArray &key) const
|
||||||
|
Loading…
x
Reference in New Issue
Block a user