summaryrefslogtreecommitdiffstats
path: root/src/httpserver/qhttpserverrequest.cpp
diff options
context:
space:
mode:
authorMikhail Svetkin <mikhail.svetkin@gmail.com>2019-10-31 21:32:49 +0100
committerMikhail Svetkin <mikhail.svetkin@gmail.com>2019-11-10 20:10:25 +0000
commitdba675ed617592a25b7ca17420c77e92640a3f12 (patch)
tree9395002e25b2ad6a2fa43b39bfcf0bd1bed53028 /src/httpserver/qhttpserverrequest.cpp
parent5fcfbb6629f4b6447ffd44fe6d01944a78b2a3cb (diff)
Refactor QHttpServerRequestPrivate::parseUrl
There are few changes: 1. Remove std::function. A lambda can be converted to a function pointer if it does not capture. 2. Change std::map to std::array and mark it as constexpr. It will eliminate run-time initialization and checking. 3. Remove unnecessary nesting Change-Id: I5379cb11016def47aee0c57c3de39677aae072d7 Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/httpserver/qhttpserverrequest.cpp')
-rw-r--r--src/httpserver/qhttpserverrequest.cpp40
1 files changed, 21 insertions, 19 deletions
diff --git a/src/httpserver/qhttpserverrequest.cpp b/src/httpserver/qhttpserverrequest.cpp
index 1b4c38a..f2ab750 100644
--- a/src/httpserver/qhttpserverrequest.cpp
+++ b/src/httpserver/qhttpserverrequest.cpp
@@ -38,6 +38,8 @@
#include <QtNetwork/qsslsocket.h>
#endif
+#include <array>
+
Q_LOGGING_CATEGORY(lc, "qt.httpserver.request")
QT_BEGIN_NAMESPACE
@@ -65,6 +67,16 @@ QDebug operator<<(QDebug debug, const http_parser *const httpParser)
}
#endif
+static const std::array<void(*)(const QString &, QUrl *), UF_MAX> parseUrlFunctions {
+ [](const QString &string, QUrl *url) { url->setScheme(string); },
+ [](const QString &string, QUrl *url) { url->setHost(string); },
+ [](const QString &string, QUrl *url) { url->setPort(string.toInt()); },
+ [](const QString &string, QUrl *url) { url->setPath(string, QUrl::TolerantMode); },
+ [](const QString &string, QUrl *url) { url->setQuery(string); },
+ [](const QString &string, QUrl *url) { url->setFragment(string); },
+ [](const QString &string, QUrl *url) { url->setUserInfo(string); },
+};
+
http_parser_settings QHttpServerRequestPrivate::httpParserSettings {
&QHttpServerRequestPrivate::onMessageBegin,
&QHttpServerRequestPrivate::onUrl,
@@ -127,28 +139,18 @@ void QHttpServerRequestPrivate::clear()
bool QHttpServerRequestPrivate::parseUrl(const char *at, size_t length, bool connect, QUrl *url)
{
- static const std::map<std::size_t, std::function<void(const QString &, QUrl *)>> functions {
- { UF_SCHEMA, [](const QString &string, QUrl *url) { url->setScheme(string); } },
- { UF_HOST, [](const QString &string, QUrl *url) { url->setHost(string); } },
- { UF_PORT, [](const QString &string, QUrl *url) { url->setPort(string.toInt()); } },
- { UF_PATH,
- [](const QString &string, QUrl *url) { url->setPath(string, QUrl::TolerantMode); } },
- { UF_QUERY, [](const QString &string, QUrl *url) { url->setQuery(string); } },
- { UF_FRAGMENT, [](const QString &string, QUrl *url) { url->setFragment(string); } },
- { UF_USERINFO, [](const QString &string, QUrl *url) { url->setUserInfo(string); } },
- };
struct http_parser_url u;
- if (http_parser_parse_url(at, length, connect ? 1 : 0, &u) == 0) {
- for (auto i = 0u; i < UF_MAX; i++) {
- if (u.field_set & (1 << i)) {
- functions.find(i)->second(QString::fromUtf8(at + u.field_data[i].off,
- u.field_data[i].len),
- url);
- }
+ if (http_parser_parse_url(at, length, connect ? 1 : 0, &u) != 0)
+ return false;
+
+ for (auto i = 0u; i < UF_MAX; i++) {
+ if (u.field_set & (1 << i)) {
+ parseUrlFunctions[i](QString::fromUtf8(at + u.field_data[i].off,
+ u.field_data[i].len),
+ url);
}
- return true;
}
- return false;
+ return true;
}
QHttpServerRequestPrivate *QHttpServerRequestPrivate::instance(http_parser *httpParser)