summaryrefslogtreecommitdiffstats
path: root/qt-ui/subsurfacewebservices.cpp
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2015-06-12 06:31:16 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-06-12 09:22:36 -0700
commit4d06e8f7b520625d743816afbd9e0dc6945a25db (patch)
treefae81fef7d790e887c2ce38024af0d3822daaca9 /qt-ui/subsurfacewebservices.cpp
parent5e0c546beb48fd04758520bd3c6ff68bcc5b6d16 (diff)
downloadsubsurface-4d06e8f7b520625d743816afbd9e0dc6945a25db.tar.gz
Cloud storage: check connection before connecting
libgit2 takes forever (a minute or so) to figure out that it can't connect to a remote server. So if we are using https as connection protocol, quickly check utilizing RFCs 2324/7168 to make sure we can reach the cloud server (and not some captive portal or something). Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui/subsurfacewebservices.cpp')
-rw-r--r--qt-ui/subsurfacewebservices.cpp46
1 files changed, 45 insertions, 1 deletions
diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp
index 825d4f03d..16faac61d 100644
--- a/qt-ui/subsurfacewebservices.cpp
+++ b/qt-ui/subsurfacewebservices.cpp
@@ -944,7 +944,6 @@ QNetworkReply* UserSurveyServices::sendSurvey(QString values)
CloudStorageAuthenticate::CloudStorageAuthenticate(QObject *parent) : QObject(parent)
{
userAgent = getUserAgent();
-
}
#define CLOUDURL "https://cloud.subsurface-divelog.org/"
@@ -998,3 +997,48 @@ void CloudStorageAuthenticate::sslErrors(QList<QSslError> errorList)
qDebug() << err.errorString();
}
}
+
+CheckCloudConnection::CheckCloudConnection(QObject *parent)
+{
+
+}
+
+#define TEAPOT "https://cloud.subsurface-divelog.org/make-latte?number-of-shots=3"
+#define HTTP_I_AM_A_TEAPOT 418
+#define MILK "Linus does not like non-fat milk"
+bool CheckCloudConnection::checkServer()
+{
+ QTimer timer;
+ timer.setSingleShot(true);
+ QEventLoop loop;
+ QNetworkRequest request;
+ request.setRawHeader("Accept", "text/plain");
+ request.setRawHeader("User-Agent", getUserAgent().toUtf8());
+ request.setUrl(QString(TEAPOT));
+ QNetworkAccessManager *mgr = new QNetworkAccessManager();
+ QNetworkReply *reply = mgr->get(request);
+ connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
+ connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
+ timer.start(2000); // wait two seconds
+ loop.exec();
+ if (timer.isActive()) {
+ // didn't time out, did we get the right response?
+ timer.stop();
+ if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == HTTP_I_AM_A_TEAPOT &&
+ reply->readAll() == QByteArray(MILK)) {
+ reply->deleteLater();
+ mgr->deleteLater();
+ return true;
+ }
+ // qDebug() << "did not get expected response - server unreachable" <<
+ // reply->error() << reply->errorString() <<
+ // reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() <<
+ // reply->readAll();
+ } else {
+ disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
+ reply->abort();
+ }
+ reply->deleteLater();
+ mgr->deleteLater();
+ return false;
+}