response_handler.go 5.4 KB

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