Skip to content

Commit

Permalink
revert from gzip
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperlarson committed Sep 11, 2024
1 parent 511acdf commit 3a5be80
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 41 deletions.
45 changes: 5 additions & 40 deletions include/forti_api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <cstdlib>
#include <stdexcept>
#include <regex>
#include <zlib.h>

inline static std::regex ipv4("(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])");
inline static std::regex ipv6("((([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}");
Expand Down Expand Up @@ -228,7 +227,6 @@ class FortiAPI {

struct curl_slist *headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Content-Encoding: gzip");
headers = curl_slist_append(headers, FortiAuth::get_auth_header().c_str());

curl_easy_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, 1L);
Expand All @@ -246,60 +244,27 @@ class FortiAPI {
curl_easy_setopt(curl, CURLOPT_SSLCERT, FortiAuth::get_ssl_cert_path().c_str());
curl_easy_setopt(curl, CURLOPT_KEYPASSWD, FortiAuth::get_cert_password().c_str());

std::string json_payload = convert_keys_to_hyphens(data).dump();

// Compress the payload if the method is POST, PUT, or DELETE
std::string compressed_payload;
if (method == "POST" || method == "PUT" || method == "DELETE") {
compressed_payload = compress_gzip(json_payload);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, compressed_payload.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, compressed_payload.size());
}
std::string json_payload = convert_keys_to_hyphens(data).dump(); // do not simplify by deleting this
if (method == "POST" || method == "PUT")
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_payload.c_str());

if (method != "POST" && method != "GET")
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());

#ifdef ENABLE_DEBUG
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_debug_callback);
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, nullptr);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_debug_callback);
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, nullptr);
#endif

res = curl_easy_perform(curl);
if (res != CURLE_OK) std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;

curl_easy_cleanup(curl);
}

return convert_keys_to_underscores(nlohmann::json::parse(readBuffer));
}

static std::string compress_gzip(const std::string &input) {
z_stream stream;
memset(&stream, 0, sizeof(stream));
if (deflateInit2(&stream, Z_BEST_COMPRESSION, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
throw std::runtime_error("Failed to initialize zlib for gzip compression");
}

stream.next_in = (Bytef*)input.data();
stream.avail_in = input.size();

std::string compressed;
char buffer[4096];

do {
stream.next_out = reinterpret_cast<Bytef*>(buffer);
stream.avail_out = sizeof(buffer);

deflate(&stream, Z_FINISH);
compressed.append(buffer, sizeof(buffer) - stream.avail_out);
} while (stream.avail_out == 0);

deflateEnd(&stream);
return compressed;
}


static Response validate(const std::string &method, const std::string &path, const nlohmann::json &data = {}) {
auto response = request<Response>(method, path, data);
if (response.status != "success") std::cerr << nlohmann::json(response).dump(4) << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ foreach cpp_file : run_command('find', source_root + '/tests', '-type', 'f', '-n
endforeach

if get_option('buildtype') == 'debug'
add_project_arguments('-DENABLE_DEBUG', language: 'cpp')
# add_project_arguments('-DENABLE_DEBUG', language: 'cpp')

test('runTests', executable('runTests', test_sources, dependencies: test_deps))

Expand Down

0 comments on commit 3a5be80

Please sign in to comment.