response_handler.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package fetcher // import "miniflux.app/v2/internal/reader/fetcher"
  4. import (
  5. "crypto/x509"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "log/slog"
  10. "net"
  11. "net/http"
  12. "net/url"
  13. "os"
  14. "strings"
  15. "miniflux.app/v2/internal/locale"
  16. )
  17. type ResponseHandler struct {
  18. httpResponse *http.Response
  19. clientErr error
  20. }
  21. func NewResponseHandler(httpResponse *http.Response, clientErr error) *ResponseHandler {
  22. return &ResponseHandler{httpResponse: httpResponse, clientErr: clientErr}
  23. }
  24. func (r *ResponseHandler) EffectiveURL() string {
  25. return r.httpResponse.Request.URL.String()
  26. }
  27. func (r *ResponseHandler) ContentType() string {
  28. return r.httpResponse.Header.Get("Content-Type")
  29. }
  30. func (r *ResponseHandler) LastModified() string {
  31. // Ignore caching headers for feeds that do not want any cache.
  32. if r.httpResponse.Header.Get("Expires") == "0" {
  33. return ""
  34. }
  35. return r.httpResponse.Header.Get("Last-Modified")
  36. }
  37. func (r *ResponseHandler) ETag() string {
  38. // Ignore caching headers for feeds that do not want any cache.
  39. if r.httpResponse.Header.Get("Expires") == "0" {
  40. return ""
  41. }
  42. return r.httpResponse.Header.Get("ETag")
  43. }
  44. func (r *ResponseHandler) IsModified(lastEtagValue, lastModifiedValue string) bool {
  45. if r.httpResponse.StatusCode == http.StatusNotModified {
  46. return false
  47. }
  48. if r.ETag() != "" {
  49. return r.ETag() != lastEtagValue
  50. }
  51. if r.LastModified() != "" {
  52. return r.LastModified() != lastModifiedValue
  53. }
  54. return true
  55. }
  56. func (r *ResponseHandler) Close() {
  57. if r.httpResponse != nil && r.httpResponse.Body != nil && r.clientErr == nil {
  58. r.httpResponse.Body.Close()
  59. }
  60. }
  61. func (r *ResponseHandler) getReader(maxBodySize int64) io.ReadCloser {
  62. contentEncoding := strings.ToLower(r.httpResponse.Header.Get("Content-Encoding"))
  63. slog.Debug("Request response",
  64. slog.String("effective_url", r.EffectiveURL()),
  65. slog.String("content_length", r.httpResponse.Header.Get("Content-Length")),
  66. slog.String("content_encoding", contentEncoding),
  67. slog.String("content_type", r.httpResponse.Header.Get("Content-Type")),
  68. )
  69. reader := r.httpResponse.Body
  70. switch contentEncoding {
  71. case "br":
  72. reader = NewBrotliReadCloser(r.httpResponse.Body)
  73. case "gzip":
  74. reader = NewGzipReadCloser(r.httpResponse.Body)
  75. }
  76. return http.MaxBytesReader(nil, reader, maxBodySize)
  77. }
  78. func (r *ResponseHandler) Body(maxBodySize int64) io.ReadCloser {
  79. return r.getReader(maxBodySize)
  80. }
  81. func (r *ResponseHandler) ReadBody(maxBodySize int64) ([]byte, *locale.LocalizedErrorWrapper) {
  82. limitedReader := r.getReader(maxBodySize)
  83. buffer, err := io.ReadAll(limitedReader)
  84. if err != nil && err != io.EOF {
  85. if err, ok := err.(*http.MaxBytesError); ok {
  86. return nil, locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: response body too large: %d bytes", err.Limit), "error.http_response_too_large")
  87. }
  88. return nil, locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: unable to read response body: %w", err), "error.http_body_read", err)
  89. }
  90. if len(buffer) == 0 {
  91. return nil, locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: empty response body"), "error.http_empty_response_body")
  92. }
  93. return buffer, nil
  94. }
  95. func (r *ResponseHandler) LocalizedError() *locale.LocalizedErrorWrapper {
  96. if r.clientErr != nil {
  97. switch {
  98. case isSSLError(r.clientErr):
  99. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.tls_error", r.clientErr)
  100. case isNetworkError(r.clientErr):
  101. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.network_operation", r.clientErr)
  102. case os.IsTimeout(r.clientErr):
  103. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.network_timeout", r.clientErr)
  104. case errors.Is(r.clientErr, io.EOF):
  105. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.http_empty_response")
  106. default:
  107. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: %w", r.clientErr), "error.http_client_error", r.clientErr)
  108. }
  109. }
  110. switch r.httpResponse.StatusCode {
  111. case http.StatusUnauthorized:
  112. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: access unauthorized (401 status code)"), "error.http_not_authorized")
  113. case http.StatusForbidden:
  114. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: access forbidden (403 status code)"), "error.http_forbidden")
  115. case http.StatusTooManyRequests:
  116. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: too many requests (429 status code)"), "error.http_too_many_requests")
  117. case http.StatusNotFound, http.StatusGone:
  118. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: resource not found (%d status code)", r.httpResponse.StatusCode), "error.http_resource_not_found")
  119. case http.StatusInternalServerError:
  120. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: remote server error (%d status code)", r.httpResponse.StatusCode), "error.http_internal_server_error")
  121. case http.StatusBadGateway:
  122. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: bad gateway (%d status code)", r.httpResponse.StatusCode), "error.http_bad_gateway")
  123. case http.StatusServiceUnavailable:
  124. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: service unavailable (%d status code)", r.httpResponse.StatusCode), "error.http_service_unavailable")
  125. case http.StatusGatewayTimeout:
  126. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: gateway timeout (%d status code)", r.httpResponse.StatusCode), "error.http_gateway_timeout")
  127. }
  128. if r.httpResponse.StatusCode >= 400 {
  129. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: unexpected status code (%d status code)", r.httpResponse.StatusCode), "error.http_unexpected_status_code", r.httpResponse.StatusCode)
  130. }
  131. if r.httpResponse.StatusCode != 304 {
  132. // Content-Length = -1 when no Content-Length header is sent.
  133. if r.httpResponse.ContentLength == 0 {
  134. return locale.NewLocalizedErrorWrapper(fmt.Errorf("fetcher: empty response body"), "error.http_empty_response_body")
  135. }
  136. }
  137. return nil
  138. }
  139. func isNetworkError(err error) bool {
  140. if _, ok := err.(*url.Error); ok {
  141. return true
  142. }
  143. if err == io.EOF {
  144. return true
  145. }
  146. var opErr *net.OpError
  147. if ok := errors.As(err, &opErr); ok {
  148. return true
  149. }
  150. return false
  151. }
  152. func isSSLError(err error) bool {
  153. var certErr x509.UnknownAuthorityError
  154. if errors.As(err, &certErr) {
  155. return true
  156. }
  157. var hostErr x509.HostnameError
  158. if errors.As(err, &hostErr) {
  159. return true
  160. }
  161. var algErr x509.InsecureAlgorithmError
  162. return errors.As(err, &algErr)
  163. }