request_builder.go 4.1 KB

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