From d01b901e99a191d93aab09031a311a288b0b0331 Mon Sep 17 00:00:00 2001 From: Willard Date: Fri, 25 Dec 2020 17:55:59 +0100 Subject: [PATCH] gzip data --- .github/workflows/ccpp.yml | 2 +- src/CMakeLists.txt | 2 +- src/main.cpp | 35 +++++++++++++++++++++++++---------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index 8092318..9c88e97 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -51,7 +51,7 @@ jobs: #setupOnly: true vcpkgDirectory: '${{ github.workspace }}/vcpkg' vcpkgTriplet: '${{ matrix.triplet }}' - vcpkgArguments: 'nlohmann-json boost-program-options boost-property-tree cpr' + vcpkgArguments: 'nlohmann-json boost-program-options boost-property-tree boost-iostreams cpr' vcpkgGitCommitId: 'acb6b10e7fdf5e8519c18398d0b069e1d58ca025' # Build with CMake and Ninja diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7e338b6..5d7e3fa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,7 +28,7 @@ find_package(cpr CONFIG REQUIRED) # Boost set(Boost_USE_STATIC_LIBS ON) -FIND_PACKAGE(Boost 1.69 COMPONENTS program_options REQUIRED) +FIND_PACKAGE(Boost 1.69 COMPONENTS program_options iostreams REQUIRED) #include the Intercept headers from the submodule set(INTERCEPT_CLIENT_PATH "${CMAKE_SOURCE_DIR}/intercept/src/client") diff --git a/src/main.cpp b/src/main.cpp index a7c4e43..3075d5e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,11 @@ #include #include +// Gzip +#include +#include +#include + #include #include #include @@ -26,6 +31,7 @@ static inline std::string GRAD_REPLAY_USER_AGENT = "grad_replay_intercept/" + namespace nl = nlohmann; namespace fs = std::filesystem; +namespace bi = boost::iostreams; using namespace intercept; using namespace grad::replay; @@ -183,21 +189,30 @@ game_value sendReplay(game_state& gs, SQFPar right_arg) { std::thread sendReplayThread([obj, now]() { try { - std::stringstream ss; - ss << obj; - - auto header = cpr::Header{ - {"Content-Type","application/json"}, - {"content-length", std::to_string(ss.str().size())}, - {"Connection", "close"}, - {"Authorization", std::string("Bearer ").append(token)}, - {"User-Agent", GRAD_REPLAY_USER_AGENT} + std::stringstream inputStream(obj.dump()); + + bi::filtering_istream fis; + fis.push(bi::gzip_compressor(bi::gzip_params(bi::gzip::best_compression))); + fis.push(inputStream); + + std::stringstream outputStream; + bi::copy(fis, outputStream); + + std::string outputString(outputStream.str()); + + auto header = cpr::Header { + { "Content-Type", "application/json" }, + { "Content-Encoding", "gzip" }, + { "Content-Length", std::to_string(outputString.size()) }, + { "Connection", "close" }, + { "Authorization", std::string("Bearer ").append(token) }, + { "User-Agent", GRAD_REPLAY_USER_AGENT } }; cpr::Response response = cpr::Post( cpr::Url{ url }, header, - cpr::Body(ss.str()), + cpr::Body(outputString), cpr::Timeout(60000) );