54 : QObject(parent), manager(nullptr)
56 qsrand(QDateTime::currentDateTime().toTime_t());
58 manager =
new QNetworkAccessManager(
this);
67 bool need_utf_encoding =
false;
69 QByteArray input_c = input.toLocal8Bit();
71 for (
int i = 0;
i < input_c.length();
i++) {
73 if (c ==
'\\' || c ==
'/' || c ==
'\0' || c < ' ' || c >
'~') {
75 need_utf_encoding =
true;
85 if (result.length() == 0) {
86 need_utf_encoding =
true;
89 if (!need_utf_encoding) {
91 return QString(
"%1=\"%2\"").arg(attribute_name, result);
94 QString result_utf8 =
"";
95 for (
int i = 0;
i < input_c.length();
i++) {
98 (c >=
'0' && c <=
'9')
99 || (c >=
'A' && c <=
'Z')
100 || (c >=
'a' && c <=
'z')
105 result_utf8 +=
"%" + QString::number(static_cast<unsigned char>(input_c.at(
i)), 16).toUpper();
110 return QString(
"%1=\"%2\"; %1*=utf-8''%3").arg(attribute_name, result, result_utf8);
117 QByteArray request_content =
"";
121 bool isFormData =
false;
126 if (input->
files.length() > 0) {
136 QString boundary =
"";
141 if (input->
vars.count() > 0) {
144 foreach (QString key, input->
vars.keys()) {
146 request_content.append(
"&");
150 request_content.append(QUrl::toPercentEncoding(key));
151 request_content.append(
"=");
152 request_content.append(QUrl::toPercentEncoding(input->
vars.value(key)));
156 input->
url_str +=
"?" + request_content;
157 request_content =
"";
164 boundary =
"__-----------------------" 165 + QString::number(QDateTime::currentDateTime().toTime_t())
166 + QString::number(qrand());
167 QString boundary_delimiter =
"--";
168 QString new_line =
"\r\n";
171 foreach (QString key, input->
vars.keys()) {
173 request_content.append(boundary_delimiter);
174 request_content.append(boundary);
175 request_content.append(new_line);
178 request_content.append(
"Content-Disposition: form-data; ");
180 request_content.append(new_line);
181 request_content.append(
"Content-Type: text/plain");
182 request_content.append(new_line);
185 request_content.append(new_line);
188 request_content.append(input->
vars.value(key));
189 request_content.append(new_line);
193 for (QList<SWGHttpRequestInputFileElement>::iterator file_info = input->
files.begin(); file_info != input->
files.end(); file_info++) {
194 QFileInfo fi(file_info->local_filename);
198 file_info->local_filename ==
nullptr || file_info->local_filename.isEmpty()
199 || file_info->variable_name ==
nullptr || file_info->variable_name.isEmpty()
200 || !fi.exists() || !fi.isFile() || !fi.isReadable()
206 QFile file(file_info->local_filename);
207 if (!file.open(QIODevice::ReadOnly)) {
213 if (file_info->request_filename ==
nullptr || file_info->request_filename.isEmpty()) {
214 file_info->request_filename = fi.fileName();
215 if (file_info->request_filename.isEmpty()) {
216 file_info->request_filename =
"file";
221 request_content.append(boundary_delimiter);
222 request_content.append(boundary);
223 request_content.append(new_line);
226 request_content.append(QString(
"Content-Disposition: form-data; %1; %2").
arg(
230 request_content.append(new_line);
232 if (file_info->mime_type !=
nullptr && !file_info->mime_type.isEmpty()) {
233 request_content.append(
"Content-Type: ");
234 request_content.append(file_info->mime_type);
235 request_content.append(new_line);
238 request_content.append(
"Content-Transfer-Encoding: binary");
239 request_content.append(new_line);
242 request_content.append(new_line);
245 request_content.append(file.readAll());
246 request_content.append(new_line);
252 request_content.append(boundary_delimiter);
253 request_content.append(boundary);
254 request_content.append(boundary_delimiter);
258 qDebug() <<
"got a request body";
259 request_content.clear();
264 QNetworkRequest request = QNetworkRequest(QUrl(input->
url_str));
268 request.setRawHeader(
"User-Agent",
"Swagger-Client");
269 foreach(QString key, input->
headers.keys()) {
270 request.setRawHeader(key.toStdString().c_str(), input->
headers.value(key).toStdString().c_str());
273 if (request_content.size() > 0 && !isFormData) {
274 request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/json");
277 request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
280 request.setHeader(QNetworkRequest::ContentTypeHeader,
"multipart/form-data; boundary=" + boundary);
287 manager->post(request, request_content);
290 manager->put(request, request_content);
296 manager->deleteResource(request);
299 #if (QT_VERSION >= 0x050800) 302 QBuffer *buffer =
new QBuffer;
303 buffer->setData(request_content);
304 buffer->open(QIODevice::ReadOnly);
306 QNetworkReply* reply =
manager->sendCustomRequest(request, input->
http_method.toLatin1(), buffer);
307 buffer->setParent(reply);
318 reply->deleteLater();
void execute(SWGHttpRequestInput *input)
void on_execution_finished(SWGHttpRequestWorker *worker)
virtual ~SWGHttpRequestWorker()
Fixed< IntType, IntBits > arg(const std::complex< Fixed< IntType, IntBits > > &val)
void on_manager_finished(QNetworkReply *reply)
static QSslConfiguration * sslDefaultConfiguration
QString http_attribute_encode(QString attribute_name, QString input)
QNetworkAccessManager * manager
QNetworkReply::NetworkError error_type
SWGHttpRequestWorker(QObject *parent=0)