diff options
context:
space:
mode:
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Keep error pages out of the cache
-rw-r--r--source/cgit.c5
-rw-r--r--source/ui-shared.c6
-rwxr-xr-xtests/t0020-validate-cache.sh14
3 files changed, 22 insertions, 3 deletions
diff --git a/source/cgit.c b/source/cgit.c
index ac8922b..12981c2 100644
--- a/source/cgit.c
+++ b/source/cgit.c
@@ -82,9 +82,8 @@ static void isolate_git_environment(void)
*/
static NORETURN void die_routine(const char *msg, va_list params)
{
- // A page is rendered with stdout pointed at the cache file, so the
- // message would otherwise be written there instead of to the visitor.
- cache_abandon_fill();
+ // The error page abandons any cache fill in progress, so the message
+ // reaches the visitor rather than the cache file stdout points at.
cgit_vprint_error_page(400, "Bad request", msg, params);
exit(0);
}
diff --git a/source/ui-shared.c b/source/ui-shared.c
index 066c25d..05fa30b 100644
--- a/source/ui-shared.c
+++ b/source/ui-shared.c
@@ -10,6 +10,7 @@
#define USE_THE_REPOSITORY_VARIABLE
+#include "cache.h"
#include "cgit.h"
#include "html.h"
#include "shared.h"
@@ -1293,6 +1294,11 @@ void cgit_print_error_page(int code, const char *msg, const char *fmt, ...)
void cgit_vprint_error_page(int code, const char *msg, const char *fmt,
va_list ap)
{
+ // An error page describes a condition that may not outlast the slot
+ // holding it. A request naming a commit not yet pushed would cache its
+ // 404 under the never-expiring static ttl and keep serving it after
+ // the push, so the error is rendered straight to the visitor instead.
+ cache_abandon_fill();
ctx.page.status = code;
ctx.page.statusmsg = msg;
cgit_print_layout_start();
diff --git a/tests/t0020-validate-cache.sh b/tests/t0020-validate-cache.sh
index 0cf7433..feb6275 100755
--- a/tests/t0020-validate-cache.sh
+++ b/tests/t0020-validate-cache.sh
@@ -154,4 +154,18 @@ test_expect_success 'an ordinary key still fills a slot' '
test_line_count = 1 key.slots
'
+# An error page reports a condition the repository may grow out of, and a
+# request pinned to an object id would cache it under the never-expiring
+# static ttl. Asking for a commit that does not exist yet must therefore
+# leave nothing behind, or the real page could never appear once it does.
+test_expect_success 'an error page leaves no slot behind' '
+ rm -rf cache3 && mkdir cache3 &&
+ CGIT_CONFIG="$PWD/bigkeyrc" \
+ QUERY_STRING="url=bigpage/commit/&id=0123456789abcdef0123456789abcdef01234567" \
+ cgit >error.out &&
+ grep "Bad commit reference" error.out &&
+ ls cache3 >error.slots &&
+ test_line_count = 0 error.slots
+'
+
test_done