* @brief Lua C closure for apiCall(method, params?) returning a result table. */
| 616 | * @brief Lua C closure for apiCall(method, params?) returning a result table. |
| 617 | */ |
| 618 | static int luaApiCall(lua_State* L) |
| 619 | { |
| 620 | const int sourceId = static_cast<int>(lua_tointeger(L, lua_upvalueindex(1))); |
| 621 | |
| 622 | if (!lua_isstring(L, 1)) { |
| 623 | pushLuaErr(L, QStringLiteral("apiCall: method must be a string")); |
| 624 | return 1; |
| 625 | } |
| 626 | |
| 627 | size_t mlen = 0; |
| 628 | const char* mstr = lua_tolstring(L, 1, &mlen); |
| 629 | const QString method(QString::fromUtf8(mstr, static_cast<int>(mlen))); |
| 630 | if (method.isEmpty()) { |
| 631 | pushLuaErr(L, QStringLiteral("apiCall: method must not be empty")); |
| 632 | return 1; |
| 633 | } |
| 634 | |
| 635 | if (!API::CommandRegistry::instance().hasCommand(method)) { |
| 636 | pushApiResult(L, dispatchApiCall(method, QJsonObject())); |
| 637 | return 1; |
| 638 | } |
| 639 | |
| 640 | if (!isAllowed(method)) { |
| 641 | pushLuaErr(L, |
| 642 | QStringLiteral("apiCall: method '%1' not in default allow-list " |
| 643 | "(enable apiCall.allowFullSurface in project to opt in)") |
| 644 | .arg(method), |
| 645 | QStringLiteral("METHOD_NOT_ALLOWED")); |
| 646 | return 1; |
| 647 | } |
| 648 | |
| 649 | QJsonObject params; |
| 650 | if (lua_gettop(L) >= 2 && !lua_isnil(L, 2)) { |
| 651 | if (!lua_istable(L, 2)) { |
| 652 | pushLuaErr(L, QStringLiteral("apiCall: params must be a table")); |
| 653 | return 1; |
| 654 | } |
| 655 | |
| 656 | const QJsonValue v = luaTableToJson(L, 2, 0); |
| 657 | if (v.isObject()) |
| 658 | params = v.toObject(); |
| 659 | else { |
| 660 | pushLuaErr(L, QStringLiteral("apiCall: params must be an object-like table")); |
| 661 | return 1; |
| 662 | } |
| 663 | |
| 664 | if (approxJsonSize(params) > kMaxBodyBytes) { |
| 665 | pushLuaErr(L, QStringLiteral("apiCall: params exceed %1 byte cap").arg(kMaxBodyBytes)); |
| 666 | return 1; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | if (!consumeRateToken(sourceId)) { |
| 671 | pushLuaErr(L, QStringLiteral("apiCall: rate-limited or concurrency cap reached for source")); |
| 672 | return 1; |
| 673 | } |
| 674 | |
| 675 | try { |
nothing calls this directly
no test coverage detected