From 3e018b0fa65b6fa44e38c130ad9a77907d29a30b Mon Sep 17 00:00:00 2001 From: Victor Tran Date: Mon, 11 Sep 2023 21:13:06 +1000 Subject: [PATCH] Implement content types for sending --- libthezzz/providers/bodyprovider.cpp | 16 +++- libthezzz/providers/bodyprovider.h | 3 + libthezzz/translations/en.ts | 52 +++++++++++- libthezzz/translations/en_US.ts | 52 +++++++++++- libthezzz/translations/he.ts | 52 +++++++++++- libthezzz/translations/vi_VN.ts | 52 +++++++++++- .../providereditor/bodyprovidereditor.cpp | 66 ++++++++++++++- .../providereditor/bodyprovidereditor.h | 5 ++ .../providereditor/bodyprovidereditor.ui | 83 +++++++++++++++++++ 9 files changed, 373 insertions(+), 8 deletions(-) diff --git a/libthezzz/providers/bodyprovider.cpp b/libthezzz/providers/bodyprovider.cpp index fc19619..ab30e49 100644 --- a/libthezzz/providers/bodyprovider.cpp +++ b/libthezzz/providers/bodyprovider.cpp @@ -6,11 +6,13 @@ struct BodyProviderPrivate { QByteArray body; + QString contentType; }; BodyProvider::BodyProvider(WorkspaceFile* parent) : ZzzProvider{parent} { d = new BodyProviderPrivate(); + d->contentType = "text/plain"; } BodyProvider::~BodyProvider() { @@ -26,6 +28,15 @@ void BodyProvider::setBody(QByteArray body) { emit this->workspace()->dataChanged(); } +QString BodyProvider::contentType() { + return d->contentType; +} + +void BodyProvider::setContentType(QString contentType) { + d->contentType = contentType; + emit this->workspace()->dataChanged(); +} + QString BodyProvider::jsonKey() { return QStringLiteral("body"); } @@ -33,11 +44,13 @@ QString BodyProvider::jsonKey() { void BodyProvider::loadJson(QJsonValue obj, QJsonValue localObj) { auto object = obj.toObject(); d->body = QByteArray::fromBase64(object.value("payload").toString().toUtf8()); + d->contentType = object.value("contentType").toString("text/plain"); } QJsonValue BodyProvider::toJson() { return QJsonObject{ - {"payload", QString(d->body.toBase64())} + {"payload", QString(d->body.toBase64())}, + {"contentType", d->contentType } }; } @@ -48,5 +61,6 @@ QList BodyProvider::editor() { ZzzHeaders BodyProvider::implicitHeaders() { return { {QStringLiteral("Content-Length").toUtf8(), QStringLiteral("%1").arg(d->body.length()).toUtf8()}, + {QStringLiteral("Content-Type").toUtf8(), d->contentType.toUtf8() } }; } diff --git a/libthezzz/providers/bodyprovider.h b/libthezzz/providers/bodyprovider.h index 21a9427..220c526 100644 --- a/libthezzz/providers/bodyprovider.h +++ b/libthezzz/providers/bodyprovider.h @@ -12,6 +12,9 @@ class BodyProvider : public ZzzProvider { QByteArray body(); void setBody(QByteArray body); + QString contentType(); + void setContentType(QString contentType); + private: BodyProviderPrivate* d; diff --git a/libthezzz/translations/en.ts b/libthezzz/translations/en.ts index 213f8bd..ac4e295 100644 --- a/libthezzz/translations/en.ts +++ b/libthezzz/translations/en.ts @@ -16,11 +16,61 @@ - + + Plain Text + + + + + Custom + + + + + JSON + + + + + Multipart Data + + + + + Form URL-Encoded + + + + + XML + + + + + YAML + + + + + Content Type + + + + Body Short for Body + + + Custom Content Type + + + + + What content type are you sending? + + DescriptionProviderEditor diff --git a/libthezzz/translations/en_US.ts b/libthezzz/translations/en_US.ts index 6ed4f1e..4ddcba4 100644 --- a/libthezzz/translations/en_US.ts +++ b/libthezzz/translations/en_US.ts @@ -16,11 +16,61 @@ - + + Plain Text + + + + + Custom + + + + + JSON + + + + + Multipart Data + + + + + Form URL-Encoded + + + + + XML + + + + + YAML + + + + + Content Type + + + + Body Short for Body + + + Custom Content Type + + + + + What content type are you sending? + + DescriptionProviderEditor diff --git a/libthezzz/translations/he.ts b/libthezzz/translations/he.ts index 91c9477..f53465f 100644 --- a/libthezzz/translations/he.ts +++ b/libthezzz/translations/he.ts @@ -16,11 +16,61 @@ - + + Plain Text + + + + + Custom + + + + + JSON + + + + + Multipart Data + + + + + Form URL-Encoded + + + + + XML + + + + + YAML + + + + + Content Type + + + + Body Short for Body + + + Custom Content Type + + + + + What content type are you sending? + + DescriptionProviderEditor diff --git a/libthezzz/translations/vi_VN.ts b/libthezzz/translations/vi_VN.ts index b92e4bd..1a8b137 100644 --- a/libthezzz/translations/vi_VN.ts +++ b/libthezzz/translations/vi_VN.ts @@ -16,11 +16,61 @@ - + + Plain Text + + + + + Custom + + + + + JSON + + + + + Multipart Data + + + + + Form URL-Encoded + + + + + XML + + + + + YAML + + + + + Content Type + + + + Body Short for Body + + + Custom Content Type + + + + + What content type are you sending? + + DescriptionProviderEditor diff --git a/libthezzz/widgets/providereditor/bodyprovidereditor.cpp b/libthezzz/widgets/providereditor/bodyprovidereditor.cpp index 833a8d4..9829243 100644 --- a/libthezzz/widgets/providereditor/bodyprovidereditor.cpp +++ b/libthezzz/widgets/providereditor/bodyprovidereditor.cpp @@ -3,10 +3,15 @@ #include "providers/bodyprovider.h" #include "workspacefile.h" +#include +#include +#include #include struct BodyProviderEditorPrivate { BodyProvider* provider; + QActionGroup* actionGroup; + QList> contentTypeMapping; }; BodyProviderEditor::BodyProviderEditor(BodyProvider* bodyProvider, QWidget* parent) : @@ -20,6 +25,33 @@ BodyProviderEditor::BodyProviderEditor(BodyProvider* bodyProvider, QWidget* pare connect(ui->editor->editor(), &TextEditor::textChanged, this, [this] { d->provider->setBody(ui->editor->editor()->text().toUtf8()); }); + + d->contentTypeMapping.append({ui->actionPlain_Text, QStringLiteral("text/plain")}); + d->contentTypeMapping.append({ui->actionJSON, QStringLiteral("application/json")}); + d->contentTypeMapping.append({ui->actionForm_Data, QStringLiteral("multipart/form-data")}); + d->contentTypeMapping.append({ui->actionx_www_form_urlencoded, QStringLiteral("application/x-www-form-urlencoded")}); + d->contentTypeMapping.append({ui->actionXML, QStringLiteral("application/xml")}); + d->contentTypeMapping.append({ui->actionYAML, QStringLiteral("text/yaml")}); + d->contentTypeMapping.append({ui->actionCustom, QStringLiteral("")}); + + QMenu* contentTypeMenu = new QMenu(this); + contentTypeMenu->addSection(tr("Content Type")); + d->actionGroup = new QActionGroup(this); + d->actionGroup->setExclusive(true); + for (const auto& action : d->contentTypeMapping) { + d->actionGroup->addAction(action.first); + contentTypeMenu->addAction(action.first); + } + ui->contentTypeButton->setMenu(contentTypeMenu); + + connect(d->actionGroup, &QActionGroup::triggered, this, [this](QAction* action) { + for (auto a : d->contentTypeMapping) { + if (a.first == action && !a.second.isEmpty() && a.second != QStringLiteral("multipart/form-data")) { + d->provider->setContentType(a.second); + return; + } + } + }); } BodyProviderEditor::~BodyProviderEditor() { @@ -36,8 +68,36 @@ int BodyProviderEditor::order() { } void BodyProviderEditor::updateData() { - if (ui->editor->editor()->hasFocus()) return; + if (!ui->editor->editor()->hasFocus()) { + QSignalBlocker blocker(ui->editor->editor()); + ui->editor->editor()->setText(d->provider->body()); + } + + auto contentType = d->provider->contentType().split(";").first().trimmed(); + + for (const auto& a : d->contentTypeMapping) { + if (a.second == contentType) { + ui->contentTypeButton->setText(a.first->text()); + a.first->setChecked(true); + return; + } + } + + ui->contentTypeButton->setText(contentType); + ui->actionCustom->setChecked(true); +} + +void BodyProviderEditor::on_actionCustom_triggered() { + bool ok; + auto contentType = tInputDialog::getText(this->window(), tr("Custom Content Type"), tr("What content type are you sending?"), QLineEdit::Normal, d->provider->contentType(), &ok); + if (ok) { + d->provider->setContentType(contentType); + } + + this->updateData(); +} - QSignalBlocker blocker(ui->editor->editor()); - ui->editor->editor()->setText(d->provider->body()); +void BodyProviderEditor::on_actionForm_Data_triggered() { + auto boundary = QStringLiteral("--XXX-BOUNDARY-%1-XXX-").arg(QUuid::createUuid().toString(QUuid::WithoutBraces)); + d->provider->setContentType(QStringLiteral("multipart/form-data; boundary=%1").arg(boundary)); } diff --git a/libthezzz/widgets/providereditor/bodyprovidereditor.h b/libthezzz/widgets/providereditor/bodyprovidereditor.h index c70714a..759c0d3 100644 --- a/libthezzz/widgets/providereditor/bodyprovidereditor.h +++ b/libthezzz/widgets/providereditor/bodyprovidereditor.h @@ -22,6 +22,11 @@ class BodyProviderEditor : public ProviderEditor { protected: void updateData(); + private slots: + void on_actionCustom_triggered(); + + void on_actionForm_Data_triggered(); + private: Ui::BodyProviderEditor* ui; BodyProviderEditorPrivate* d; diff --git a/libthezzz/widgets/providereditor/bodyprovidereditor.ui b/libthezzz/widgets/providereditor/bodyprovidereditor.ui index 9d273a6..ffe9a5a 100644 --- a/libthezzz/widgets/providereditor/bodyprovidereditor.ui +++ b/libthezzz/widgets/providereditor/bodyprovidereditor.ui @@ -52,6 +52,33 @@ + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + QToolButton::InstantPopup + + + + + @@ -63,6 +90,62 @@ + + + true + + + Plain Text + + + + + true + + + Custom + + + + + true + + + JSON + + + + + true + + + Multipart Data + + + + + true + + + Form URL-Encoded + + + + + true + + + XML + + + + + true + + + YAML + +