nginx: build with zlib-ng in compatibility mode (#399565)

authored by Pol Dellaiera and committed by GitHub 8c742e83 e518c266

+3 -283
-12
pkgs/servers/http/nginx/generic.nix
··· 220 220 ./nix-etag-1.15.4.patch 221 221 ./nix-skip-check-logs-path.patch 222 222 ] 223 - ++ 224 - lib.optionals 225 - (lib.elem pname [ 226 - "nginx" 227 - "nginxQuic" 228 - "tengine" 229 - ]) 230 - [ 231 - # https://github.com/NixOS/nixpkgs/issues/357522 232 - # https://github.com/zlib-ng/patches/blob/5a036c0a00120c75ee573b27f4f44ade80d82ff2/nginx/README.md 233 - ./nginx-zlib-ng.patch 234 - ] 235 223 ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [ 236 224 (fetchpatch { 237 225 url = "https://raw.githubusercontent.com/openwrt/packages/c057dfb09c7027287c7862afab965a4cd95293a3/net/nginx/patches/102-sizeof_test_fix.patch";
-271
pkgs/servers/http/nginx/nginx-zlib-ng.patch
··· 1 - --- a/auto/lib/zlib/conf 2 - +++ b/auto/lib/zlib/conf 3 - @@ -33,8 +33,8 @@ if [ $ZLIB != NONE ]; then 4 - 5 - *) 6 - have=NGX_ZLIB . auto/have 7 - - LINK_DEPS="$LINK_DEPS $ZLIB/libz.a" 8 - - CORE_LIBS="$CORE_LIBS $ZLIB/libz.a" 9 - + LINK_DEPS="$LINK_DEPS $ZLIB/libz-ng.a" 10 - + CORE_LIBS="$CORE_LIBS $ZLIB/libz-ng.a" 11 - #CORE_LIBS="$CORE_LIBS -L $ZLIB -lz" 12 - ;; 13 - 14 - @@ -50,10 +50,10 @@ else 15 - ngx_feature="zlib library" 16 - ngx_feature_name="NGX_ZLIB" 17 - ngx_feature_run=no 18 - - ngx_feature_incs="#include <zlib.h>" 19 - + ngx_feature_incs="#include <zlib-ng.h>" 20 - ngx_feature_path= 21 - - ngx_feature_libs="-lz" 22 - - ngx_feature_test="z_stream z; deflate(&z, Z_NO_FLUSH)" 23 - + ngx_feature_libs="-lz-ng" 24 - + ngx_feature_test="zng_stream z; zng_deflate(&z, Z_NO_FLUSH)" 25 - . auto/feature 26 - 27 - 28 - diff --git a/src/core/ngx_config.h b/src/core/ngx_config.h 29 - index 1861be6..bd427b8 100644 30 - --- a/src/core/ngx_config.h 31 - +++ b/src/core/ngx_config.h 32 - @@ -141,5 +141,9 @@ typedef intptr_t ngx_flag_t; 33 - 34 - #endif 35 - 36 - +/* Force enable ZLIB-NG */ 37 - +#ifndef NGX_ZLIB_NG 38 - +#define NGX_ZLIB_NG 1 39 - +#endif 40 - 41 - #endif /* _NGX_CONFIG_H_INCLUDED_ */ 42 - diff --git a/src/http/modules/ngx_http_gunzip_filter_module.c b/src/http/modules/ngx_http_gunzip_filter_module.c 43 - index 5d170a1..d8dcc96 100644 44 - --- a/src/http/modules/ngx_http_gunzip_filter_module.c 45 - +++ b/src/http/modules/ngx_http_gunzip_filter_module.c 46 - @@ -10,7 +10,14 @@ 47 - #include <ngx_core.h> 48 - #include <ngx_http.h> 49 - 50 - -#include <zlib.h> 51 - +#if defined(NGX_ZLIB_NG) 52 - +# include <zlib-ng.h> 53 - +# define ZPREFIX(x) zng_ ## x 54 - +# define z_stream zng_stream 55 - +#elif defined(NGX_ZLIB) 56 - +# include <zlib.h> 57 - +# define ZPREFIX(x) x 58 - +#endif 59 - 60 - 61 - typedef struct { 62 - @@ -312,7 +319,7 @@ ngx_http_gunzip_filter_inflate_start(ngx_http_request_t *r, 63 - ctx->zstream.opaque = ctx; 64 - 65 - /* windowBits +16 to decode gzip, zlib 1.2.0.4+ */ 66 - - rc = inflateInit2(&ctx->zstream, MAX_WBITS + 16); 67 - + rc = ZPREFIX(inflateInit2)(&ctx->zstream, MAX_WBITS + 16); 68 - 69 - if (rc != Z_OK) { 70 - ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 71 - @@ -435,7 +442,7 @@ ngx_http_gunzip_filter_inflate(ngx_http_request_t *r, 72 - ctx->zstream.avail_in, ctx->zstream.avail_out, 73 - ctx->flush, ctx->redo); 74 - 75 - - rc = inflate(&ctx->zstream, ctx->flush); 76 - + rc = ZPREFIX(inflate)(&ctx->zstream, ctx->flush); 77 - 78 - if (rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) { 79 - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 80 - @@ -533,7 +540,7 @@ ngx_http_gunzip_filter_inflate(ngx_http_request_t *r, 81 - 82 - if (rc == Z_STREAM_END && ctx->zstream.avail_in > 0) { 83 - 84 - - rc = inflateReset(&ctx->zstream); 85 - + rc = ZPREFIX(inflateReset)(&ctx->zstream); 86 - 87 - if (rc != Z_OK) { 88 - ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 89 - @@ -584,7 +591,7 @@ ngx_http_gunzip_filter_inflate_end(ngx_http_request_t *r, 90 - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 91 - "gunzip inflate end"); 92 - 93 - - rc = inflateEnd(&ctx->zstream); 94 - + rc = ZPREFIX(inflateEnd)(&ctx->zstream); 95 - 96 - if (rc != Z_OK) { 97 - ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 98 - diff --git a/src/http/modules/ngx_http_gzip_filter_module.c b/src/http/modules/ngx_http_gzip_filter_module.c 99 - index 7113df6..d6c2ea1 100644 100 - --- a/src/http/modules/ngx_http_gzip_filter_module.c 101 - +++ b/src/http/modules/ngx_http_gzip_filter_module.c 102 - @@ -9,7 +9,14 @@ 103 - #include <ngx_core.h> 104 - #include <ngx_http.h> 105 - 106 - -#include <zlib.h> 107 - +#if defined(NGX_ZLIB_NG) 108 - +# include <zlib-ng.h> 109 - +# define ZPREFIX(x) zng_ ## x 110 - +# define z_stream zng_stream 111 - +#elif defined(NGX_ZLIB) 112 - +# include <zlib.h> 113 - +# define ZPREFIX(x) x 114 - +#endif 115 - 116 - 117 - typedef struct { 118 - @@ -454,7 +461,7 @@ failed: 119 - ctx->done = 1; 120 - 121 - if (ctx->preallocated) { 122 - - deflateEnd(&ctx->zstream); 123 - + ZPREFIX(deflateEnd)(&ctx->zstream); 124 - 125 - ngx_pfree(r->pool, ctx->preallocated); 126 - } 127 - @@ -527,10 +534,20 @@ ngx_http_gzip_filter_memory(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx) 128 - wbits = ngx_max(wbits, 13); 129 - } 130 - 131 - - ctx->allocated = 8192 + 16 + (1 << (wbits + 2)) 132 - - + 131072 + (5 << (memlevel + 6)) 133 - - + 4 * (64 + sizeof(void*)); 134 - ctx->zlib_ng = 1; 135 - + ctx->allocated = 6144 // State 136 - + + 65536 // Window 137 - + + 65536 // Prev 138 - + + 131072 // Head 139 - + + 163840 // Pending 140 - + + 56 + 8 // Alloc struct + padding 141 - +#if (defined(__s390__) || defined(__s390x__) || defined(__zarch__)) 142 - + + 4096 // Required to fix allocation alignment 143 - +#else 144 - + + 64 // Required to fix allocation alignment 145 - +#endif 146 - + + 256; // Extra to allow for future changes 147 - + 148 - } 149 - } 150 - 151 - @@ -623,7 +640,7 @@ ngx_http_gzip_filter_deflate_start(ngx_http_request_t *r, 152 - ctx->zstream.zfree = ngx_http_gzip_filter_free; 153 - ctx->zstream.opaque = ctx; 154 - 155 - - rc = deflateInit2(&ctx->zstream, (int) conf->level, Z_DEFLATED, 156 - + rc = ZPREFIX(deflateInit2)(&ctx->zstream, (int) conf->level, Z_DEFLATED, 157 - ctx->wbits + 16, ctx->memlevel, Z_DEFAULT_STRATEGY); 158 - 159 - if (rc != Z_OK) { 160 - @@ -758,7 +775,7 @@ ngx_http_gzip_filter_deflate(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx) 161 - ctx->zstream.avail_in, ctx->zstream.avail_out, 162 - ctx->flush, ctx->redo); 163 - 164 - - rc = deflate(&ctx->zstream, ctx->flush); 165 - + rc = ZPREFIX(deflate)(&ctx->zstream, ctx->flush); 166 - 167 - if (rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) { 168 - ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 169 - @@ -882,7 +899,7 @@ ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r, 170 - ctx->zin = ctx->zstream.total_in; 171 - ctx->zout = ctx->zstream.total_out; 172 - 173 - - rc = deflateEnd(&ctx->zstream); 174 - + rc = ZPREFIX(deflateEnd)(&ctx->zstream); 175 - 176 - if (rc != Z_OK) { 177 - ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, 178 - diff --git a/src/http/modules/ngx_http_log_module.c b/src/http/modules/ngx_http_log_module.c 179 - index f7c4bd2..ad71d4d 100644 180 - --- a/src/http/modules/ngx_http_log_module.c 181 - +++ b/src/http/modules/ngx_http_log_module.c 182 - @@ -9,8 +9,13 @@ 183 - #include <ngx_core.h> 184 - #include <ngx_http.h> 185 - 186 - -#if (NGX_ZLIB) 187 - -#include <zlib.h> 188 - +#if defined(NGX_ZLIB_NG) 189 - +# include <zlib-ng.h> 190 - +# define ZPREFIX(x) zng_ ## x 191 - +# define z_stream zng_stream 192 - +#elif defined(NGX_ZLIB) 193 - +# include <zlib.h> 194 - +# define ZPREFIX(x) x 195 - #endif 196 - 197 - 198 - @@ -634,7 +639,7 @@ ngx_http_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level, 199 - zstream.next_out = out; 200 - zstream.avail_out = size; 201 - 202 - - rc = deflateInit2(&zstream, (int) level, Z_DEFLATED, wbits + 16, memlevel, 203 - + rc = ZPREFIX(deflateInit2)(&zstream, (int) level, Z_DEFLATED, wbits + 16, memlevel, 204 - Z_DEFAULT_STRATEGY); 205 - 206 - if (rc != Z_OK) { 207 - @@ -647,7 +652,7 @@ ngx_http_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level, 208 - zstream.next_in, zstream.next_out, 209 - zstream.avail_in, zstream.avail_out); 210 - 211 - - rc = deflate(&zstream, Z_FINISH); 212 - + rc = ZPREFIX(deflate)(&zstream, Z_FINISH); 213 - 214 - if (rc != Z_STREAM_END) { 215 - ngx_log_error(NGX_LOG_ALERT, log, 0, 216 - @@ -663,7 +668,7 @@ ngx_http_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level, 217 - 218 - size -= zstream.avail_out; 219 - 220 - - rc = deflateEnd(&zstream); 221 - + rc = ZPREFIX(deflateEnd)(&zstream); 222 - 223 - if (rc != Z_OK) { 224 - ngx_log_error(NGX_LOG_ALERT, log, 0, "deflateEnd() failed: %d", rc); 225 - diff --git a/src/stream/ngx_stream_log_module.c b/src/stream/ngx_stream_log_module.c 226 - index 0ff7f42..0b9d12c 100644 227 - --- a/src/stream/ngx_stream_log_module.c 228 - +++ b/src/stream/ngx_stream_log_module.c 229 - @@ -9,8 +9,13 @@ 230 - #include <ngx_core.h> 231 - #include <ngx_stream.h> 232 - 233 - -#if (NGX_ZLIB) 234 - -#include <zlib.h> 235 - +#if defined(NGX_ZLIB_NG) 236 - +# include <zlib-ng.h> 237 - +# define ZPREFIX(x) zng_ ## x 238 - +# define z_stream zng_stream 239 - +#elif defined(NGX_ZLIB) 240 - +# include <zlib.h> 241 - +# define ZPREFIX(x) x 242 - #endif 243 - 244 - 245 - @@ -525,7 +530,7 @@ ngx_stream_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level, 246 - zstream.next_out = out; 247 - zstream.avail_out = size; 248 - 249 - - rc = deflateInit2(&zstream, (int) level, Z_DEFLATED, wbits + 16, memlevel, 250 - + rc = ZPREFIX(deflateInit2)(&zstream, (int) level, Z_DEFLATED, wbits + 16, memlevel, 251 - Z_DEFAULT_STRATEGY); 252 - 253 - if (rc != Z_OK) { 254 - @@ -538,7 +543,7 @@ ngx_stream_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level, 255 - zstream.next_in, zstream.next_out, 256 - zstream.avail_in, zstream.avail_out); 257 - 258 - - rc = deflate(&zstream, Z_FINISH); 259 - + rc = ZPREFIX(deflate)(&zstream, Z_FINISH); 260 - 261 - if (rc != Z_STREAM_END) { 262 - ngx_log_error(NGX_LOG_ALERT, log, 0, 263 - @@ -554,7 +559,7 @@ ngx_stream_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level, 264 - 265 - size -= zstream.avail_out; 266 - 267 - - rc = deflateEnd(&zstream); 268 - + rc = ZPREFIX(deflateEnd)(&zstream); 269 - 270 - if (rc != Z_OK) { 271 - ngx_log_error(NGX_LOG_ALERT, log, 0, "deflateEnd() failed: %d", rc);
+3
pkgs/top-level/all-packages.nix
··· 10568 10568 nginx = nginxStable; 10569 10569 10570 10570 nginxQuic = callPackage ../servers/http/nginx/quic.nix { 10571 + zlib-ng = zlib-ng.override { withZlibCompat = true; }; 10571 10572 withPerl = false; 10572 10573 # We don't use `with` statement here on purpose! 10573 10574 # See https://github.com/NixOS/nixpkgs/pull/10474#discussion_r42369334 ··· 10581 10582 }; 10582 10583 10583 10584 nginxStable = callPackage ../servers/http/nginx/stable.nix { 10585 + zlib-ng = zlib-ng.override { withZlibCompat = true; }; 10584 10586 withPerl = false; 10585 10587 # We don't use `with` statement here on purpose! 10586 10588 # See https://github.com/NixOS/nixpkgs/pull/10474#discussion_r42369334 ··· 10592 10594 }; 10593 10595 10594 10596 nginxMainline = callPackage ../servers/http/nginx/mainline.nix { 10597 + zlib-ng = zlib-ng.override { withZlibCompat = true; }; 10595 10598 withKTLS = true; 10596 10599 withPerl = false; 10597 10600 # We don't use `with` statement here on purpose!