| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | engine::runtime::ArtifactKind parse_artifact_kind(const std::string & name, const std::string & where) { |
| 93 | for (const auto & entry : kArtifactKinds) { |
| 94 | if (name == entry.name) { |
| 95 | return entry.kind; |
| 96 | } |
| 97 | } |
| 98 | std::string names; |
| 99 | for (const auto & entry : kArtifactKinds) { |
| 100 | names += (names.empty() ? "" : ", ") + std::string(entry.name); |
| 101 | } |
| 102 | throw InvalidRequestError(where + ": unknown kind '" + name + "', expected one of " + names); |
| 103 | } |
| 104 | |
| 105 | // A field that is absent or null reads as not given. |
| 106 | const engine::io::json::Value * artifact_field( |
| 107 | const engine::io::json::Value & entry, |
| 108 | const std::string & key) { |
| 109 | const auto * value = entry.find(key); |
| 110 | return value == nullptr || value->is_null() ? nullptr : value; |
| 111 | } |
| 112 | |
| 113 | std::string artifact_string( |
| 114 | const engine::io::json::Value & entry, |
| 115 | const std::string & key, |
| 116 | const std::string & where) { |
| 117 | const auto * value = artifact_field(entry, key); |
| 118 | if (value == nullptr || !value->is_string() || value->as_string().empty()) { |
| 119 | throw InvalidRequestError(where + ": " + key + " must be a non-empty string"); |
| 120 | } |
| 121 | return value->as_string(); |
| 122 | } |
| 123 | |
| 124 | [[noreturn]] void throw_artifact_limit(const std::string & where) { |
| 125 | throw InvalidRequestError( |
| 126 | where + ": the request's artifact payloads total more than " + |
| 127 | std::to_string(kMaxRequestArtifactBytes) + " bytes"); |
| 128 | } |
| 129 | |
| 130 | std::vector<std::byte> decode_artifact_payload( |
| 131 | const engine::io::json::Value & payload, |
| 132 | uint64_t budget, |
| 133 | const std::string & where) { |
| 134 | if (!payload.is_string()) { |
| 135 | throw InvalidRequestError(where + ": payload must be a base64 string"); |
| 136 | } |
| 137 | // An inline payload is already in memory as part of the request, so the |
| 138 | // server's request body limit bounds this decode. |
| 139 | std::vector<std::byte> bytes; |
| 140 | try { |
| 141 | bytes = minitts::app::base64_decode_bytes(payload.as_string()); |
| 142 | } catch (const std::runtime_error & error) { |
| 143 | throw InvalidRequestError(where + ": payload is not valid base64 (" + error.what() + ")"); |
| 144 | } |
| 145 | if (bytes.size() > budget) { |
| 146 | throw_artifact_limit(where); |
| 147 | } |
| 148 | return bytes; |
no test coverage detected