diff options
Diffstat (limited to 'src/oauth/qoauthhttpserverreplyhandler.cpp')
| -rw-r--r-- | src/oauth/qoauthhttpserverreplyhandler.cpp | 53 |
1 files changed, 35 insertions, 18 deletions
diff --git a/src/oauth/qoauthhttpserverreplyhandler.cpp b/src/oauth/qoauthhttpserverreplyhandler.cpp index 4bd1c2d..7381899 100644 --- a/src/oauth/qoauthhttpserverreplyhandler.cpp +++ b/src/oauth/qoauthhttpserverreplyhandler.cpp @@ -72,11 +72,20 @@ using namespace Qt::StringLiterals; \section1 IPv4 and IPv6 - Currently if the handler is a loopback address, IPv4 any address, - or IPv6 any address, the used callback is in the form of - \e {http://localhost:{port}/{path}}. Otherwise, for specific - IP addresses, the actual IP literal is used. For instance - \e {http://192.168.0.2:{port}/{path}} in the case of IPv4. + If the handler is an \e any address handler + (\l {QHostAddress::SpecialAddress}{AnyIPv4, AnyIPv6, or Any}), + the used callback is in the form of \c {http://localhost:{port}/{path}}. + Handler will first attempt to listen on IPv4 loopback address, + and then on IPv6. \c {localhost} is used because it resolves correctly + on both IPv4 and IPv6 interfaces. + + For loopback addresses + (\l {QHostAddress::SpecialAddress}{LocalHost or LocalHostIPv6}) + the IP literals (\c {127.0.0.1} and \c {::1}) are used. + + For specific IP addresses the provided IP literal is used directly, + for instance: + \e {http://192.168.0.123:{port}/{path}} in the case of an IPv4 address. \section1 HTTP and HTTPS Callbacks @@ -85,7 +94,7 @@ using namespace Qt::StringLiterals; providing an appropriate \l QSslConfiguration when calling \l {listen(const QSslConfiguration &, const QHostAddress &, quint16)}{listen()}. Internally the handler will then use \l QSslServer, and the callback - (redirect URL) will be of the form \e {https://localhost:{port}/{path}}. + (redirect URL) will be of the form \e {https://{host}:{port}/{path}}. Following example illustrates this: \snippet src_oauth_replyhandlers.cpp localhost-https-scheme-setup @@ -137,17 +146,25 @@ QString QOAuthHttpServerReplyHandlerPrivate::callback() const #endif url.setPort(callbackPort); url.setPath(path); + url.setHost(callbackHost()); + return url.toString(QUrl::EncodeSpaces | QUrl::EncodeUnicode | QUrl::EncodeDelimiters + | QUrl::EncodeReserved); +} - // convert Any and Localhost addresses to "localhost" - if (callbackAddress.isLoopback() || callbackAddress == QHostAddress::AnyIPv4 - || callbackAddress == QHostAddress::Any || callbackAddress == QHostAddress::AnyIPv6) { - url.setHost(u"localhost"_s); +QString QOAuthHttpServerReplyHandlerPrivate::callbackHost() const +{ + QString host; + if (callbackAddress == QHostAddress::AnyIPv4 || callbackAddress == QHostAddress::Any + || callbackAddress == QHostAddress::AnyIPv6) { + // Convert Any addresses to "localhost" + host = u"localhost"_s; } else { - url.setHost(callbackAddress.toString()); + // For other than Any addresses, use QHostAddress::toString() which returns an + // IP literal. This includes user-provided addresses, as well as special addresses + // such as LocalHost (127.0.0.1) and LocalHostIPv6 (::1) + host = callbackAddress.toString(); } - - return url.toString(QUrl::EncodeSpaces | QUrl::EncodeUnicode | QUrl::EncodeDelimiters - | QUrl::EncodeReserved); + return host; } void QOAuthHttpServerReplyHandlerPrivate::_q_clientConnected() @@ -376,23 +393,23 @@ bool QOAuthHttpServerReplyHandlerPrivate::QHttpRequest::readHeader(QTcpSocket *s /*! Constructs a QOAuthHttpServerReplyHandler object using \a parent as a parent object. Calls \l {listen()} with port \c 0 and address - \l {QHostAddress::SpecialAddress}{Null}. + \l {QHostAddress::SpecialAddress}{LocalHost}. \sa listen() */ QOAuthHttpServerReplyHandler::QOAuthHttpServerReplyHandler(QObject *parent) : - QOAuthHttpServerReplyHandler(QHostAddress::Null, 0, parent) + QOAuthHttpServerReplyHandler(QHostAddress::LocalHost, 0, parent) {} /*! Constructs a QOAuthHttpServerReplyHandler object using \a parent as a parent object. Calls \l {listen()} with \a port and address - \l {QHostAddress::SpecialAddress}{Null}. + \l {QHostAddress::SpecialAddress}{LocalHost}. \sa listen() */ QOAuthHttpServerReplyHandler::QOAuthHttpServerReplyHandler(quint16 port, QObject *parent) : - QOAuthHttpServerReplyHandler(QHostAddress::Null, port, parent) + QOAuthHttpServerReplyHandler(QHostAddress::LocalHost, port, parent) {} /*! |
