mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-21 09:47:07 +00:00
[C++] [Qt5] Add request compression and handling identity (#5088)
* Add request and response compression and handling identity * Minor updates for identity and if conditions
This commit is contained in:
@@ -96,8 +96,12 @@ void {{prefix}}HttpRequestWorker::setWorkingDirectory(const QString &path) {
|
||||
}
|
||||
}
|
||||
|
||||
void {{prefix}}HttpRequestWorker::setCompressionEnabled(bool enable) {
|
||||
isCompressionEnabled = enable;
|
||||
void {{prefix}}HttpRequestWorker::setResponseCompressionEnabled(bool enable) {
|
||||
isResponseCompressionEnabled = enable;
|
||||
}
|
||||
|
||||
void {{prefix}}HttpRequestWorker::setRequestCompressionEnabled(bool enable) {
|
||||
isRequestCompressionEnabled = enable;
|
||||
}
|
||||
|
||||
QString {{prefix}}HttpRequestWorker::http_attribute_encode(QString attribute_name, QString input) {
|
||||
@@ -285,7 +289,11 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
|
||||
if (input->request_body.size() > 0) {
|
||||
qDebug() << "got a request body";
|
||||
request_content.clear();
|
||||
request_content.append(input->request_body);
|
||||
if(!isFormData && (input->var_layout != MULTIPART) && isRequestCompressionEnabled){
|
||||
request_content.append(compress(input->request_body, 7, {{prefix}}CompressionType::Gzip));
|
||||
} else {
|
||||
request_content.append(input->request_body);
|
||||
}
|
||||
}
|
||||
// prepare connection
|
||||
|
||||
@@ -302,14 +310,19 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
|
||||
} else {
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, input->headers.value("Content-Type"));
|
||||
}
|
||||
if(isRequestCompressionEnabled){
|
||||
request.setRawHeader("Content-Encoding", "gzip");
|
||||
}
|
||||
} else if (input->var_layout == URL_ENCODED) {
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
} else if (input->var_layout == MULTIPART) {
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + boundary);
|
||||
}
|
||||
|
||||
if(isCompressionEnabled){
|
||||
request.setRawHeader("Accept-Encoding", "deflate, gzip");
|
||||
if(isResponseCompressionEnabled){
|
||||
request.setRawHeader("Accept-Encoding", "gzip");
|
||||
} else {
|
||||
request.setRawHeader("Accept-Encoding", "identity");
|
||||
}
|
||||
|
||||
if (input->http_method == "GET") {
|
||||
@@ -391,6 +404,8 @@ void {{prefix}}HttpRequestWorker::process_response(QNetworkReply *reply) {
|
||||
auto compressionTypes = encoding.first().split(',', QString::SkipEmptyParts);
|
||||
if(compressionTypes.contains("gzip", Qt::CaseInsensitive) || compressionTypes.contains("deflate", Qt::CaseInsensitive)){
|
||||
response = decompress(reply->readAll());
|
||||
} else if(compressionTypes.contains("identity", Qt::CaseInsensitive)){
|
||||
response = reply->readAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -402,11 +417,11 @@ void {{prefix}}HttpRequestWorker::process_response(QNetworkReply *reply) {
|
||||
}
|
||||
|
||||
QByteArray {{prefix}}HttpRequestWorker::decompress(const QByteArray& data){
|
||||
QByteArray result;
|
||||
bool sts = false;{{#contentCompression}}
|
||||
{{#contentCompression}}QByteArray result;
|
||||
bool sts = false;
|
||||
do{
|
||||
z_stream strm{};
|
||||
static const int CHUNK_SIZE = 2048;
|
||||
static const int CHUNK_SIZE = 8*1024;
|
||||
char out[CHUNK_SIZE];
|
||||
if (data.size() <= 4) {
|
||||
break;
|
||||
@@ -427,8 +442,65 @@ QByteArray {{prefix}}HttpRequestWorker::decompress(const QByteArray& data){
|
||||
sts = true;
|
||||
} while (strm.avail_out == 0);
|
||||
inflateEnd(&strm);
|
||||
} while(false);{{/contentCompression}}
|
||||
return sts ? result : QByteArray();
|
||||
} while(false);
|
||||
return sts ? result : QByteArray();{{/contentCompression}}{{^contentCompression}}
|
||||
Q_UNUSED(data);
|
||||
return QByteArray();{{/contentCompression}}
|
||||
}
|
||||
|
||||
QByteArray {{prefix}}HttpRequestWorker::compress(const QByteArray& input, int level, {{prefix}}CompressionType compressType) {
|
||||
{{#contentCompression}}QByteArray output;
|
||||
static const int GZIP_WINDOW_BIT = 15+16;
|
||||
static const int ZLIB_WINDOW_BIT = 15;
|
||||
static const int CHUNK_SIZE = 8*1024;
|
||||
int windowBits;
|
||||
if( compressType == {{prefix}}CompressionType::Gzip ){
|
||||
windowBits = GZIP_WINDOW_BIT;
|
||||
} else if ( compressType == {{prefix}}CompressionType::Zlib ){
|
||||
windowBits = ZLIB_WINDOW_BIT;
|
||||
}
|
||||
do{
|
||||
int flush = 0, ret = 0;
|
||||
bool error_sts = false;
|
||||
z_stream strm{};
|
||||
if(input.length() <= 0) {
|
||||
break;
|
||||
}
|
||||
if (deflateInit2(&strm, qMax(-1, qMin(9, level)), Z_DEFLATED, windowBits, 8, Z_DEFAULT_STRATEGY) != Z_OK){
|
||||
break;
|
||||
}
|
||||
output.clear();
|
||||
auto input_data = input.data();
|
||||
int input_data_left = input.length();
|
||||
do {
|
||||
int chunk_size = qMin(CHUNK_SIZE, input_data_left);
|
||||
strm.next_in = (unsigned char*)input_data;
|
||||
strm.avail_in = chunk_size;
|
||||
input_data += chunk_size;
|
||||
input_data_left -= chunk_size;
|
||||
flush = (input_data_left <= 0 ? Z_FINISH : Z_NO_FLUSH);
|
||||
do {
|
||||
char out[CHUNK_SIZE];
|
||||
strm.next_out = (unsigned char*)out;
|
||||
strm.avail_out = CHUNK_SIZE;
|
||||
ret = deflate(&strm, flush);
|
||||
if(ret == Z_STREAM_ERROR) {
|
||||
error_sts = true;
|
||||
break;
|
||||
}
|
||||
auto have = (CHUNK_SIZE - strm.avail_out);
|
||||
if(have > 0){
|
||||
output.append((char*)out, have);
|
||||
}
|
||||
} while (strm.avail_out == 0);
|
||||
} while ((flush != Z_FINISH) && !(error_sts));
|
||||
deflateEnd(&strm);
|
||||
} while(false);
|
||||
return output;{{/contentCompression}}{{^contentCompression}}
|
||||
Q_UNUSED(input);
|
||||
Q_UNUSED(level);
|
||||
Q_UNUSED(compressType);
|
||||
return QByteArray();{{/contentCompression}}
|
||||
}
|
||||
|
||||
QSslConfiguration *{{prefix}}HttpRequestWorker::sslDefaultConfiguration;
|
||||
|
||||
@@ -63,21 +63,28 @@ public:
|
||||
void setWorkingDirectory(const QString &path);
|
||||
{{prefix}}HttpFileElement getHttpFileElement(const QString &fieldname = QString());
|
||||
QByteArray *getMultiPartField(const QString &fieldname = QString());
|
||||
void setCompressionEnabled(bool enable);
|
||||
void setResponseCompressionEnabled(bool enable);
|
||||
void setRequestCompressionEnabled(bool enable);
|
||||
signals:
|
||||
void on_execution_finished({{prefix}}HttpRequestWorker *worker);
|
||||
|
||||
private:
|
||||
enum {{prefix}}CompressionType{
|
||||
Zlib,
|
||||
Gzip
|
||||
};
|
||||
QNetworkAccessManager *manager;
|
||||
QMap<QString, QString> headers;
|
||||
QMap<QString, {{prefix}}HttpFileElement> files;
|
||||
QMap<QString, QByteArray *> multiPartFields;
|
||||
QString workingDirectory;
|
||||
int _timeOut;
|
||||
bool isCompressionEnabled;
|
||||
bool isResponseCompressionEnabled;
|
||||
bool isRequestCompressionEnabled;
|
||||
void on_manager_timeout(QNetworkReply *reply);
|
||||
void process_response(QNetworkReply *reply);
|
||||
QByteArray decompress(const QByteArray& data);
|
||||
QByteArray compress(const QByteArray& input, int level, {{prefix}}CompressionType compressType);
|
||||
private slots:
|
||||
void on_manager_finished(QNetworkReply *reply);
|
||||
};
|
||||
|
||||
@@ -15,7 +15,8 @@ namespace {{this}} {
|
||||
_port(port),
|
||||
_basePath(basePath),
|
||||
_timeOut(timeOut),
|
||||
_compress(false) {}
|
||||
isResponseCompressionEnabled(false),
|
||||
isRequestCompressionEnabled(false) {}
|
||||
|
||||
{{classname}}::~{{classname}}() {
|
||||
}
|
||||
@@ -48,8 +49,12 @@ void {{classname}}::addHeaders(const QString &key, const QString &value) {
|
||||
defaultHeaders.insert(key, value);
|
||||
}
|
||||
|
||||
void {{classname}}::enableContentCompression() {
|
||||
_compress = true;
|
||||
void {{classname}}::enableRequestCompression() {
|
||||
isRequestCompressionEnabled = true;
|
||||
}
|
||||
|
||||
void {{classname}}::enableResponseCompression() {
|
||||
isResponseCompressionEnabled = true;
|
||||
}
|
||||
|
||||
{{#operations}}
|
||||
@@ -113,8 +118,9 @@ void {{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName
|
||||
{{/collectionFormat}}{{/queryParams}}
|
||||
{{prefix}}HttpRequestWorker *worker = new {{prefix}}HttpRequestWorker(this);
|
||||
worker->setTimeOut(_timeOut);
|
||||
worker->setWorkingDirectory(_workingDirectory);
|
||||
worker->setCompressionEnabled(_compress);
|
||||
worker->setWorkingDirectory(_workingDirectory);{{#contentCompression}}
|
||||
worker->setResponseCompressionEnabled(isResponseCompressionEnabled);
|
||||
worker->setRequestCompressionEnabled(isRequestCompressionEnabled);{{/contentCompression}}
|
||||
{{prefix}}HttpRequestInput input(fullPath, "{{httpMethod}}");
|
||||
{{#formParams}}{{^isFile}}
|
||||
input.add_var("{{baseName}}", ::{{cppNamespace}}::toStringValue({{paramName}}));{{/isFile}}{{#isFile}}
|
||||
|
||||
@@ -27,7 +27,8 @@ public:
|
||||
void setTimeOut(const int timeOut);
|
||||
void setWorkingDirectory(const QString &path);
|
||||
void addHeaders(const QString &key, const QString &value);
|
||||
void enableContentCompression();
|
||||
void enableRequestCompression();
|
||||
void enableResponseCompression();
|
||||
{{#operations}}{{#operation}}
|
||||
void {{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{/operation}}{{/operations}}
|
||||
|
||||
@@ -38,7 +39,8 @@ private:
|
||||
int _timeOut;
|
||||
QString _workingDirectory;
|
||||
QMap<QString, QString> defaultHeaders;
|
||||
bool _compress;
|
||||
bool isResponseCompressionEnabled;
|
||||
bool isRequestCompressionEnabled;
|
||||
{{#operations}}{{#operation}}
|
||||
void {{nickname}}Callback({{prefix}}HttpRequestWorker *worker);{{/operation}}{{/operations}}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user