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-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/helper"
|
2017-12-16 03:55:57 +01:00
|
|
|
"github.com/miniflux/miniflux/logger"
|
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
|
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) {
|
|
|
|
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[HttpClient] url=%s", c.url))
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:12:37 +01:00
|
|
|
response := &Response{
|
2017-11-20 06:10:04 +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"),
|
|
|
|
}
|
|
|
|
|
2017-12-19 05:52:46 +01:00
|
|
|
logger.Debug("[HttpClient:%s] OriginalURL=%s, StatusCode=%d, ETag=%s, LastModified=%s, EffectiveURL=%s",
|
|
|
|
request.Method,
|
2017-12-19 02:59:35 +01:00
|
|
|
c.url,
|
|
|
|
response.StatusCode,
|
|
|
|
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-19 05:52:46 +01:00
|
|
|
headers.Add("Accept", "text/html,application/xhtml+xml,application/xml,application/json,image/*")
|
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
|
|
|
}
|