diff options
Diffstat (limited to 'src/httpserver/qhttpserverrouterrule.cpp')
-rw-r--r-- | src/httpserver/qhttpserverrouterrule.cpp | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/src/httpserver/qhttpserverrouterrule.cpp b/src/httpserver/qhttpserverrouterrule.cpp index d486218..151e411 100644 --- a/src/httpserver/qhttpserverrouterrule.cpp +++ b/src/httpserver/qhttpserverrouterrule.cpp @@ -30,7 +30,9 @@ #include <QtHttpServer/qhttpserverrouterrule.h> #include <private/qhttpserverrouterrule_p.h> +#include <private/qhttpserverrequest_p.h> +#include <QtCore/qmetaobject.h> #include <QtCore/qloggingcategory.h> #include <QtCore/qregularexpression.h> #include <QtCore/qdebug.h> @@ -39,6 +41,27 @@ QT_BEGIN_NAMESPACE Q_LOGGING_CATEGORY(lcRouterRule, "qt.httpserver.router.rule") +static QHttpServerRequest::Methods strToMethods(const char *strMethods) +{ + QHttpServerRequest::Methods methods; + + static const auto index = QHttpServerRequest::staticMetaObject.indexOfEnumerator("Method"); + if (index == -1) { + qCWarning(lcRouterRule, "Can not find QMetaEnum for enum Method"); + return methods; + } + + static const QMetaEnum en = QHttpServerRequest::staticMetaObject.enumerator(index); + bool ok = false; + const int val = en.keysToValue(strMethods, &ok); + if (ok) + methods = static_cast<decltype(methods)>(val); + else + qCWarning(lcRouterRule, "Can not convert %s to QHttpServerRequest::Method", strMethods); + + return methods; +} + /*! \class QHttpServerRouterRule \brief The QHttpServerRouterRule is the base class for QHttpServerRouter rules. @@ -94,12 +117,14 @@ Q_LOGGING_CATEGORY(lcRouterRule, "qt.httpserver.router.rule") /*! Constructs a rule with pathPattern \a pathPattern, and routerHandler \a routerHandler. - The rule accepts any HTTP method. + The rule accepts all HTTP methods by default. + + \sq QHttpServerRequest::Method */ QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, RouterHandler &&routerHandler) : QHttpServerRouterRule(pathPattern, - QHttpServerRequest::Methods(), + QHttpServerRequest::Method::All, std::forward<RouterHandler>(routerHandler)) { } @@ -107,6 +132,10 @@ QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, /*! Constructs a rule with pathPattern \a pathPattern, methods \a methods and routerHandler \a routerHandler. + + The rule accepts any combinations of available HTTP methods. + + \sa QHttpServerRequest::Method */ QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, const QHttpServerRequest::Methods methods, @@ -119,6 +148,24 @@ QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, } /*! + Constructs a rule with pathPattern \a pathPattern, methods \a methods + and routerHandler \a routerHandler. + + \note \a methods shall be joined with | as separator (not spaces or commas) + and that either the upper-case or the capitalised form may be used. + + \sa QMetaEnum::keysToValue +*/ +QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, + const char *methods, + RouterHandler &&routerHandler) + : QHttpServerRouterRule(pathPattern, + strToMethods(methods), + std::forward<RouterHandler>(routerHandler)) +{ +} + +/*! \internal */ QHttpServerRouterRule::QHttpServerRouterRule(QHttpServerRouterRulePrivate *d) @@ -134,6 +181,15 @@ QHttpServerRouterRule::~QHttpServerRouterRule() } /*! + Returns true if the methods is valid +*/ +bool QHttpServerRouterRule::hasValidMethods() const +{ + Q_D(const QHttpServerRouterRule); + return d->methods & QHttpServerRequest::Method::All; +} + +/*! This function is called by QHttpServerRouter when a new request is received. */ bool QHttpServerRouterRule::exec(const QHttpServerRequest &request, |