Mercurial > hg > piper-cpp
comparison vamp-json/VampJson.h @ 128:615fc5a47509
Better checking for incoming rpc requests
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Wed, 02 Nov 2016 18:26:21 +0000 |
parents | 5b113c87b6e6 |
children | 1c58149b64a2 |
comparison
equal
deleted
inserted
replaced
127:5b113c87b6e6 | 128:615fc5a47509 |
---|---|
1068 } | 1068 } |
1069 | 1069 |
1070 private: // go private briefly for a couple of helper functions | 1070 private: // go private briefly for a couple of helper functions |
1071 | 1071 |
1072 static void | 1072 static void |
1073 checkTypeField(json11::Json j, std::string expected, std::string &err) { | 1073 checkRpcRequestType(json11::Json j, std::string expected, std::string &err) { |
1074 if (!j["method"].is_string()) { | 1074 if (!j["method"].is_string()) { |
1075 err = "string expected for method"; | 1075 err = "string expected for method"; |
1076 return; | 1076 return; |
1077 } | 1077 } |
1078 if (j["method"].string_value() != expected) { | 1078 if (j["method"].string_value() != expected) { |
1079 err = "expected value \"" + expected + "\" for type"; | 1079 err = "expected value \"" + expected + "\" for type"; |
1080 return; | 1080 return; |
1081 } | |
1082 if (!j["params"].is_null() && | |
1083 !j["params"].is_object()) { | |
1084 err = "object expected for params"; | |
1085 return; | |
1086 } | |
1087 if (!j["id"].is_null() && | |
1088 !j["id"].is_number() && | |
1089 !j["id"].is_string()) { | |
1090 err = "number or string expected for id"; | |
1091 return; | |
1092 } | |
1093 if (!j["jsonrpc"].is_null() && | |
1094 !j["jsonrpc"].is_string()) { | |
1095 err = "string expected for jsonrpc"; | |
1096 return; | |
1097 } | |
1098 for (const auto &kv: j.object_items()) { | |
1099 if (kv.first != "method" && | |
1100 kv.first != "params" && | |
1101 kv.first != "id" && | |
1102 kv.first != "jsonrpc") { | |
1103 err = "unexpected field \"" + kv.first + "\" in rpc request object"; | |
1104 return; | |
1105 } | |
1081 } | 1106 } |
1082 } | 1107 } |
1083 | 1108 |
1084 static bool | 1109 static bool |
1085 successful(json11::Json j, std::string &err) { | 1110 successful(json11::Json j, std::string &err) { |
1320 } | 1345 } |
1321 | 1346 |
1322 static ListRequest | 1347 static ListRequest |
1323 toRpcRequest_List(json11::Json j, std::string &err) { | 1348 toRpcRequest_List(json11::Json j, std::string &err) { |
1324 | 1349 |
1325 checkTypeField(j, "list", err); | 1350 checkRpcRequestType(j, "list", err); |
1326 if (failed(err)) return {}; | 1351 if (failed(err)) return {}; |
1327 return toListRequest(j["params"], err); | 1352 return toListRequest(j["params"], err); |
1328 } | 1353 } |
1329 | 1354 |
1330 static ListResponse | 1355 static ListResponse |
1338 } | 1363 } |
1339 | 1364 |
1340 static LoadRequest | 1365 static LoadRequest |
1341 toRpcRequest_Load(json11::Json j, std::string &err) { | 1366 toRpcRequest_Load(json11::Json j, std::string &err) { |
1342 | 1367 |
1343 checkTypeField(j, "load", err); | 1368 checkRpcRequestType(j, "load", err); |
1344 if (failed(err)) return {}; | 1369 if (failed(err)) return {}; |
1345 return toLoadRequest(j["params"], err); | 1370 return toLoadRequest(j["params"], err); |
1346 } | 1371 } |
1347 | 1372 |
1348 static LoadResponse | 1373 static LoadResponse |
1360 static ConfigurationRequest | 1385 static ConfigurationRequest |
1361 toRpcRequest_Configure(json11::Json j, | 1386 toRpcRequest_Configure(json11::Json j, |
1362 const PluginHandleMapper &pmapper, | 1387 const PluginHandleMapper &pmapper, |
1363 std::string &err) { | 1388 std::string &err) { |
1364 | 1389 |
1365 checkTypeField(j, "configure", err); | 1390 checkRpcRequestType(j, "configure", err); |
1366 if (failed(err)) return {}; | 1391 if (failed(err)) return {}; |
1367 return toConfigurationRequest(j["params"], pmapper, err); | 1392 return toConfigurationRequest(j["params"], pmapper, err); |
1368 } | 1393 } |
1369 | 1394 |
1370 static ConfigurationResponse | 1395 static ConfigurationResponse |
1381 | 1406 |
1382 static ProcessRequest | 1407 static ProcessRequest |
1383 toRpcRequest_Process(json11::Json j, const PluginHandleMapper &pmapper, | 1408 toRpcRequest_Process(json11::Json j, const PluginHandleMapper &pmapper, |
1384 BufferSerialisation &serialisation, std::string &err) { | 1409 BufferSerialisation &serialisation, std::string &err) { |
1385 | 1410 |
1386 checkTypeField(j, "process", err); | 1411 checkRpcRequestType(j, "process", err); |
1387 if (failed(err)) return {}; | 1412 if (failed(err)) return {}; |
1388 return toProcessRequest(j["params"], pmapper, serialisation, err); | 1413 return toProcessRequest(j["params"], pmapper, serialisation, err); |
1389 } | 1414 } |
1390 | 1415 |
1391 static ProcessResponse | 1416 static ProcessResponse |
1407 | 1432 |
1408 static FinishRequest | 1433 static FinishRequest |
1409 toRpcRequest_Finish(json11::Json j, const PluginHandleMapper &pmapper, | 1434 toRpcRequest_Finish(json11::Json j, const PluginHandleMapper &pmapper, |
1410 std::string &err) { | 1435 std::string &err) { |
1411 | 1436 |
1412 checkTypeField(j, "finish", err); | 1437 checkRpcRequestType(j, "finish", err); |
1413 if (failed(err)) return {}; | 1438 if (failed(err)) return {}; |
1414 FinishRequest req; | 1439 FinishRequest req; |
1415 req.plugin = pmapper.handleToPlugin | 1440 req.plugin = pmapper.handleToPlugin |
1416 (j["params"]["handle"].int_value()); | 1441 (j["params"]["handle"].int_value()); |
1417 return req; | 1442 return req; |