From 989d00832fbceaf2bf18da3a91c4df3acd042fce Mon Sep 17 00:00:00 2001
From: Moritz Marquardt <git@momar.de>
Date: Thu, 2 Dec 2021 19:11:13 +0100
Subject: [PATCH] Fix (half) empty cache issue

---
 handler.go | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/handler.go b/handler.go
index 44865ac..675c96a 100644
--- a/handler.go
+++ b/handler.go
@@ -418,7 +418,7 @@ func upstream(ctx *fasthttp.RequestCtx, targetOwner string, targetRepo string, t
 	var res *fasthttp.Response
 	var cachedResponse fileResponse
 	var err error
-	if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + strconv.FormatInt(options.BranchTimestamp.Unix(), 10)); ok {
+	if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + strconv.FormatInt(options.BranchTimestamp.Unix(), 10)); ok && len(cachedValue.(fileResponse).body) > 0 {
 		cachedResponse = cachedValue.(fileResponse)
 	} else {
 		req = fasthttp.AcquireRequest()
@@ -504,7 +504,7 @@ func upstream(ctx *fasthttp.RequestCtx, targetOwner string, targetRepo string, t
 	}
 	s.Step("response")
 
-	if res != nil {
+	if res != nil && ctx.Err() == nil {
 		cachedResponse.exists = true
 		cachedResponse.mimeType = mimeType
 		cachedResponse.body = cacheBodyWriter.Bytes()