From 08814b9b05d96a87c1d21aa9c90d609ea857d3af Mon Sep 17 00:00:00 2001 From: Elijah Zupancic Date: Wed, 14 Dec 2022 16:45:12 -0800 Subject: [PATCH] Fixes #68 This commit changes the behavior of the gateway so that by default when a listing is requested on an empty bucket a message saying no files are available is displayed. For the previous behavior (which was returning a 404), it can be enabled by setting the the following environment variable: FOUR_O_FOUR_ON_EMPTY_BUCKET=true Signed-off-by: Elijah Zupancic --- common/etc/nginx/include/listing.xsl | 6 +++-- common/etc/nginx/include/s3gateway.js | 35 +++++++++++++++------------ common/etc/nginx/nginx.conf | 1 + 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/common/etc/nginx/include/listing.xsl b/common/etc/nginx/include/listing.xsl index 34f95695..62056ad4 100644 --- a/common/etc/nginx/include/listing.xsl +++ b/common/etc/nginx/include/listing.xsl @@ -15,11 +15,13 @@ + - Not Found + No Files Available for Listing -

Not Found

+

No Files Available for Listing

diff --git a/common/etc/nginx/include/s3gateway.js b/common/etc/nginx/include/s3gateway.js index 9feea15b..a6e68fb5 100644 --- a/common/etc/nginx/include/s3gateway.js +++ b/common/etc/nginx/include/s3gateway.js @@ -34,7 +34,7 @@ const DEBUG = _parseBoolean(process.env['S3_DEBUG']); const ALLOW_LISTING = _parseBoolean(process.env['ALLOW_DIRECTORY_LIST']); const PROVIDE_INDEX_PAGE = _parseBoolean(process.env['PROVIDE_INDEX_PAGE']); const APPEND_SLASH = _parseBoolean(process.env['APPEND_SLASH_FOR_POSSIBLE_DIRECTORY']); - +const FOUR_O_FOUR_ON_EMPTY_BUCKET = _parseBoolean(process.env['FOUR_O_FOUR_ON_EMPTY_BUCKET']); const S3_STYLE = process.env['S3_STYLE']; const ADDITIONAL_HEADER_PREFIXES_TO_STRIP = _parseArray(process.env['HEADER_PREFIXES_TO_STRIP']); @@ -501,9 +501,10 @@ function signatureV2(r, bucket, credentials) { } /** - * Processes the directory listing output as returned from S3 and corrupts the - * XML output by inserting 'junk' into causing nginx to return a 404 for empty - * directory listings. + * Processes the directory listing output as returned from S3. If + * FOUR_O_FOUR_ON_EMPTY_BUCKET is enabled, this function will corrupt the + * XML output by inserting the string 'junk' into the output thereby causing + * nginx to return a 404 for empty directory listings. * * If anyone finds a better way to do this, please submit a PR. * @@ -512,20 +513,24 @@ function signatureV2(r, bucket, credentials) { * @param flags contains field that indicates that a chunk is last */ function filterListResponse(r, data, flags) { - let indexIsEmpty = _parseBoolean(r.variables.indexIsEmpty); + if (FOUR_O_FOUR_ON_EMPTY_BUCKET) { + let indexIsEmpty = _parseBoolean(r.variables.indexIsEmpty); - if (indexIsEmpty && data.indexOf('= 0) { - r.variables.indexIsEmpty = false; - indexIsEmpty = false; - } + if (indexIsEmpty && data.indexOf('= 0) { + r.variables.indexIsEmpty = false; + indexIsEmpty = false; + } - if (indexIsEmpty && data.indexOf('= 0) { - r.variables.indexIsEmpty = false; - indexIsEmpty = false; - } + if (indexIsEmpty && data.indexOf('= 0) { + r.variables.indexIsEmpty = false; + indexIsEmpty = false; + } - if (flags.last && indexIsEmpty) { - r.sendBuffer('junk', flags); + if (flags.last && indexIsEmpty) { + r.sendBuffer('junk', flags); + } else { + r.sendBuffer(data, flags); + } } else { r.sendBuffer(data, flags); } diff --git a/common/etc/nginx/nginx.conf b/common/etc/nginx/nginx.conf index ed3058d5..e53f124d 100644 --- a/common/etc/nginx/nginx.conf +++ b/common/etc/nginx/nginx.conf @@ -26,6 +26,7 @@ env PROXY_CACHE_VALID_OK; env PROXY_CACHE_VALID_NOTFOUND; env PROXY_CACHE_VALID_FORBIDDEN; env HEADER_PREFIXES_TO_STRIP; +env FOUR_O_FOUR_ON_EMPTY_BUCKET; events { worker_connections 1024;