bolar_2 backup
This commit is contained in:
135
main/ota/ota_endpoint.cc
Normal file
135
main/ota/ota_endpoint.cc
Normal file
@@ -0,0 +1,135 @@
|
||||
#include "ota/ota_endpoint.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr const char kWildcardHost[] = "0.0.0.0";
|
||||
constexpr size_t kWildcardHostLength = sizeof(kWildcardHost) - 1;
|
||||
|
||||
bool Contains(const char* haystack, const char* needle)
|
||||
{
|
||||
return haystack != nullptr && needle != nullptr &&
|
||||
std::strstr(haystack, needle) != nullptr;
|
||||
}
|
||||
|
||||
// Extract the host portion of "<scheme>://<host>[:port][/path]".
|
||||
// Returns 0 when the URL has no scheme separator or an empty host.
|
||||
size_t HostSpan(const char* url, const char** host_begin)
|
||||
{
|
||||
if (url == nullptr || host_begin == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
const char* separator = std::strstr(url, "://");
|
||||
if (separator == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
const char* begin = separator + 3;
|
||||
const char* cursor = begin;
|
||||
while (*cursor != '\0' && *cursor != ':' && *cursor != '/') {
|
||||
cursor++;
|
||||
}
|
||||
*host_begin = begin;
|
||||
return static_cast<size_t>(cursor - begin);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool IsRolloverEndpoint(const char* url)
|
||||
{
|
||||
if (url == nullptr || url[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
if (Contains(url, ".lkall.com") &&
|
||||
(Contains(url, "-dev.") || Contains(url, "-moe.") ||
|
||||
Contains(url, "borlaws.lkall.com") ||
|
||||
Contains(url, "borla-ota.lkall.com"))) {
|
||||
return true;
|
||||
}
|
||||
return Contains(url, "192.168.1.121") ||
|
||||
Contains(url, "117.50.44.247");
|
||||
}
|
||||
|
||||
bool SubstituteWildcardHost(
|
||||
const char* url,
|
||||
const char* ota_url,
|
||||
char* output,
|
||||
size_t capacity)
|
||||
{
|
||||
if (url == nullptr || output == nullptr || capacity == 0) {
|
||||
return false;
|
||||
}
|
||||
const char* wildcard = std::strstr(url, kWildcardHost);
|
||||
if (wildcard == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const char* ota_host = nullptr;
|
||||
const size_t ota_host_size = HostSpan(ota_url, &ota_host);
|
||||
if (ota_host_size == 0 ||
|
||||
(ota_host_size == kWildcardHostLength &&
|
||||
std::strncmp(ota_host, kWildcardHost, kWildcardHostLength) == 0)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t prefix_size = static_cast<size_t>(wildcard - url);
|
||||
const char* suffix = wildcard + kWildcardHostLength;
|
||||
const size_t suffix_size = std::strlen(suffix);
|
||||
const size_t total = prefix_size + ota_host_size + suffix_size;
|
||||
if (total >= capacity) {
|
||||
return false;
|
||||
}
|
||||
std::memcpy(output, url, prefix_size);
|
||||
std::memcpy(output + prefix_size, ota_host, ota_host_size);
|
||||
std::memcpy(
|
||||
output + prefix_size + ota_host_size, suffix, suffix_size);
|
||||
output[total] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ResolveRealtimeUrl(
|
||||
const char* url,
|
||||
const char* ota_url,
|
||||
const char* default_url,
|
||||
char* output,
|
||||
size_t capacity)
|
||||
{
|
||||
if (output == nullptr || capacity == 0) {
|
||||
return false;
|
||||
}
|
||||
const char* candidate = url;
|
||||
if (candidate == nullptr || candidate[0] == '\0') {
|
||||
candidate = nullptr;
|
||||
}
|
||||
|
||||
if (candidate != nullptr) {
|
||||
if (SubstituteWildcardHost(
|
||||
candidate, ota_url, output, capacity)) {
|
||||
// A substituted URL is by construction the reachable OTA host, so
|
||||
// it is never treated as a rollover default.
|
||||
return true;
|
||||
}
|
||||
if (std::strstr(candidate, kWildcardHost) != nullptr) {
|
||||
// Wildcard host that could not be resolved is unusable.
|
||||
candidate = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (candidate != nullptr && IsRolloverEndpoint(candidate)) {
|
||||
candidate = nullptr;
|
||||
}
|
||||
if (candidate == nullptr) {
|
||||
candidate = default_url;
|
||||
}
|
||||
if (candidate == nullptr || candidate[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
const size_t size = std::strlen(candidate);
|
||||
if (size >= capacity) {
|
||||
return false;
|
||||
}
|
||||
std::memcpy(output, candidate, size + 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user