2017-11-20 00:10:04 -05: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 (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
2017-11-20 20:12:37 -05:00
|
|
|
|
|
|
|
"github.com/miniflux/miniflux2/helper"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2017-11-20 20:12:37 -05:00
|
|
|
const userAgent = "Miniflux <https://miniflux.net/>"
|
2017-11-20 22:44:28 -05:00
|
|
|
const requestTimeout = 300
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2017-11-20 20:12:37 -05:00
|
|
|
// Client is a HTTP Client :)
|
|
|
|
type Client struct {
|
2017-11-20 00:10:04 -05:00
|
|
|
url string
|
|
|
|
etagHeader string
|
|
|
|
lastModifiedHeader string
|
|
|
|
Insecure bool
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:12:37 -05:00
|
|
|
// Get execute a GET HTTP request.
|
|
|
|
func (h *Client) Get() (*Response, error) {
|
2017-11-20 00:10:04 -05:00
|
|
|
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[HttpClient:Get] url=%s", h.url))
|
|
|
|
u, _ := url.Parse(h.url)
|
|
|
|
|
|
|
|
req := &http.Request{
|
|
|
|
URL: u,
|
2017-11-25 01:29:20 -05:00
|
|
|
Method: http.MethodGet,
|
2017-11-20 00:10:04 -05:00
|
|
|
Header: h.buildHeaders(),
|
|
|
|
}
|
|
|
|
|
|
|
|
client := h.buildClient()
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:12:37 -05:00
|
|
|
response := &Response{
|
2017-11-20 00:10:04 -05: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"),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("[HttpClient:Get]",
|
|
|
|
"OriginalURL:", h.url,
|
|
|
|
"StatusCode:", response.StatusCode,
|
|
|
|
"ETag:", response.ETag,
|
|
|
|
"LastModified:", response.LastModified,
|
|
|
|
"EffectiveURL:", response.EffectiveURL,
|
|
|
|
)
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:12:37 -05:00
|
|
|
func (h *Client) buildClient() http.Client {
|
2017-11-20 22:44:28 -05:00
|
|
|
client := http.Client{Timeout: time.Duration(requestTimeout * time.Second)}
|
2017-11-20 00:10:04 -05:00
|
|
|
if h.Insecure {
|
2017-11-20 22:44:28 -05:00
|
|
|
client.Transport = &http.Transport{
|
2017-11-20 00:10:04 -05:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:44:28 -05:00
|
|
|
return client
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 20:12:37 -05:00
|
|
|
func (h *Client) buildHeaders() http.Header {
|
2017-11-20 00:10:04 -05:00
|
|
|
headers := make(http.Header)
|
2017-11-20 20:12:37 -05:00
|
|
|
headers.Add("User-Agent", userAgent)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
if h.etagHeader != "" {
|
|
|
|
headers.Add("If-None-Match", h.etagHeader)
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.lastModifiedHeader != "" {
|
|
|
|
headers.Add("If-Modified-Since", h.lastModifiedHeader)
|
|
|
|
}
|
|
|
|
|
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:12:37 -05:00
|
|
|
// NewClient returns a new HTTP client.
|
|
|
|
func NewClient(url string) *Client {
|
|
|
|
return &Client{url: url, Insecure: false}
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 20:12:37 -05: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 00:10:04 -05:00
|
|
|
}
|