mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
Prevent crash calling uninitialized service via JSON API
This commit is contained in:
parent
cc6f0b1f05
commit
7a37c11e47
@ -45,6 +45,10 @@ registerHandler("$%apiPath%$",
|
||||
if(jReq.HasMember(kcd))
|
||||
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator());
|
||||
|
||||
if( !checkRsServicePtrReady(
|
||||
$%instanceName%$, "$%instanceName%$", cAns, session ) )
|
||||
return;
|
||||
|
||||
$%paramsDeclaration%$
|
||||
|
||||
$%inputParamsDeserialization%$
|
||||
|
@ -112,7 +112,6 @@ int main(int argc, char *argv[])
|
||||
QDomNode member = members.item(i);
|
||||
QString refid(member.attributes().namedItem("refid").nodeValue());
|
||||
QString methodName(member.firstChildElement("name").toElement().text());
|
||||
QString wrapperName(instanceName+methodName+"Wrapper");
|
||||
QString defFilePath(doxPrefix + refid.split('_')[0] + ".xml");
|
||||
|
||||
qDebug() << "Looking for" << typeName << methodName << "into"
|
||||
@ -325,7 +324,7 @@ int main(int argc, char *argv[])
|
||||
substitutionsMap.insert("paramsDeclaration", paramsDeclaration);
|
||||
substitutionsMap.insert("inputParamsDeserialization", inputParamsDeserialization);
|
||||
substitutionsMap.insert("outputParamsSerialization", outputParamsSerialization);
|
||||
substitutionsMap.insert("wrapperName", wrapperName);
|
||||
substitutionsMap.insert("instanceName", instanceName);
|
||||
substitutionsMap.insert("headerFileName", headerFileName);
|
||||
substitutionsMap.insert("functionCall", functionCall);
|
||||
substitutionsMap.insert("apiPath", apiPath);
|
||||
|
@ -36,7 +36,11 @@ registerHandler("$%apiPath%$",
|
||||
// if caller specified caller_data put it back in the answhere
|
||||
const char kcd[] = "caller_data";
|
||||
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%$
|
||||
|
||||
|
@ -30,6 +30,33 @@
|
||||
// Generated at compile time
|
||||
#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(
|
||||
uint16_t port, const std::function<void(int)> shutdownCallback) :
|
||||
mPort(port), mShutdownCallback(shutdownCallback)
|
||||
@ -62,6 +89,9 @@ JsonApiServer::JsonApiServer(
|
||||
if(jReq.HasMember(kcd))
|
||||
jAns.AddMember(kcd, jReq[kcd], jAns.GetAllocator());
|
||||
|
||||
if(!checkRsServicePtrReady(rsFiles, "rsFiles", cAns, session))
|
||||
return;
|
||||
|
||||
RsFileHash hash;
|
||||
uint64_t offset;
|
||||
uint32_t requested_size;
|
||||
|
Loading…
Reference in New Issue
Block a user