WIP: new features for HTTP streams.

This commit is contained in:
Matthias Koefferlein 2017-03-26 23:27:51 +02:00
parent a5d0461284
commit d98495c18a
4 changed files with 75 additions and 12 deletions

View File

@ -78,7 +78,7 @@ public:
static QNetworkAccessManager *s_network_manager (0); static QNetworkAccessManager *s_network_manager (0);
InputHttpStream::InputHttpStream (const std::string &url) InputHttpStream::InputHttpStream (const std::string &url)
: m_url (url) : m_url (url), m_request ("GET"), mp_buffer (0)
{ {
if (! s_network_manager) { if (! s_network_manager) {
s_network_manager = new QNetworkAccessManager(0); s_network_manager = new QNetworkAccessManager(0);
@ -87,7 +87,7 @@ InputHttpStream::InputHttpStream (const std::string &url)
connect (s_network_manager, SIGNAL (finished (QNetworkReply *)), this, SLOT (finished (QNetworkReply *))); connect (s_network_manager, SIGNAL (finished (QNetworkReply *)), this, SLOT (finished (QNetworkReply *)));
connect (s_network_manager, SIGNAL (authenticationRequired (QNetworkReply *, QAuthenticator *)), this, SLOT (authenticationRequired (QNetworkReply *, QAuthenticator *))); connect (s_network_manager, SIGNAL (authenticationRequired (QNetworkReply *, QAuthenticator *)), this, SLOT (authenticationRequired (QNetworkReply *, QAuthenticator *)));
connect (s_network_manager, SIGNAL (proxyAuthenticationRequired (const QNetworkProxy &, QAuthenticator *)), this, SLOT (proxyAuthenticationRequired (const QNetworkProxy &, QAuthenticator *))); connect (s_network_manager, SIGNAL (proxyAuthenticationRequired (const QNetworkProxy &, QAuthenticator *)), this, SLOT (proxyAuthenticationRequired (const QNetworkProxy &, QAuthenticator *)));
s_network_manager->get (QNetworkRequest (QUrl (tl::to_qstring (url)))); issue_request (QUrl (tl::to_qstring (url)));
mp_reply = 0; mp_reply = 0;
} }
@ -97,6 +97,24 @@ InputHttpStream::~InputHttpStream ()
mp_reply = 0; mp_reply = 0;
} }
void
InputHttpStream::set_request (const char *r)
{
m_request = QByteArray (r);
}
void
InputHttpStream::set_data (const char *data)
{
m_data = QByteArray (data);
}
void
InputHttpStream::set_data (const char *data, size_t n)
{
m_data = QByteArray (data, int (n));
}
void void
InputHttpStream::authenticationRequired (QNetworkReply *reply, QAuthenticator *auth) InputHttpStream::authenticationRequired (QNetworkReply *reply, QAuthenticator *auth)
{ {
@ -117,13 +135,27 @@ InputHttpStream::finished (QNetworkReply *reply)
QVariant redirect_target = reply->attribute (QNetworkRequest::RedirectionTargetAttribute); QVariant redirect_target = reply->attribute (QNetworkRequest::RedirectionTargetAttribute);
if (reply->error () == QNetworkReply::NoError && ! redirect_target.isNull ()) { if (reply->error () == QNetworkReply::NoError && ! redirect_target.isNull ()) {
m_url = tl::to_string (redirect_target.toString ()); m_url = tl::to_string (redirect_target.toString ());
s_network_manager->get (QNetworkRequest (QUrl (redirect_target.toString ()))); issue_request (QUrl (redirect_target.toString ()));
delete reply; delete reply;
} else { } else {
mp_reply = reply; mp_reply = reply;
} }
} }
void
InputHttpStream::issue_request (const QUrl &url)
{
delete mp_buffer;
mp_buffer = 0;
if (m_data.isEmpty ()) {
s_network_manager->sendCustomRequest (QNetworkRequest (url), m_request);
} else {
mp_buffer = new QBuffer (&m_data);
s_network_manager->sendCustomRequest (QNetworkRequest (url), m_request, mp_buffer);
}
}
size_t size_t
InputHttpStream::read (char *b, size_t n) InputHttpStream::read (char *b, size_t n)
{ {
@ -160,4 +192,3 @@ InputHttpStream::filename () const
} }
} }

View File

@ -27,6 +27,8 @@
#include "tlStream.h" #include "tlStream.h"
#include <QObject> #include <QObject>
#include <QBuffer>
#include <QByteArray>
class QNetworkAccessManager; class QNetworkAccessManager;
class QNetworkReply; class QNetworkReply;
@ -68,9 +70,28 @@ public:
*/ */
virtual ~InputHttpStream (); virtual ~InputHttpStream ();
/**
* @brief Sets the request verb
* The default verb is "GET"
*/
void set_request (const char *r);
/**
* @brief Sets data to be sent with the request
* If data is given, it is sent along with the request.
* This version takes a null-terminated string.
*/
void set_data (const char *data);
/**
* @brief Sets data to be sent with the request
* If data is given, it is sent along with the request.
* This version takes a data plus length.
*/
void set_data (const char *data, size_t n);
/** /**
* @brief Read from the stream * @brief Read from the stream
*
* Implements the basic read method. * Implements the basic read method.
*/ */
virtual size_t read (char *b, size_t n); virtual size_t read (char *b, size_t n);
@ -89,14 +110,19 @@ public:
virtual std::string filename () const; virtual std::string filename () const;
private:
std::string m_url;
QNetworkReply *mp_reply;
private slots: private slots:
void finished (QNetworkReply *); void finished (QNetworkReply *);
void authenticationRequired (QNetworkReply *, QAuthenticator *); void authenticationRequired (QNetworkReply *, QAuthenticator *);
void proxyAuthenticationRequired (const QNetworkProxy &, QAuthenticator *); void proxyAuthenticationRequired (const QNetworkProxy &, QAuthenticator *);
private:
std::string m_url;
QNetworkReply *mp_reply;
QByteArray m_request;
QByteArray m_data;
QBuffer *mp_buffer;
void issue_request (const QUrl &url);
}; };
} }

View File

@ -28,6 +28,11 @@ std::string test_url1 ("http://www.klayout.org/svn-public/klayout-resources/trun
TEST(1) TEST(1)
{ {
EXPECT_EQ (to_string (12.5), "12.5"); tl::InputHttpStream stream (test_url1);
char b[100];
size_t n = stream.read (b, sizeof (b));
std::string res (b, n);
EXPECT_EQ (res, "hello, world.\n");
} }

View File

@ -97,7 +97,8 @@ SOURCES = \
gsiTest.cc \ gsiTest.cc \
tlFileSystemWatcher.cc \ tlFileSystemWatcher.cc \
laySalt.cc \ laySalt.cc \
tlFileUtils.cc tlFileUtils.cc \
tlHttpStream.cc
# main components: # main components:
SOURCES += \ SOURCES += \