99#include < cstdint>
1010#include < cstring>
1111#include " App.h"
12+
13+ #ifndef UWS_NO_ZLIB
1214#include < zlib.h>
15+ #endif
1316
1417#if defined(__linux__)
1518#include < sys/inotify.h>
@@ -39,7 +42,7 @@ int inotify_fd;
3942#endif
4043unsigned long fileSizes = 0 ;
4144
42- // Loads file content and compresses it with zlib if beneficial
45+ // Loads file content; compresses with zlib if UWS_NO_ZLIB is not defined and compression is beneficial
4346std::pair<std::string, bool > load_file_content (const std::filesystem::path& path) {
4447 std::ifstream file (path, std::ios::binary);
4548 if (!file) {
@@ -52,31 +55,29 @@ std::pair<std::string, bool> load_file_content(const std::filesystem::path& path
5255 std::string content (size, 0 );
5356 file.read (&content[0 ], size);
5457
58+ #ifndef UWS_NO_ZLIB
5559 z_stream zs = {};
56- if (deflateInit2 (&zs, Z_BEST_COMPRESSION, Z_DEFLATED, 15 + 16 , 8 , Z_DEFAULT_STRATEGY) != Z_OK) {
57- std::cerr << " Failed to initialize zlib for " << path << std::endl;
58- return {" " , false };
59- }
60- zs.next_in = reinterpret_cast <Bytef*>(content.data ());
61- zs.avail_in = content.size ();
62- size_t bound = deflateBound (&zs, content.size ());
63- std::string compressed (bound, 0 );
64- zs.next_out = reinterpret_cast <Bytef*>(&compressed[0 ]);
65- zs.avail_out = bound;
66- int ret = deflate (&zs, Z_FINISH);
67- if (ret != Z_STREAM_END) {
68- deflateEnd (&zs);
69- std::cerr << " Failed to compress " << path << std::endl;
70- return {" " , false };
71- }
72- size_t compressed_size = zs.total_out ;
73- deflateEnd (&zs);
74- compressed.resize (compressed_size);
75-
76- if (compressed_size < size) {
77- fileSizes += compressed_size;
78- return {compressed, true };
60+ if (deflateInit2 (&zs, Z_BEST_COMPRESSION, Z_DEFLATED, 15 + 16 , 8 , Z_DEFAULT_STRATEGY) == Z_OK) {
61+ zs.next_in = reinterpret_cast <Bytef*>(content.data ());
62+ zs.avail_in = content.size ();
63+ size_t bound = deflateBound (&zs, content.size ());
64+ std::string compressed (bound, 0 );
65+ zs.next_out = reinterpret_cast <Bytef*>(&compressed[0 ]);
66+ zs.avail_out = bound;
67+ int ret = deflate (&zs, Z_FINISH);
68+ if (ret == Z_STREAM_END) {
69+ size_t compressed_size = zs.total_out ;
70+ deflateEnd (&zs);
71+ compressed.resize (compressed_size);
72+ if (compressed_size < size) {
73+ fileSizes += compressed_size;
74+ return {compressed, true };
75+ }
76+ } else {
77+ deflateEnd (&zs);
78+ }
7979 }
80+ #endif
8081 fileSizes += size;
8182 return {content, false };
8283}
@@ -158,7 +159,7 @@ int main(int argc, char** argv) {
158159 // Static key for uWS handlers
159160 static char handler_key;
160161
161- // Add post and pre handlers to lock the mutex around event loop iterations (unchanged as requested)
162+ // Add post and pre handlers to lock the mutex around event loop iterations
162163 uWS::Loop::get ()->addPostHandler (&handler_key, [](uWS::Loop* /* loop*/ ) {
163164 std::lock_guard<std::mutex> lock (map_mutex);
164165 });
0 commit comments