core: client API Majestic, modèles et CLI check
- libopenpu : validation VIN, modèles JSON (getAvailableUpdate, devices), classification ecuType -> type/guide, sélection des mises à jour - couche Transport abstraite + implémentation libcurl (proxy env, redirections, streaming, timeout d'inactivité) - tolérance au EOF TLS sans close_notify de la passerelle Majestic, limitée aux corps délimités par la fermeture (ni Content-Length ni chunked) - openpu-cli : check / raw-devices / raw-updates - tests doctest (VIN, parsing, classification, client API avec faux transport), fixtures dont une capture réelle anonymisée (NAC wave 2) - qualité : presets CMake (debug, asan, tidy), .clang-tidy, tools/check.sh (tests, ASan/UBSan, valgrind, clang-tidy, cppcheck) - validé contre l'API réelle sur 3 véhicules (NAC w4, RCC, NAC w2) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WDASLCiN764v685boRVYar
This commit is contained in:
1 parent
660a420a3e
commit
95704f9882
28 files changed
+1877
-2
No files matched your search
@@ -0,0 +1,111 @@
|
||||
#include "openpu/http.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
namespace openpu {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string trim(std::string s)
|
||||
{
|
||||
const auto notSpace = [](unsigned char c) { return std::isspace(c) == 0; };
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), notSpace));
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(), notSpace).base(), s.end());
|
||||
return s;
|
||||
}
|
||||
|
||||
int hexValue(char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
return c - '0';
|
||||
if (c >= 'a' && c <= 'f')
|
||||
return c - 'a' + 10;
|
||||
if (c >= 'A' && c <= 'F')
|
||||
return c - 'A' + 10;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string percentDecode(const std::string& s)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(s.size());
|
||||
for (std::size_t i = 0; i < s.size(); ++i) {
|
||||
if (s[i] == '%' && i + 2 < s.size()) {
|
||||
const int hi = hexValue(s[i + 1]);
|
||||
const int lo = hexValue(s[i + 2]);
|
||||
if (hi >= 0 && lo >= 0) {
|
||||
out.push_back(static_cast<char>(hi * 16 + lo));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
out.push_back(s[i]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Refuses names that could escape the target directory.
|
||||
std::string sanitizeFilename(std::string name)
|
||||
{
|
||||
const auto slash = name.find_last_of("/\\");
|
||||
if (slash != std::string::npos)
|
||||
name = name.substr(slash + 1);
|
||||
if (name == "." || name == "..")
|
||||
return {};
|
||||
name.erase(std::remove_if(name.begin(), name.end(),
|
||||
[](unsigned char c) { return c < 0x20 || c == 0x7f; }),
|
||||
name.end());
|
||||
return name;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string filenameFromContentDisposition(const std::string& header)
|
||||
{
|
||||
// Supports `filename="x"`, `filename=x` and RFC 5987 `filename*=UTF-8''x` (preferred).
|
||||
std::string lower(header);
|
||||
std::transform(lower.begin(), lower.end(), lower.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||||
|
||||
auto extract = [&](std::size_t valueStart) {
|
||||
std::string value;
|
||||
if (valueStart < header.size() && header[valueStart] == '"') {
|
||||
const auto end = header.find('"', valueStart + 1);
|
||||
value = header.substr(valueStart + 1, end == std::string::npos ? std::string::npos : end - valueStart - 1);
|
||||
} else {
|
||||
const auto end = header.find(';', valueStart);
|
||||
value = trim(header.substr(valueStart, end == std::string::npos ? std::string::npos : end - valueStart));
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
const auto star = lower.find("filename*=");
|
||||
if (star != std::string::npos) {
|
||||
std::string v = extract(star + 10);
|
||||
const auto quotes = v.find("''");
|
||||
if (quotes != std::string::npos)
|
||||
v = v.substr(quotes + 2);
|
||||
v = sanitizeFilename(percentDecode(v));
|
||||
if (!v.empty())
|
||||
return v;
|
||||
}
|
||||
const auto plain = lower.find("filename=");
|
||||
if (plain == std::string::npos)
|
||||
return {};
|
||||
return sanitizeFilename(extract(plain + 9));
|
||||
}
|
||||
|
||||
std::string filenameFromUrl(const std::string& url)
|
||||
{
|
||||
std::string path = url.substr(0, url.find_first_of("?#"));
|
||||
const auto schemeEnd = path.find("://");
|
||||
if (schemeEnd != std::string::npos) {
|
||||
const auto pathStart = path.find('/', schemeEnd + 3);
|
||||
path = pathStart == std::string::npos ? std::string() : path.substr(pathStart);
|
||||
}
|
||||
const auto slash = path.find_last_of('/');
|
||||
return sanitizeFilename(percentDecode(slash == std::string::npos ? path : path.substr(slash + 1)));
|
||||
}
|
||||
|
||||
} // namespace openpu
|
||||
Reference in new issue
Block a user