Skip to content

Commit

Permalink
http: add onRead and onReadString to allow to handle streaming input
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Feb 25, 2025
1 parent 7132688 commit 133d79e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/ossia-qt/http/http_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,40 @@ void http_protocol::slot_push(http_parameter* addr_p, const ossia::value& v)
auto& addr = *addr_p;
auto& dat = addr.data();

auto setupReply = [this, addr_p](QNetworkReply* rep) {
auto& dat = addr_p->data();
if(dat.onRead.isCallable() || dat.onReadString.isCallable())
{
QObject::connect(rep, &QNetworkReply::readyRead, this, [this, rep, addr_p] {
const auto ba = rep->readAll();
auto& dat = addr_p->data();
if(dat.onRead.isCallable())
dat.onRead.call({m_engine->toScriptValue(ba)});
if(dat.onReadString.isCallable())
dat.onReadString.call({m_engine->toScriptValue(QString::fromUtf8(ba))});
});
}
m_replies[rep] = addr_p;
};

if(auto url = this->requestUrl(addr_p, v); url.isValid())
{
auto request_data = this->requestData(addr_p, v);
if(dat.is_post)
{
auto rep = m_access->post(QNetworkRequest(url), request_data);
m_replies[rep] = addr_p;
setupReply(m_access->post(QNetworkRequest(url), request_data));
}
else
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
if(!request_data.isEmpty())
{
auto rep = m_access->get(QNetworkRequest(url), request_data);
m_replies[rep] = addr_p;
setupReply(m_access->get(QNetworkRequest(url), request_data));
}
else
#endif
{
auto rep = m_access->get(QNetworkRequest(url));
m_replies[rep] = addr_p;
setupReply(m_access->get(QNetworkRequest(url)));
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/ossia-qt/http/http_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ struct http_parameter_data_base
{
is_post = true;
}
if(auto r = val.property("onRead"); r.isCallable())
{
onRead = r;
}
if(auto r = val.property("onReadString"); r.isCallable())
{
onReadString = r;
}
}

bool requestIsValid() const noexcept
Expand All @@ -59,6 +67,8 @@ struct http_parameter_data_base
QJSValue request;
QJSValue answer;
QJSValue requestData;
QJSValue onRead;
QJSValue onReadString;
bool is_post{};
};

Expand Down

0 comments on commit 133d79e

Please sign in to comment.