1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-09-15 18:57:04 +00:00
This commit is contained in:
Julien Voisin 2025-09-14 21:13:53 +02:00 committed by GitHub
commit 32229cecd5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -68,11 +68,9 @@ func (r *ResponseHandler) Expires() time.Duration {
func (r *ResponseHandler) CacheControlMaxAge() time.Duration { func (r *ResponseHandler) CacheControlMaxAge() time.Duration {
cacheControlHeaderValue := r.httpResponse.Header.Get("Cache-Control") cacheControlHeaderValue := r.httpResponse.Header.Get("Cache-Control")
if cacheControlHeaderValue != "" { if cacheControlHeaderValue != "" {
for _, directive := range strings.Split(cacheControlHeaderValue, ",") { for directive := range strings.SplitSeq(cacheControlHeaderValue, ",") {
directive = strings.TrimSpace(directive) if after, ok := strings.CutPrefix(strings.TrimSpace(directive), "max-age="); ok {
if strings.HasPrefix(directive, "max-age=") { if maxAge, err := strconv.Atoi(after); err == nil {
maxAge, err := strconv.Atoi(strings.TrimPrefix(directive, "max-age="))
if err == nil {
return time.Duration(maxAge) * time.Second return time.Duration(maxAge) * time.Second
} }
} }
@ -118,12 +116,7 @@ func (r *ResponseHandler) IsModified(lastEtagValue, lastModifiedValue string) bo
} }
func (r *ResponseHandler) IsRedirect() bool { func (r *ResponseHandler) IsRedirect() bool {
return r.httpResponse != nil && return r.httpResponse != nil && r.httpResponse.StatusCode >= 300 && r.httpResponse.StatusCode < 400
(r.httpResponse.StatusCode == http.StatusMovedPermanently ||
r.httpResponse.StatusCode == http.StatusFound ||
r.httpResponse.StatusCode == http.StatusSeeOther ||
r.httpResponse.StatusCode == http.StatusTemporaryRedirect ||
r.httpResponse.StatusCode == http.StatusPermanentRedirect)
} }
func (r *ResponseHandler) Close() { func (r *ResponseHandler) Close() {
@ -144,9 +137,9 @@ func (r *ResponseHandler) getReader(maxBodySize int64) io.ReadCloser {
reader := r.httpResponse.Body reader := r.httpResponse.Body
switch contentEncoding { switch contentEncoding {
case "br": case "br":
reader = NewBrotliReadCloser(r.httpResponse.Body) reader = NewBrotliReadCloser(reader)
case "gzip": case "gzip":
reader = NewGzipReadCloser(r.httpResponse.Body) reader = NewGzipReadCloser(reader)
} }
return http.MaxBytesReader(nil, reader, maxBodySize) return http.MaxBytesReader(nil, reader, maxBodySize)
} }
@ -176,17 +169,18 @@ func (r *ResponseHandler) ReadBody(maxBodySize int64) ([]byte, *locale.Localized
func (r *ResponseHandler) LocalizedError() *locale.LocalizedErrorWrapper { func (r *ResponseHandler) LocalizedError() *locale.LocalizedErrorWrapper {
if r.clientErr != nil { if r.clientErr != nil {
err := fmt.Errorf("fetcher: %w", r.clientErr)
switch { switch {
case isSSLError(r.clientErr): case isSSLError(r.clientErr):
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.tls_error", r.clientErr) return locale.NewLocalizedErrorWrapper(err, "error.tls_error", r.clientErr)
case isNetworkError(r.clientErr): case isNetworkError(r.clientErr):
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.network_operation", r.clientErr) return locale.NewLocalizedErrorWrapper(err, "error.network_operation", r.clientErr)
case os.IsTimeout(r.clientErr): case os.IsTimeout(r.clientErr):
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.network_timeout", r.clientErr) return locale.NewLocalizedErrorWrapper(err, "error.network_timeout", r.clientErr)
case errors.Is(r.clientErr, io.EOF): case errors.Is(r.clientErr, io.EOF):
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.http_empty_response") return locale.NewLocalizedErrorWrapper(err, "error.http_empty_response")
default: default:
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.http_client_error", r.clientErr) return locale.NewLocalizedErrorWrapper(err, "error.http_client_error", r.clientErr)
} }
} }
@ -197,16 +191,18 @@ func (r *ResponseHandler) LocalizedError() *locale.LocalizedErrorWrapper {
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: access forbidden (403 status code)"), "error.http_forbidden") return locale.NewLocalizedErrorWrapper(errors.New("fetcher: access forbidden (403 status code)"), "error.http_forbidden")
case http.StatusTooManyRequests: case http.StatusTooManyRequests:
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: too many requests (429 status code)"), "error.http_too_many_requests") return locale.NewLocalizedErrorWrapper(errors.New("fetcher: too many requests (429 status code)"), "error.http_too_many_requests")
case http.StatusNotFound, http.StatusGone: case http.StatusNotFound:
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: resource not found (%d status code)", r.httpResponse.StatusCode), "error.http_resource_not_found") return locale.NewLocalizedErrorWrapper(errors.New("fetcher: resource not found (404 status code)"), "error.http_resource_not_found")
case http.StatusGone:
return locale.NewLocalizedErrorWrapper(errors.New("fetcher: resource not found (410 status code)"), "error.http_resource_not_found")
case http.StatusInternalServerError: case http.StatusInternalServerError:
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: remote server error (%d status code)", r.httpResponse.StatusCode), "error.http_internal_server_error") return locale.NewLocalizedErrorWrapper(errors.New("fetcher: remote server error (500 status code)"), "error.http_internal_server_error")
case http.StatusBadGateway: case http.StatusBadGateway:
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: bad gateway (%d status code)", r.httpResponse.StatusCode), "error.http_bad_gateway") return locale.NewLocalizedErrorWrapper(errors.New("fetcher: bad gateway (502 status code)"), "error.http_bad_gateway")
case http.StatusServiceUnavailable: case http.StatusServiceUnavailable:
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: service unavailable (%d status code)", r.httpResponse.StatusCode), "error.http_service_unavailable") return locale.NewLocalizedErrorWrapper(errors.New("fetcher: service unavailable (503 status code)"), "error.http_service_unavailable")
case http.StatusGatewayTimeout: case http.StatusGatewayTimeout:
return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: gateway timeout (%d status code)", r.httpResponse.StatusCode), "error.http_gateway_timeout") return locale.NewLocalizedErrorWrapper(errors.New("fetcher: gateway timeout (504 status code)"), "error.http_gateway_timeout")
} }
if r.httpResponse.StatusCode >= 400 { if r.httpResponse.StatusCode >= 400 {