forked from raviqqe/muffet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasthttp_http_client.go
51 lines (40 loc) · 1.01 KB
/
fasthttp_http_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"net/url"
"strings"
"time"
"github.com/valyala/fasthttp"
)
type fasthttpHttpClient struct {
client *fasthttp.Client
timeout time.Duration
headers map[string]string
}
func newFasthttpHttpClient(c *fasthttp.Client, timeout time.Duration, headers map[string]string) httpClient {
return &fasthttpHttpClient{c, timeout, headers}
}
func (c *fasthttpHttpClient) Get(u *url.URL) (httpResponse, error) {
req, res := fasthttp.Request{}, fasthttp.Response{}
req.SetRequestURI(u.String())
req.SetConnectionClose()
for k, v := range c.headers {
req.Header.Add(k, v)
}
// Some HTTP servers require "Accept" headers set explicitly.
if !includeHeader(c.headers, "Accept") {
req.Header.Add("Accept", "*/*")
}
err := c.client.DoTimeout(&req, &res, c.timeout)
if err != nil {
return nil, err
}
return newFasthttpHttpResponse(req.URI(), &res), nil
}
func includeHeader(hs map[string]string, h string) bool {
for k := range hs {
if strings.EqualFold(k, h) {
return true
}
}
return false
}