Skip to content

Commit

Permalink
Fix 404 error on error pages when a resource is specified (#192)
Browse files Browse the repository at this point in the history
* Enhance localHandler to verify file presence in zip and fallback to index.html

* Improve NotFound error handling in localHandler
  • Loading branch information
thomas-senechal authored Dec 16, 2024
1 parent 79c4666 commit f5b9269
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 13 additions & 0 deletions server/int/api/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ func localHandler(w http.ResponseWriter, zipBytes []byte, resourceName string) {
resourceName = "index.html"
}

isPresent, err := zipper.VerifyFilePresence(zipBytes, resourceName)
if err != nil && !zipper.IsNotFoundError(err, resourceName) {
logger.Errorf("localHandler: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}

// If requested resource is not present, it might be the original requested resource.
// In this case, we try to serve the index.html file of the zip.
if !isPresent {
logger.Debugf("localHandler: Resource %s not found in zip, changing to index.html", resourceName)
resourceName = "index.html"
}

content, err := zipper.ReadFileFromZip(zipBytes, resourceName)
if err != nil {
logger.Warnf("File not found: %t", zipper.IsNotFoundError(err, resourceName))
Expand Down
13 changes: 7 additions & 6 deletions server/int/zipper/zipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ func IsValidZip(zipFile []byte) bool {
return err == nil
}

func VerifyFilePresence(filePath string, fileName string) (bool, error) {
reader, err := zip.OpenReader(filePath)
func VerifyFilePresence(zipFile []byte, fileName string) (bool, error) {
reader := bytes.NewReader(zipFile)

zipReader, err := zip.NewReader(reader, int64(reader.Len()))
if err != nil {
return false, fmt.Errorf("provided filepath is not a valid zip file: %w", err)
return false, fmt.Errorf("failed to initiate zip reader: %v", err)
}
defer reader.Close()

for _, file := range reader.File {
for _, file := range zipReader.File {
if file.Name == fileName {
return true, nil
}
}

return false, fmt.Errorf("%s not found in zip file", fileName)
return false, fmt.Errorf(notFoundErrorTemplate, fileName)
}

0 comments on commit f5b9269

Please sign in to comment.