2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2017-12-19 05:52:46 +01:00
|
|
|
"bytes"
|
2017-11-20 06:10:04 +01:00
|
|
|
"crypto/tls"
|
2017-12-19 05:52:46 +01:00
|
|
|
"encoding/json"
|
2017-11-20 06:10:04 +01:00
|
|
|
"fmt"
|
2017-12-19 05:52:46 +01:00
|
|
|
"io"
|
2017-11-20 06:10:04 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-12-19 05:52:46 +01:00
|
|
|
"strings"
|
2017-11-20 06:10:04 +01:00
|
|
|
"time"
|
2017-11-21 02:12:37 +01:00
|
|
|
|
2017-12-16 03:55:57 +01:00
|
|
|
"github.com/miniflux/miniflux/logger"
|
2018-01-03 04:15:08 +01:00
|
|
|
"github.com/miniflux/miniflux/timer"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2017-12-19 02:59:35 +01:00
|
|
|
// Note: Some websites have a user agent filter.
|
|
|
|
const userAgent = "Mozilla/5.0 (like Gecko, like Safari, like Chrome) - Miniflux <https://miniflux.net/>"
|
2017-11-21 04:44:28 +01:00
|
|
|
const requestTimeout = 300
|
2018-01-03 03:30:26 +01:00
|
|
|
const maxBodySize = 1024 * 1024 * 15
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2017-11-21 02:12:37 +01:00
|
|
|
// Client is a HTTP Client :)
|
|
|
|
type Client struct {
|
2017-12-19 05:52:46 +01:00
|
|
|
url string
|
|
|
|
etagHeader string
|
|
|
|
lastModifiedHeader string
|
|
|
|
authorizationHeader string
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
Insecure bool
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 02:12:37 +01:00
|
|
|
// Get execute a GET HTTP request.
|
2017-12-03 04:32:14 +01:00
|
|
|
func (c *Client) Get() (*Response, error) {
|
2017-12-19 05:52:46 +01:00
|
|
|
request, err := c.buildRequest(http.MethodGet, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.executeRequest(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostForm execute a POST HTTP request with form values.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostJSON execute a POST HTTP request with JSON payload.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) executeRequest(request *http.Request) (*Response, error) {
|
2018-01-03 04:15:08 +01:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[HttpClient] url=%s", c.url))
|
2017-12-19 05:52:46 +01:00
|
|
|
|
2017-12-03 04:32:14 +01:00
|
|
|
client := c.buildClient()
|
2017-12-19 05:52:46 +01:00
|
|
|
resp, err := client.Do(request)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-03 03:30:26 +01:00
|
|
|
if resp.ContentLength > maxBodySize {
|
|
|
|
return nil, fmt.Errorf("client: response too large (%d bytes)", resp.ContentLength)
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:12:37 +01:00
|
|
|
response := &Response{
|
2018-01-05 03:32:36 +01:00
|
|
|
Body: resp.Body,
|
|
|
|
StatusCode: resp.StatusCode,
|
|
|
|
EffectiveURL: resp.Request.URL.String(),
|
|
|
|
LastModified: resp.Header.Get("Last-Modified"),
|
|
|
|
ETag: resp.Header.Get("ETag"),
|
|
|
|
ContentType: resp.Header.Get("Content-Type"),
|
|
|
|
ContentLength: resp.ContentLength,
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 03:30:26 +01:00
|
|
|
logger.Debug("[HttpClient:%s] OriginalURL=%s, StatusCode=%d, ContentLength=%d, ETag=%s, LastModified=%s, EffectiveURL=%s",
|
2017-12-19 05:52:46 +01:00
|
|
|
request.Method,
|
2017-12-19 02:59:35 +01:00
|
|
|
c.url,
|
|
|
|
response.StatusCode,
|
2018-01-03 03:30:26 +01:00
|
|
|
resp.ContentLength,
|
2017-12-19 02:59:35 +01:00
|
|
|
response.ETag,
|
|
|
|
response.LastModified,
|
|
|
|
response.EffectiveURL,
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2017-12-19 05:52:46 +01:00
|
|
|
func (c *Client) buildRequest(method string, body io.Reader) (*http.Request, error) {
|
|
|
|
request, err := http.NewRequest(method, c.url, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-12-03 04:32:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.username != "" && c.password != "" {
|
|
|
|
request.SetBasicAuth(c.username, c.password)
|
|
|
|
}
|
|
|
|
|
2017-12-19 05:52:46 +01:00
|
|
|
request.Header = c.buildHeaders()
|
|
|
|
return request, nil
|
2017-12-03 04:32:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) buildClient() http.Client {
|
2017-11-21 04:44:28 +01:00
|
|
|
client := http.Client{Timeout: time.Duration(requestTimeout * time.Second)}
|
2017-12-03 04:32:14 +01:00
|
|
|
if c.Insecure {
|
2017-11-21 04:44:28 +01:00
|
|
|
client.Transport = &http.Transport{
|
2017-11-20 06:10:04 +01:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 04:44:28 +01:00
|
|
|
return client
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-03 04:32:14 +01:00
|
|
|
func (c *Client) buildHeaders() http.Header {
|
2017-11-20 06:10:04 +01:00
|
|
|
headers := make(http.Header)
|
2017-11-21 02:12:37 +01:00
|
|
|
headers.Add("User-Agent", userAgent)
|
2017-12-28 04:44:23 +01:00
|
|
|
headers.Add("Accept", "*/*")
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2017-12-03 04:32:14 +01:00
|
|
|
if c.etagHeader != "" {
|
|
|
|
headers.Add("If-None-Match", c.etagHeader)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-03 04:32:14 +01:00
|
|
|
if c.lastModifiedHeader != "" {
|
|
|
|
headers.Add("If-Modified-Since", c.lastModifiedHeader)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 05:52:46 +01:00
|
|
|
if c.authorizationHeader != "" {
|
|
|
|
headers.Add("Authorization", c.authorizationHeader)
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:12:37 +01:00
|
|
|
// NewClient returns a new HTTP client.
|
|
|
|
func NewClient(url string) *Client {
|
|
|
|
return &Client{url: url, Insecure: false}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 05:52:46 +01:00
|
|
|
// NewClientWithCredentials returns a new HTTP client that requires authentication.
|
2017-12-03 04:32:14 +01:00
|
|
|
func NewClientWithCredentials(url, username, password string) *Client {
|
|
|
|
return &Client{url: url, Insecure: false, username: username, password: password}
|
|
|
|
}
|
|
|
|
|
2017-12-19 05:52:46 +01:00
|
|
|
// NewClientWithAuthorization returns a new client with a custom authorization header.
|
|
|
|
func NewClientWithAuthorization(url, authorization string) *Client {
|
|
|
|
return &Client{url: url, Insecure: false, authorizationHeader: authorization}
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:12:37 +01:00
|
|
|
// NewClientWithCacheHeaders returns a new HTTP client that send cache headers.
|
|
|
|
func NewClientWithCacheHeaders(url, etagHeader, lastModifiedHeader string) *Client {
|
|
|
|
return &Client{url: url, etagHeader: etagHeader, lastModifiedHeader: lastModifiedHeader, Insecure: false}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|