-
Notifications
You must be signed in to change notification settings - Fork 4
/
3.3.2-http3-client.go
77 lines (69 loc) · 1.55 KB
/
3.3.2-http3-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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言高级开发与实战》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 知乎:https://www.zhihu.com/people/shirdonl
// 公众号:源码大数据
// 仓库地址:https://gitee.com/shirdonl/goAdvanced
// 仓库地址:https://github.com/shirdonl/goAdvanced
//++++++++++++++++++++++++++++++++++++++++
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"gitee.com/shirdonl/goAdvanced/chapter3/testdata"
"github.com/lucas-clemente/quic-go"
"io"
"log"
"net/http"
"sync"
"github.com/lucas-clemente/quic-go/http3"
)
func main() {
//解析命令行参数
flag.Parse()
urls := flag.Args()
//HTTP3 密钥配置
pool, err := x509.SystemCertPool()
if err != nil {
log.Fatal(err)
}
testdata.AddRootCA(pool)
var qconf quic.Config
//HTTP3配置
roundTripper := &http3.RoundTripper{
TLSClientConfig: &tls.Config{
RootCAs: pool,
},
QuicConfig: &qconf,
}
defer roundTripper.Close()
httpClient := &http.Client{
Transport: roundTripper,
}
var wg sync.WaitGroup
wg.Add(len(urls))
for _, addr := range urls {
fmt.Printf("GET %s", addr)
go func(addr string) {
rsp, err := httpClient.Get(addr)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Got response for %s: %#v", addr, rsp)
body := &bytes.Buffer{}
_, err = io.Copy(body, rsp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Request Body:")
fmt.Printf("%s", body.Bytes())
wg.Done()
}(addr)
}
wg.Wait()
}