From 961fd91323271704b0cd503bc1efe633f3b07442 Mon Sep 17 00:00:00 2001
From: Eugen <97041180+eugen15@users.noreply.github.com>
Date: Tue, 18 Apr 2023 15:58:12 -0400
Subject: [PATCH 01/10] Upload brief crash info (Windows only)
---
crash-handler-process/CMakeLists.txt | 6 +-
.../brief-crash-info-uploader.cpp | 118 +++
.../brief-crash-info-uploader.hpp | 41 +
.../crash-handler-process.exe.txt | Bin 866 -> 948 bytes
crash-handler-process/http-helper.hpp | 64 ++
crash-handler-process/main.cpp | 24 +-
.../platforms/http-helper-win.cpp | 186 +++++
.../platforms/http-helper-win.hpp | 45 ++
crash-handler-process/platforms/httprequest.h | 700 ++++++++++++++++++
.../platforms/httprequest_i.c | 88 +++
10 files changed, 1269 insertions(+), 3 deletions(-)
create mode 100644 crash-handler-process/brief-crash-info-uploader.cpp
create mode 100644 crash-handler-process/brief-crash-info-uploader.hpp
create mode 100644 crash-handler-process/http-helper.hpp
create mode 100644 crash-handler-process/platforms/http-helper-win.cpp
create mode 100644 crash-handler-process/platforms/http-helper-win.hpp
create mode 100644 crash-handler-process/platforms/httprequest.h
create mode 100644 crash-handler-process/platforms/httprequest_i.c
diff --git a/crash-handler-process/CMakeLists.txt b/crash-handler-process/CMakeLists.txt
index 3ff8461..92e7e1b 100644
--- a/crash-handler-process/CMakeLists.txt
+++ b/crash-handler-process/CMakeLists.txt
@@ -38,7 +38,9 @@ ENDIF()
SET(PROJECT_SOURCE
"${PROJECT_SOURCE_DIR}/process.hpp"
"${PROJECT_SOURCE_DIR}/process-manager.cpp" "${PROJECT_SOURCE_DIR}/process-manager.hpp"
+ "${PROJECT_SOURCE_DIR}/brief-crash-info-uploader.cpp" "${PROJECT_SOURCE_DIR}/brief-crash-info-uploader.hpp"
"${PROJECT_SOURCE_DIR}/message.cpp" "${PROJECT_SOURCE_DIR}/message.hpp"
+ "${PROJECT_SOURCE_DIR}/http-helper.hpp"
"${PROJECT_SOURCE_DIR}/socket.hpp"
"${PROJECT_SOURCE_DIR}/logger.cpp" "${PROJECT_SOURCE_DIR}/logger.hpp"
"${PROJECT_SOURCE_DIR}/main.cpp"
@@ -50,7 +52,9 @@ IF(WIN32)
"${PROJECT_SOURCE_DIR}/platforms/util-win.cpp"
"${PROJECT_SOURCE_DIR}/platforms/socket-win.cpp" "${PROJECT_SOURCE_DIR}/platforms/socket-win.hpp"
"${PROJECT_SOURCE_DIR}/platforms/process-win.cpp" "${PROJECT_SOURCE_DIR}/platforms/process-win.hpp"
- "${PROJECT_SOURCE_DIR}/platforms/upload-window-win.cpp" "${PROJECT_SOURCE_DIR}/platforms/upload-window-win.hpp"
+ "${PROJECT_SOURCE_DIR}/platforms/upload-window-win.cpp" "${PROJECT_SOURCE_DIR}/platforms/upload-window-win.hpp"
+ "${PROJECT_SOURCE_DIR}/platforms/httprequest_i.c" "${PROJECT_SOURCE_DIR}/platforms/httprequest.h"
+ "${PROJECT_SOURCE_DIR}/platforms/http-helper-win.cpp" "${PROJECT_SOURCE_DIR}/platforms/http-helper-win.hpp"
"${PROJECT_SOURCE_DIR}/minizip/zip.c" "${PROJECT_SOURCE_DIR}/minizip/zip.h"
"${PROJECT_SOURCE_DIR}/minizip/ioapi.c" "${PROJECT_SOURCE_DIR}/minizip/ioapi.h"
"${PROJECT_SOURCE_DIR}/minizip/iowin32.c" "${PROJECT_SOURCE_DIR}/minizip/iowin32.h"
diff --git a/crash-handler-process/brief-crash-info-uploader.cpp b/crash-handler-process/brief-crash-info-uploader.cpp
new file mode 100644
index 0000000..6e14bcf
--- /dev/null
+++ b/crash-handler-process/brief-crash-info-uploader.cpp
@@ -0,0 +1,118 @@
+/******************************************************************************
+ Copyright (C) 2016-2020 by Streamlabs (General Workings Inc)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+
+******************************************************************************/
+
+#include
+#include
+#include
+#include
+#include
+
+#include "logger.hpp"
+#include "util.hpp"
+
+#include "http-helper.hpp"
+#include "brief-crash-info-uploader.hpp"
+
+#if defined(_WIN32)
+std::string_view g_pathSeparator("\\");
+#else
+std::string_view g_pathSeparator("/");
+#endif
+
+std::string_view g_briefCrashDataFilename("brief-crash-info.json");
+
+BriefCrashInfoUploader::BriefCrashInfoUploader(const std::string& appDataPath)
+ : m_filename(appDataPath)
+{
+ if (*m_filename.rbegin() != '/' && *m_filename.rbegin() != '\\') {
+ m_filename += g_pathSeparator;
+ }
+ m_filename += g_briefCrashDataFilename;
+}
+
+BriefCrashInfoUploader::~BriefCrashInfoUploader()
+{
+}
+
+void BriefCrashInfoUploader::Run(std::int64_t maxFileWaitTimeMs)
+{
+ std::string json = ProcessBriefCrashInfoJson(maxFileWaitTimeMs);
+ if (json.size()) {
+ UploadJson(json);
+ }
+}
+
+std::string BriefCrashInfoUploader::ProcessBriefCrashInfoJson(std::int64_t maxFileWaitTimeMs)
+{
+ log_info << "Waiting for brief crash info file: " << m_filename << std::endl;
+ std::string json;
+ std::filesystem::path briefCrashInfoPath = std::filesystem::u8path(m_filename);
+ std::int64_t realFileWaitTimeMs = 0;
+ while (realFileWaitTimeMs < maxFileWaitTimeMs) {
+ if (std::filesystem::exists(briefCrashInfoPath) && std::filesystem::is_regular_file(briefCrashInfoPath)) {
+ json = LoadBriefCrashInfoJson();
+ std::filesystem::remove(briefCrashInfoPath);
+ log_info << "The brief crash info file has been loaded." << std::endl;
+ break;
+ }
+ // Obviously the sleep can take more than 100ms but not much so we ignore the difference
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ realFileWaitTimeMs += 100;
+ }
+ log_info << "Waited for the brief crash info file (ms): " << realFileWaitTimeMs << std::endl;
+ return json;
+}
+
+std::string BriefCrashInfoUploader::LoadBriefCrashInfoJson()
+{
+#if defined(_WIN32)
+ std::wstring_convert> converter;
+ std::ifstream file(converter.from_bytes(m_filename));
+#else
+ std::ifstream file(m_filename);
+#endif
+ file.seekg(0, std::ios::end);
+ size_t fileSize = file.tellg();
+
+ std::string buffer(fileSize, ' ');
+ file.seekg(0);
+ file.read(&buffer.front(), fileSize);
+ file.close();
+
+ return buffer;
+}
+
+void BriefCrashInfoUploader::UploadJson(const std::string& json)
+{
+ log_info << "Uploading the brief crash info..." << std::endl;
+
+ HttpHelper::Headers requestHeaders = {{"Content-Type", "application/json; Charset=UTF-8"}};
+ HttpHelper::Headers responseHeaders;
+ std::string response;
+
+ auto httpHelper = HttpHelper::Create();
+ HttpHelper::Result result = httpHelper->PostRequest("https://httpbin.org/post",
+ requestHeaders, json, &responseHeaders, &response);
+
+ log_info << "Brief crash info upload result (0 means SUCCESS): " << static_cast(result) << std::endl;
+ log_info << "Brief crash info upload response: " << response << std::endl;
+
+ for (const auto& [header, value] : responseHeaders) {
+ log_info << "Brief crash info upload response header: " << header << " = " << value << std::endl;
+ }
+}
\ No newline at end of file
diff --git a/crash-handler-process/brief-crash-info-uploader.hpp b/crash-handler-process/brief-crash-info-uploader.hpp
new file mode 100644
index 0000000..d2780ef
--- /dev/null
+++ b/crash-handler-process/brief-crash-info-uploader.hpp
@@ -0,0 +1,41 @@
+/******************************************************************************
+ Copyright (C) 2016-2020 by Streamlabs (General Workings Inc)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+
+******************************************************************************/
+
+#pragma once
+
+#include
+#include
+#include
+
+class BriefCrashInfoUploader final
+{
+public:
+
+ BriefCrashInfoUploader(const std::string& appDataPath);
+ ~BriefCrashInfoUploader();
+
+ void Run(std::int64_t maxFileWaitTimeMs = 10000);
+
+private:
+ std::string ProcessBriefCrashInfoJson(std::int64_t maxFileWaitTimeMs);
+ std::string LoadBriefCrashInfoJson();
+ void UploadJson(const std::string& json);
+
+ std::string m_filename;
+
+};
\ No newline at end of file
diff --git a/crash-handler-process/dependencies/crash-handler-process.exe.txt b/crash-handler-process/dependencies/crash-handler-process.exe.txt
index a5603d0df7e4e67fc58fd34dacdf0b9d0dad904a..ca0cdfeb3aa98a013e897c6dc29535dc45a100f5 100644
GIT binary patch
delta 40
ucmaFFwuOCz1@puO9IW{aISi?j=QAmC`!o13xH330gffHx`I3_@n6&`#MhbiY
delta 11
ScmdnO{)laZ1@q)CW-S03cmvr0
diff --git a/crash-handler-process/http-helper.hpp b/crash-handler-process/http-helper.hpp
new file mode 100644
index 0000000..6adde66
--- /dev/null
+++ b/crash-handler-process/http-helper.hpp
@@ -0,0 +1,64 @@
+/******************************************************************************
+ Copyright (C) 2016-2020 by Streamlabs (General Workings Inc)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+
+******************************************************************************/
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include