2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package client // import "miniflux.app/v2/internal/http/client"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
2017-12-18 23:52:46 -05:00
|
|
|
"bytes"
|
2021-02-21 16:42:49 -05:00
|
|
|
"crypto/tls"
|
2018-02-08 21:16:54 -05:00
|
|
|
"crypto/x509"
|
2017-12-18 23:52:46 -05:00
|
|
|
"encoding/json"
|
2017-11-20 00:10:04 -05:00
|
|
|
"fmt"
|
2017-12-18 23:52:46 -05:00
|
|
|
"io"
|
2018-02-08 21:16:54 -05:00
|
|
|
"net"
|
2017-11-20 00:10:04 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-12-18 23:52:46 -05:00
|
|
|
"strings"
|
2017-11-20 00:10:04 -05:00
|
|
|
"time"
|
2017-11-20 20:12:37 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
|
"miniflux.app/v2/internal/errors"
|
|
|
|
"miniflux.app/v2/internal/logger"
|
|
|
|
"miniflux.app/v2/internal/timer"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
const (
|
|
|
|
defaultHTTPClientTimeout = 20
|
|
|
|
defaultHTTPClientMaxBodySize = 15 * 1024 * 1024
|
|
|
|
)
|
|
|
|
|
2018-02-08 21:16:54 -05:00
|
|
|
var (
|
2022-08-09 00:33:38 -04:00
|
|
|
errInvalidCertificate = "Invalid SSL certificate (original error: %q)"
|
|
|
|
errNetworkOperation = "This website is unreachable (original error: %q)"
|
|
|
|
errRequestTimeout = "Website unreachable, the request timed out after %d seconds"
|
2018-02-08 21:16:54 -05:00
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
// Client builds and executes HTTP requests.
|
2017-11-20 20:12:37 -05:00
|
|
|
type Client struct {
|
2020-09-27 17:29:48 -04:00
|
|
|
inputURL string
|
|
|
|
|
|
|
|
requestEtagHeader string
|
|
|
|
requestLastModifiedHeader string
|
|
|
|
requestAuthorizationHeader string
|
|
|
|
requestUsername string
|
|
|
|
requestPassword string
|
|
|
|
requestUserAgent string
|
2021-03-22 23:27:58 -04:00
|
|
|
requestCookie string
|
2023-07-07 18:20:14 -04:00
|
|
|
customHeaders map[string]string
|
|
|
|
useProxy bool
|
|
|
|
doNotFollowRedirects bool
|
2020-09-27 17:29:48 -04:00
|
|
|
|
2021-02-21 16:42:49 -05:00
|
|
|
ClientTimeout int
|
|
|
|
ClientMaxBodySize int64
|
|
|
|
ClientProxyURL string
|
|
|
|
AllowSelfSignedCertificates bool
|
2020-09-27 17:29:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// New initializes a new HTTP client.
|
|
|
|
func New(url string) *Client {
|
|
|
|
return &Client{
|
|
|
|
inputURL: url,
|
|
|
|
ClientTimeout: defaultHTTPClientTimeout,
|
|
|
|
ClientMaxBodySize: defaultHTTPClientMaxBodySize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClientWithConfig initializes a new HTTP client with application config options.
|
|
|
|
func NewClientWithConfig(url string, opts *config.Options) *Client {
|
|
|
|
return &Client{
|
|
|
|
inputURL: url,
|
2020-12-17 00:16:04 -05:00
|
|
|
requestUserAgent: opts.HTTPClientUserAgent(),
|
2020-09-27 17:29:48 -04:00
|
|
|
ClientTimeout: opts.HTTPClientTimeout(),
|
|
|
|
ClientMaxBodySize: opts.HTTPClientMaxBodySize(),
|
|
|
|
ClientProxyURL: opts.HTTPClientProxy(),
|
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2019-12-26 18:26:23 -05:00
|
|
|
func (c *Client) String() string {
|
2020-09-27 17:29:48 -04:00
|
|
|
etagHeader := c.requestEtagHeader
|
|
|
|
if c.requestEtagHeader == "" {
|
2020-06-06 00:50:59 -04:00
|
|
|
etagHeader = "None"
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
lastModifiedHeader := c.requestLastModifiedHeader
|
|
|
|
if c.requestLastModifiedHeader == "" {
|
2020-06-06 00:50:59 -04:00
|
|
|
lastModifiedHeader = "None"
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:26:23 -05:00
|
|
|
return fmt.Sprintf(
|
2021-09-11 13:58:48 -04:00
|
|
|
`InputURL=%q ETag=%s LastMod=%s Auth=%v UserAgent=%q Verify=%v`,
|
2019-12-26 18:26:23 -05:00
|
|
|
c.inputURL,
|
2020-06-06 00:50:59 -04:00
|
|
|
etagHeader,
|
|
|
|
lastModifiedHeader,
|
2020-09-27 17:29:48 -04:00
|
|
|
c.requestAuthorizationHeader != "" || (c.requestUsername != "" && c.requestPassword != ""),
|
|
|
|
c.requestUserAgent,
|
2021-02-21 16:42:49 -05:00
|
|
|
!c.AllowSelfSignedCertificates,
|
2019-12-26 18:26:23 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-04-28 13:51:07 -04:00
|
|
|
// WithCredentials defines the username/password for HTTP Basic authentication.
|
|
|
|
func (c *Client) WithCredentials(username, password string) *Client {
|
2018-06-20 01:58:29 -04:00
|
|
|
if username != "" && password != "" {
|
2020-09-27 17:29:48 -04:00
|
|
|
c.requestUsername = username
|
|
|
|
c.requestPassword = password
|
2018-06-20 01:58:29 -04:00
|
|
|
}
|
2018-04-28 13:51:07 -04:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
// WithAuthorization defines the authorization HTTP header value.
|
2018-04-28 13:51:07 -04:00
|
|
|
func (c *Client) WithAuthorization(authorization string) *Client {
|
2020-09-27 17:29:48 -04:00
|
|
|
c.requestAuthorizationHeader = authorization
|
2018-04-28 13:51:07 -04:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-07-07 18:20:14 -04:00
|
|
|
// WithCustomHeaders defines custom HTTP headers.
|
|
|
|
func (c *Client) WithCustomHeaders(customHeaders map[string]string) *Client {
|
|
|
|
c.customHeaders = customHeaders
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-04-28 13:51:07 -04:00
|
|
|
// WithCacheHeaders defines caching headers.
|
|
|
|
func (c *Client) WithCacheHeaders(etagHeader, lastModifiedHeader string) *Client {
|
2021-02-05 23:28:00 -05:00
|
|
|
c.requestEtagHeader = etagHeader
|
2020-09-27 17:29:48 -04:00
|
|
|
c.requestLastModifiedHeader = lastModifiedHeader
|
2018-04-28 13:51:07 -04:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-11-06 20:39:10 -05:00
|
|
|
// WithProxy enables proxy for the current HTTP request.
|
2020-09-10 02:28:54 -04:00
|
|
|
func (c *Client) WithProxy() *Client {
|
2020-09-27 17:29:48 -04:00
|
|
|
c.useProxy = true
|
2020-09-10 02:28:54 -04:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-11-06 20:39:10 -05:00
|
|
|
// WithoutRedirects disables HTTP redirects.
|
|
|
|
func (c *Client) WithoutRedirects() *Client {
|
|
|
|
c.doNotFollowRedirects = true
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
// WithUserAgent defines the User-Agent header to use for HTTP requests.
|
2018-09-19 21:19:24 -04:00
|
|
|
func (c *Client) WithUserAgent(userAgent string) *Client {
|
|
|
|
if userAgent != "" {
|
2020-09-27 17:29:48 -04:00
|
|
|
c.requestUserAgent = userAgent
|
2018-09-19 21:19:24 -04:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-03-22 23:27:58 -04:00
|
|
|
// WithCookie defines the Cookies to use for HTTP requests.
|
|
|
|
func (c *Client) WithCookie(cookie string) *Client {
|
|
|
|
if cookie != "" {
|
|
|
|
c.requestCookie = cookie
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
// Get performs a GET HTTP request.
|
2017-12-02 22:32:14 -05:00
|
|
|
func (c *Client) Get() (*Response, error) {
|
2017-12-18 23:52:46 -05:00
|
|
|
request, err := c.buildRequest(http.MethodGet, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.executeRequest(request)
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
// PostForm performs a POST HTTP request with form encoded values.
|
2017-12-18 23:52:46 -05:00
|
|
|
func (c *Client) PostForm(values url.Values) (*Response, error) {
|
|
|
|
request, err := c.buildRequest(http.MethodPost, strings.NewReader(values.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
return c.executeRequest(request)
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
// PostJSON performs a POST HTTP request with a JSON payload.
|
2017-12-18 23:52:46 -05:00
|
|
|
func (c *Client) PostJSON(data interface{}) (*Response, error) {
|
|
|
|
b, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request, err := c.buildRequest(http.MethodPost, bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Add("Content-Type", "application/json")
|
|
|
|
return c.executeRequest(request)
|
|
|
|
}
|
|
|
|
|
2023-07-07 18:20:14 -04:00
|
|
|
// PatchJSON performs a Patch HTTP request with a JSON payload.
|
|
|
|
func (c *Client) PatchJSON(data interface{}) (*Response, error) {
|
|
|
|
b, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request, err := c.buildRequest(http.MethodPatch, bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Add("Content-Type", "application/json")
|
|
|
|
return c.executeRequest(request)
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:52:46 -05:00
|
|
|
func (c *Client) executeRequest(request *http.Request) (*Response, error) {
|
2019-12-26 18:26:23 -05:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[HttpClient] inputURL=%s", c.inputURL))
|
|
|
|
|
|
|
|
logger.Debug("[HttpClient:Before] Method=%s %s",
|
|
|
|
request.Method,
|
|
|
|
c.String(),
|
|
|
|
)
|
2017-12-18 23:52:46 -05:00
|
|
|
|
2017-12-02 22:32:14 -05:00
|
|
|
client := c.buildClient()
|
2017-12-18 23:52:46 -05:00
|
|
|
resp, err := client.Do(request)
|
2018-06-19 23:13:13 -04:00
|
|
|
if resp != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2018-02-08 21:16:54 -05:00
|
|
|
if uerr, ok := err.(*url.Error); ok {
|
|
|
|
switch uerr.Err.(type) {
|
|
|
|
case x509.CertificateInvalidError, x509.HostnameError:
|
|
|
|
err = errors.NewLocalizedError(errInvalidCertificate, uerr.Err)
|
|
|
|
case *net.OpError:
|
2022-08-09 00:33:38 -04:00
|
|
|
err = errors.NewLocalizedError(errNetworkOperation, uerr.Err)
|
2018-02-08 21:16:54 -05:00
|
|
|
case net.Error:
|
|
|
|
nerr := uerr.Err.(net.Error)
|
|
|
|
if nerr.Timeout() {
|
2020-09-27 17:29:48 -04:00
|
|
|
err = errors.NewLocalizedError(errRequestTimeout, c.ClientTimeout)
|
2018-02-08 21:16:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
if resp.ContentLength > c.ClientMaxBodySize {
|
2018-01-02 21:30:26 -05:00
|
|
|
return nil, fmt.Errorf("client: response too large (%d bytes)", resp.ContentLength)
|
|
|
|
}
|
|
|
|
|
2021-02-17 00:19:03 -05:00
|
|
|
buf, err := io.ReadAll(resp.Body)
|
2018-04-30 02:11:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("client: error while reading body %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:12:37 -05:00
|
|
|
response := &Response{
|
2018-04-30 02:11:10 -04:00
|
|
|
Body: bytes.NewReader(buf),
|
2018-01-04 21:32:36 -05:00
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
EffectiveURL: resp.Request.URL.String(),
|
|
|
|
LastModified: resp.Header.Get("Last-Modified"),
|
|
|
|
ETag: resp.Header.Get("ETag"),
|
2019-12-26 18:26:23 -05:00
|
|
|
Expires: resp.Header.Get("Expires"),
|
2018-01-04 21:32:36 -05:00
|
|
|
ContentType: resp.Header.Get("Content-Type"),
|
|
|
|
ContentLength: resp.ContentLength,
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2019-12-26 18:26:23 -05:00
|
|
|
logger.Debug("[HttpClient:After] Method=%s %s; Response => %s",
|
2017-12-18 23:52:46 -05:00
|
|
|
request.Method,
|
2019-12-26 18:26:23 -05:00
|
|
|
c.String(),
|
|
|
|
response,
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2018-04-09 23:18:54 -04:00
|
|
|
// Ignore caching headers for feeds that do not want any cache.
|
|
|
|
if resp.Header.Get("Expires") == "0" {
|
|
|
|
logger.Debug("[HttpClient] Ignore caching headers for %q", response.EffectiveURL)
|
|
|
|
response.ETag = ""
|
|
|
|
response.LastModified = ""
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:52:46 -05:00
|
|
|
func (c *Client) buildRequest(method string, body io.Reader) (*http.Request, error) {
|
2021-09-11 13:58:48 -04:00
|
|
|
request, err := http.NewRequest(method, c.inputURL, body)
|
2017-12-18 23:52:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-12-02 22:32:14 -05:00
|
|
|
}
|
|
|
|
|
2018-02-25 14:49:08 -05:00
|
|
|
request.Header = c.buildHeaders()
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
if c.requestUsername != "" && c.requestPassword != "" {
|
|
|
|
request.SetBasicAuth(c.requestUsername, c.requestPassword)
|
2017-12-02 22:32:14 -05:00
|
|
|
}
|
|
|
|
|
2017-12-18 23:52:46 -05:00
|
|
|
return request, nil
|
2017-12-02 22:32:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) buildClient() http.Client {
|
2020-11-06 20:39:10 -05:00
|
|
|
client := http.Client{
|
|
|
|
Timeout: time.Duration(c.ClientTimeout) * time.Second,
|
|
|
|
}
|
|
|
|
|
2020-09-27 16:08:55 -04:00
|
|
|
transport := &http.Transport{
|
2020-10-30 22:03:41 -04:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2020-09-27 16:08:55 -04:00
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
// Default is 30s.
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
|
|
|
|
// Default is 30s.
|
|
|
|
KeepAlive: 15 * time.Second,
|
|
|
|
}).DialContext,
|
|
|
|
|
|
|
|
// Default is 100.
|
|
|
|
MaxIdleConns: 50,
|
|
|
|
|
|
|
|
// Default is 90s.
|
|
|
|
IdleConnTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
2021-02-21 16:42:49 -05:00
|
|
|
if c.AllowSelfSignedCertificates {
|
|
|
|
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
}
|
|
|
|
|
2020-11-06 20:39:10 -05:00
|
|
|
if c.doNotFollowRedirects {
|
|
|
|
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
if c.useProxy && c.ClientProxyURL != "" {
|
|
|
|
proxyURL, err := url.Parse(c.ClientProxyURL)
|
2020-09-10 02:28:54 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("[HttpClient] Proxy URL error: %v", err)
|
|
|
|
} else {
|
|
|
|
logger.Debug("[HttpClient] Use proxy: %s", proxyURL)
|
|
|
|
transport.Proxy = http.ProxyURL(proxyURL)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 02:28:54 -04:00
|
|
|
client.Transport = transport
|
|
|
|
|
2017-11-20 22:44:28 -05:00
|
|
|
return client
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-12-02 22:32:14 -05:00
|
|
|
func (c *Client) buildHeaders() http.Header {
|
2017-11-20 00:10:04 -05:00
|
|
|
headers := make(http.Header)
|
2017-12-27 22:44:23 -05:00
|
|
|
headers.Add("Accept", "*/*")
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2020-12-17 00:16:04 -05:00
|
|
|
if c.requestUserAgent != "" {
|
|
|
|
headers.Add("User-Agent", c.requestUserAgent)
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
if c.requestEtagHeader != "" {
|
|
|
|
headers.Add("If-None-Match", c.requestEtagHeader)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
if c.requestLastModifiedHeader != "" {
|
|
|
|
headers.Add("If-Modified-Since", c.requestLastModifiedHeader)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2020-09-27 17:29:48 -04:00
|
|
|
if c.requestAuthorizationHeader != "" {
|
|
|
|
headers.Add("Authorization", c.requestAuthorizationHeader)
|
2017-12-18 23:52:46 -05:00
|
|
|
}
|
|
|
|
|
2021-03-22 23:27:58 -04:00
|
|
|
if c.requestCookie != "" {
|
|
|
|
headers.Add("Cookie", c.requestCookie)
|
|
|
|
}
|
|
|
|
|
2023-07-07 18:20:14 -04:00
|
|
|
for key, value := range c.customHeaders {
|
|
|
|
headers.Add(key, value)
|
|
|
|
}
|
|
|
|
|
2018-06-19 23:21:24 -04:00
|
|
|
headers.Add("Connection", "close")
|
2017-11-20 00:10:04 -05:00
|
|
|
return headers
|
|
|
|
}
|