forked from raviqqe/muffet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasthttp_http_client.go
60 lines (48 loc) · 1.14 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
52
53
54
55
56
57
58
59
60
package main
import (
"net/http"
"net/url"
"strings"
"time"
"github.com/valyala/fasthttp"
)
type fasthttpHttpClient struct {
client *fasthttp.Client
timeout time.Duration
header http.Header
}
func newFasthttpHttpClient(c *fasthttp.Client, timeout time.Duration, header http.Header) httpClient {
return &fasthttpHttpClient{c, timeout, header}
}
func (c *fasthttpHttpClient) Get(u *url.URL, header http.Header) (httpResponse, error) {
req, res := fasthttp.Request{}, fasthttp.Response{}
req.SetRequestURI(u.String())
req.SetConnectionClose()
for k, vs := range c.header {
for _, v := range vs {
req.Header.Add(k, v)
}
}
for k, vs := range header {
for _, v := range vs {
req.Header.Add(k, v)
}
}
// Some HTTP servers require "Accept" headers set explicitly.
if !includeHeader(c.header, "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(h http.Header, k string) bool {
for kk := range h {
if strings.EqualFold(kk, k) {
return true
}
}
return false
}