~stolowski/unity-scopes-api/fix-user-agent-in-preview-rtm

« back to all changes in this revision

Viewing changes to src/scopes/internal/smartscopes/HttpClientQtThread.cpp

  • Committer: Pawel Stolowski
  • Date: 2014-11-04 14:33:07 UTC
  • mfrom: (241.1.20 unity-scopes-api)
  • Revision ID: pawel.stolowski@canonical.com-20141104143307-cs70gjyowjtwpk69
Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
15
 *
16
16
 * Authored by: Marcus Tomlinson <marcus.tomlinson@canonical.com>
 
17
 *              Pawel Stolowski <pawel.stolowski@canonical.com>
17
18
 */
18
19
 
19
20
#include "unity/scopes/internal/smartscopes/HttpClientQtThread.h"
22
23
#include <QNetworkAccessManager>
23
24
#include <QNetworkReply>
24
25
#include <QTimer>
 
26
#include <cassert>
 
27
#include <iostream>
25
28
 
26
29
namespace unity
27
30
{
35
38
namespace smartscopes
36
39
{
37
40
 
38
 
HttpClientQtThread::HttpClientQtThread(const QUrl& url, uint timeout)
 
41
HttpClientQtThread::HttpClientQtThread(const QUrl& url, uint timeout, std::function<void(std::string const&)> const& lineData, HttpHeaders const& headers)
39
42
    : QThread()
40
43
    , url_(url)
 
44
    , lineDataCallback_(lineData)
 
45
    , headers_(headers)
41
46
    , timeout_(timeout)
42
47
    , success_(false)
43
48
{
63
68
    QNetworkAccessManager* manager = new QNetworkAccessManager();
64
69
 
65
70
    QNetworkRequest request(url_);
 
71
    for (auto const& hdr: headers_)
 
72
    {
 
73
        request.setRawHeader(QString::fromStdString(hdr.first).toUtf8(), QString::fromStdString(hdr.second).toUtf8());
 
74
    }
66
75
 
67
76
    QNetworkReply* reply = manager->get(request);
 
77
    reply->setReadBufferSize(0); // unlimited buffer
68
78
 
69
 
    connect(manager, &QNetworkAccessManager::finished, this, &HttpClientQtThread::got_reply, Qt::DirectConnection);
 
79
    connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(got_reply(QNetworkReply *)));
 
80
    connect(reply, SIGNAL(readyRead()), this, SLOT(dataReady()));
70
81
    connect(this, &HttpClientQtThread::abort, reply, &QNetworkReply::abort);
71
82
 
72
83
    QTimer timeout;
73
84
    timeout.singleShot(timeout_, this, SLOT(timeout()));
74
85
    QThread::exec();  // enter event loop
75
86
 
76
 
    delete reply;
77
 
    delete manager;
 
87
    reply->deleteLater();
 
88
    manager->deleteLater();
78
89
}
79
90
 
80
91
void HttpClientQtThread::cancel()
84
95
    success_ = false;
85
96
    reply_ = "Request cancelled: " + url_.url().toStdString();
86
97
 
87
 
    emit abort();
 
98
    emit HttpClientQtThread::abort();
88
99
    quit();
89
100
}
90
101
 
95
106
    success_ = false;
96
107
    reply_ = "Request timed out: " + url_.url().toStdString();
97
108
 
98
 
    emit abort();
 
109
    emit HttpClientQtThread::abort();
99
110
    quit();
100
111
}
101
112
 
 
113
void HttpClientQtThread::dataReady()
 
114
{
 
115
    QNetworkReply* net_reply = qobject_cast<QNetworkReply*>(sender());
 
116
    if (net_reply)
 
117
    {
 
118
        if (net_reply->canReadLine())
 
119
        {
 
120
            QByteArray data = net_reply->readLine();
 
121
            const std::string replyLine(data.constData(), data.size());
 
122
            lineDataCallback_(replyLine);
 
123
        }
 
124
    }
 
125
}
 
126
 
102
127
void HttpClientQtThread::got_reply(QNetworkReply* reply)
103
128
{
104
129
    std::lock_guard<std::mutex> lock(reply_mutex_);
129
154
    else
130
155
    {
131
156
        success_ = true;
132
 
        QByteArray byte_array = reply->readAll();
133
 
        reply_ = std::string(byte_array.constData(), byte_array.size());
 
157
        // read any remaining lines
 
158
        while (reply->canReadLine())
 
159
        {
 
160
            const QByteArray byte_array = reply->readLine();
 
161
            lineDataCallback_(std::string(byte_array.constData(), byte_array.size()));
 
162
        }
 
163
        // there may be data left which is not "\n" terminated
 
164
        if (reply->bytesAvailable())
 
165
        {
 
166
            const QByteArray byte_array = reply->readAll();
 
167
            lineDataCallback_(std::string(byte_array.constData(), byte_array.size()));
 
168
        }
134
169
    }
135
170
 
136
171
    quit();