request_builder.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/tls"
  6. "encoding/base64"
  7. "log/slog"
  8. "net"
  9. "net/http"
  10. "net/url"
  11. "time"
  12. )
  13. const (
  14. defaultHTTPClientTimeout = 20
  15. defaultHTTPClientMaxBodySize = 15 * 1024 * 1024
  16. )
  17. type RequestBuilder struct {
  18. headers http.Header
  19. clientProxyURL string
  20. useClientProxy bool
  21. clientTimeout int
  22. withoutRedirects bool
  23. ignoreTLSErrors bool
  24. }
  25. func NewRequestBuilder() *RequestBuilder {
  26. return &RequestBuilder{
  27. headers: make(http.Header),
  28. clientTimeout: defaultHTTPClientTimeout,
  29. }
  30. }
  31. func (r *RequestBuilder) WithHeader(key, value string) *RequestBuilder {
  32. r.headers.Set(key, value)
  33. return r
  34. }
  35. func (r *RequestBuilder) WithETag(etag string) *RequestBuilder {
  36. if etag != "" {
  37. r.headers.Set("If-None-Match", etag)
  38. }
  39. return r
  40. }
  41. func (r *RequestBuilder) WithLastModified(lastModified string) *RequestBuilder {
  42. if lastModified != "" {
  43. r.headers.Set("If-Modified-Since", lastModified)
  44. }
  45. return r
  46. }
  47. func (r *RequestBuilder) WithUserAgent(userAgent string) *RequestBuilder {
  48. if userAgent != "" {
  49. r.headers.Set("User-Agent", userAgent)
  50. } else {
  51. r.headers.Del("User-Agent")
  52. }
  53. return r
  54. }
  55. func (r *RequestBuilder) WithCookie(cookie string) *RequestBuilder {
  56. if cookie != "" {
  57. r.headers.Set("Cookie", cookie)
  58. }
  59. return r
  60. }
  61. func (r *RequestBuilder) WithUsernameAndPassword(username, password string) *RequestBuilder {
  62. if username != "" && password != "" {
  63. r.headers.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
  64. }
  65. return r
  66. }
  67. func (r *RequestBuilder) WithProxy(proxyURL string) *RequestBuilder {
  68. r.clientProxyURL = proxyURL
  69. return r
  70. }
  71. func (r *RequestBuilder) UseProxy(value bool) *RequestBuilder {
  72. r.useClientProxy = value
  73. return r
  74. }
  75. func (r *RequestBuilder) WithTimeout(timeout int) *RequestBuilder {
  76. r.clientTimeout = timeout
  77. return r
  78. }
  79. func (r *RequestBuilder) WithoutRedirects() *RequestBuilder {
  80. r.withoutRedirects = true
  81. return r
  82. }
  83. func (r *RequestBuilder) IgnoreTLSErrors(value bool) *RequestBuilder {
  84. r.ignoreTLSErrors = value
  85. return r
  86. }
  87. func (r *RequestBuilder) ExecuteRequest(requestURL string) (*http.Response, error) {
  88. transport := &http.Transport{
  89. Proxy: http.ProxyFromEnvironment,
  90. DialContext: (&net.Dialer{
  91. // Default is 30s.
  92. Timeout: 10 * time.Second,
  93. // Default is 30s.
  94. KeepAlive: 15 * time.Second,
  95. }).DialContext,
  96. // Default is 100.
  97. MaxIdleConns: 50,
  98. // Default is 90s.
  99. IdleConnTimeout: 10 * time.Second,
  100. TLSClientConfig: &tls.Config{
  101. InsecureSkipVerify: r.ignoreTLSErrors,
  102. },
  103. }
  104. if r.useClientProxy && r.clientProxyURL != "" {
  105. if proxyURL, err := url.Parse(r.clientProxyURL); err != nil {
  106. slog.Warn("Unable to parse proxy URL",
  107. slog.String("proxy_url", r.clientProxyURL),
  108. slog.Any("error", err),
  109. )
  110. } else {
  111. transport.Proxy = http.ProxyURL(proxyURL)
  112. }
  113. }
  114. client := &http.Client{
  115. Timeout: time.Duration(r.clientTimeout) * time.Second,
  116. }
  117. if r.withoutRedirects {
  118. client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
  119. return http.ErrUseLastResponse
  120. }
  121. }
  122. client.Transport = transport
  123. req, err := http.NewRequest("GET", requestURL, nil)
  124. if err != nil {
  125. return nil, err
  126. }
  127. req.Header = r.headers
  128. req.Header.Set("Accept", "*/*")
  129. req.Header.Set("Connection", "close")
  130. slog.Debug("Making outgoing request", slog.Group("request",
  131. slog.String("method", req.Method),
  132. slog.String("url", req.URL.String()),
  133. slog.Any("headers", req.Header),
  134. slog.Bool("without_redirects", r.withoutRedirects),
  135. slog.Bool("with_proxy", r.useClientProxy),
  136. slog.String("proxy_url", r.clientProxyURL),
  137. ))
  138. return client.Do(req)
  139. }