fix: do not cache invalid html
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/felipemarinho97/torrent-indexer/cache"
|
"github.com/felipemarinho97/torrent-indexer/cache"
|
||||||
|
"github.com/felipemarinho97/torrent-indexer/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -77,8 +78,8 @@ func (i *Requster) GetDocument(ctx context.Context, url string) (io.ReadCloser,
|
|||||||
fmt.Printf("request served from plain client: %s\n", url)
|
fmt.Printf("request served from plain client: %s\n", url)
|
||||||
}
|
}
|
||||||
|
|
||||||
// save response to cache if it's not a challange and body is not empty
|
// save response to cache if it's not a challange, body is not empty and is valid HTML
|
||||||
if !hasChallange(bodyByte) && len(bodyByte) > 0 {
|
if !hasChallange(bodyByte) && len(bodyByte) > 0 && utils.IsValidHTML(string(bodyByte)) {
|
||||||
err = i.c.SetWithExpiration(ctx, key, bodyByte, i.shortLivedCacheExpiration)
|
err = i.c.SetWithExpiration(ctx, key, bodyByte, i.shortLivedCacheExpiration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to save response to cache: %v\n", err)
|
fmt.Printf("failed to save response to cache: %v\n", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user