Skip to content

Commit

Permalink
Factor out s3_make_date by refactoring performOp
Browse files Browse the repository at this point in the history
Moved the creation of the signData string to performOp making
s3_make_date() unneeded. Added signDataKey to allow for optional
signData key for methods like getFile(). Should also make adding
methods that user other Methods easier.

Also cleanes up the converting of date to QString and just converts it
to QBytearray when needed.
  • Loading branch information
Thaodan committed Jun 28, 2020
1 parent 7211075 commit 472ad31
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
42 changes: 29 additions & 13 deletions src/s3access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,36 +110,56 @@ QString S3Access::bucket() const

S3ListResult *S3Access::list(QString const &prefix)
{
char *date;
QString signData;
QString url;

QNetworkReply *reply;
S3ListResult *result;

date = s3_make_date();
signData = "GET\n\n\n" + QString(date) + "\n/" + m_bucket + "/";
url = "http://" + m_baseUrl + "/" + m_bucket + "/?delimiter=/";
if (!prefix.isEmpty()) {
url += "&prefix=" + prefix;
}

reply = performOp(GET, url, signData, date, nullptr, nullptr, nullptr);
reply = performOp(GET, url, nullptr, nullptr, nullptr);
result = new S3ListResult(reply, this);
free(date);

return result;
}

QNetworkReply *S3Access::performOp(Method method, QString const &url, QString const &sign_data, const char *date, QIODevice *in, const char *content_md5, const char *content_type)
QNetworkReply *S3Access::performOp(Method method, QString const &url, QIODevice *in, const char *content_md5, const char *content_type, QString signDataKey)
{
QNetworkRequest request;
char *digest;
QString methodStr;
QString date = QDateTime::currentDateTimeUtc().toString(Qt::RFC2822Date);
QNetworkReply *reply;

switch (method) {
case DELETE:
methodStr = "DELETE";
qDebug() << "DELETE request";
break;
case PUT:
methodStr = "PUT";
qDebug() << "PUT request";
break;
case GET:
default: // GET
methodStr = "GET";
qDebug() << "GET request";
break;
}

QString signData = methodStr + "\n\n\n" + + "\n/" + m_bucket + "/";

if (signDataKey != nullptr)
signData+=signDataKey;

request.setUrl(QUrl(QString(url)));
digest = s3_hmac_sign(m_secret.toLatin1().data(), sign_data.toLatin1().data(), sign_data.toLatin1().size());
digest = s3_hmac_sign(m_secret.toLatin1().data(), signData.toLatin1().data(), signData.toLatin1().size());

request.setRawHeader("Date", date);
request.setRawHeader("Date", date.toLocal8Bit());
request.setRawHeader("Authorization", QString(QStringLiteral("AWS %1:%2")).arg(m_id).arg(digest).toLocal8Bit());
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);

Expand Down Expand Up @@ -220,19 +240,15 @@ QByteArray S3GetResult::data() const

S3GetFileResult *S3Access::getFile(QString const &key, QString const &filename)
{
char *date;
QString signData;
QString url;
QNetworkReply *reply;
S3GetFileResult *result;

date = s3_make_date();
signData = "GET\n\n\n" + QString(date) + "\n/" + m_bucket + "/" + key;
url = "http://" + m_baseUrl + "/" + m_bucket + "/" + key;

reply = performOp(GET, url, signData, date, nullptr, nullptr, nullptr);
reply = performOp(GET, url, nullptr, nullptr, nullptr, key);
result = new S3GetFileResult(reply, filename, this);
free(date);

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/s3access.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public slots:
DELETE
};
void initialise();
QNetworkReply *performOp(Method method, QString const &url, QString const &sign_data, const char *date, QIODevice *in, const char *content_md5, const char *content_type);
QNetworkReply *performOp(Method method, QString const &url, QIODevice *in, const char *content_md5, const char *content_type, QString signDatakey = nullptr);

private:
QString m_id;
Expand Down

0 comments on commit 472ad31

Please sign in to comment.