#include #include #include #include #include #include #include #include #include #include #include #include #if defined(_WIN32) #include #else #include #include #include #endif #if defined(VBS_HAS_VOID_NET) #include #endif namespace Void::Build::Report { namespace { Void::Result::Error make_error(std::string msg) { return Void::System::make_system_error(Void::System::SystemErrorCode::invalid_argument, std::move(msg)); } std::string xml_escape(std::string_view text) { std::string out; out.reserve(text.size()); for (const char c : text) { switch (c) { case '&': out += "&"; break; case '<': out += "<"; break; case '>': out += ">"; break; case '"': out += """; break; case '\'': out += "'"; break; default: if (static_cast(c) >= 0x20 || c == '\n' || c == '\t') { out.push_back(c); } break; } } return out; } std::string utc_timestamp() { const auto now = std::chrono::system_clock::now(); const std::time_t seconds = std::chrono::system_clock::to_time_t(now); std::tm tm{}; #if defined(_WIN32) ::gmtime_s(&tm, &seconds); #else ::gmtime_r(&seconds, &tm); #endif return std::format("{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); } std::string host_name() { #if defined(_WIN32) char buffer[256]{}; DWORD size = static_cast(sizeof(buffer) - 1); if (::GetComputerNameA(buffer, &size) != 0) { return buffer; } return "localhost"; #else char buffer[256]{}; if (::gethostname(buffer, sizeof(buffer) - 1) == 0) { return buffer; } return "localhost"; #endif } } // namespace std::string render_cdash_test_xml(const CdashReportOptions &options) { const std::string site = options.site_name.empty() ? host_name() : options.site_name; const std::string timestamp = utc_timestamp(); std::string xml = "\n"; xml += std::format("\n", xml_escape(options.build_name), xml_escape(options.build_stamp), xml_escape(site), xml_escape(options.generator), xml_escape(options.os_name), xml_escape(site)); xml += " \n"; xml += " " + timestamp + "\n"; xml += " 0\n"; xml += " \n"; for (const auto &test : options.tests) { xml += std::format(" \n", test.passed ? "passed" : "failed"); xml += " " + xml_escape(test.name) + "\n"; xml += " .\n"; xml += " " + xml_escape(test.name) + "\n"; xml += " vbs-test\n"; xml += " .\n"; xml += " " + xml_escape(test.output) + "\n"; xml += " " + std::string{test.passed ? "passed" : "failed"} + "\n"; xml += std::format(" {:.3f}\n", test.elapsed_seconds); xml += " \n"; } xml += " \n"; xml += " " + timestamp + "\n"; xml += " 0\n"; xml += " 0\n"; xml += " \n"; xml += "\n"; return xml; } CdashSubmission build_cdash_submission(const CdashReportOptions &options, std::string_view project) { constexpr std::string_view boundary = "vbs-cdash-boundary"; const std::string xml = render_cdash_test_xml(options); CdashSubmission submission; submission.content_type = std::format("multipart/form-data; boundary={}", boundary); const auto add_field = [&](std::string_view name, std::string_view value) { submission.body += "--" + std::string{boundary} + "\r\n"; submission.body += "Content-Disposition: form-data; name=\"" + std::string{name} + "\"\r\n\r\n"; submission.body += value; submission.body += "\r\n"; }; add_field("type", "Test"); add_field("project", project); add_field("build", options.build_name); submission.body += "--" + std::string{boundary} + "\r\n"; submission.body += "Content-Disposition: form-data; name=\"content\"; filename=\"Test.xml\"\r\n"; submission.body += "Content-Type: text/xml\r\n\r\n"; submission.body += xml; submission.body += "\r\n--" + std::string{boundary} + "--\r\n"; return submission; } Void::Result::Result submit_cdash_report(std::string_view cdash_url, std::string_view project, const CdashReportOptions &options) { // Parse scheme://host[:port][/base]. std::string url{cdash_url}; while (!url.empty() && url.back() == '/') { url.pop_back(); } std::string scheme = "http"; if (const auto scheme_end = url.find("://"); scheme_end != std::string::npos) { scheme = url.substr(0, scheme_end); url.erase(0, scheme_end + 3); } if (scheme != "http" && scheme != "https") { return std::unexpected{make_error("CDash URL must be http:// or https://")}; } std::string host_port = url; std::string base_path; if (const auto slash = url.find('/'); slash != std::string::npos) { host_port = url.substr(0, slash); base_path = url.substr(slash); } std::string host = host_port; std::string port = scheme == "https" ? "443" : "80"; if (const auto colon = host_port.rfind(':'); colon != std::string::npos) { host = host_port.substr(0, colon); port = host_port.substr(colon + 1); } const auto submission = build_cdash_submission(options, project); const std::string path = base_path + "/submit.php?project=" + Void::Encoding::Url::encode(project); #if defined(VBS_HAS_VOID_NET) Void::Net::Request request; request.method = Void::Net::Method::Post; request.url = std::format("{}://{}:{}{}", scheme, host, port, path); request.headers.push_back(Void::Net::Header{"Content-Type", submission.content_type}); request.body = submission.body; request.timeout = std::chrono::seconds{30}; const Void::Net::HttpClient client = Void::Net::HttpClient::secure(); auto response = client.send(request); if (!response) { return std::unexpected{ make_error(std::format("CDash submission failed: {}", response.error().message()))}; } if (response->status < 200 || response->status >= 300) { return std::unexpected{ make_error(std::format("CDash submission rejected with HTTP {}: {}", response->status, response->body.substr(0, 200)))}; } return {}; #else #if defined(_WIN32) // Windows: curl performs the submission for http and https (Void::Net not linked). auto curl = Platform::resolve_executable("curl"); if (!curl) { return std::unexpected{make_error( "CDash submission on Windows requires curl (Void::Net not linked)")}; } Platform::ProcessRequest req; req.executable = *curl; req.arguments = {"--silent", "--show-error", "-X", "POST", "-H", "Content-Type: " + submission.content_type, "--data-binary", "@-", "-w", "\n%{http_code}", std::format("{}://{}:{}{}", scheme, host, port, path)}; req.stdin_data = submission.body; req.timeout = std::chrono::seconds{60}; auto proc = Platform::run_supervised(req); if (!proc || !proc->succeeded()) { return std::unexpected{make_error(std::format( "CDash submission failed: {}", proc ? proc->stderr_data : proc.error().message()))}; } int status = 0; if (const auto nl = proc->stdout_data.rfind('\n'); nl != std::string::npos) { (void)Void::Str::to_int(Void::Str::trim_ascii(proc->stdout_data.substr(nl + 1)), status); } if (status < 200 || status >= 300) { return std::unexpected{ make_error(std::format("CDash submission rejected with HTTP {}", status))}; } return {}; #else // Plain TCP HTTP/1.1 POST for http:// (Void::Net server transport not linked). if (scheme != "http") { return std::unexpected{ make_error("HTTPS CDash submission requires the Void::Net HTTP client")}; } struct addrinfo hints{}; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; struct addrinfo *resolved = nullptr; if (::getaddrinfo(host.c_str(), port.c_str(), &hints, &resolved) != 0 || resolved == nullptr) { return std::unexpected{ make_error(std::format("CDash submission: cannot resolve host '{}'", host))}; } auto free_ai = Void::Scope::on_exit([&] { ::freeaddrinfo(resolved); }); const int fd = ::socket(resolved->ai_family, resolved->ai_socktype | SOCK_CLOEXEC, resolved->ai_protocol); if (fd < 0) { return std::unexpected{make_error("CDash submission: socket() failed")}; } Void::System::OwnedFd guard{fd}; struct timeval tv{30, 0}; (void)::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); (void)::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); if (::connect(fd, resolved->ai_addr, resolved->ai_addrlen) != 0) { return std::unexpected{ make_error(std::format("CDash submission: connect to {}:{} failed", host, port))}; } std::string request = std::format("POST {} HTTP/1.1\r\nHost: {}:{}\r\nConnection: close\r\n" "Content-Type: {}\r\nContent-Length: {}\r\n\r\n", path, host, port, submission.content_type, submission.body.size()); request += submission.body; std::size_t sent = 0; while (sent < request.size()) { const ssize_t n = ::send(fd, request.data() + sent, request.size() - sent, MSG_NOSIGNAL); if (n < 0) { if (errno == EINTR) { continue; } return std::unexpected{make_error("CDash submission: send() failed")}; } sent += static_cast(n); } std::string raw; std::array buffer{}; for (;;) { const ssize_t n = ::recv(fd, buffer.data(), buffer.size(), 0); if (n > 0) { raw.append(buffer.data(), static_cast(n)); } else if (n == 0) { break; } else if (errno != EINTR) { return std::unexpected{make_error("CDash submission: recv() failed")}; } } int status = 0; if (const auto sp = raw.find(' '); sp != std::string::npos) { (void)Void::Str::to_int(std::string_view{raw}.substr(sp + 1, 3), status); } if (status < 200 || status >= 300) { return std::unexpected{ make_error(std::format("CDash submission rejected with HTTP {}", status))}; } return {}; #endif #endif } } // namespace Void::Build::Report