Enable specifying raw headers for the request
Use "--headers=file" where the file contains the raw headers to send. This is useful for replaying requests from log files. Change-Id: I3bbe582d96fc9797f692a0d5772e8164f8265ce0 Reviewed-by: Martin Petersson <Martin.Petersson@nokia.com>
This commit is contained in:
parent
1aeaf0e708
commit
32adb82741
@ -61,23 +61,20 @@ DownloadManager::~DownloadManager()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadManager::get(const QUrl &url, const QString &user, const QString &password)
|
void DownloadManager::get(const QNetworkRequest &request, const QString &user, const QString &password)
|
||||||
{
|
{
|
||||||
DownloadItem *dl = new DownloadItem(QNetworkRequest(url), user, password, nam);
|
DownloadItem *dl = new DownloadItem(request, user, password, nam);
|
||||||
transfers.append(dl);
|
transfers.append(dl);
|
||||||
connect(dl, SIGNAL(downloadFinished(TransferItem*)), SLOT(downloadFinished(TransferItem*)));
|
connect(dl, SIGNAL(downloadFinished(TransferItem*)), SLOT(downloadFinished(TransferItem*)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadManager::upload(const QUrl &url, const QString &user, const QString &password, const QString &filename, const QString &contentType, TransferItem::Method method)
|
void DownloadManager::upload(const QNetworkRequest &request, const QString &user, const QString &password, const QString &filename, TransferItem::Method method)
|
||||||
{
|
{
|
||||||
QScopedPointer<QFile> file(new QFile(filename));
|
QScopedPointer<QFile> file(new QFile(filename));
|
||||||
if (!file->open(QFile::ReadOnly)) {
|
if (!file->open(QFile::ReadOnly)) {
|
||||||
qDebug() << "Can't open input file" << file->fileName() << file->errorString();
|
qDebug() << "Can't open input file" << file->fileName() << file->errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QNetworkRequest request(url);
|
|
||||||
if (!contentType.isEmpty())
|
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
|
|
||||||
UploadItem *ul = new UploadItem(request, user, password, nam, file.take(), method);
|
UploadItem *ul = new UploadItem(request, user, password, nam, file.take(), method);
|
||||||
transfers.append(ul);
|
transfers.append(ul);
|
||||||
connect(ul, SIGNAL(downloadFinished(TransferItem*)), SLOT(downloadFinished(TransferItem*)));
|
connect(ul, SIGNAL(downloadFinished(TransferItem*)), SLOT(downloadFinished(TransferItem*)));
|
||||||
|
@ -74,6 +74,7 @@ void printUsage()
|
|||||||
<< " ,socks SOCKS5 proxy" << endl
|
<< " ,socks SOCKS5 proxy" << endl
|
||||||
<< " ,ftp FTP proxy" << endl
|
<< " ,ftp FTP proxy" << endl
|
||||||
<< " ,httpcaching HTTP caching proxy (no CONNECT method)" << endl
|
<< " ,httpcaching HTTP caching proxy (no CONNECT method)" << endl
|
||||||
|
<< "--headers=filename Set request headers from file contents" << endl
|
||||||
<< "--post=filename upload the file to the next url using HTTP POST" << endl
|
<< "--post=filename upload the file to the next url using HTTP POST" << endl
|
||||||
<< "--put=filename upload the file to the next url using HTTP PUT" << endl
|
<< "--put=filename upload the file to the next url using HTTP PUT" << endl
|
||||||
<< "--content-type=<MIME> set content-type header for upload" << endl
|
<< "--content-type=<MIME> set content-type header for upload" << endl
|
||||||
@ -96,6 +97,7 @@ int main(int argc, char *argv[])
|
|||||||
QString contentType;
|
QString contentType;
|
||||||
QString httpUser;
|
QString httpUser;
|
||||||
QString httpPassword;
|
QString httpPassword;
|
||||||
|
QString headersFile;
|
||||||
TransferItem::Method method = TransferItem::Get;
|
TransferItem::Method method = TransferItem::Get;
|
||||||
//arguments match wget where possible
|
//arguments match wget where possible
|
||||||
foreach (QString str, app.arguments().mid(1)) {
|
foreach (QString str, app.arguments().mid(1)) {
|
||||||
@ -161,19 +163,43 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
else if (str.startsWith("--content-type="))
|
else if (str.startsWith("--content-type="))
|
||||||
contentType=str.mid(15);
|
contentType=str.mid(15);
|
||||||
|
else if (str.startsWith("--headers="))
|
||||||
|
headersFile=str.mid(10);
|
||||||
else if (str == "--serial")
|
else if (str == "--serial")
|
||||||
dl.setQueueMode(DownloadManager::Serial);
|
dl.setQueueMode(DownloadManager::Serial);
|
||||||
else if (str.startsWith("-"))
|
else if (str.startsWith("-"))
|
||||||
qDebug() << "unsupported option" << str;
|
qDebug() << "unsupported option" << str;
|
||||||
else {
|
else {
|
||||||
QUrl url(QUrl::fromUserInput(str));
|
QUrl url(QUrl::fromUserInput(str));
|
||||||
|
QNetworkRequest request(url);
|
||||||
|
//set headers
|
||||||
|
if (!headersFile.isEmpty()) {
|
||||||
|
QFile f(headersFile);
|
||||||
|
if (!f.open(QFile::ReadOnly | QFile::Text)) {
|
||||||
|
qDebug() << "can't open headers file: " << headersFile;
|
||||||
|
} else {
|
||||||
|
while (!f.atEnd()) {
|
||||||
|
QByteArray line = f.readLine().trimmed();
|
||||||
|
if (line.isEmpty()) break;
|
||||||
|
int colon = line.indexOf(':');
|
||||||
|
qDebug() << line;
|
||||||
|
if (colon > 0 && colon < line.length() - 2) {
|
||||||
|
request.setRawHeader(line.left(colon), line.mid(colon+2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!contentType.isEmpty())
|
||||||
|
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
|
||||||
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case TransferItem::Put:
|
case TransferItem::Put:
|
||||||
case TransferItem::Post:
|
case TransferItem::Post:
|
||||||
dl.upload(url, httpUser, httpPassword, uploadFileName, contentType, method);
|
dl.upload(request, httpUser, httpPassword, uploadFileName, method);
|
||||||
break;
|
break;
|
||||||
case TransferItem::Get:
|
case TransferItem::Get:
|
||||||
dl.get(url, httpUser, httpPassword);
|
dl.get(request, httpUser, httpPassword);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
method = TransferItem::Get; //default for urls without a request type before it
|
method = TransferItem::Get; //default for urls without a request type before it
|
||||||
|
@ -98,8 +98,8 @@ class DownloadManager : public QObject
|
|||||||
public:
|
public:
|
||||||
DownloadManager();
|
DownloadManager();
|
||||||
~DownloadManager();
|
~DownloadManager();
|
||||||
void get(const QUrl &url, const QString &user, const QString &password);
|
void get(const QNetworkRequest &request, const QString &user, const QString &password);
|
||||||
void upload(const QUrl &url, const QString &user, const QString &password, const QString &filename, const QString &contentType, TransferItem::Method method);
|
void upload(const QNetworkRequest &request, const QString &user, const QString &password, const QString &filename, TransferItem::Method method);
|
||||||
void setProxy(const QNetworkProxy &proxy) { nam.setProxy(proxy); }
|
void setProxy(const QNetworkProxy &proxy) { nam.setProxy(proxy); }
|
||||||
void setProxyUser(const QString &user) { proxyUser = user; }
|
void setProxyUser(const QString &user) { proxyUser = user; }
|
||||||
void setProxyPassword(const QString &password) { proxyPassword = password; }
|
void setProxyPassword(const QString &password) { proxyPassword = password; }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user