miniflux/http/client.go

118 lines
3 KiB
Go
Raw Normal View History

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 (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"time"
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
)
// 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
// Client is a HTTP Client :)
type Client struct {
2017-11-20 06:10:04 +01:00
url string
etagHeader string
lastModifiedHeader string
2017-12-03 04:32:14 +01:00
username string
password string
2017-11-20 06:10:04 +01:00
Insecure bool
}
// Get execute a GET HTTP request.
2017-12-03 04:32:14 +01:00
func (c *Client) Get() (*Response, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[HttpClient:Get] url=%s", c.url))
2017-11-20 06:10:04 +01:00
2017-12-03 04:32:14 +01:00
client := c.buildClient()
resp, err := client.Do(c.buildRequest())
2017-11-20 06:10:04 +01:00
if err != nil {
return nil, err
}
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"),
}
logger.Debug("[HttpClient:Get] OriginalURL=%s, StatusCode=%d, ETag=%s, LastModified=%s, EffectiveURL=%s",
c.url,
response.StatusCode,
response.ETag,
response.LastModified,
response.EffectiveURL,
2017-11-20 06:10:04 +01:00
)
return response, err
}
2017-12-03 04:32:14 +01:00
func (c *Client) buildRequest() *http.Request {
link, _ := url.Parse(c.url)
request := &http.Request{
URL: link,
Method: http.MethodGet,
Header: c.buildHeaders(),
}
if c.username != "" && c.password != "" {
request.SetBasicAuth(c.username, c.password)
}
return request
}
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)
headers.Add("User-Agent", userAgent)
headers.Add("Accept", "text/html,application/xhtml+xml,application/xml,application/json")
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
}
return headers
}
// 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-03 04:32:14 +01:00
// NewClientWithCredentials returns a new HTTP client that require authentication.
func NewClientWithCredentials(url, username, password string) *Client {
return &Client{url: url, Insecure: false, username: username, password: password}
}
// 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
}