response_handler.go 5.8 KB

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