feat: add shared size limit constants
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mal/internal/domain"
|
"mal/internal/domain"
|
||||||
|
"mal/pkg/net/limits"
|
||||||
"mal/pkg/net/useragent"
|
"mal/pkg/net/useragent"
|
||||||
"mal/pkg/net/utls"
|
"mal/pkg/net/utls"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -279,7 +280,7 @@ func (c *AllAnimeProvider) graphqlRequest(ctx context.Context, query string, var
|
|||||||
}
|
}
|
||||||
defer func() { _ = resp.Body.Close() }()
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("read graphql response: %w", err)
|
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() }()
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("read response: %w", err)
|
return nil, fmt.Errorf("read response: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"mal/pkg/net/limits"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -53,7 +54,7 @@ func (e *providerExtractor) ExtractVideoLinks(ctx context.Context, providerPath
|
|||||||
|
|
||||||
defer func() { _ = resp.Body.Close() }()
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("read provider response: %w", err)
|
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() }()
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"mal/pkg/net/limits"
|
||||||
"mal/pkg/net/useragent"
|
"mal/pkg/net/useragent"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -109,7 +110,7 @@ func fetchDocument(ctx context.Context, httpClient *http.Client, url string) (*g
|
|||||||
|
|
||||||
if response.StatusCode != http.StatusOK {
|
if response.StatusCode != http.StatusOK {
|
||||||
// limit body read for error context; avoid reading large error pages
|
// 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{
|
return nil, &HTTPStatusError{
|
||||||
StatusCode: response.StatusCode,
|
StatusCode: response.StatusCode,
|
||||||
URL: url,
|
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)
|
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 {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to read proxy response: %w", err)
|
return "", fmt.Errorf("failed to read proxy response: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mal/internal/domain"
|
"mal/internal/domain"
|
||||||
|
"mal/pkg/net/limits"
|
||||||
"mal/pkg/net/proxytransport"
|
"mal/pkg/net/proxytransport"
|
||||||
"mal/pkg/net/useragent"
|
"mal/pkg/net/useragent"
|
||||||
"maps"
|
"maps"
|
||||||
@@ -321,7 +322,7 @@ func (h *PlaybackHandler) HandleProxySubtitle(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
defer func() { _ = resp.Body.Close() }()
|
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 {
|
if err != nil {
|
||||||
c.Status(http.StatusBadGateway)
|
c.Status(http.StatusBadGateway)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"mal/integrations/jikan"
|
"mal/integrations/jikan"
|
||||||
"mal/internal/db"
|
"mal/internal/db"
|
||||||
"mal/internal/domain"
|
"mal/internal/domain"
|
||||||
|
"mal/pkg/net/limits"
|
||||||
"mal/pkg/net/useragent"
|
"mal/pkg/net/useragent"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -372,7 +373,7 @@ func (s *playbackService) fetchSkipSegments(ctx context.Context, malID int, epis
|
|||||||
return []SkipSegment{}
|
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 {
|
if err != nil {
|
||||||
return []SkipSegment{}
|
return []SkipSegment{}
|
||||||
}
|
}
|
||||||
|
|||||||
10
pkg/net/limits/limits.go
Normal file
10
pkg/net/limits/limits.go
Normal 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
|
||||||
|
)
|
||||||
|
|
||||||
Reference in New Issue
Block a user