- interface gtkmm-4 : véhicules, ajout par VIN, mises à jour, dossier, téléchargement avec reprise et arrêt, choix de la clé, confirmation, écriture avec progression, fin avec guide d'installation ; travail dans un thread, résultats renvoyés via Glib::Dispatcher - préférences : dossier de téléchargement, langue (système par défaut), recherche automatique de nouvelle version d'OpenPU - recherche de nouvelle version (releases GitHub, format tags Gitea accepté) avec ouverture de la page dans le navigateur ; openpu-cli app-update - versions : project(VERSION) + git describe régénéré à chaque build, openpu-cli --version, À propos, User-Agent, CHANGELOG, tools/release.sh (étiquette annotée, push vers tous les remotes, release GitHub) - traductions gettext : textes anglais, fr.po complet (96 chaînes), extracteur de secours sans xgettext - core : module job (téléchargement licence/fw/carte), liens des guides PDF, ordre des véhicules conservé (ordered_json) - licence MIT (LICENSE, en-têtes SPDX) ; dépôt principal sur GitHub - validé dans WSLg : parcours complet jusqu'à l'écriture d'une clé loop (FAT32 propre, fichiers téléchargés supprimés) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WDASLCiN764v685boRVYar
113 lines
3.5 KiB
C++
113 lines
3.5 KiB
C++
// SPDX-License-Identifier: MIT
|
|
#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
|