diff options
Diffstat (limited to 'tests/auto/network/access/http2')
-rw-r--r-- | tests/auto/network/access/http2/http2srv.cpp | 217 | ||||
-rw-r--r-- | tests/auto/network/access/http2/http2srv.h | 46 | ||||
-rw-r--r-- | tests/auto/network/access/http2/tst_http2.cpp | 94 |
3 files changed, 268 insertions, 89 deletions
diff --git a/tests/auto/network/access/http2/http2srv.cpp b/tests/auto/network/access/http2/http2srv.cpp index d0686eb01c..69e480b164 100644 --- a/tests/auto/network/access/http2/http2srv.cpp +++ b/tests/auto/network/access/http2/http2srv.cpp @@ -76,13 +76,11 @@ void fill_push_header(const HttpHeader &originalRequest, HttpHeader &promisedReq } -Http2Server::Http2Server(bool h2c, const Http2Settings &ss, const Http2Settings &cs) +Http2Server::Http2Server(bool h2c, const Http2::RawSettings &ss, const Http2::RawSettings &cs) : serverSettings(ss), + expectedClientSettings(cs), clearTextHTTP2(h2c) { - for (const auto &s : cs) - expectedClientSettings[quint16(s.identifier)] = s.value; - responseBody = "<html>\n" "<head>\n" "<title>Sample \"Hello, World\" Application</title>\n" @@ -132,8 +130,23 @@ void Http2Server::startServer() if (!clearTextHTTP2) return; #endif - if (listen()) + if (listen()) { + if (clearTextHTTP2) + authority = QStringLiteral("127.0.0.1:%1").arg(serverPort()).toLatin1(); emit serverStarted(serverPort()); + } +} + +bool Http2Server::sendProtocolSwitchReply() +{ + Q_ASSERT(socket); + Q_ASSERT(clearTextHTTP2 && upgradeProtocol); + // The first and the last HTTP/1.1 response we send: + const char response[] = "HTTP/1.1 101 Switching Protocols\r\n" + "Connection: Upgrade\r\n" + "Upgrade: h2c\r\n\r\n"; + const qint64 size = sizeof response - 1; + return socket->write(response, size) == size; } void Http2Server::sendServerSettings() @@ -144,11 +157,11 @@ void Http2Server::sendServerSettings() return; writer.start(FrameType::SETTINGS, FrameFlag::EMPTY, connectionStreamID); - for (const auto &s : serverSettings) { - writer.append(s.identifier); - writer.append(s.value); - if (s.identifier == Settings::INITIAL_WINDOW_SIZE_ID) - streamRecvWindowSize = s.value; + for (auto it = serverSettings.cbegin(); it != serverSettings.cend(); ++it) { + writer.append(it.key()); + writer.append(it.value()); + if (it.key() == Settings::INITIAL_WINDOW_SIZE_ID) + streamRecvWindowSize = it.value(); } writer.write(*socket); // Now, let's update our peer on a session recv window size: @@ -232,6 +245,7 @@ void Http2Server::incomingConnection(qintptr socketDescriptor) Q_ASSERT(set); // Stop listening: close(); + upgradeProtocol = true; QMetaObject::invokeMethod(this, "connectionEstablished", Qt::QueuedConnection); } else { @@ -269,25 +283,83 @@ void Http2Server::incomingConnection(qintptr socketDescriptor) quint32 Http2Server::clientSetting(Http2::Settings identifier, quint32 defaultValue) { - const auto it = expectedClientSettings.find(quint16(identifier)); + const auto it = expectedClientSettings.find(identifier); if (it != expectedClientSettings.end()) - return it->second; + return it.value(); return defaultValue; } +bool Http2Server::readMethodLine() +{ + // We know for sure that Qt did the right thing sending us the correct + // Request-line with CRLF at the end ... + // We're overly simplistic here but all we need to know - the method. + while (socket->bytesAvailable()) { + char c = 0; + if (socket->read(&c, 1) != 1) + return false; + if (c == '\n' && requestLine.endsWith('\r')) { + if (requestLine.startsWith("GET")) + requestType = QHttpNetworkRequest::Get; + else if (requestLine.startsWith("POST")) + requestType = QHttpNetworkRequest::Post; + else + requestType = QHttpNetworkRequest::Custom; // 'invalid'. + requestLine.clear(); + + return true; + } else { + requestLine.append(c); + } + } + + return false; +} + +bool Http2Server::verifyProtocolUpgradeRequest() +{ + Q_ASSERT(protocolUpgradeHandler.data()); + + bool connectionOk = false; + bool upgradeOk = false; + bool settingsOk = false; + + QHttpNetworkReplyPrivate *firstRequestReader = protocolUpgradeHandler->d_func(); + + // That's how we append them, that's what I expect to find: + for (const auto &header : firstRequestReader->fields) { + if (header.first == "Connection") + connectionOk = header.second.contains("Upgrade, HTTP2-Settings"); + else if (header.first == "Upgrade") + upgradeOk = header.second.contains("h2c"); + else if (header.first == "HTTP2-Settings") + settingsOk = true; + } + + return connectionOk && upgradeOk && settingsOk; +} + +void Http2Server::triggerGOAWAYEmulation() +{ + Q_ASSERT(testingGOAWAY); + auto timer = new QTimer(this); + timer->setSingleShot(true); + connect(timer, &QTimer::timeout, [this]() { + sendGOAWAY(quint32(connectionStreamID), quint32(INTERNAL_ERROR), 0); + }); + timer->start(goawayTimeout); +} + void Http2Server::connectionEstablished() { using namespace Http2; - if (testingGOAWAY) { - auto timer = new QTimer(this); - timer->setSingleShot(true); - connect(timer, &QTimer::timeout, [this]() { - sendGOAWAY(quint32(connectionStreamID), quint32(INTERNAL_ERROR), 0); - }); - timer->start(goawayTimeout); - return; - } + if (testingGOAWAY && !clearTextHTTP2) + return triggerGOAWAYEmulation(); + + // For clearTextHTTP2 we first have to respond with 'protocol switch' + // and then continue with whatever logic we have (testingGOAWAY or not), + // otherwise our 'peer' cannot process HTTP/2 frames yet. connect(socket.data(), SIGNAL(readyRead()), this, SLOT(readReady())); @@ -296,9 +368,17 @@ void Http2Server::connectionEstablished() waitingClientAck = false; waitingClientSettings = false; settingsSent = false; - // We immediately send our settings so that our client - // can use flow control correctly. - sendServerSettings(); + + if (clearTextHTTP2) { + requestLine.clear(); + // Now we have to handle HTTP/1.1 request. We use Get/Post in our test, + // so set requestType to something unsupported: + requestType = QHttpNetworkRequest::Options; + } else { + // We immediately send our settings so that our client + // can use flow control correctly. + sendServerSettings(); + } if (socket->bytesAvailable()) readReady(); @@ -328,7 +408,9 @@ void Http2Server::readReady() if (connectionError) return; - if (waitingClientPreface) { + if (upgradeProtocol) { + handleProtocolUpgrade(); + } else if (waitingClientPreface) { handleConnectionPreface(); } else { const auto status = reader.read(*socket); @@ -348,6 +430,79 @@ void Http2Server::readReady() QMetaObject::invokeMethod(this, "readReady", Qt::QueuedConnection); } +void Http2Server::handleProtocolUpgrade() +{ + using ReplyPrivate = QHttpNetworkReplyPrivate; + Q_ASSERT(upgradeProtocol); + + if (!protocolUpgradeHandler.data()) + protocolUpgradeHandler.reset(new Http11Reply); + + QHttpNetworkReplyPrivate *firstRequestReader = protocolUpgradeHandler->d_func(); + + // QHttpNetworkReplyPrivate parses ... reply. It will, unfortunately, fail + // on the first line ... which is a part of request. So we read this line + // and extract the method first. + if (firstRequestReader->state == ReplyPrivate::NothingDoneState) { + if (!readMethodLine()) + return; + + if (requestType != QHttpNetworkRequest::Get && requestType != QHttpNetworkRequest::Post) { + emit invalidRequest(1); + return; + } + + firstRequestReader->state = ReplyPrivate::ReadingHeaderState; + } + + if (!socket->bytesAvailable()) + return; + + if (firstRequestReader->state == ReplyPrivate::ReadingHeaderState) + firstRequestReader->readHeader(socket.data()); + else if (firstRequestReader->state == ReplyPrivate::ReadingDataState) + firstRequestReader->readBodyFast(socket.data(), &firstRequestReader->responseData); + + switch (firstRequestReader->state) { + case ReplyPrivate::ReadingHeaderState: + return; + case ReplyPrivate::ReadingDataState: + if (requestType == QHttpNetworkRequest::Post) + return; + break; + case ReplyPrivate::AllDoneState: + break; + default: + socket->close(); + return; + } + + if (!verifyProtocolUpgradeRequest() || !sendProtocolSwitchReply()) { + socket->close(); + return; + } + + upgradeProtocol = false; + protocolUpgradeHandler.reset(nullptr); + + if (testingGOAWAY) + return triggerGOAWAYEmulation(); + + // HTTP/1.1 'fields' we have in firstRequestRead are useless (they are not + // even allowed in HTTP/2 header). Let's pretend we have received + // valid HTTP/2 headers and can extract fields we need: + HttpHeader h2header; + h2header.push_back(HeaderField(":scheme", "http")); // we are in clearTextHTTP2 mode. + h2header.push_back(HeaderField(":authority", authority)); + activeRequests[1] = std::move(h2header); + // After protocol switch we immediately send our SETTINGS. + sendServerSettings(); + if (requestType == QHttpNetworkRequest::Get) + emit receivedRequest(1); + else + emit receivedData(1); +} + void Http2Server::handleConnectionPreface() { Q_ASSERT(waitingClientPreface); @@ -382,6 +537,16 @@ void Http2Server::handleIncomingFrame() // 7. RST_STREAM // 8. GOAWAY + if (testingGOAWAY) { + // GOAWAY test is simplistic for now: after HTTP/2 was + // negotiated (via ALPN/NPN or a protocol switch), send + // a GOAWAY frame after some (probably non-zero) timeout. + // We do not handle any frames, but timeout gives QNAM + // more time to initiate more streams and thus make the + // test more interesting/complex (on a client side). + return; + } + inboundFrame = std::move(reader.inboundFrame()); if (continuedRequest.size()) { @@ -456,7 +621,7 @@ void Http2Server::handleSETTINGS() const auto notFound = expectedClientSettings.end(); while (src != end) { - const auto id = qFromBigEndian<quint16>(src); + const auto id = Http2::Settings(qFromBigEndian<quint16>(src)); const auto value = qFromBigEndian<quint32>(src + 2); if (expectedClientSettings.find(id) == notFound || expectedClientSettings[id] != value) { diff --git a/tests/auto/network/access/http2/http2srv.h b/tests/auto/network/access/http2/http2srv.h index 63a4a4c8e9..ff4a1319e2 100644 --- a/tests/auto/network/access/http2/http2srv.h +++ b/tests/auto/network/access/http2/http2srv.h @@ -29,11 +29,14 @@ #ifndef HTTP2SRV_H #define HTTP2SRV_H +#include <QtNetwork/private/qhttpnetworkrequest_p.h> +#include <QtNetwork/private/qhttpnetworkreply_p.h> #include <QtNetwork/private/http2protocol_p.h> #include <QtNetwork/private/http2frames_p.h> #include <QtNetwork/private/hpack_p.h> #include <QtNetwork/qabstractsocket.h> +#include <QtCore/qsharedpointer.h> #include <QtCore/qscopedpointer.h> #include <QtNetwork/qtcpserver.h> #include <QtCore/qbytearray.h> @@ -45,25 +48,25 @@ QT_BEGIN_NAMESPACE -struct Http2Setting +// At the moment we do not have any public API parsing HTTP headers. Even worse - +// the code that can do this exists only in QHttpNetworkReplyPrivate class. +// To be able to access reply's d_func() we have these classes: +class Http11ReplyPrivate : public QHttpNetworkReplyPrivate { - Http2::Settings identifier; - quint32 value = 0; - - Http2Setting(Http2::Settings ident, quint32 v) - : identifier(ident), - value(v) - {} }; -using Http2Settings = std::vector<Http2Setting>; +class Http11Reply : public QHttpNetworkReply +{ +public: + Q_DECLARE_PRIVATE(Http11Reply) +}; class Http2Server : public QTcpServer { Q_OBJECT public: - Http2Server(bool clearText, const Http2Settings &serverSettings, - const Http2Settings &clientSettings); + Http2Server(bool clearText, const Http2::RawSettings &serverSettings, + const Http2::RawSettings &clientSettings); ~Http2Server(); @@ -75,6 +78,7 @@ public: // Invokables, since we can call them from the main thread, // but server (can) work on its own thread. Q_INVOKABLE void startServer(); + bool sendProtocolSwitchReply(); Q_INVOKABLE void sendServerSettings(); Q_INVOKABLE void sendGOAWAY(quint32 streamID, quint32 error, quint32 lastStreamID); @@ -82,6 +86,7 @@ public: Q_INVOKABLE void sendDATA(quint32 streamID, quint32 windowSize); Q_INVOKABLE void sendWINDOW_UPDATE(quint32 streamID, quint32 delta); + Q_INVOKABLE void handleProtocolUpgrade(); Q_INVOKABLE void handleConnectionPreface(); Q_INVOKABLE void handleIncomingFrame(); Q_INVOKABLE void handleSETTINGS(); @@ -114,6 +119,9 @@ private: void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE; quint32 clientSetting(Http2::Settings identifier, quint32 defaultValue); + bool readMethodLine(); + bool verifyProtocolUpgradeRequest(); + void triggerGOAWAYEmulation(); QScopedPointer<QAbstractSocket> socket; @@ -123,8 +131,8 @@ private: bool settingsSent = false; bool waitingClientAck = false; - Http2Settings serverSettings; - std::map<quint16, quint32> expectedClientSettings; + Http2::RawSettings serverSettings; + Http2::RawSettings expectedClientSettings; bool connectionError = false; @@ -166,6 +174,18 @@ private: bool testingGOAWAY = false; int goawayTimeout = 0; + // Clear text HTTP/2, we have to deal with the protocol upgrade request + // from the initial HTTP/1.1 request. + bool upgradeProtocol = false; + QByteArray requestLine; + QHttpNetworkRequest::Operation requestType; + // We need QHttpNetworkReply (actually its private d-object) to handle the + // first HTTP/1.1 request. QHttpNetworkReplyPrivate does parsing + in case + // of POST it is also reading the body for us. + QScopedPointer<Http11Reply> protocolUpgradeHandler; + // We need it for PUSH_PROMISE, with the correct port number appended, + // when replying to essentially 1.1 request. + QByteArray authority; protected slots: void ignoreErrorSlot(); }; diff --git a/tests/auto/network/access/http2/tst_http2.cpp b/tests/auto/network/access/http2/tst_http2.cpp index d7a57f5e26..51e1849512 100644 --- a/tests/auto/network/access/http2/tst_http2.cpp +++ b/tests/auto/network/access/http2/tst_http2.cpp @@ -30,6 +30,7 @@ #include "http2srv.h" +#include <QtNetwork/private/http2protocol_p.h> #include <QtNetwork/qnetworkaccessmanager.h> #include <QtNetwork/qnetworkrequest.h> #include <QtNetwork/qnetworkreply.h> @@ -47,10 +48,12 @@ #include <cstdlib> #include <string> -// At the moment our HTTP/2 imlpementation requires ALPN and this means OpenSSL. #if !defined(QT_NO_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(OPENSSL_NO_TLSEXT) +// HTTP/2 over TLS requires ALPN/NPN to negotiate the protocol version. const bool clearTextHTTP2 = false; #else +// No ALPN/NPN support to negotiate HTTP/2, we'll use cleartext 'h2c' with +// a protocol upgrade procedure. const bool clearTextHTTP2 = true; #endif @@ -94,8 +97,8 @@ private: // small payload. void runEventLoop(int ms = 5000); void stopEventLoop(); - Http2Server *newServer(const Http2Settings &serverSettings, - const Http2Settings &clientSettings = defaultClientSettings); + Http2Server *newServer(const Http2::RawSettings &serverSettings, + const Http2::ProtocolParameters &clientSettings = {}); // Send a get or post request, depending on a payload (empty or not). void sendRequest(int streamNumber, QNetworkRequest::Priority priority = QNetworkRequest::NormalPriority, @@ -116,13 +119,10 @@ private: bool prefaceOK = false; bool serverGotSettingsACK = false; - static const Http2Settings defaultServerSettings; - static const Http2Settings defaultClientSettings; + static const Http2::RawSettings defaultServerSettings; }; -const Http2Settings tst_Http2::defaultServerSettings{{Http2::Settings::MAX_CONCURRENT_STREAMS_ID, 100}}; -const Http2Settings tst_Http2::defaultClientSettings{{Http2::Settings::MAX_FRAME_SIZE_ID, quint32(Http2::maxFrameSize)}, - {Http2::Settings::ENABLE_PUSH_ID, quint32(0)}}; +const Http2::RawSettings tst_Http2::defaultServerSettings{{Http2::Settings::MAX_CONCURRENT_STREAMS_ID, 100}}; namespace { @@ -139,27 +139,6 @@ struct ServerDeleter using ServerPtr = QScopedPointer<Http2Server, ServerDeleter>; -struct EnvVarGuard -{ - EnvVarGuard(const char *name, const QByteArray &value) - : varName(name), - prevValue(qgetenv(name)) - { - Q_ASSERT(name); - qputenv(name, value); - } - ~EnvVarGuard() - { - if (prevValue.size()) - qputenv(varName.c_str(), prevValue); - else - qunsetenv(varName.c_str()); - } - - const std::string varName; - const QByteArray prevValue; -}; - } // unnamed namespace tst_Http2::tst_Http2() @@ -238,12 +217,14 @@ void tst_Http2::multipleRequests() // Just to make the order a bit more interesting // we'll index this randomly: - QNetworkRequest::Priority priorities[] = {QNetworkRequest::HighPriority, - QNetworkRequest::NormalPriority, - QNetworkRequest::LowPriority}; + const QNetworkRequest::Priority priorities[] = { + QNetworkRequest::HighPriority, + QNetworkRequest::NormalPriority, + QNetworkRequest::LowPriority + }; for (int i = 0; i < nRequests; ++i) - sendRequest(i, priorities[std::rand() % 3]); + sendRequest(i, priorities[QRandomGenerator::global()->bounded(3)]); runEventLoop(); @@ -255,11 +236,11 @@ void tst_Http2::multipleRequests() void tst_Http2::flowControlClientSide() { // Create a server but impose limits: - // 1. Small MAX frame size, so we test CONTINUATION frames. - // 2. Small client windows so server responses cause client streams - // to suspend and server sends WINDOW_UPDATE frames. - // 3. Few concurrent streams, to test protocol handler can resume - // suspended requests. + // 1. Small client receive windows so server's responses cause client + // streams to suspend and protocol handler has to send WINDOW_UPDATE + // frames. + // 2. Few concurrent streams supported by the server, to test protocol + // handler in the client can suspend and then resume streams. using namespace Http2; clearHTTP2State(); @@ -268,11 +249,20 @@ void tst_Http2::flowControlClientSide() nRequests = 10; windowUpdates = 0; - const Http2Settings serverSettings = {{Settings::MAX_CONCURRENT_STREAMS_ID, 3}}; + Http2::ProtocolParameters params; + // A small window size for a session, and even a smaller one per stream - + // this will result in WINDOW_UPDATE frames both on connection stream and + // per stream. + params.maxSessionReceiveWindowSize = Http2::defaultSessionWindowSize * 5; + params.settingsFrameData[Settings::INITIAL_WINDOW_SIZE_ID] = Http2::defaultSessionWindowSize; + // Inform our manager about non-default settings: + manager.setProperty(Http2::http2ParametersPropertyName, QVariant::fromValue(params)); - ServerPtr srv(newServer(serverSettings)); + const Http2::RawSettings serverSettings = {{Settings::MAX_CONCURRENT_STREAMS_ID, quint32(3)}}; + ServerPtr srv(newServer(serverSettings, params)); - const QByteArray respond(int(Http2::defaultSessionWindowSize * 50), 'x'); + + const QByteArray respond(int(Http2::defaultSessionWindowSize * 10), 'x'); srv->setResponseBody(respond); QMetaObject::invokeMethod(srv.data(), "startServer", Qt::QueuedConnection); @@ -306,7 +296,7 @@ void tst_Http2::flowControlServerSide() serverPort = 0; nRequests = 30; - const Http2Settings serverSettings = {{Settings::MAX_CONCURRENT_STREAMS_ID, 7}}; + const Http2::RawSettings serverSettings = {{Settings::MAX_CONCURRENT_STREAMS_ID, 7}}; ServerPtr srv(newServer(serverSettings)); @@ -338,11 +328,12 @@ void tst_Http2::pushPromise() serverPort = 0; nRequests = 1; - const EnvVarGuard env("QT_HTTP2_ENABLE_PUSH_PROMISE", "1"); - const Http2Settings clientSettings{{Settings::MAX_FRAME_SIZE_ID, quint32(Http2::maxFrameSize)}, - {Settings::ENABLE_PUSH_ID, quint32(1)}}; + Http2::ProtocolParameters params; + // Defaults are good, except ENABLE_PUSH: + params.settingsFrameData[Settings::ENABLE_PUSH_ID] = 1; + manager.setProperty(Http2::http2ParametersPropertyName, QVariant::fromValue(params)); - ServerPtr srv(newServer(defaultServerSettings, clientSettings)); + ServerPtr srv(newServer(defaultServerSettings, params)); srv->enablePushPromise(true, QByteArray("/script.js")); QMetaObject::invokeMethod(srv.data(), "startServer", Qt::QueuedConnection); @@ -416,7 +407,7 @@ void tst_Http2::goaway() serverPort = 0; nRequests = 3; - ServerPtr srv(newServer(defaultServerSettings, defaultClientSettings)); + ServerPtr srv(newServer(defaultServerSettings)); srv->emulateGOAWAY(responseTimeoutMS); QMetaObject::invokeMethod(srv.data(), "startServer", Qt::QueuedConnection); runEventLoop(); @@ -459,6 +450,7 @@ void tst_Http2::clearHTTP2State() windowUpdates = 0; prefaceOK = false; serverGotSettingsACK = false; + manager.setProperty(Http2::http2ParametersPropertyName, QVariant()); } void tst_Http2::runEventLoop(int ms) @@ -474,11 +466,12 @@ void tst_Http2::stopEventLoop() eventLoop.quit(); } -Http2Server *tst_Http2::newServer(const Http2Settings &serverSettings, - const Http2Settings &clientSettings) +Http2Server *tst_Http2::newServer(const Http2::RawSettings &serverSettings, + const Http2::ProtocolParameters &clientSettings) { using namespace Http2; - auto srv = new Http2Server(clearTextHTTP2, serverSettings, clientSettings); + auto srv = new Http2Server(clearTextHTTP2, serverSettings, + clientSettings.settingsFrameData); using Srv = Http2Server; using Cl = tst_Http2; @@ -507,6 +500,7 @@ void tst_Http2::sendRequest(int streamNumber, QNetworkRequest request(url); request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, QVariant(true)); + request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); request.setPriority(priority); QNetworkReply *reply = nullptr; |