Loading client/publishclient.cpp +35 −13 Changes for client/publishclient.cpp: 35 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -33,6 +33,8 @@ #include <QJsonObject> #include <QJsonArray> #include <QUrlQuery> #include <QRegularExpression> #include <QHttpMultiPart> PublishClient::PublishClient(QObject *parent) : QObject(parent) Loading Loading @@ -214,22 +216,29 @@ void PublishClient::renderOnServer(const QString &html, bool mobile, const QStri QUrl url(_serverUrl + "/render"); QNetworkRequest req(url); req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); req.setAttribute(QNetworkRequest::Http2AllowedAttribute, false); QUrlQuery params; QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); auto addPart = [multiPart](const QString &name, const QString &value) { QHttpPart part; part.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"").arg(name)); part.setBody(value.toUtf8()); multiPart->append(part); }; if (!_authToken.isEmpty()) params.addQueryItem("authid", _authToken); params.addQueryItem("html", html); params.addQueryItem("mobile", mobile ? "true" : "false"); addPart("authid", _authToken); addPart("html", html); addPart("mobile", mobile ? "true" : "false"); if (!target.isEmpty()) params.addQueryItem("target", target); addPart("target", target); for (auto it = extraParams.cbegin(); it != extraParams.cend(); ++it) params.addQueryItem(it.key(), it.value()); addPart(it.key(), it.value()); QByteArray postData = params.query(QUrl::FullyEncoded).toUtf8(); QNetworkReply *reply = _nam->post(req, postData); QNetworkReply *reply = _nam->post(req, multiPart); multiPart->setParent(reply); // ensure multiPart is deleted with reply _activeReply = reply; if (_ignoreSsl) connect(reply, &QNetworkReply::sslErrors, reply, qOverload<>(&QNetworkReply::ignoreSslErrors)); Loading @@ -241,12 +250,25 @@ void PublishClient::renderOnServer(const QString &html, bool mobile, const QStri return; } int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); QByteArray body = reply->readAll(); if (status == 200) { cb(true, QString::fromUtf8(reply->readAll())); cb(true, QString::fromUtf8(body)); } else { /* Try to extract a useful message from the HTML error body */ QString errMsg; QString bodyStr = QString::fromUtf8(body); /* Look for text inside <span>...</span> which is the blogi error format */ QRegularExpression rx("<span>(.*?)</span>"); QRegularExpressionMatch match = rx.match(bodyStr); if (match.hasMatch()) { errMsg = match.captured(1); } else if (reply->error() != QNetworkReply::NoError) { cb(false, reply->errorString()); errMsg = reply->errorString(); } else { cb(false, "Server returned HTTP " + QString::number(status)); errMsg = "Server returned HTTP " + QString::number(status); } qWarning() << "renderOnServer failed: HTTP" << status << errMsg; cb(false, errMsg); } }); } Loading client/qml/PreviewPane.qml +20 −5 Changes for client/qml/PreviewPane.qml: 20 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -8,15 +8,19 @@ Item { property bool mobileMode: false property bool useServerPreview: false property string serverHtml: "" property bool engineReady: false function reload() { if (!engineReady) return if (useServerPreview && serverHtml.length > 0) { var surl = localApi.serverUrl() webView.loadHtml(serverHtml, surl.length > 0 ? surl : "about:blank") } else { var html = localApi.renderPreview() webView.loadHtml( "<!DOCTYPE html><html><head><meta charset='utf-8'></head><body>" + html + "</body></html>", "<!DOCTYPE html><html><head><meta charset='utf-8'>" + "<style>html,body{margin:0;padding:8px;background:#fff;color:#000;font-family:sans-serif;}</style>" + "</head><body>" + html + "</body></html>", "about:blank" ) } Loading @@ -29,14 +33,25 @@ Item { WebEngineView { id: webView anchors.centerIn: parent width: mobileMode ? 375 : parent.width height: parent.height anchors.fill: parent anchors.leftMargin: mobileMode ? Math.max(0, (parent.width - 375) / 2) : 0 anchors.rightMargin: anchors.leftMargin settings.localContentCanAccessRemoteUrls: true settings.javascriptEnabled: true Component.onCompleted: reload() backgroundColor: "#ffffff" onLoadingChanged: function(loadRequest) { if (!previewPane.engineReady && loadRequest.status !== WebEngineView.LoadStartedStatus) { previewPane.engineReady = true reload() } } Component.onCompleted: { webView.url = "about:blank" } } Connections { Loading src/blogi.cpp +32 −12 Changes for src/blogi.cpp: 32 added lines, 12 removed lines. Original line number Diff line number Diff line Loading @@ -2135,21 +2135,41 @@ RETRY_REQUEST: std::string html; json_object *publishParams = nullptr; for(const auto &curdat : renderopts.urlData()){ if(curdat.key=="mobile" && curdat.value=="true"){ // Helper lambda to process key/value pairs from either source auto processField = [&](const std::string &key, const std::string &value) { if(key=="mobile" && value=="true"){ mobile=true; }else if(curdat.key=="target"){ publishTarget=curdat.value; }else if(curdat.key=="authid"){ authid=curdat.value; }else if(curdat.key=="html"){ html=curdat.value; }else{ // Collect remaining params for the publish preview }else if(key=="target"){ publishTarget=value; }else if(key=="authid"){ authid=value; }else if(key=="html"){ html=value; }else if(!key.empty()){ if(!publishParams) publishParams = json_object_new_object(); json_object_object_add(publishParams, curdat.key.c_str(), json_object_new_string(curdat.value.c_str())); json_object_object_add(publishParams, key.c_str(), json_object_new_string(value.c_str())); } }; // Read URL-encoded form data for(const auto &curdat : renderopts.urlData()){ processField(curdat.key, curdat.value); } // Read multipart form data (used by Qt editor to avoid %00 encoding issues) for(const auto &entry : renderopts.multipartData()){ std::string fieldName; for(const auto &dispo : entry.dispositions){ if(dispo.key == "name"){ fieldName = dispo.value; break; } } if(!fieldName.empty()){ std::string fieldValue(entry.value.begin(), entry.value.end()); processField(fieldName, fieldValue); } } Loading Loading
client/publishclient.cpp +35 −13 Changes for client/publishclient.cpp: 35 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -33,6 +33,8 @@ #include <QJsonObject> #include <QJsonArray> #include <QUrlQuery> #include <QRegularExpression> #include <QHttpMultiPart> PublishClient::PublishClient(QObject *parent) : QObject(parent) Loading Loading @@ -214,22 +216,29 @@ void PublishClient::renderOnServer(const QString &html, bool mobile, const QStri QUrl url(_serverUrl + "/render"); QNetworkRequest req(url); req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); req.setAttribute(QNetworkRequest::Http2AllowedAttribute, false); QUrlQuery params; QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); auto addPart = [multiPart](const QString &name, const QString &value) { QHttpPart part; part.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"").arg(name)); part.setBody(value.toUtf8()); multiPart->append(part); }; if (!_authToken.isEmpty()) params.addQueryItem("authid", _authToken); params.addQueryItem("html", html); params.addQueryItem("mobile", mobile ? "true" : "false"); addPart("authid", _authToken); addPart("html", html); addPart("mobile", mobile ? "true" : "false"); if (!target.isEmpty()) params.addQueryItem("target", target); addPart("target", target); for (auto it = extraParams.cbegin(); it != extraParams.cend(); ++it) params.addQueryItem(it.key(), it.value()); addPart(it.key(), it.value()); QByteArray postData = params.query(QUrl::FullyEncoded).toUtf8(); QNetworkReply *reply = _nam->post(req, postData); QNetworkReply *reply = _nam->post(req, multiPart); multiPart->setParent(reply); // ensure multiPart is deleted with reply _activeReply = reply; if (_ignoreSsl) connect(reply, &QNetworkReply::sslErrors, reply, qOverload<>(&QNetworkReply::ignoreSslErrors)); Loading @@ -241,12 +250,25 @@ void PublishClient::renderOnServer(const QString &html, bool mobile, const QStri return; } int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); QByteArray body = reply->readAll(); if (status == 200) { cb(true, QString::fromUtf8(reply->readAll())); cb(true, QString::fromUtf8(body)); } else { /* Try to extract a useful message from the HTML error body */ QString errMsg; QString bodyStr = QString::fromUtf8(body); /* Look for text inside <span>...</span> which is the blogi error format */ QRegularExpression rx("<span>(.*?)</span>"); QRegularExpressionMatch match = rx.match(bodyStr); if (match.hasMatch()) { errMsg = match.captured(1); } else if (reply->error() != QNetworkReply::NoError) { cb(false, reply->errorString()); errMsg = reply->errorString(); } else { cb(false, "Server returned HTTP " + QString::number(status)); errMsg = "Server returned HTTP " + QString::number(status); } qWarning() << "renderOnServer failed: HTTP" << status << errMsg; cb(false, errMsg); } }); } Loading
client/qml/PreviewPane.qml +20 −5 Changes for client/qml/PreviewPane.qml: 20 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -8,15 +8,19 @@ Item { property bool mobileMode: false property bool useServerPreview: false property string serverHtml: "" property bool engineReady: false function reload() { if (!engineReady) return if (useServerPreview && serverHtml.length > 0) { var surl = localApi.serverUrl() webView.loadHtml(serverHtml, surl.length > 0 ? surl : "about:blank") } else { var html = localApi.renderPreview() webView.loadHtml( "<!DOCTYPE html><html><head><meta charset='utf-8'></head><body>" + html + "</body></html>", "<!DOCTYPE html><html><head><meta charset='utf-8'>" + "<style>html,body{margin:0;padding:8px;background:#fff;color:#000;font-family:sans-serif;}</style>" + "</head><body>" + html + "</body></html>", "about:blank" ) } Loading @@ -29,14 +33,25 @@ Item { WebEngineView { id: webView anchors.centerIn: parent width: mobileMode ? 375 : parent.width height: parent.height anchors.fill: parent anchors.leftMargin: mobileMode ? Math.max(0, (parent.width - 375) / 2) : 0 anchors.rightMargin: anchors.leftMargin settings.localContentCanAccessRemoteUrls: true settings.javascriptEnabled: true Component.onCompleted: reload() backgroundColor: "#ffffff" onLoadingChanged: function(loadRequest) { if (!previewPane.engineReady && loadRequest.status !== WebEngineView.LoadStartedStatus) { previewPane.engineReady = true reload() } } Component.onCompleted: { webView.url = "about:blank" } } Connections { Loading
src/blogi.cpp +32 −12 Changes for src/blogi.cpp: 32 added lines, 12 removed lines. Original line number Diff line number Diff line Loading @@ -2135,21 +2135,41 @@ RETRY_REQUEST: std::string html; json_object *publishParams = nullptr; for(const auto &curdat : renderopts.urlData()){ if(curdat.key=="mobile" && curdat.value=="true"){ // Helper lambda to process key/value pairs from either source auto processField = [&](const std::string &key, const std::string &value) { if(key=="mobile" && value=="true"){ mobile=true; }else if(curdat.key=="target"){ publishTarget=curdat.value; }else if(curdat.key=="authid"){ authid=curdat.value; }else if(curdat.key=="html"){ html=curdat.value; }else{ // Collect remaining params for the publish preview }else if(key=="target"){ publishTarget=value; }else if(key=="authid"){ authid=value; }else if(key=="html"){ html=value; }else if(!key.empty()){ if(!publishParams) publishParams = json_object_new_object(); json_object_object_add(publishParams, curdat.key.c_str(), json_object_new_string(curdat.value.c_str())); json_object_object_add(publishParams, key.c_str(), json_object_new_string(value.c_str())); } }; // Read URL-encoded form data for(const auto &curdat : renderopts.urlData()){ processField(curdat.key, curdat.value); } // Read multipart form data (used by Qt editor to avoid %00 encoding issues) for(const auto &entry : renderopts.multipartData()){ std::string fieldName; for(const auto &dispo : entry.dispositions){ if(dispo.key == "name"){ fieldName = dispo.value; break; } } if(!fieldName.empty()){ std::string fieldValue(entry.value.begin(), entry.value.end()); processField(fieldName, fieldValue); } } Loading