Files
moloapp/src/google/enterprise/caller.cpp
T

216 lines
8.2 KiB
C++
Raw Normal View History

#include "google/enterprise/caller.h"
#include <regex>
#include "clients/url.h"
#include "google/enterprise/caps.h"
#include "google/errors.h"
namespace molo::google::enterprise {
using clients::body::Json;
namespace {
bool seg_char(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' ||
c == '_' || c == '~' || c == '-';
}
bool valid_segment_text(const std::string& s) {
if (s.empty() || s.size() > EnterpriseCaps::kMaxSegmentChars) return false;
if (s == "." || s == "..") return false;
for (char c : s)
if (!seg_char(c)) return false;
return true;
}
[[noreturn]] void bad(const std::string& m) { throw GoogleError::invalid(m); }
std::string enc(const std::string& s) { return clients::Url::encode_component(s); }
} // namespace
bool valid_resource_name(const std::string& name) {
if (name.empty() || name.size() > EnterpriseCaps::kMaxResourceChars) return false;
std::size_t start = 0;
while (true) {
std::size_t slash = name.find('/', start);
std::string seg = name.substr(start, slash == std::string::npos ? std::string::npos : slash - start);
if (!valid_segment_text(seg)) return false;
if (slash == std::string::npos) return true;
start = slash + 1;
}
}
Caller::Caller(std::shared_ptr<GoogleClient> client, Api api, std::string default_location,
Sleeper sleeper)
: Service(std::move(client), endpoint_for(api, "v1", "global")),
api_(api),
location_(std::move(default_location)),
sleeper_(std::move(sleeper)) {}
std::string Caller::location_of(const Invocation& inv) const {
for (const auto& kv : inv.path) {
std::size_t p = kv.second.find("locations/");
if (p == std::string::npos || (p != 0 && kv.second[p - 1] != '/')) continue;
std::size_t s = p + 10;
std::size_t e = kv.second.find('/', s);
return kv.second.substr(s, e == std::string::npos ? std::string::npos : e - s);
}
return location_;
}
Prepared Caller::prepare(const Invocation& inv) const {
const CallSpec& spec = find_call(api_, inv.rpc);
Prepared out;
out.spec = &spec;
// Path.
std::string path = spec.path;
std::size_t slash = path.find('/');
path = path.substr(slash); // drop the version prefix
std::size_t used = 0;
for (const auto& p : spec.path_params()) {
auto it = inv.path.find(p.name);
if (it == inv.path.end()) bad("missing path parameter " + p.name);
++used;
const std::string& v = it->second;
if (p.plus) {
if (!valid_resource_name(v)) bad("bad resource name for " + p.name);
} else if (!valid_segment_text(v)) {
bad("bad id for " + p.name);
}
if (!p.pattern.empty()) {
bool ok = false;
try {
ok = std::regex_match(v, std::regex(p.pattern.substr(p.pattern.front() == '^' ? 1 : 0,
p.pattern.size() -
(p.pattern.front() == '^' ? 1 : 0) -
(p.pattern.back() == '$' ? 1 : 0))));
} catch (const std::regex_error&) {
ok = true; // a pattern std::regex cannot compile is left to Google
}
if (!ok) bad(p.name + " does not match " + p.pattern);
}
std::string token = std::string("{") + (p.plus ? "+" : "") + p.name + "}";
std::size_t at = path.find(token);
path.replace(at, token.size(), v); // segments are already URL safe
}
if (used != inv.path.size()) bad("unexpected path parameter");
// Query.
std::vector<std::string> allowed = spec.query_params();
if (inv.query.size() > EnterpriseCaps::kMaxQueryParams) bad("too many query parameters");
std::string qs;
for (const auto& kv : inv.query) {
bool known = false;
for (const auto& a : allowed) known = known || a == kv.first;
if (!known) bad("unknown query parameter " + kv.first);
if (kv.second.size() > EnterpriseCaps::kMaxQueryValueChars) bad("query value too long");
for (unsigned char c : kv.second)
if (c < 0x20) bad("control character in query value");
qs += (qs.empty() ? "?" : "&") + kv.first + "=" + enc(kv.second);
}
for (const auto& r : spec.required_query())
if (!inv.query.count(r)) bad("missing query parameter " + r);
out.path = path + qs;
// Body.
if (std::string(spec.request).empty()) {
if (!inv.body.is_null()) bad("this call takes no body");
} else {
out.has_body = true;
out.body = inv.body.is_null() ? Json::object() : inv.body;
if (!out.body.is_object()) bad("body must be a JSON object");
std::vector<std::string> fields = spec.body_field_list();
for (const auto& kv : out.body.as_object()) {
bool known = false;
for (const auto& f : fields) known = known || f == kv.first;
if (!known) bad("unknown body field " + kv.first);
}
if (out.body.dump().size() > EnterpriseCaps::kMaxBodyBytes) bad("body too large");
}
out.endpoint = endpoint_for(api_, spec.version, location_of(inv));
return out;
}
Json Caller::invoke(const Invocation& inv) const {
Prepared p = prepare(inv);
if (p.spec->destructive()) bad("destructive call: use invoke_destructive");
if (p.spec->streaming()) bad("streaming call: use invoke_stream");
return client_->request(p.spec->method(), p.endpoint, p.path, p.has_body ? &p.body : nullptr);
}
Json Caller::invoke_destructive(const Invocation& inv, bool confirm) const {
Prepared p = prepare(inv);
if (!p.spec->destructive()) bad("not a destructive call: use invoke");
if (!confirm) bad("destructive call needs confirm=true");
return client_->request(p.spec->method(), p.endpoint, p.path, p.has_body ? &p.body : nullptr);
}
std::size_t Caller::invoke_stream(const Invocation& inv, const GoogleClient::EventSink& sink) const {
Prepared p = prepare(inv);
if (!p.spec->streaming()) bad("not a streaming call");
std::size_t n = 0;
client_->stream_json(p.endpoint, p.path, p.has_body ? p.body : Json::object(),
[&](const Json& ev) {
++n;
return sink(ev) && n < EnterpriseCaps::kMaxStreamEvents;
});
return n;
}
ListResult Caller::list(const Invocation& inv) const {
const CallSpec& spec = find_call(api_, inv.rpc);
if (!spec.paginated()) bad("not a paginated call");
ListResult out;
Invocation page = inv;
bool sized = false;
for (const auto& q : spec.query_params()) sized = sized || q == "pageSize";
if (sized && !page.query.count("pageSize"))
page.query["pageSize"] = std::to_string(EnterpriseCaps::kPageSize);
for (int i = 0; i < EnterpriseCaps::kMaxListPages; ++i) {
Prepared p = prepare(page);
Json doc = client_->request(p.spec->method(), p.endpoint, p.path, p.has_body ? &p.body : nullptr);
++out.pages;
out.next_page_token.clear();
if (doc.is_object()) {
if (const Json* arr = doc.find(spec.items_key); arr && arr->is_array())
for (const Json& item : arr->as_array()) {
if (out.size() >= EnterpriseCaps::kMaxListItems) {
out.truncated = true;
return out;
}
out.items.push(item);
}
if (const Json* t = doc.find("nextPageToken"); t && t->is_string())
out.next_page_token = t->as_string();
}
if (out.next_page_token.empty()) return out;
page.query["pageToken"] = out.next_page_token;
}
out.truncated = !out.next_page_token.empty();
return out;
}
void Caller::check_operation_name(const std::string& name) {
if (name.rfind("projects/", 0) != 0 || !valid_resource_name(name) ||
name.find("/operations/") == std::string::npos)
bad("bad operation name");
}
Operation Caller::operation(const std::string& name, const std::string& version,
const std::string& location) const {
check_operation_name(name);
return Operation::from_json(
client_->get_json(endpoint_for(api_, version, location.empty() ? location_ : location), "/" + name));
}
Operation Caller::wait(const std::string& name, const std::string& version,
const std::string& location) const {
check_operation_name(name);
return wait_operation(*client_, endpoint_for(api_, version, location.empty() ? location_ : location),
name, sleeper_);
}
} // namespace molo::google::enterprise