client.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package http
  5. import (
  6. "crypto/tls"
  7. "fmt"
  8. "log"
  9. "net/http"
  10. "net/url"
  11. "time"
  12. "github.com/miniflux/miniflux2/helper"
  13. )
  14. const userAgent = "Miniflux <https://miniflux.net/>"
  15. const requestTimeout = 300
  16. // Client is a HTTP Client :)
  17. type Client struct {
  18. url string
  19. etagHeader string
  20. lastModifiedHeader string
  21. username string
  22. password string
  23. Insecure bool
  24. }
  25. // Get execute a GET HTTP request.
  26. func (c *Client) Get() (*Response, error) {
  27. defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[HttpClient:Get] url=%s", c.url))
  28. client := c.buildClient()
  29. resp, err := client.Do(c.buildRequest())
  30. if err != nil {
  31. return nil, err
  32. }
  33. response := &Response{
  34. Body: resp.Body,
  35. StatusCode: resp.StatusCode,
  36. EffectiveURL: resp.Request.URL.String(),
  37. LastModified: resp.Header.Get("Last-Modified"),
  38. ETag: resp.Header.Get("ETag"),
  39. ContentType: resp.Header.Get("Content-Type"),
  40. }
  41. log.Println("[HttpClient:Get]",
  42. "OriginalURL:", c.url,
  43. "StatusCode:", response.StatusCode,
  44. "ETag:", response.ETag,
  45. "LastModified:", response.LastModified,
  46. "EffectiveURL:", response.EffectiveURL,
  47. )
  48. return response, err
  49. }
  50. func (c *Client) buildRequest() *http.Request {
  51. link, _ := url.Parse(c.url)
  52. request := &http.Request{
  53. URL: link,
  54. Method: http.MethodGet,
  55. Header: c.buildHeaders(),
  56. }
  57. if c.username != "" && c.password != "" {
  58. request.SetBasicAuth(c.username, c.password)
  59. }
  60. return request
  61. }
  62. func (c *Client) buildClient() http.Client {
  63. client := http.Client{Timeout: time.Duration(requestTimeout * time.Second)}
  64. if c.Insecure {
  65. client.Transport = &http.Transport{
  66. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  67. }
  68. }
  69. return client
  70. }
  71. func (c *Client) buildHeaders() http.Header {
  72. headers := make(http.Header)
  73. headers.Add("User-Agent", userAgent)
  74. if c.etagHeader != "" {
  75. headers.Add("If-None-Match", c.etagHeader)
  76. }
  77. if c.lastModifiedHeader != "" {
  78. headers.Add("If-Modified-Since", c.lastModifiedHeader)
  79. }
  80. return headers
  81. }
  82. // NewClient returns a new HTTP client.
  83. func NewClient(url string) *Client {
  84. return &Client{url: url, Insecure: false}
  85. }
  86. // NewClientWithCredentials returns a new HTTP client that require authentication.
  87. func NewClientWithCredentials(url, username, password string) *Client {
  88. return &Client{url: url, Insecure: false, username: username, password: password}
  89. }
  90. // NewClientWithCacheHeaders returns a new HTTP client that send cache headers.
  91. func NewClientWithCacheHeaders(url, etagHeader, lastModifiedHeader string) *Client {
  92. return &Client{url: url, etagHeader: etagHeader, lastModifiedHeader: lastModifiedHeader, Insecure: false}
  93. }