Revision control
- /**
- * OpenAPI Petstore
- *
- * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
- * https://openapi-generator.tech
- * Do not edit the class manually.
- */
-
- #include "OAIHttpRequest.h"
-
- #include <QtCore/qdatetime.h>
- #include <QtCore/qdir.h>
- #include <QtCore/qfileinfo.h>
- #include <QtCore/QRandomGenerator>
- #include <QtCore/qurl.h>
- #include <QtCore/quuid.h>
- #include <QtCore/QtGlobal>
- #include <QtNetwork/qformdatabuilder.h>
- #include <QtNetwork/qhttpmultipart.h>
- #include <QtNetwork/qnetworkaccessmanager.h>
- #include <QtNetwork/qnetworkrequestfactory.h>
- #include <QtNetwork/qrestaccessmanager.h>
-
-
- namespace OpenAPI {
-
- // Internal url encoding helper function
- QString toFormUrlEncoding(const QString &input)
- {
- // Space ( ) → replaced with +
- // Unsafe or non-alphanumeric characters → percent-encoded (%XX)
- // Alphanumeric characters (a–z, A–Z, 0–9) → left as-is
- // The following characters are always percent-encoded in x-www-form-urlencoded:
- // "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
- return QUrl::toPercentEncoding(input).replace("%20", "+");
- }
-
- OAIHttpRequestInput::OAIHttpRequestInput()
- {
- initialize();
- }
-
- OAIHttpRequestInput::OAIHttpRequestInput(const QString &vUrlStr, const QString &vHttpMethod)
- {
- initialize();
- m_urlStr = vUrlStr;
- m_httpMethod = vHttpMethod;
- }
-
- void OAIHttpRequestInput::initialize()
- {
- m_varLayout = NOT_SET;
- m_urlStr = "";
- m_httpMethod = "GET";
- m_multiPart.reset();
- }
-
- OAIHttpRequestInput::OAIHttpRequestInput(OAIHttpRequestInput &other)
- {
- m_urlStr = other.m_urlStr;
- m_httpMethod = other.m_httpMethod;
- m_varLayout = other.m_varLayout;
- m_vars = other.m_vars;
- m_fieldHeaders = other.m_fieldHeaders;
- m_headers = other.m_headers;
- m_files = other.m_files;
- m_requestBody = other.m_requestBody;
- m_queryItem = other.m_queryItem;
- }
-
- void OAIHttpRequestInput::addVar(const QString &key, const QString &value)
- {
- if (m_varLayout == URL_ENCODED)
- m_queryItem.addQueryItem(toFormUrlEncoding(key), toFormUrlEncoding(value));
- else
- m_vars[key] = value;
- }
-
- void OAIHttpRequestInput::addFieldHeaders(const QString &key, const QString &value)
- {
- m_fieldHeaders[key] = value;
- }
-
- void OAIHttpRequestInput::addFile(const QString &variableName, const QString &localFilename, const QString &requestFilename, const QString &mimeType)
- {
- OAIHttpFileElement file;
- file.m_variableName = variableName;
- file.m_localFilename = localFilename;
- file.m_requestFilename = requestFilename;
- file.m_mimeType = mimeType;
- m_files.append(file);
- }
-
- void OAIHttpRequestInput::addVarLayout(OAIHttpRequestVarLayout layout)
- {
- m_varLayout = layout;
- }
-
- void OAIHttpRequestInput::setHeaders(const QHttpHeaders &newHeaders)
- {
- m_headers = newHeaders;
- }
-
- namespace OAIHttpRequestWorker {
-
- OAIHttpFileElement getHttpFileElement(const QMap<QString, OAIHttpFileElement> &files, const QString &fieldname)
- {
- if (!files.isEmpty()) {
- if (fieldname.isEmpty()) {
- return files.first();
- } else if (files.contains(fieldname)) {
- return files[fieldname];
- }
- }
- return OAIHttpFileElement();
- }
-
- QByteArray *getMultiPartField(const QMap<QString, QByteArray *> &multiPartFields, const QString &fieldname)
- {
- if (!multiPartFields.isEmpty()) {
- if (fieldname.isEmpty()) {
- return multiPartFields.first();
- } else if (multiPartFields.contains(fieldname)) {
- return multiPartFields[fieldname];
- }
- }
- return nullptr;
- }
-
- QString httpAttributeEncode(const QString &attributeName, const QString &input)
- {
- // result structure follows RFC 5987
- bool needUtfEncoding = false;
- QString result = "";
- const QByteArray inputC = input.toLocal8Bit();
- char c;
- for (int i = 0; i < inputC.length(); i++) {
- c = inputC.at(i);
- if (c == '\\' || c == '/' || c == '\0' || c < ' ' || c > '~') {
- // ignore and request utf-8 version
- needUtfEncoding = true;
- } else if (c == '"') {
- result += "\\\"";
- } else {
- result += c;
- }
- }
-
- if (result.length() == 0) {
- needUtfEncoding = true;
- }
-
- if (!needUtfEncoding) {
- // return simple version
- return QString("%1=\"%2\"").arg(attributeName, result);
- }
-
- QString resultUtf8 = "";
- for (int i = 0; i < inputC.length(); i++) {
- c = inputC.at(i);
- if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
- resultUtf8 += c;
- } else {
- resultUtf8 += "%" + QString::number(static_cast<unsigned char>(inputC.at(i)), 16).toUpper();
- }
- }
-
- // return enhanced version with UTF-8 support
- return QString("%1=\"%2\"; %1*=utf-8''%3").arg(attributeName, result, resultUtf8);
- }
-
- QNetworkRequest getNetworkRequest(OAIHttpRequestInput &input, QByteArray &requestContent, std::shared_ptr<QNetworkRequestFactory> factory, bool responseCompressionEnabled, bool requestCompressionEnabled)
- {
- QRandomGenerator randomGenerator = QRandomGenerator(QDateTime::currentDateTime().toSecsSinceEpoch());
- // reset variables
- requestContent = "";
- bool isFormData = false;
-
- if (input.m_varLayout == NOT_SET) {
- input.m_varLayout = input.m_httpMethod == "GET" || input.m_httpMethod == "HEAD" ? ADDRESS : URL_ENCODED;
- }
- // prepare request content
- if (input.m_varLayout == ADDRESS || input.m_varLayout == URL_ENCODED) {
- // variable layout is ADDRESS or URL_ENCODED
- if (!input.m_queryItem.isEmpty()) {
- if (input.m_varLayout == ADDRESS) {
- input.m_urlStr += "?";
- input.m_urlStr += input.m_queryItem.toString(QUrl::FullyEncoded).toUtf8();
- } else {
- requestContent = input.m_queryItem.toString(QUrl::FullyEncoded).toUtf8();
- }
- }
- } else {
- QFormDataBuilder builder;
- // add variables
- for (const QString &key : input.m_vars.keys()) {
- auto part = builder.part(key);
- const QByteArray value = input.m_vars.value(key).toUtf8();
- if (input.m_fieldHeaders.contains(key)) {
- part.setBody(value, QString(), input.m_fieldHeaders[key]);
- } else {
- part.setBody(value);
- }
- }
-
- // add files
- QList<QFile *> addedFiles;
- for (QList<OAIHttpFileElement>::iterator fileInfo = input.m_files.begin(); fileInfo != input.m_files.end(); fileInfo++) {
- QFileInfo fi(fileInfo->m_localFilename);
- // ensure necessary variables are available
- if (fileInfo->m_localFilename == nullptr
- || fileInfo->m_localFilename.isEmpty()
- || fileInfo->m_variableName == nullptr
- || fileInfo->m_variableName.isEmpty()
- || !fi.exists()
- || !fi.isFile()
- || !fi.isReadable()) {
- // silent abort for the current file
- continue;
- }
- QFile *file = new QFile(fileInfo->m_localFilename);
- if (!file->open(QIODevice::ReadOnly)) {
- qWarning() << "Cannot open the file: " << fileInfo->m_localFilename;
- continue;
- }
- // ensure filename for the request
- if (fileInfo->m_requestFilename == nullptr || fileInfo->m_requestFilename.isEmpty()) {
- fileInfo->m_requestFilename = fi.fileName();
- if (fileInfo->m_requestFilename.isEmpty()) {
- fileInfo->m_requestFilename = "file";
- }
- }
- auto part = builder.part(fileInfo->m_variableName);
- part.setBodyDevice(file, fileInfo->m_localFilename, fileInfo->m_mimeType.isEmpty() ? QString("application/octet-stream") : fileInfo->m_mimeType);
- addedFiles.append(file);
- }
- // Build the multipart object
- input.m_multiPart = builder.buildMultiPart();
- // Need to take care about opened files
- for (QFile *file: addedFiles) {
- if (file)
- file->setParent(input.m_multiPart.get()); // we cannot delete the file now, so delete it with the multiPart
- }
- }
-
- if (input.m_requestBody.size() > 0) {
- requestContent.clear();
- if (!isFormData && (input.m_varLayout != MULTIPART) && requestCompressionEnabled) {
- requestContent.append(compress(input.m_requestBody, 7, OAICompressionType::Gzip));
- } else {
- requestContent.append(input.m_requestBody);
- }
- }
- QNetworkRequest request = factory->createRequest(input.m_urlStr);
- // TBD: Remove the line below after merging the fix QTBUG-138878
- request.setUrl(factory->baseUrl().toString() + input.m_urlStr);
- if (request.header(QNetworkRequest::UserAgentHeader).isNull())
- request.setHeader(QNetworkRequest::UserAgentHeader, "OpenAPI-Generator/1.0.0/cpp-qt");
-
- if (requestContent.size() > 0 && !isFormData && (input.m_varLayout != MULTIPART)) {
- if (!input.m_headers.contains(QHttpHeaders::WellKnownHeader::ContentType)) {
- request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
- } else {
- request.setHeader(QNetworkRequest::ContentTypeHeader,
- input.m_headers.combinedValue(QHttpHeaders::WellKnownHeader::ContentType));
- }
- if (requestCompressionEnabled) {
- request.setRawHeader("Content-Encoding", "gzip");
- }
- } else if (input.m_varLayout == URL_ENCODED) {
- request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
- }
-
- if (responseCompressionEnabled) {
- request.setRawHeader("Accept-Encoding", "gzip");
- } else {
- request.setRawHeader("Accept-Encoding", "identity");
- }
- return request;
- }
-
- QByteArray parseResponse(const QRestReply &reply, const QString &workDir, QMap<QString, OAIHttpFileElement> *files)
- {
- QByteArray result;
- QString contentDispositionHdr;
- QString contentTypeHdr;
- QString contentEncodingHdr;
-
- if (!reply.networkReply())
- return result;
-
- QHttpHeaders headers;
- if (reply.networkReply()->rawHeaderPairs().count() > 0)
- headers = reply.networkReply()->headers();
-
- for (qsizetype i = 0; i < headers.size(); ++i) {
- const auto name = headers.nameAt(i);
- const auto value = headers.valueAt(i);
- if (name.compare(QString("Content-Disposition"), Qt::CaseInsensitive) == 0) {
- contentDispositionHdr = QString::fromLatin1(value);
- }
- if (name.compare(QString("Content-Type"), Qt::CaseInsensitive) == 0) {
- contentTypeHdr = QString::fromLatin1(value);
- }
- if (name.compare(QString("Content-Encoding"), Qt::CaseInsensitive) == 0) {
- contentEncodingHdr = QString::fromLatin1(value);
- }
- }
-
- if (!contentDispositionHdr.isEmpty()) {
- auto contentDisposition = contentDispositionHdr.split(QString(";"), Qt::SkipEmptyParts);
- auto contentType =
- !contentTypeHdr.isEmpty() ? contentTypeHdr.split(QString(";"), Qt::SkipEmptyParts).first() : QStringLiteral("");
- if ((contentDisposition.count() > 0) && (contentDisposition.first() == QString("attachment"))) {
- QString filename = QUuid::createUuid().toString();
- for (const auto &file : contentDisposition) {
- if (file.contains(QString("filename"))) {
- filename = file.split(QString("="), Qt::SkipEmptyParts).at(1);
- break;
- }
- }
- OAIHttpFileElement felement;
- felement.saveToFile(QStringLiteral(""), workDir + QDir::separator() + filename, filename, contentType, reply.networkReply()->readAll());
- if (files)
- files->insert(filename, felement);
- }
-
- } else if (!contentTypeHdr.isEmpty()) {
- auto contentType = contentTypeHdr.split(QString(";"), Qt::SkipEmptyParts);
- if ((contentType.count() > 0) && (contentType.first() == QString("multipart/form-data"))) {
- // TODO : Handle Multipart responses
- } else {
- if (!contentEncodingHdr.isEmpty()) {
- auto encoding = contentEncodingHdr.split(QString(";"), Qt::SkipEmptyParts);
- if (encoding.count() > 0) {
- auto compressionTypes = encoding.first().split(',', Qt::SkipEmptyParts);
- if (compressionTypes.contains("gzip", Qt::CaseInsensitive) || compressionTypes.contains("deflate", Qt::CaseInsensitive)) {
- result = decompress(reply.networkReply()->readAll());
- } else if (compressionTypes.contains("identity", Qt::CaseInsensitive)) {
- result = reply.networkReply()->readAll();
- }
- }
- } else {
- result = reply.networkReply()->readAll();
- }
- }
- }
- reply.networkReply()->deleteLater();
- return result;
- }
-
- QByteArray decompress(const QByteArray& data)
- {
- Q_UNUSED(data);
- return QByteArray();
- }
-
- QByteArray compress(const QByteArray& input, int level, OAICompressionType compressType)
- {
- Q_UNUSED(input);
- Q_UNUSED(level);
- Q_UNUSED(compressType);
- return QByteArray();
- }
- } // namespace OAIHttpRequestWorker
-
-
-
- } // namespace OpenAPI