request_builder.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, defaultUserAgent string) *RequestBuilder {
  48. if userAgent != "" {
  49. r.headers.Set("User-Agent", userAgent)
  50. } else {
  51. r.headers.Set("User-Agent", defaultUserAgent)
  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. // Setting `DialContext` disables HTTP/2, this option forces the transport to try HTTP/2 regardless.
  91. ForceAttemptHTTP2: true,
  92. DialContext: (&net.Dialer{
  93. // Default is 30s.
  94. Timeout: 10 * time.Second,
  95. // Default is 30s.
  96. KeepAlive: 15 * time.Second,
  97. }).DialContext,
  98. // Default is 100.
  99. MaxIdleConns: 50,
  100. // Default is 90s.
  101. IdleConnTimeout: 10 * time.Second,
  102. TLSClientConfig: &tls.Config{
  103. InsecureSkipVerify: r.ignoreTLSErrors,
  104. },
  105. }
  106. if r.useClientProxy && r.clientProxyURL != "" {
  107. if proxyURL, err := url.Parse(r.clientProxyURL); err != nil {
  108. slog.Warn("Unable to parse proxy URL",
  109. slog.String("proxy_url", r.clientProxyURL),
  110. slog.Any("error", err),
  111. )
  112. } else {
  113. transport.Proxy = http.ProxyURL(proxyURL)
  114. }
  115. }
  116. client := &http.Client{
  117. Timeout: time.Duration(r.clientTimeout) * time.Second,
  118. }
  119. if r.withoutRedirects {
  120. client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
  121. return http.ErrUseLastResponse
  122. }
  123. }
  124. client.Transport = transport
  125. req, err := http.NewRequest("GET", requestURL, nil)
  126. if err != nil {
  127. return nil, err
  128. }
  129. req.Header = r.headers
  130. req.Header.Set("Accept", "*/*")
  131. req.Header.Set("Connection", "close")
  132. slog.Debug("Making outgoing request", slog.Group("request",
  133. slog.String("method", req.Method),
  134. slog.String("url", req.URL.String()),
  135. slog.Any("headers", req.Header),
  136. slog.Bool("without_redirects", r.withoutRedirects),
  137. slog.Bool("with_proxy", r.useClientProxy),
  138. slog.String("proxy_url", r.clientProxyURL),
  139. ))
  140. return client.Do(req)
  141. }