request_builder.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. defaultAcceptHeader = "application/xml, application/atom+xml, application/rss+xml, application/rdf+xml, application/feed+json, text/html, */*;q=0.9"
  17. )
  18. type RequestBuilder struct {
  19. headers http.Header
  20. clientProxyURL string
  21. useClientProxy bool
  22. clientTimeout int
  23. withoutRedirects bool
  24. ignoreTLSErrors bool
  25. disableHTTP2 bool
  26. }
  27. func NewRequestBuilder() *RequestBuilder {
  28. return &RequestBuilder{
  29. headers: make(http.Header),
  30. clientTimeout: defaultHTTPClientTimeout,
  31. }
  32. }
  33. func (r *RequestBuilder) WithHeader(key, value string) *RequestBuilder {
  34. r.headers.Set(key, value)
  35. return r
  36. }
  37. func (r *RequestBuilder) WithETag(etag string) *RequestBuilder {
  38. if etag != "" {
  39. r.headers.Set("If-None-Match", etag)
  40. }
  41. return r
  42. }
  43. func (r *RequestBuilder) WithLastModified(lastModified string) *RequestBuilder {
  44. if lastModified != "" {
  45. r.headers.Set("If-Modified-Since", lastModified)
  46. }
  47. return r
  48. }
  49. func (r *RequestBuilder) WithUserAgent(userAgent string, defaultUserAgent string) *RequestBuilder {
  50. if userAgent != "" {
  51. r.headers.Set("User-Agent", userAgent)
  52. } else {
  53. r.headers.Set("User-Agent", defaultUserAgent)
  54. }
  55. return r
  56. }
  57. func (r *RequestBuilder) WithCookie(cookie string) *RequestBuilder {
  58. if cookie != "" {
  59. r.headers.Set("Cookie", cookie)
  60. }
  61. return r
  62. }
  63. func (r *RequestBuilder) WithUsernameAndPassword(username, password string) *RequestBuilder {
  64. if username != "" && password != "" {
  65. r.headers.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
  66. }
  67. return r
  68. }
  69. func (r *RequestBuilder) WithProxy(proxyURL string) *RequestBuilder {
  70. r.clientProxyURL = proxyURL
  71. return r
  72. }
  73. func (r *RequestBuilder) UseProxy(value bool) *RequestBuilder {
  74. r.useClientProxy = value
  75. return r
  76. }
  77. func (r *RequestBuilder) WithTimeout(timeout int) *RequestBuilder {
  78. r.clientTimeout = timeout
  79. return r
  80. }
  81. func (r *RequestBuilder) WithoutRedirects() *RequestBuilder {
  82. r.withoutRedirects = true
  83. return r
  84. }
  85. func (r *RequestBuilder) DisableHTTP2(value bool) *RequestBuilder {
  86. r.disableHTTP2 = value
  87. return r
  88. }
  89. func (r *RequestBuilder) IgnoreTLSErrors(value bool) *RequestBuilder {
  90. r.ignoreTLSErrors = value
  91. return r
  92. }
  93. func (r *RequestBuilder) ExecuteRequest(requestURL string) (*http.Response, error) {
  94. // We get the safe ciphers
  95. ciphers := tls.CipherSuites()
  96. if r.ignoreTLSErrors {
  97. // and the insecure ones if we are ignoring TLS errors. This allows to connect to badly configured servers anyway
  98. ciphers = append(ciphers, tls.InsecureCipherSuites()...)
  99. }
  100. cipherSuites := []uint16{}
  101. for _, cipher := range ciphers {
  102. cipherSuites = append(cipherSuites, cipher.ID)
  103. }
  104. transport := &http.Transport{
  105. Proxy: http.ProxyFromEnvironment,
  106. // Setting `DialContext` disables HTTP/2, this option forces the transport to try HTTP/2 regardless.
  107. ForceAttemptHTTP2: true,
  108. DialContext: (&net.Dialer{
  109. // Default is 30s.
  110. Timeout: 10 * time.Second,
  111. // Default is 30s.
  112. KeepAlive: 15 * time.Second,
  113. }).DialContext,
  114. // Default is 100.
  115. MaxIdleConns: 50,
  116. // Default is 90s.
  117. IdleConnTimeout: 10 * time.Second,
  118. TLSClientConfig: &tls.Config{
  119. CipherSuites: cipherSuites,
  120. InsecureSkipVerify: r.ignoreTLSErrors,
  121. },
  122. }
  123. if r.disableHTTP2 {
  124. transport.ForceAttemptHTTP2 = false
  125. // https://pkg.go.dev/net/http#hdr-HTTP_2
  126. // Programs that must disable HTTP/2 can do so by setting [Transport.TLSNextProto] (for clients) or [Server.TLSNextProto] (for servers) to a non-nil, empty map.
  127. transport.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{}
  128. }
  129. if r.useClientProxy && r.clientProxyURL != "" {
  130. if proxyURL, err := url.Parse(r.clientProxyURL); err != nil {
  131. slog.Warn("Unable to parse proxy URL",
  132. slog.String("proxy_url", r.clientProxyURL),
  133. slog.Any("error", err),
  134. )
  135. } else {
  136. transport.Proxy = http.ProxyURL(proxyURL)
  137. }
  138. }
  139. client := &http.Client{
  140. Timeout: time.Duration(r.clientTimeout) * time.Second,
  141. }
  142. if r.withoutRedirects {
  143. client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
  144. return http.ErrUseLastResponse
  145. }
  146. }
  147. client.Transport = transport
  148. req, err := http.NewRequest("GET", requestURL, nil)
  149. if err != nil {
  150. return nil, err
  151. }
  152. req.Header = r.headers
  153. req.Header.Set("Accept-Encoding", "br, gzip")
  154. req.Header.Set("Accept", defaultAcceptHeader)
  155. req.Header.Set("Connection", "close")
  156. slog.Debug("Making outgoing request", slog.Group("request",
  157. slog.String("method", req.Method),
  158. slog.String("url", req.URL.String()),
  159. slog.Any("headers", req.Header),
  160. slog.Bool("without_redirects", r.withoutRedirects),
  161. slog.Bool("with_proxy", r.useClientProxy),
  162. slog.String("proxy_url", r.clientProxyURL),
  163. slog.Bool("ignore_tls_errors", r.ignoreTLSErrors),
  164. slog.Bool("disable_http2", r.disableHTTP2),
  165. ))
  166. return client.Do(req)
  167. }