/
http_fast.go
146 lines (125 loc) · 4.17 KB
/
http_fast.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package http
import (
"bytes"
"crypto/tls"
"fmt"
"os"
"strings"
"time"
"github.com/valyala/fasthttp"
)
var FasthttpDialer = &fasthttp.TCPDialer{
Concurrency: 1000, // Concurrent Dials
DNSCacheDuration: fasthttp.DefaultDNSCacheDuration * 60,
Resolver: DnsResolver,
}
var clientFast = &fasthttp.Client{
NoDefaultUserAgentHeader: true,
DisableHeaderNamesNormalizing: true,
MaxResponseBodySize: 1024 * 1024 * 10,
ReadBufferSize: 4096 * 3,
ReadTimeout: time.Second * 180,
Dial: FasthttpHTTPDialerProxyTimeout(strings.TrimPrefix(os.Getenv("PROXY_URL"), "http://"), time.Second*60),
// Dial: func(addr string) (net.Conn, error) {
// return FasthttpDialer.DialTimeout(addr, time.Second*60)
// },
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
func GetFast(requestURI string) ([]byte, int, error) {
// Acquire request and response from pool
req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
defer func() {
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(res)
}()
// Set new request
req.SetRequestURI(requestURI)
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
req.Header.Set("Accept-Encoding", "gzip, deflate, br")
req.Header.Set("Accept-Language", "en-US,en;q=0.9")
req.Header.Set("Sec-Ch-Ua", `"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"`)
req.Header.Set("Sec-Ch-Ua-Mobile", "?0")
req.Header.Set("Sec-Ch-Ua-Platform", `"macOS"`)
req.Header.Set("Sec-Fetch-Dest", "document")
req.Header.Set("Sec-Fetch-Mode", "navigate")
req.Header.Set("Sec-Fetch-Site", "none")
req.Header.Set("Sec-Fetch-User", "?1")
req.Header.Set("Upgrade-Insecure-Requests", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36")
// Do request
err := clientFast.DoRedirects(req, res, 10)
if err != nil {
return nil, res.StatusCode(), fmt.Errorf("client do: %w", err)
}
body, err := handleResponseFast(res)
if err != nil {
return nil, res.StatusCode(), fmt.Errorf("handle response err: %w", err)
}
return body, res.StatusCode(), nil
}
func handleResponseFast(res *fasthttp.Response) ([]byte, error) {
// Check if its HTML
contentType := res.Header.Peek(fasthttp.HeaderContentType)
if !bytes.Contains(contentType, []byte("html")) {
return nil, fmt.Errorf("not HTML")
}
// Check status code
if res.StatusCode() != 200 {
return nil, fmt.Errorf("status not 200: %v", res.StatusCode())
}
// Read and decode response body
body, err := decodeResponseFast(res)
if err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
return body, nil
}
func decodeResponseFast(res *fasthttp.Response) ([]byte, error) {
var err error
var body []byte
contentEncoding := res.Header.Peek(fasthttp.HeaderContentEncoding)
switch string(contentEncoding) {
case "gzip":
body, err = res.BodyGunzip()
if err != nil {
return nil, fmt.Errorf("gunzip: %w", err)
}
case "deflate":
body, err = res.BodyInflate()
if err != nil {
return nil, fmt.Errorf("inflate: %w", err)
}
case "br":
body, err = res.BodyUnbrotli()
if err != nil {
return nil, fmt.Errorf("unbrotli: %w", err)
}
default:
body = res.Body()
}
// Charset Decoding
// contentType := res.Header.Peek(fasthttp.HeaderContentType)
// _, params, err := mime.ParseMediaType(string(contentType))
// if err != nil {
// return nil, fmt.Errorf("parse media type: %w", err)
// }
// if cs, ok := params["charset"]; ok {
// encoding, name := charset.Lookup(cs)
// if encoding == nil {
// return nil, fmt.Errorf("charset lookup: %v", name)
// }
// if encoding != nil {
// // If encoding is not nil, wrap body in a reader that converts from the given encoding to UTF-8.
// bodyReader := transform.NewReader(bytes.NewReader(body), encoding.NewDecoder())
// // Read the entire body, now decoded to UTF-8.
// var err error
// body, err = io.ReadAll(bodyReader)
// if err != nil {
// return nil, fmt.Errorf("read all: %w", err)
// }
// }
// }
return body, nil
}