Skip to content

Commit

Permalink
Change MythDownloadManager to allow for multiple headers
Browse files Browse the repository at this point in the history
Seems we should be setting the Content-Type header for schedulesdirect calls,
and QT 4.8 might be putting in the wrong thing as a default if we don't
specify.  This makes it so we can send a hash of headers rather than just one
key/value pair.
  • Loading branch information
Beirdo committed Jul 25, 2012
1 parent bdfcabc commit 1850a25
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
43 changes: 21 additions & 22 deletions mythtv/libs/libmythbase/mythdownloadmanager.cpp
Expand Up @@ -49,8 +49,7 @@ class MythDownloadInfo
m_processReply(true), m_done(false), m_bytesReceived(0),
m_bytesTotal(0), m_lastStat(MythDate::current()),
m_authCallback(NULL), m_authArg(NULL),
m_header(NULL), m_headerVal(NULL),
m_errorCode(QNetworkReply::NoError)
m_headers(NULL), m_errorCode(QNetworkReply::NoError)
{
qRegisterMetaType<QNetworkReply::NetworkError>("QNetworkReply::NetworkError");
}
Expand Down Expand Up @@ -88,8 +87,7 @@ class MythDownloadInfo
QDateTime m_lastStat;
AuthCallback m_authCallback;
void *m_authArg;
const QByteArray *m_header;
const QByteArray *m_headerVal;
const QHash<QByteArray, QByteArray> *m_headers;

QNetworkReply::NetworkError m_errorCode;
};
Expand Down Expand Up @@ -365,16 +363,14 @@ void MythDownloadManager::queueItem(const QString &url, QNetworkRequest *req,
* \param reload Force reloading of the URL
* \param authCallback AuthCallback function for authentication
* \param authArg Opaque argument for callback function
* \param header Optional HTTP header to add to the request
* \param headerVal Value for the optional HTTP header to add to the request
* \param headers Hash of optional HTTP header to add to the request
*/
bool MythDownloadManager::processItem(const QString &url, QNetworkRequest *req,
const QString &dest, QByteArray *data,
const MRequestType reqType,
const bool reload,
AuthCallback authCallback, void *authArg,
const QByteArray *header,
const QByteArray *headerVal)
const QHash<QByteArray, QByteArray> *headers)
{
MythDownloadInfo *dlInfo = new MythDownloadInfo;

Expand All @@ -387,8 +383,7 @@ bool MythDownloadManager::processItem(const QString &url, QNetworkRequest *req,
dlInfo->m_syncMode = true;
dlInfo->m_authCallback = authCallback;
dlInfo->m_authArg = authArg;
dlInfo->m_header = header;
dlInfo->m_headerVal = headerVal;
dlInfo->m_headers = headers;

return downloadNow(dlInfo);
}
Expand Down Expand Up @@ -509,16 +504,15 @@ bool MythDownloadManager::download(QNetworkRequest *req, QByteArray *data)
* \param reload Whether to force reloading of the URL or not
* \param authCallback AuthCallback function for use with authentication
* \param authArg Opaque argument for callback function
* \param header Optional HTTP header to add to the request
* \param headerVal Value for the optional HTTP header to add to the request
* \param headers Hash of optional HTTP header to add to the request
* \return true if download was successful, false otherwise.
*/
bool MythDownloadManager::downloadAuth(const QString &url, const QString &dest,
const bool reload, AuthCallback authCallback, void *authArg,
const QByteArray *header, const QByteArray *headerVal)
const QHash<QByteArray, QByteArray> *headers)
{
return processItem(url, NULL, dest, NULL, kRequestGet, reload, authCallback,
authArg, header, headerVal);
authArg, headers);
}


Expand Down Expand Up @@ -609,14 +603,12 @@ bool MythDownloadManager::post(QNetworkRequest *req, QByteArray *data)
* \param data Location holding post and response data
* \param authCallback AuthCallback function for authentication
* \param authArg Opaque argument for callback function
* \param header Optional HTTP header to add to the request
* \param headerVal Value for the optional HTTP header to add to the request
* \param headers Hash of optional HTTP headers to add to the request
* \return true if post was successful, false otherwise.
*/
bool MythDownloadManager::postAuth(const QString &url, QByteArray *data,
AuthCallback authCallback, void *authArg,
const QByteArray *header,
const QByteArray *headerVal)
const QHash<QByteArray, QByteArray> *headers)
{
LOG(VB_FILE, LOG_DEBUG, LOC + QString("postAuth('%1', '%2')")
.arg(url).arg((long long)data));
Expand All @@ -628,7 +620,7 @@ bool MythDownloadManager::postAuth(const QString &url, QByteArray *data,
}

return processItem(url, NULL, NULL, data, kRequestPost, false, authCallback,
authArg, header, headerVal);
authArg, headers);
}

