videodownloader/network/misc/contentdispositionparser.cpp

133 lines
3.2 KiB
C++
Raw Normal View History

2015-09-08 17:05:59 +02:00
#include "./contentdispositionparser.h"
2015-04-22 19:32:04 +02:00
namespace Network {
/*!
* \class ContentDispositionParser
* \brief The ContentDispositionParser class parses a HTTP content disposition.
*/
/*!
* \brief Constructs a new ContentDispositionParser for the specified \a contentDisposition.
*/
ContentDispositionParser::ContentDispositionParser(const QString &contentDisposition)
{
this->m_contentDisposition = contentDisposition;
}
/*!
* \brief Adds data (internally used only).
*/
void ContentDispositionParser::addData(QString fieldName, QString value)
{
2017-05-01 03:22:50 +02:00
while (fieldName.length() > 0 && fieldName.at(0) == ' ') {
2015-04-22 19:32:04 +02:00
fieldName = fieldName.mid(1);
}
2017-05-01 03:22:50 +02:00
while (value.length() > 0 && value.at(0) == ' ') {
2015-04-22 19:32:04 +02:00
value = value.mid(1);
}
2017-05-01 03:22:50 +02:00
while (fieldName.endsWith(' ')) {
2015-04-22 19:32:04 +02:00
fieldName = fieldName.mid(0, fieldName.length() - 1);
}
2017-05-01 03:22:50 +02:00
while (value.endsWith(' ')) {
2015-04-22 19:32:04 +02:00
value = value.mid(0, value.length() - 1);
}
2017-05-01 03:22:50 +02:00
if (fieldName.compare("attachment", Qt::CaseInsensitive)) {
2015-04-22 19:32:04 +02:00
m_attachment = true;
2017-05-01 03:22:50 +02:00
} else if (fieldName.compare("filename", Qt::CaseInsensitive)) {
2015-04-22 19:32:04 +02:00
m_fileName = value;
}
m_data.insert(fieldName, value);
}
/*!
* \brief Parses the content disposition.
*/
void ContentDispositionParser::pharse()
{
bool inQuotationMarks = false;
PharsingPosition pos = FieldName;
QString fieldName;
QString value;
2017-05-01 03:22:50 +02:00
foreach (QChar c, m_contentDisposition) {
if (c == '\"') {
2015-04-22 19:32:04 +02:00
inQuotationMarks = !inQuotationMarks;
2017-05-01 03:22:50 +02:00
} else if (c == ';') {
if (inQuotationMarks) {
switch (pos) {
2015-04-22 19:32:04 +02:00
case FieldName:
fieldName.append(c);
break;
case Value:
value.append(c);
break;
}
} else {
addData(fieldName, value);
fieldName.clear();
value.clear();
pos = FieldName;
}
2017-05-01 03:22:50 +02:00
} else if (c == '=') {
if (inQuotationMarks) {
switch (pos) {
2015-04-22 19:32:04 +02:00
case FieldName:
fieldName.append(c);
break;
case Value:
value.append(c);
break;
}
} else {
2017-05-01 03:22:50 +02:00
switch (pos) {
2015-04-22 19:32:04 +02:00
case FieldName:
pos = Value;
break;
case Value:
value.append(c);
break;
}
}
} else {
2017-05-01 03:22:50 +02:00
switch (pos) {
2015-04-22 19:32:04 +02:00
case FieldName:
fieldName.append(c);
break;
case Value:
value.append(c);
break;
}
}
}
2017-05-01 03:22:50 +02:00
if (!fieldName.isEmpty()) {
2015-04-22 19:32:04 +02:00
addData(fieldName, value);
}
}
/*!
* \brief Returns the file name.
*/
const QString &ContentDispositionParser::fileName()
{
return m_fileName;
}
/*!
* \brief Returns whether the content is an attachment.
*/
bool ContentDispositionParser::isAttachment()
{
return m_attachment;
}
/*!
* \brief Returns the data.
*/
const QMap<QString, QString> &ContentDispositionParser::data()
{
return m_data;
}
2019-07-20 20:20:58 +02:00
} // namespace Network