-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.go
66 lines (54 loc) · 1.53 KB
/
main.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
package main
import (
"fmt"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"net"
"net/http"
"os"
)
func checkErr(err error, msg string) {
if err == nil {
return
}
fmt.Printf("ERROR: %s: %s\n", msg, err)
os.Exit(1)
}
func main() {
H2CServerUpgrade()
//H2CServerPrior()
}
// H2CServerUpgrade server supports "H2C upgrade" and "H2C prior knowledge" along with
// standard HTTP/2 and HTTP/1.1 that golang natively supports.
func H2CServerUpgrade() {
h2s := &http2.Server{}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %v, http: %v", r.URL.Path, r.TLS == nil)
})
server := &http.Server{
Addr: "0.0.0.0:1010",
Handler: h2c.NewHandler(handler, h2s),
}
checkErr(http2.ConfigureServer(server, h2s), "during call to ConfigureServer()")
fmt.Printf("Listening [0.0.0.0:1010]...\n")
checkErr(server.ListenAndServe(), "while listening")
}
// H2CServerPrior server only supports "H2C prior knowledge".
// You can add standard HTTP/2 support by adding a TLS config.
func H2CServerPrior() {
server := http2.Server{}
l, err := net.Listen("tcp", "0.0.0.0:1010")
checkErr(err, "while listening")
fmt.Printf("Listening [0.0.0.0:1010]...\n")
for {
conn, err := l.Accept()
checkErr(err, "during accept")
server.ServeConn(conn, &http2.ServeConnOpts{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("New Connection: %+v\n", r)
fmt.Fprintf(w, "Hello, %v, http: %v", r.URL.Path, r.TLS == nil)
}),
})
_ = conn.Close()
}
}