Jelajahi Sumber

Migrate to QUrl for Channel Address definition (#110)

Viktor Kopp 5 tahun lalu
induk
melakukan
e0978195ae

+ 2 - 1
examples/simplechat/simplechatengine.cpp

@@ -74,7 +74,8 @@ void SimpleChatEngine::login(const QString &name, const QString &password)
     conf.setProtocol(QSsl::TlsV1_2);
     conf.setAllowedNextProtocols({QSslConfiguration::ALPNProtocolHTTP2});
 
-    std::shared_ptr<qtprotobuf::AbstractChannel> channel(new qtprotobuf::Http2Channel("localhost", 65002, qtprotobuf::SslCredentials(conf) |
+    QUrl url("https://localhost:65002");
+    std::shared_ptr<qtprotobuf::AbstractChannel> channel(new qtprotobuf::Http2Channel(url, qtprotobuf::SslCredentials(conf) |
                                                                                       AuthCredentials(name, QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex())));
 
     m_client->attachChannel(channel);

+ 19 - 0
src/grpc/http2channel.cpp

@@ -172,6 +172,20 @@ struct Http2ChannelPrivate {
         }
     }
 
+
+    Http2ChannelPrivate(const QUrl &_url, const AbstractCredentials &_credentials)
+        : url(_url)
+        , credentials(_credentials)
+    {
+        if (url.scheme() == "https") {
+            if (!credentials.channelCredentials().contains(QLatin1String("sslConfig"))) {
+                throw std::invalid_argument("Https connection requested but not ssl configuration provided.");
+            }
+            sslConfig = credentials.channelCredentials().value(QLatin1String("sslConfig")).value<QSslConfiguration>();
+        } else if (url.scheme().isEmpty()) {
+            url.setScheme("http");
+        }
+    }
 };
 
 }
@@ -183,6 +197,11 @@ Http2Channel::Http2Channel(const QString &addr, quint16 port, const AbstractCred
     d->url.setPort(port);
 }
 
+Http2Channel::Http2Channel(const QUrl &url, const AbstractCredentials &credentials) : AbstractChannel()
+  , d(new Http2ChannelPrivate(url, credentials))
+{
+}
+
 Http2Channel::~Http2Channel()
 {
     delete d;

+ 3 - 4
src/grpc/http2channel.h

@@ -27,10 +27,7 @@
 
 #include "abstractchannel.h"
 
-#include <QString>
-#include <QByteArray>
-
-#include "qtgrpc_global.h"
+#include <QUrl>
 
 namespace qtprotobuf {
 
@@ -40,7 +37,9 @@ class AbstractCredentials;
 class QTGRPCSHARED_EXPORT Http2Channel final : public AbstractChannel
 {
 public:
+    // this contructor is obsolete and is going to be removed soon
     Http2Channel(const QString &addr, quint16 port, const AbstractCredentials &credentials);
+    Http2Channel(const QUrl &url, const AbstractCredentials &credentials);
     ~Http2Channel();
 
     StatusCodes call(const QString &method, const QString &service, const QByteArray &args, QByteArray &ret) override;

+ 9 - 7
tests/test_grpc/clienttest.cpp

@@ -45,10 +45,12 @@ protected:
     }
     static QCoreApplication m_app;
     static int m_argc;
+    static QUrl m_echoServerAddress;
 };
 
 int ClientTest::m_argc(0);
 QCoreApplication ClientTest::m_app(m_argc, nullptr);
+QUrl ClientTest::m_echoServerAddress("http://localhost:50051", QUrl::StrictMode);
 
 TEST_F(ClientTest, CheckMethodsGeneration)
 {
@@ -65,7 +67,7 @@ TEST_F(ClientTest, CheckMethodsGeneration)
 TEST_F(ClientTest, StringEchoTest)
 {
     TestServiceClient testClient;
-    testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
+    testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
     SimpleStringMessage request;
     QPointer<SimpleStringMessage> result(new SimpleStringMessage);
     request.setTestFieldString("Hello beach!");
@@ -77,7 +79,7 @@ TEST_F(ClientTest, StringEchoTest)
 TEST_F(ClientTest, StringEchoAsyncTest)
 {
     TestServiceClient testClient;
-    testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
+    testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
     SimpleStringMessage request;
     SimpleStringMessage result;
     request.setTestFieldString("Hello beach!");
@@ -98,7 +100,7 @@ TEST_F(ClientTest, StringEchoAsyncTest)
 TEST_F(ClientTest, StringEchoAsync2Test)
 {
     TestServiceClient testClient;
-    testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
+    testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
     SimpleStringMessage result;
     SimpleStringMessage request;
     request.setTestFieldString("Hello beach!");
@@ -117,7 +119,7 @@ TEST_F(ClientTest, StringEchoAsync2Test)
 TEST_F(ClientTest, StringEchoAsyncAbortTest)
 {
     TestServiceClient testClient;
-    testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
+    testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
     SimpleStringMessage result;
     SimpleStringMessage request;
     request.setTestFieldString("sleep");
@@ -170,7 +172,7 @@ TEST_F(ClientTest, StringEchoAsyncAbortTest)
 TEST_F(ClientTest, StringEchoStreamTest)
 {
     TestServiceClient testClient;
-    testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
+    testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
     SimpleStringMessage result;
     SimpleStringMessage request;
     request.setTestFieldString("Stream");
@@ -201,7 +203,7 @@ TEST_F(ClientTest, StringEchoStreamTest)
 TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
 {
     TestServiceClient testClient;
-    testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
+    testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
     SimpleStringMessage request;
     QPointer<SimpleStringMessage> result(new SimpleStringMessage);
 
@@ -228,7 +230,7 @@ TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
 TEST_F(ClientTest, HugeBlobEchoStreamTest)
 {
     TestServiceClient testClient;
-    testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051, InsecureCredentials()));
+    testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
     BlobMessage result;
     BlobMessage request;
     QFile testFile("testfile");