feat: add shared size limit constants

This commit is contained in:
2026-05-18 15:08:26 +02:00
parent 56c8618f5f
commit 943285b356
6 changed files with 23 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ import (
"fmt"
"io"
"mal/internal/domain"
"mal/pkg/net/limits"
"mal/pkg/net/useragent"
"mal/pkg/net/utls"
"net/http"
@@ -279,7 +280,7 @@ func (c *AllAnimeProvider) graphqlRequest(ctx context.Context, query string, var
}
defer func() { _ = resp.Body.Close() }()
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1024))
respBody, err := io.ReadAll(io.LimitReader(resp.Body, limits.MiB2))
if err != nil {
return nil, fmt.Errorf("read graphql response: %w", err)
}
@@ -334,7 +335,7 @@ func (c *AllAnimeProvider) graphqlRequestWithHash(ctx context.Context, showID, e
}
defer func() { _ = resp.Body.Close() }()
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1022))
respBody, err := io.ReadAll(io.LimitReader(resp.Body, limits.MiB2))
if err != nil {
return nil, fmt.Errorf("read response: %w", err)
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"mal/pkg/net/limits"
"net/http"
"regexp"
"strconv"
@@ -53,7 +54,7 @@ func (e *providerExtractor) ExtractVideoLinks(ctx context.Context, providerPath
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1024)) // 2MB limit
body, err := io.ReadAll(io.LimitReader(resp.Body, limits.MiB2)) // 2MB limit
if err != nil {
return nil, fmt.Errorf("read provider response: %w", err)
}
@@ -157,7 +158,7 @@ func (e *providerExtractor) parseM3U8(ctx context.Context, masterURL string, ref
}
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(io.LimitReader(resp.Body, 512*1024)) // 512KB limit
body, err := io.ReadAll(io.LimitReader(resp.Body, limits.KiB512)) // 512KB limit
if err != nil {
return nil, err
}

View File

@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"mal/pkg/net/limits"
"mal/pkg/net/useragent"
"net/http"
"regexp"
@@ -109,7 +110,7 @@ func fetchDocument(ctx context.Context, httpClient *http.Client, url string) (*g
if response.StatusCode != http.StatusOK {
// limit body read for error context; avoid reading large error pages
body, _ := io.ReadAll(io.LimitReader(response.Body, 512))
body, _ := io.ReadAll(io.LimitReader(response.Body, limits.Bytes512))
return nil, &HTTPStatusError{
StatusCode: response.StatusCode,
URL: url,
@@ -240,7 +241,7 @@ func fetchProxyText(ctx context.Context, httpClient *http.Client, url string) (s
return "", fmt.Errorf("proxy status %d", response.StatusCode)
}
body, err := io.ReadAll(io.LimitReader(response.Body, 2*1024*1024))
body, err := io.ReadAll(io.LimitReader(response.Body, limits.MiB2))
if err != nil {
return "", fmt.Errorf("failed to read proxy response: %w", err)
}

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"io"
"mal/internal/domain"
"mal/pkg/net/limits"
"mal/pkg/net/proxytransport"
"mal/pkg/net/useragent"
"maps"
@@ -321,7 +322,7 @@ func (h *PlaybackHandler) HandleProxySubtitle(c *gin.Context) {
}
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1024))
body, err := io.ReadAll(io.LimitReader(resp.Body, limits.MiB2))
if err != nil {
c.Status(http.StatusBadGateway)
return

View File

@@ -12,6 +12,7 @@ import (
"mal/integrations/jikan"
"mal/internal/db"
"mal/internal/domain"
"mal/pkg/net/limits"
"mal/pkg/net/useragent"
"net/http"
"net/url"
@@ -372,7 +373,7 @@ func (s *playbackService) fetchSkipSegments(ctx context.Context, malID int, epis
return []SkipSegment{}
}
body, err := io.ReadAll(io.LimitReader(resp.Body, 512*1024))
body, err := io.ReadAll(io.LimitReader(resp.Body, limits.KiB512))
if err != nil {
return []SkipSegment{}
}

10
pkg/net/limits/limits.go Normal file
View File

@@ -0,0 +1,10 @@
package limits
// Common size limits used when reading upstream responses.
const (
Bytes512 = 512
KiB512 int64 = 512 << 10
MiB2 int64 = 2 << 20
)