-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathh1quic.go
93 lines (70 loc) · 1.42 KB
/
h1quic.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
package main
import (
"bufio"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/http/httputil"
"time"
"github.com/lucas-clemente/quic-go"
)
func h1OverQUIC(network, local, addr, rawurl string,
tlsCfg *tls.Config, cfg *quic.Config,
buffer []byte, dst io.Writer) {
d := time.Now()
var session quic.Session
stream, err := dial(local, tlsCfg, cfg, session)(network, addr)
if err != nil {
fmt.Println(err)
return
}
defer func() {
if session != nil {
session.Close()
}
}()
defer stream.Close()
handshake := time.Now()
req, err := http.NewRequest("GET", rawurl, nil)
if err != nil {
fmt.Println(err)
return
}
data, err := httputil.DumpRequestOut(req, true)
if err != nil {
fmt.Println(err)
return
}
fmt.Println()
fmt.Println(string(data))
if _, err := stream.Write(data); err != nil {
fmt.Println(err)
return
}
reader := bufio.NewReader(stream)
resp, err := http.ReadResponse(reader, req)
if err != nil {
fmt.Println(err)
return
}
readresp := time.Now()
fmt.Println("handshake:", handshake.Sub(d).String())
fmt.Println("recvresp:", readresp.Sub(handshake).String())
fmt.Println()
rp, err := DumpResponse(resp)
fmt.Println(string(rp))
if err != nil {
fmt.Println(err)
}
if resp != nil && resp.Body != nil {
if _, err := io.CopyBuffer(dst, resp.Body, buffer); err != nil {
fmt.Println(err)
return
}
}
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
return
}