Skip to content

Commit

Permalink
Fixes nginxinc#68
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dekobon committed Dec 15, 2022
1 parent c4212df commit 08814b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
6 changes: 4 additions & 2 deletions common/etc/nginx/include/listing.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

</xsl:template>

<!-- When FOUR_O_FOUR_ON_EMPTY_BUCKET is disabled (the default setting),
the following template will be executed when the bucket is empty. -->
<xsl:template name="no_contents">
<html>
<head><title>Not Found</title></head>
<head><title>No Files Available for Listing</title></head>
<body>
<h1>Not Found</h1>
<h1>No Files Available for Listing</h1>
</body>
</html>
</xsl:template>
Expand Down
35 changes: 20 additions & 15 deletions common/etc/nginx/include/s3gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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.
*
Expand All @@ -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('<Contents') >= 0) {
r.variables.indexIsEmpty = false;
indexIsEmpty = false;
}
if (indexIsEmpty && data.indexOf('<Contents') >= 0) {
r.variables.indexIsEmpty = false;
indexIsEmpty = false;
}

if (indexIsEmpty && data.indexOf('<CommonPrefixes') >= 0) {
r.variables.indexIsEmpty = false;
indexIsEmpty = false;
}
if (indexIsEmpty && data.indexOf('<CommonPrefixes') >= 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);
}
Expand Down
1 change: 1 addition & 0 deletions common/etc/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 08814b9

Please sign in to comment.