Skip to content

Commit 08814b9

Browse files
committed
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 <[email protected]>
1 parent c4212df commit 08814b9

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

common/etc/nginx/include/listing.xsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
</xsl:template>
1717

18+
<!-- When FOUR_O_FOUR_ON_EMPTY_BUCKET is disabled (the default setting),
19+
the following template will be executed when the bucket is empty. -->
1820
<xsl:template name="no_contents">
1921
<html>
20-
<head><title>Not Found</title></head>
22+
<head><title>No Files Available for Listing</title></head>
2123
<body>
22-
<h1>Not Found</h1>
24+
<h1>No Files Available for Listing</h1>
2325
</body>
2426
</html>
2527
</xsl:template>

common/etc/nginx/include/s3gateway.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const DEBUG = _parseBoolean(process.env['S3_DEBUG']);
3434
const ALLOW_LISTING = _parseBoolean(process.env['ALLOW_DIRECTORY_LIST']);
3535
const PROVIDE_INDEX_PAGE = _parseBoolean(process.env['PROVIDE_INDEX_PAGE']);
3636
const APPEND_SLASH = _parseBoolean(process.env['APPEND_SLASH_FOR_POSSIBLE_DIRECTORY']);
37-
37+
const FOUR_O_FOUR_ON_EMPTY_BUCKET = _parseBoolean(process.env['FOUR_O_FOUR_ON_EMPTY_BUCKET']);
3838
const S3_STYLE = process.env['S3_STYLE'];
3939

4040
const ADDITIONAL_HEADER_PREFIXES_TO_STRIP = _parseArray(process.env['HEADER_PREFIXES_TO_STRIP']);
@@ -501,9 +501,10 @@ function signatureV2(r, bucket, credentials) {
501501
}
502502

503503
/**
504-
* Processes the directory listing output as returned from S3 and corrupts the
505-
* XML output by inserting 'junk' into causing nginx to return a 404 for empty
506-
* directory listings.
504+
* Processes the directory listing output as returned from S3. If
505+
* FOUR_O_FOUR_ON_EMPTY_BUCKET is enabled, this function will corrupt the
506+
* XML output by inserting the string 'junk' into the output thereby causing
507+
* nginx to return a 404 for empty directory listings.
507508
*
508509
* If anyone finds a better way to do this, please submit a PR.
509510
*
@@ -512,20 +513,24 @@ function signatureV2(r, bucket, credentials) {
512513
* @param flags contains field that indicates that a chunk is last
513514
*/
514515
function filterListResponse(r, data, flags) {
515-
let indexIsEmpty = _parseBoolean(r.variables.indexIsEmpty);
516+
if (FOUR_O_FOUR_ON_EMPTY_BUCKET) {
517+
let indexIsEmpty = _parseBoolean(r.variables.indexIsEmpty);
516518

517-
if (indexIsEmpty && data.indexOf('<Contents') >= 0) {
518-
r.variables.indexIsEmpty = false;
519-
indexIsEmpty = false;
520-
}
519+
if (indexIsEmpty && data.indexOf('<Contents') >= 0) {
520+
r.variables.indexIsEmpty = false;
521+
indexIsEmpty = false;
522+
}
521523

522-
if (indexIsEmpty && data.indexOf('<CommonPrefixes') >= 0) {
523-
r.variables.indexIsEmpty = false;
524-
indexIsEmpty = false;
525-
}
524+
if (indexIsEmpty && data.indexOf('<CommonPrefixes') >= 0) {
525+
r.variables.indexIsEmpty = false;
526+
indexIsEmpty = false;
527+
}
526528

527-
if (flags.last && indexIsEmpty) {
528-
r.sendBuffer('junk', flags);
529+
if (flags.last && indexIsEmpty) {
530+
r.sendBuffer('junk', flags);
531+
} else {
532+
r.sendBuffer(data, flags);
533+
}
529534
} else {
530535
r.sendBuffer(data, flags);
531536
}

common/etc/nginx/nginx.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ env PROXY_CACHE_VALID_OK;
2626
env PROXY_CACHE_VALID_NOTFOUND;
2727
env PROXY_CACHE_VALID_FORBIDDEN;
2828
env HEADER_PREFIXES_TO_STRIP;
29+
env FOUR_O_FOUR_ON_EMPTY_BUCKET;
2930

3031
events {
3132
worker_connections 1024;

0 commit comments

Comments
 (0)