Skip to content

Commit

Permalink
Change QString for headers in QHttpServerRequest to QByteArray
Browse files Browse the repository at this point in the history
This is because QHttpServerResponder uses QByteArray for headers' key/
value.

Change-Id: I21b5af4d08e43ee58a1edc95b714c6da0ae10790
Reviewed-by: Edward Welbourne <[email protected]>
Reviewed-by: Jesus Fernandez <[email protected]>
Reviewed-by: Mikhail Svetkin <[email protected]>
  • Loading branch information
Tasuku Suzuki committed May 9, 2019
1 parent 9f7bf66 commit 26b4df2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/httpserver/qabstracthttpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ void QAbstractHttpServerPrivate::handleReadyRead()

if (requestPrivate->httpParser.upgrade) { // Upgrade
const auto &headers = requestPrivate->headers;
const auto upgradeHash = requestPrivate->headerHash(QStringLiteral("upgrade"));
const auto upgradeHash = requestPrivate->headerHash(QByteArrayLiteral("upgrade"));
const auto it = headers.find(upgradeHash);
if (it != headers.end()) {
#if defined(QT_WEBSOCKETS_LIB)
if (it.value().second.compare(QLatin1String("websocket"), Qt::CaseInsensitive) == 0) {
if (it.value().second.compare(QByteArrayLiteral("websocket"), Qt::CaseInsensitive) == 0) {
static const auto signal = QMetaMethod::fromSignal(
&QAbstractHttpServer::newWebSocketConnection);
if (q->isSignalConnected(signal)) {
Expand Down
14 changes: 7 additions & 7 deletions src/httpserver/qhttpserverrequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ QHttpServerRequestPrivate::QHttpServerRequestPrivate()
httpParser.data = this;
}

QString QHttpServerRequestPrivate::header(const QString &key) const
QByteArray QHttpServerRequestPrivate::header(const QByteArray &key) const
{
return headers.value(headerHash(key)).second;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ bool QHttpServerRequestPrivate::parse(QIODevice *socket)
return true;
}

uint QHttpServerRequestPrivate::headerHash(const QString &key) const
uint QHttpServerRequestPrivate::headerHash(const QByteArray &key) const
{
return qHash(key.toLower(), headersSeed);
}
Expand Down Expand Up @@ -183,8 +183,8 @@ int QHttpServerRequestPrivate::onHeaderField(http_parser *httpParser, const char
qCDebug(lc) << httpParser << QString::fromUtf8(at, int(length));
auto i = instance(httpParser);
i->state = State::OnHeaders;
const auto key = QString::fromUtf8(at, int(length));
i->headers.insert(i->headerHash(key), qMakePair(key, QString()));
const auto key = QByteArray(at, int(length));
i->headers.insert(i->headerHash(key), qMakePair(key, QByteArray()));
i->lastHeader = key;
return 0;
}
Expand All @@ -195,9 +195,9 @@ int QHttpServerRequestPrivate::onHeaderValue(http_parser *httpParser, const char
auto i = instance(httpParser);
i->state = State::OnHeaders;
Q_ASSERT(!i->lastHeader.isEmpty());
const auto value = QString::fromUtf8(at, int(length));
const auto value = QByteArray(at, int(length));
i->headers[i->headerHash(i->lastHeader)] = qMakePair(i->lastHeader, value);
if (i->lastHeader.compare(QStringLiteral("host"), Qt::CaseInsensitive) == 0)
if (i->lastHeader.compare(QByteArrayLiteral("host"), Qt::CaseInsensitive) == 0)
parseUrl(at, length, true, &i->url);
#if defined(QT_DEBUG)
i->lastHeader.clear();
Expand Down Expand Up @@ -261,7 +261,7 @@ QHttpServerRequest::QHttpServerRequest(const QHttpServerRequest &other) :
QHttpServerRequest::~QHttpServerRequest()
{}

QString QHttpServerRequest::value(const QString &key) const
QByteArray QHttpServerRequest::value(const QByteArray &key) const
{
return d->headers.value(d->headerHash(key)).second;
}
Expand Down
2 changes: 1 addition & 1 deletion src/httpserver/qhttpserverrequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Q_HTTPSERVER_EXPORT QHttpServerRequest : public QObjectUserData
Q_DECLARE_FLAGS(Methods, Method)
Q_FLAG(Methods)

QString value(const QString &key) const;
QByteArray value(const QByteArray &key) const;
QUrl url() const;
QUrlQuery query() const;
Method method() const;
Expand Down
9 changes: 5 additions & 4 deletions src/httpserver/qhttpserverrequest_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class QHttpServerRequestPrivate : public QSharedData
public:
QHttpServerRequestPrivate();

quint16 port = 0;
enum class State {
NotStarted,
OnMessageBegin,
Expand All @@ -76,13 +77,13 @@ class QHttpServerRequestPrivate : public QSharedData

http_parser httpParser;

QString header(const QString &key) const;
QByteArray header(const QByteArray &key) const;
bool parse(QIODevice *socket);

QString lastHeader;
QHash<uint, QPair<QString, QString>> headers;
QByteArray lastHeader;
QMap<uint, QPair<QByteArray, QByteArray>> headers;
const uint headersSeed = uint(qGlobalQHashSeed());
uint headerHash(const QString &key) const;
uint headerHash(const QByteArray &key) const;

void clear();

Expand Down

0 comments on commit 26b4df2

Please sign in to comment.