Add a function in NetworkCommon parseHeaderList which parses a header list as a string into a vector of header pairs (#2623)
This commit is contained in:
@@ -30,6 +30,8 @@ set(SOURCE_FILES main.cpp
|
||||
common/LinkParser.hpp
|
||||
common/Modes.cpp
|
||||
common/Modes.hpp
|
||||
common/NetworkCommon.cpp
|
||||
common/NetworkCommon.hpp
|
||||
common/NetworkManager.cpp
|
||||
common/NetworkManager.hpp
|
||||
common/NetworkPrivate.cpp
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "common/NetworkCommon.hpp"
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
std::vector<std::pair<QByteArray, QByteArray>> parseHeaderList(
|
||||
const QString &headerListString)
|
||||
{
|
||||
std::vector<std::pair<QByteArray, QByteArray>> res;
|
||||
|
||||
// Split the string into a list of header pairs
|
||||
// e.g. "Authorization:secretkey;NextHeader:boo" turning into ["Authorization:secretkey","NextHeader:boo"]
|
||||
auto headerPairs = headerListString.split(";");
|
||||
|
||||
for (const auto &headerPair : headerPairs)
|
||||
{
|
||||
const auto headerName =
|
||||
headerPair.section(":", 0, 0).trimmed().toUtf8();
|
||||
const auto headerValue = headerPair.section(":", 1).trimmed().toUtf8();
|
||||
|
||||
if (headerName.isEmpty() || headerValue.isEmpty())
|
||||
{
|
||||
// The header part either didn't contain a : or the name/value was empty
|
||||
// Skip the value
|
||||
continue;
|
||||
}
|
||||
|
||||
res.emplace_back(headerName, headerValue);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#include <QString>
|
||||
|
||||
class QNetworkReply;
|
||||
|
||||
@@ -22,4 +25,13 @@ enum class NetworkRequestType {
|
||||
Patch,
|
||||
};
|
||||
|
||||
// parseHeaderList takes a list of headers in string form,
|
||||
// where each header pair is separated by semicolons (;) and the header name and value is divided by a colon (:)
|
||||
//
|
||||
// We return a vector of pairs, where the first value is the header name and the second value is the header value
|
||||
//
|
||||
// e.g. "Authorization:secretkey;NextHeader:boo" will return [{"Authorization", "secretkey"}, {"NextHeader", "boo"}]
|
||||
std::vector<std::pair<QByteArray, QByteArray>> parseHeaderList(
|
||||
const QString &headerListString);
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -106,16 +106,12 @@ NetworkRequest NetworkRequest::header(const char *headerName,
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::headerList(const QStringList &headers) &&
|
||||
NetworkRequest NetworkRequest::headerList(
|
||||
const std::vector<std::pair<QByteArray, QByteArray>> &headers) &&
|
||||
{
|
||||
for (const QString &header : headers)
|
||||
for (const auto &[headerName, headerValue] : headers)
|
||||
{
|
||||
const QStringList thisHeader = header.trimmed().split(":");
|
||||
if (thisHeader.size() == 2)
|
||||
{
|
||||
this->data->request_.setRawHeader(thisHeader[0].trimmed().toUtf8(),
|
||||
thisHeader[1].trimmed().toUtf8());
|
||||
}
|
||||
this->data->request_.setRawHeader(headerName, headerValue);
|
||||
}
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
@@ -54,7 +54,8 @@ public:
|
||||
NetworkRequest header(const char *headerName, const char *value) &&;
|
||||
NetworkRequest header(const char *headerName, const QByteArray &value) &&;
|
||||
NetworkRequest header(const char *headerName, const QString &value) &&;
|
||||
NetworkRequest headerList(const QStringList &headers) &&;
|
||||
NetworkRequest headerList(
|
||||
const std::vector<std::pair<QByteArray, QByteArray>> &headers) &&;
|
||||
NetworkRequest timeout(int ms) &&;
|
||||
NetworkRequest concurrent() &&;
|
||||
NetworkRequest authorizeTwitchV5(const QString &clientID,
|
||||
|
||||
@@ -128,8 +128,8 @@ void uploadImageToNuuls(RawImageData imageData, ChannelPtr channel,
|
||||
getSettings()->imageUploaderFormField.getValue().isEmpty()
|
||||
? getSettings()->imageUploaderFormField.getDefaultValue()
|
||||
: getSettings()->imageUploaderFormField);
|
||||
QStringList extraHeaders(
|
||||
getSettings()->imageUploaderHeaders.getValue().split(";"));
|
||||
auto extraHeaders =
|
||||
parseHeaderList(getSettings()->imageUploaderHeaders.getValue());
|
||||
QString originalFilePath = imageData.filePath;
|
||||
|
||||
QHttpMultiPart *payload = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
||||
|
||||
Reference in New Issue
Block a user