Support JSON response in get_simple_config.

This commit is contained in:
levlam 2022-02-10 23:23:10 +03:00
parent b8ab910b81
commit aa1f7592e8

View File

@ -261,13 +261,7 @@ static ActorOwn<> get_simple_config_dns(Slice address, Slice host, Promise<Simpl
name = is_test ? "tapv3.stel.com" : "apv3.stel.com";
}
auto get_config = [](HttpQuery &http_query) -> Result<string> {
VLOG(config_recoverer) << "Receive DNS response " << http_query.content_;
TRY_RESULT(json, json_decode(http_query.content_));
if (json.type() != JsonValue::Type::Object) {
return Status::Error("Expected JSON object");
}
auto &answer_object = json.get_object();
TRY_RESULT(answer, get_json_object_field(answer_object, "Answer", JsonValue::Type::Array, false));
auto get_data = [](JsonValue &answer) -> Result<string> {
auto &answer_array = answer.get_array();
vector<string> parts;
for (auto &answer_part : answer_array) {
@ -289,6 +283,24 @@ static ActorOwn<> get_simple_config_dns(Slice address, Slice host, Promise<Simpl
}
return data;
};
if (!http_query.get_arg("Answer").empty()) {
VLOG(config_recoverer) << "Receive DNS response " << http_query.get_arg("Answer");
TRY_RESULT(answer, json_decode(http_query.get_arg("Answer")));
if (answer.type() != JsonValue::Type::Array) {
return Status::Error("Expected JSON array");
}
return get_data(answer);
} else {
VLOG(config_recoverer) << "Receive DNS response " << http_query.content_;
TRY_RESULT(json, json_decode(http_query.content_));
if (json.type() != JsonValue::Type::Object) {
return Status::Error("Expected JSON object");
}
auto &answer_object = json.get_object();
TRY_RESULT(answer, get_json_object_field(answer_object, "Answer", JsonValue::Type::Array, false));
return get_data(answer);
}
};
return get_simple_config_impl(std::move(promise), scheduler_id,
PSTRING() << "https://" << address << "?name=" << url_encode(name) << "&type=TXT",
host.str(), {{"Accept", "application/dns-json"}}, prefer_ipv6, std::move(get_config));