summaryrefslogtreecommitdiff
path: root/platform/qt/src/http_file_source.cpp
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2015-10-21 17:24:44 -0700
committerThiago Marcos P. Santos <thiago@mapbox.com>2016-04-20 20:55:51 +0300
commit029f9f088331fa0d04a9b75a7dbdae773f749f47 (patch)
tree265e2f2619804164731a7a1158f3accfc4e28f3b /platform/qt/src/http_file_source.cpp
parente93d51c922a0d55b6f40b07185452dc54151736d (diff)
downloadqtlocation-mapboxgl-029f9f088331fa0d04a9b75a7dbdae773f749f47.tar.gz
[Qt] Introduce the Qt HTTPRequest and HTTPFileSource
Diffstat (limited to 'platform/qt/src/http_file_source.cpp')
-rw-r--r--platform/qt/src/http_file_source.cpp123
1 files changed, 123 insertions, 0 deletions
diff --git a/platform/qt/src/http_file_source.cpp b/platform/qt/src/http_file_source.cpp
new file mode 100644
index 0000000000..a38cf42363
--- /dev/null
+++ b/platform/qt/src/http_file_source.cpp
@@ -0,0 +1,123 @@
+#include "http_file_source.hpp"
+#include "http_request.hpp"
+
+#include <mbgl/platform/log.hpp>
+
+#include <QByteArray>
+#include <QDir>
+#include <QNetworkProxyFactory>
+#include <QNetworkReply>
+#include <QSslConfiguration>
+
+namespace mbgl {
+
+HTTPFileSource::Impl::Impl() : m_manager(new QNetworkAccessManager(this))
+{
+ QNetworkProxyFactory::setUseSystemConfiguration(true);
+
+#if QT_VERSION >= 0x050000
+ m_ssl.setProtocol(QSsl::SecureProtocols);
+#else
+ // Qt 4 defines SecureProtocols as TLS1 or SSL3, but we don't want SSL3.
+ m_ssl.setProtocol(QSsl::TlsV1);
+#endif
+
+ m_ssl.setCaCertificates(QSslCertificate::fromPath("ca-bundle.crt"));
+ if (m_ssl.caCertificates().isEmpty()) {
+ mbgl::Log::Warning(mbgl::Event::HttpRequest, "Could not load list of certificate authorities");
+ }
+
+ connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinish(QNetworkReply*)));
+}
+
+void HTTPFileSource::Impl::request(HTTPRequest* req)
+{
+ QUrl url = req->requestUrl();
+
+ QPair<QNetworkReply*, QVector<HTTPRequest*>>& data = m_pending[url];
+ QVector<HTTPRequest*>& requestsVector = data.second;
+ requestsVector.append(req);
+
+ if (requestsVector.size() > 1) {
+ return;
+ }
+
+ QNetworkRequest networkRequest = req->networkRequest();
+ networkRequest.setSslConfiguration(m_ssl);
+
+ data.first = m_manager->get(networkRequest);
+}
+
+void HTTPFileSource::Impl::cancel(HTTPRequest* req)
+{
+ QUrl url = req->requestUrl();
+
+ auto it = m_pending.find(url);
+ if (it == m_pending.end()) {
+ return;
+ }
+
+ QPair<QNetworkReply*, QVector<HTTPRequest*>>& data = it.value();
+ QNetworkReply* reply = data.first;
+ QVector<HTTPRequest*>& requestsVector = data.second;
+
+ for (int i = 0; i < requestsVector.size(); ++i) {
+ if (req == requestsVector.at(i)) {
+ requestsVector.remove(i);
+ break;
+ }
+ }
+
+ if (requestsVector.empty()) {
+ m_pending.erase(it);
+#if QT_VERSION >= 0x050000
+ reply->abort();
+#else
+ // XXX: We should be aborting the reply here
+ // but a bug on Qt4 causes the connection of
+ // other ongoing requests to drop if we call
+ // abort() too often (and we do).
+ Q_UNUSED(reply);
+#endif
+ }
+}
+
+void HTTPFileSource::Impl::replyFinish(QNetworkReply* reply)
+{
+ const QUrl& url = reply->request().url();
+
+ auto it = m_pending.find(url);
+ if (it == m_pending.end()) {
+ reply->deleteLater();
+ return;
+ }
+
+ QVector<HTTPRequest*>& requestsVector = it.value().second;
+ for (auto req : requestsVector) {
+ req->handleNetworkReply(reply);
+ }
+
+ m_pending.erase(it);
+ reply->deleteLater();
+}
+
+HTTPFileSource::HTTPFileSource()
+ : impl(std::make_unique<Impl>()) {
+}
+
+HTTPFileSource::~HTTPFileSource() = default;
+
+std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource, Callback callback)
+{
+ return std::make_unique<HTTPRequest>(impl.get(), resource, callback);
+}
+
+uint32_t HTTPFileSource::maximumConcurrentRequests() {
+#if QT_VERSION >= 0x050000
+ return 20;
+#else
+ return 10;
+#endif
+}
+
+} // mbgl