-
Notifications
You must be signed in to change notification settings - Fork 0
/
gohtt2_client.go
executable file
·60 lines (54 loc) · 1.33 KB
/
gohtt2_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 (
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"golang.org/x/net/http2"
"io/ioutil"
"log"
"net/http"
)
const url = "https://localhost:8000"
var httpVersion = flag.Int("version", 2, "HTTP version")
/*
code from article : https://developpaper.com/the-http-2-server-function-of-go-language-and-the-use-of-client/
*/
func main() {
flag.Parse()
client := &http.Client{}
// Create a pool with the server certificate since it is not signed
// by a known CA
caCert, err := ioutil.ReadFile("server.crt")
if err != nil {
log.Fatalf("Reading server certificate: %s", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Create TLS configuration with the certificate of the server
tlsConfig := &tls.Config{
RootCAs: caCertPool,
}
// Use the proper transport in the client
switch *httpVersion {
case 1:
client.Transport = &http.Transport{
TLSClientConfig: tlsConfig,
}
case 2:
client.Transport = &http2.Transport{
TLSClientConfig: tlsConfig,
}
}
// Perform the request
resp, err := client.Get(url)
if err != nil {
log.Fatalf("Failed get: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Failed reading response body: %s", err)
}
fmt.Printf("Got response %d: %s %s\n", resp.StatusCode, resp.Proto, string(body))
}