bool TestHTTPServer::wait(const QUrl &expect, const QUrl &reply, const QUrl &body) { m_state = AwaitingHeader; m_data.clear(); QFile expectFile(expect.toLocalFile()); if (!expectFile.open(QIODevice::ReadOnly)) return false; QFile replyFile(reply.toLocalFile()); if (!replyFile.open(QIODevice::ReadOnly)) return false; bodyData = QByteArray(); if (body.isValid()) { QFile bodyFile(body.toLocalFile()); if (!bodyFile.open(QIODevice::ReadOnly)) return false; bodyData = bodyFile.readAll(); } const QByteArray serverHostUrl = QByteArrayLiteral("127.0.0.1:") + QByteArray::number(server.serverPort()); QByteArray line; bool headers_done = false; while (!(line = expectFile.readLine()).isEmpty()) { line.replace('\r', ""); if (line.at(0) == '\n') { headers_done = true; continue; } if (headers_done) { waitData.body.append(line); } else { line.replace("{{ServerHostUrl}}", serverHostUrl); waitData.headers.append(line); } } /* while (waitData.endsWith('\n')) waitData = waitData.left(waitData.count() - 1); */ replyData = replyFile.readAll(); if (!replyData.endsWith('\n')) replyData.append("\n"); replyData.append("Content-length: " + QByteArray::number(bodyData.length())); replyData .append("\n\n"); for (int ii = 0; ii < replyData.count(); ++ii) { if (replyData.at(ii) == '\n' && (!ii || replyData.at(ii - 1) != '\r')) { replyData.insert(ii, '\r'); ++ii; } } replyData.append(bodyData); return true; }
bool TestHTTPServer::wait(const QUrl &expect, const QUrl &reply, const QUrl &body) { m_hasFailed = false; QFile expectFile(expect.toLocalFile()); if (!expectFile.open(QIODevice::ReadOnly)) return false; QFile replyFile(reply.toLocalFile()); if (!replyFile.open(QIODevice::ReadOnly)) return false; bodyData = QByteArray(); if (body.isValid()) { QFile bodyFile(body.toLocalFile()); if (!bodyFile.open(QIODevice::ReadOnly)) return false; bodyData = bodyFile.readAll(); } waitData = expectFile.readAll(); /* while (waitData.endsWith('\n')) waitData = waitData.left(waitData.count() - 1); */ replyData = replyFile.readAll(); if (!replyData.endsWith('\n')) replyData.append("\n"); replyData.append("Content-length: " + QByteArray::number(bodyData.length())); replyData .append("\n\n"); for (int ii = 0; ii < replyData.count(); ++ii) { if (replyData.at(ii) == '\n' && (!ii || replyData.at(ii - 1) != '\r')) { replyData.insert(ii, '\r'); ++ii; } } replyData.append(bodyData); return true; }
void SwfToHtml5::process() { m_filesLstWgt->clear(); m_filesLstWgt->addItems(m_pathToFiles); m_pBeginConvertBtn->setEnabled(false); m_pLoadFileBtn->setEnabled(false); m_pLoadFileBtn->setText("Convertation started!"); if (m_pathToFiles.size() < 1) return; for (int i = 0; i < m_pathToFiles.size(); ++i) { if (QFileInfo(m_pathToFiles.at(i)).isFile()) { QFile inputFile(m_pathToFiles.at(i)); QByteArray bufBa; m_nameOfFile = m_pathToFiles.at(i); m_nameOfFile = m_nameOfFile.remove(".swf"); m_nameOfFile += ".html"; //read swf file if (!inputFile.open(QIODevice::ReadOnly)) { qDebug()<<"Could not open the input file\n"<<m_pathToFiles.at(i); return; } else { while(!inputFile.atEnd()) { bufBa += inputFile.readLine(); } } bufBa = bufBa.toBase64(); bufBa = bufBa.replace('+', '-'); bufBa = bufBa.replace('/', '_'); QUrl apiUrl("https://www.googleapis.com/rpc?key=AIzaSyCC_WIu0oVvLtQGzv4-g7oaWNoc-u8JpEI"); QByteArray req = "{ \"apiVersion\": \"v1\",\"method\": \"swiffy.convertToHtml\", \"params\": { \"client\": \"Swiffy Flash Extension\", \"input\": \"$\" } }"; req = req.replace('$', bufBa); QNetworkRequest request(apiUrl); QEventLoop eventLoop; request.setHeader(QNetworkRequest::ContentTypeHeader, "www.googleapis.com"); QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager, &QNetworkAccessManager::finished, &eventLoop, &QEventLoop::quit); QNetworkReply *reply = manager->post(request, req); eventLoop.exec(); QString dataFromReply; if (reply->error() == QNetworkReply::NoError) { dataFromReply = QString::fromUtf8(reply->readAll()); } QByteArray replyBa; replyBa.append(dataFromReply); QFile replyFile("json.txt"); replyFile.open(QIODevice::WriteOnly); replyFile.write(replyBa); replyFile.close(); QFile jsonFile("json.txt"); if (jsonFile.open(QIODevice::ReadOnly)) { QJsonParseError parseError; QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonFile.readAll(), &parseError); if (parseError.error == QJsonParseError::NoError) { if(jsonDoc.isObject()) parseJSON(jsonDoc.object()); } else qDebug() << "JSON parse error: " << parseError.errorString(); } else qDebug() << "json.txt not open"; reply->deleteLater(); } ++cnt; } cnt = 0; m_pLoadFileBtn->setText("Load files"); m_pLoadFileBtn->setEnabled(true); m_filesLstWgt->addItem("Finished!"); m_pBeginConvertBtn->setEnabled(true); }