bolar_2 backup

This commit is contained in:
2026-11-27 16:38:11 +08:00
commit e96d6d19df
305 changed files with 43350 additions and 0 deletions

View File

@@ -0,0 +1,143 @@
#include "protocol/websocket_uri.h"
#include "connectivity/wifi_types.h"
#include <cctype>
#include <cstring>
namespace borla {
namespace {
bool CopyRange(
const char* begin,
const char* end,
char* output,
size_t capacity)
{
if (begin == nullptr || end == nullptr || output == nullptr ||
end <= begin || static_cast<size_t>(end - begin) >= capacity) {
return false;
}
const size_t size = static_cast<size_t>(end - begin);
std::memcpy(output, begin, size);
output[size] = '\0';
return true;
}
bool ParsePort(const char* begin, const char* end, uint16_t* output)
{
if (begin == nullptr || end == nullptr || output == nullptr ||
begin >= end) {
return false;
}
uint32_t value = 0;
for (const char* cursor = begin; cursor < end; ++cursor) {
if (!std::isdigit(static_cast<unsigned char>(*cursor))) {
return false;
}
value = value * 10U +
static_cast<uint32_t>(*cursor - '0');
if (value > 65535U) {
return false;
}
}
if (value == 0) {
return false;
}
*output = static_cast<uint16_t>(value);
return true;
}
} // namespace
bool ParseWebsocketUri(const char* uri, WebsocketUri* output)
{
if (uri == nullptr || output == nullptr) {
return false;
}
const size_t uri_size = BoundedStringLength(uri, 1024);
if (uri_size == 0 || uri_size >= 1024) {
return false;
}
WebsocketUri parsed;
const char* authority = nullptr;
if (std::strncmp(uri, "ws://", 5) == 0) {
parsed.secure = false;
parsed.port = 80;
authority = uri + 5;
} else if (std::strncmp(uri, "wss://", 6) == 0) {
parsed.secure = true;
parsed.port = 443;
authority = uri + 6;
} else {
return false;
}
const char* const uri_end = uri + uri_size;
const char* path = std::strchr(authority, '/');
const char* authority_end = path == nullptr ? uri_end : path;
if (authority >= authority_end) {
return false;
}
const char* port_separator = nullptr;
if (*authority == '[') {
const char* bracket = std::strchr(authority, ']');
if (bracket == nullptr || bracket >= authority_end ||
!CopyRange(
authority + 1,
bracket,
parsed.host.data(),
parsed.host.size())) {
return false;
}
if (bracket + 1 < authority_end) {
if (*(bracket + 1) != ':') {
return false;
}
port_separator = bracket + 1;
}
} else {
for (const char* cursor = authority;
cursor < authority_end;
++cursor) {
if (*cursor == ':') {
if (port_separator != nullptr) {
return false;
}
port_separator = cursor;
}
}
const char* host_end =
port_separator == nullptr ? authority_end : port_separator;
if (!CopyRange(
authority,
host_end,
parsed.host.data(),
parsed.host.size())) {
return false;
}
}
if (port_separator != nullptr &&
!ParsePort(port_separator + 1, authority_end, &parsed.port)) {
return false;
}
if (path == nullptr) {
parsed.path[0] = '/';
parsed.path[1] = '\0';
} else if (!CopyRange(
path,
uri_end,
parsed.path.data(),
parsed.path.size())) {
return false;
}
*output = parsed;
return true;
}
} // namespace borla