/** \brief Triggers a myth:// URI download in the background via RemoteFile
Expand Down Expand Up @@ -718,10 +710,17 @@ void MythDownloadManager::downloadQNetworkRequest(MythDownloadInfo *dlInfo)
request.setRawHeader("User-Agent",
"MythTV v" MYTH_BINARY_VERSION " MythDownloadManager");

if (dlInfo->m_header && dlInfo->m_headerVal &&
!dlInfo->m_header->isEmpty() && !dlInfo->m_headerVal->isEmpty())
if (dlInfo->m_headers)
{
request.setRawHeader(*(dlInfo->m_header), *(dlInfo->m_headerVal));
QHash<QByteArray, QByteArray>::const_iterator it =
dlInfo->m_headers->constBegin();
for ( ; it != dlInfo->m_headers->constEnd(); ++it )
{
if (!it.key().isEmpty() && !it.value().isEmpty())
{
request.setRawHeader(it.key(), it.value());
}
}
}

switch (dlInfo->m_requestType)
Expand Down
12 changes: 6 additions & 6 deletions mythtv/libs/libmythbase/mythdownloadmanager.h
Expand Up @@ -10,6 +10,7 @@
#include <QNetworkProxy>
#include <QWaitCondition>
#include <QString>
#include <QHash>

#include "mythbaseexp.h"
#include "mthread.h"
Expand Down Expand Up @@ -57,8 +58,8 @@ class MBASE_PUBLIC MythDownloadManager : public QObject, public MThread
bool downloadAuth(const QString &url, const QString &dest,
const bool reload = false,
AuthCallback authCallback = NULL,
void *authArg = NULL, const QByteArray *header = NULL,
const QByteArray *headerVal = NULL);
void *authArg = NULL,
const QHash<QByteArray, QByteArray> *headers = NULL);

// Methods to POST to a URL
void queuePost(const QString &url, QByteArray *data, QObject *caller);
Expand All @@ -67,8 +68,7 @@ class MBASE_PUBLIC MythDownloadManager : public QObject, public MThread
bool post(QNetworkRequest *req, QByteArray *data);
bool postAuth(const QString &url, QByteArray *data,
AuthCallback authCallback, void *authArg,
const QByteArray *header = NULL,
const QByteArray *headerVal = NULL);
const QHash<QByteArray, QByteArray> *headers = NULL);

// Cancel a download
void cancelDownload(const QString &url);
Expand Down Expand Up @@ -112,8 +112,8 @@ class MBASE_PUBLIC MythDownloadManager : public QObject, public MThread
const MRequestType reqType = kRequestGet,
const bool reload = false,
AuthCallback authCallback = NULL,
void *authArg = NULL, const QByteArray *header = NULL,
const QByteArray *headerVal = NULL);
void *authArg = NULL,
const QHash<QByteArray, QByteArray> *headers = NULL);

void downloadRemoteFile(MythDownloadInfo *dlInfo);
void downloadQNetworkRequest(MythDownloadInfo *dlInfo);
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/mythversion.h
Expand Up @@ -12,7 +12,7 @@
/// Update this whenever the plug-in API changes.
/// Including changes in the libmythbase, libmyth, libmythtv, libmythav* and
/// libmythui class methods used by plug-ins.
#define MYTH_BINARY_VERSION "0.26.20120724-1"
#define MYTH_BINARY_VERSION "0.26.20120724-2"

/** \brief Increment this whenever the MythTV network protocol changes.
*
Expand Down
13 changes: 9 additions & 4 deletions mythtv/libs/libmythtv/datadirect.cpp
Expand Up @@ -1010,15 +1010,16 @@ bool DataDirectProcessor::DDPost(QString ddurl, QString &inputFile,
inputFile = QString("/tmp/mythtv_ddp_data");
}

const QByteArray header = "Accept-Encoding";
const QByteArray value = "gzip";
QHash<QByteArray, QByteArray> headers;
headers.insert("Accept-Encoding", "gzip");
headers.insert("Content-Type", "application/soap+xml; charset=utf-8");

LOG(VB_GENERAL, LOG_INFO, "Downloading DataDirect feed");

MythDownloadManager *manager = GetMythDownloadManager();

if (!manager->postAuth(ddurl, &postdata, &::authenticationCallback, this,
&header, &value))
&headers))
{
err_txt = QString("Download error");
return false;
Expand Down Expand Up @@ -1078,9 +1079,13 @@ bool DataDirectProcessor::GrabNextSuggestedTime(void)
postdata += "</SOAP-ENV:Body>\n";
postdata += "</SOAP-ENV:Envelope>\n";

QHash<QByteArray, QByteArray> headers;
headers.insert("Content-Type", "application/soap+xml; charset=utf-8");

MythDownloadManager *manager = GetMythDownloadManager();

if (!manager->postAuth(ddurl, &postdata, &::authenticationCallback, this))
if (!manager->postAuth(ddurl, &postdata, &::authenticationCallback, this,
&headers))
{
LOG(VB_GENERAL, LOG_ERR, LOC +
"GrabNextSuggestedTime: Could not download");
Expand Down

0 comments on commit 1850a25

Please sign in to comment.