mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-20 04:14:27 -04:00
Prevent crash calling uninitialized service via JSON API
This commit is contained in:
parent
cc6f0b1f05
commit
7a37c11e47
4 changed files with 40 additions and 3 deletions
|
@ -45,6 +45,10 @@ registerHandler("$%apiPath%$",
|
||||||
if(jReq.HasMember(kcd))
|
if(jReq.HasMember(kcd))
|
||||||
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator());
|
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator());
|
||||||
|
|
||||||
|
if( !checkRsServicePtrReady(
|
||||||
|
$%instanceName%$, "$%instanceName%$", cAns, session ) )
|
||||||
|
return;
|
||||||
|
|
||||||
$%paramsDeclaration%$
|
$%paramsDeclaration%$
|
||||||
|
|
||||||
$%inputParamsDeserialization%$
|
$%inputParamsDeserialization%$
|
||||||
|
|
|
@ -112,7 +112,6 @@ int main(int argc, char *argv[])
|
||||||
QDomNode member = members.item(i);
|
QDomNode member = members.item(i);
|
||||||
QString refid(member.attributes().namedItem("refid").nodeValue());
|
QString refid(member.attributes().namedItem("refid").nodeValue());
|
||||||
QString methodName(member.firstChildElement("name").toElement().text());
|
QString methodName(member.firstChildElement("name").toElement().text());
|
||||||
QString wrapperName(instanceName+methodName+"Wrapper");
|
|
||||||
QString defFilePath(doxPrefix + refid.split('_')[0] + ".xml");
|
QString defFilePath(doxPrefix + refid.split('_')[0] + ".xml");
|
||||||
|
|
||||||
qDebug() << "Looking for" << typeName << methodName << "into"
|
qDebug() << "Looking for" << typeName << methodName << "into"
|
||||||
|
@ -325,7 +324,7 @@ int main(int argc, char *argv[])
|
||||||
substitutionsMap.insert("paramsDeclaration", paramsDeclaration);
|
substitutionsMap.insert("paramsDeclaration", paramsDeclaration);
|
||||||
substitutionsMap.insert("inputParamsDeserialization", inputParamsDeserialization);
|
substitutionsMap.insert("inputParamsDeserialization", inputParamsDeserialization);
|
||||||
substitutionsMap.insert("outputParamsSerialization", outputParamsSerialization);
|
substitutionsMap.insert("outputParamsSerialization", outputParamsSerialization);
|
||||||
substitutionsMap.insert("wrapperName", wrapperName);
|
substitutionsMap.insert("instanceName", instanceName);
|
||||||
substitutionsMap.insert("headerFileName", headerFileName);
|
substitutionsMap.insert("headerFileName", headerFileName);
|
||||||
substitutionsMap.insert("functionCall", functionCall);
|
substitutionsMap.insert("functionCall", functionCall);
|
||||||
substitutionsMap.insert("apiPath", apiPath);
|
substitutionsMap.insert("apiPath", apiPath);
|
||||||
|
|
|
@ -38,6 +38,10 @@ registerHandler("$%apiPath%$",
|
||||||
if(jReq.HasMember(kcd))
|
if(jReq.HasMember(kcd))
|
||||||
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator());
|
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator());
|
||||||
|
|
||||||
|
if( !checkRsServicePtrReady(
|
||||||
|
$%instanceName%$, "$%instanceName%$", cAns, session ) )
|
||||||
|
return;
|
||||||
|
|
||||||
$%paramsDeclaration%$
|
$%paramsDeclaration%$
|
||||||
|
|
||||||
// deserialize input parameters from JSON
|
// deserialize input parameters from JSON
|
||||||
|
|
|
@ -30,6 +30,33 @@
|
||||||
// Generated at compile time
|
// Generated at compile time
|
||||||
#include "jsonapi-includes.inl"
|
#include "jsonapi-includes.inl"
|
||||||
|
|
||||||
|
static bool checkRsServicePtrReady(
|
||||||
|
void* serviceInstance, const std::string& serviceName,
|
||||||
|
RsGenericSerializer::SerializeContext& ctx,
|
||||||
|
const std::shared_ptr<restbed::Session> session)
|
||||||
|
{
|
||||||
|
if(serviceInstance) return true;
|
||||||
|
|
||||||
|
std::string jsonApiError;
|
||||||
|
jsonApiError += "Service: ";
|
||||||
|
jsonApiError += serviceName;
|
||||||
|
jsonApiError += " not initialized! Are you sure you logged in already?";
|
||||||
|
|
||||||
|
RsGenericSerializer::SerializeJob j(RsGenericSerializer::TO_JSON);
|
||||||
|
RS_SERIAL_PROCESS(jsonApiError);
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << ctx.mJson;
|
||||||
|
std::string&& ans(ss.str());
|
||||||
|
const std::multimap<std::string, std::string> headers
|
||||||
|
{
|
||||||
|
{ "Content-Type", "text/json" },
|
||||||
|
{ "Content-Length", std::to_string(ans.length()) }
|
||||||
|
};
|
||||||
|
session->close(rb::CONFLICT, ans, headers);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
JsonApiServer::JsonApiServer(
|
JsonApiServer::JsonApiServer(
|
||||||
uint16_t port, const std::function<void(int)> shutdownCallback) :
|
uint16_t port, const std::function<void(int)> shutdownCallback) :
|
||||||
mPort(port), mShutdownCallback(shutdownCallback)
|
mPort(port), mShutdownCallback(shutdownCallback)
|
||||||
|
@ -62,6 +89,9 @@ JsonApiServer::JsonApiServer(
|
||||||
if(jReq.HasMember(kcd))
|
if(jReq.HasMember(kcd))
|
||||||
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator());
|
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator());
|
||||||
|
|
||||||
|
if(!checkRsServicePtrReady(rsFiles, "rsFiles", cAns, session))
|
||||||
|
return;
|
||||||
|
|
||||||
RsFileHash hash;
|
RsFileHash hash;
|
||||||
uint64_t offset;
|
uint64_t offset;
|
||||||
uint32_t requested_size;
|
uint32_t requested_size;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue