From d9d26033bb3945cf69654b505f85f9786e42eade Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Wed, 27 Feb 2019 17:07:29 +0100 Subject: Fix URL adjustment for URLs with no scheme Prepend the "scheme://" part a to URL only if it is relative and doesn't have a host. Non-relative URLs having a host already contain the "://" part. Change-Id: If2ac3db4f2eb0d18ffa1893415f44b9d2c85db26 Reviewed-by: Edward Welbourne --- src/coap/qcoapclient.cpp | 2 -- src/coap/qcoaprequest.cpp | 7 +++++-- tests/auto/qcoaprequest/tst_qcoaprequest.cpp | 8 ++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index e4fd763..09e171c 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -429,8 +429,6 @@ QCoapDiscoveryReply *QCoapClient::discover(QtCoap::MulticastGroup group, QUrl discoveryUrl; discoveryUrl.setHost(base); - QString scheme = d->connection->isSecure() ? QStringLiteral("coaps") : QStringLiteral("coap"); - discoveryUrl.setScheme(scheme); discoveryUrl.setPath(discoveryPath); QCoapRequest request(discoveryUrl); diff --git a/src/coap/qcoaprequest.cpp b/src/coap/qcoaprequest.cpp index e23c806..f3fdca8 100644 --- a/src/coap/qcoaprequest.cpp +++ b/src/coap/qcoaprequest.cpp @@ -295,10 +295,13 @@ QUrl QCoapRequest::adjustedUrl(const QUrl &url, bool secure) QUrl finalizedUrl = url; const auto scheme = secure ? CoapSecureScheme : CoapScheme; - if (url.isRelative()) + if (url.host().isEmpty() && url.isRelative()) { + // In some cases host address is mistaken for part of the relative path, + // prepending the scheme fixes this. finalizedUrl = url.toString().prepend(scheme + QLatin1String("://")); - else if (url.scheme().isEmpty()) + } else if (url.scheme().isEmpty()) { finalizedUrl.setScheme(scheme); + } if (url.port() == -1) { const auto port = secure ? QtCoap::DefaultSecurePort : QtCoap::DefaultPort; diff --git a/tests/auto/qcoaprequest/tst_qcoaprequest.cpp b/tests/auto/qcoaprequest/tst_qcoaprequest.cpp index 1a5c413..5ac0b6d 100644 --- a/tests/auto/qcoaprequest/tst_qcoaprequest.cpp +++ b/tests/auto/qcoaprequest/tst_qcoaprequest.cpp @@ -88,6 +88,14 @@ void tst_QCoapRequest::adjustUrl_data() << QUrl("coap://vs0.inf.ethz.ch:5683/test") << false; QTest::newRow("no_scheme_no_port_secure") << QUrl("vs0.inf.ethz.ch/test") << QUrl("coaps://vs0.inf.ethz.ch:5684/test") << true; + + QUrl ipv6Host; + ipv6Host.setHost("::1"); + ipv6Host.setPath("/path"); + QTest::newRow("no_scheme_no_port_ipv6") << ipv6Host << QUrl("coap://[::1]:5683/path") + << false; + QTest::newRow("no_scheme_no_port_ipv6_secure") << ipv6Host << QUrl("coaps://[::1]:5684/path") + << true; } void tst_QCoapRequest::adjustUrl() -- cgit v1.2.3