Add file size failsafe

This commit is contained in:
blankie 2022-09-02 01:32:54 +07:00
parent 496a5e646c
commit e94d76b676
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 13 additions and 2 deletions

15
main.go
View File

@ -20,6 +20,7 @@ import (
) )
const TIMEOUT_NS time.Duration = time.Duration(60_000_000_000) const TIMEOUT_NS time.Duration = time.Duration(60_000_000_000)
const MAX_FILE_SIZE int64 = 512 * 1024
var ( var (
hosts tofu.KnownHosts hosts tofu.KnownHosts
@ -158,23 +159,33 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
limitedBody := io.LimitedReader{
R: resp.Body,
N: MAX_FILE_SIZE,
}
defer resp.Body.Close() defer resp.Body.Close()
mime, _, _ := strings.Cut(resp.Meta, ";") mime, _, _ := strings.Cut(resp.Meta, ";")
if mime == "application/rss+xml" || mime == "application/rss" || if mime == "application/rss+xml" || mime == "application/rss" ||
mime == "application/atom+xml" || mime == "application/atom" || mime == "application/atom+xml" || mime == "application/atom" ||
mime == "application/xml" || mime == "text/xml" { mime == "application/xml" || mime == "text/xml" {
out, err := io.ReadAll(resp.Body) out, err := io.ReadAll(&limitedBody)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if limitedBody.N <= 0 {
log.Fatalf("up to %d bytes read from the body", MAX_FILE_SIZE)
}
fmt.Print(string(out)) fmt.Print(string(out))
} else if mime == "text/gemini" { } else if mime == "text/gemini" {
aw := AtomWriter{ aw := AtomWriter{
Title: "", Title: "",
Items: nil, Items: nil,
} }
gemini.ParseLines(resp.Body, aw.Handle) gemini.ParseLines(&limitedBody, aw.Handle)
if limitedBody.N <= 0 {
log.Fatalf("up to %d bytes read from the body", MAX_FILE_SIZE)
}
sort.Sort(ByTime(aw.Items)) sort.Sort(ByTime(aw.Items))
feed := Feed{ feed := Feed{
XMLNS: "http://www.w3.org/2005/Atom", XMLNS: "http://www.w3.org/2005/Atom",