/
main.go
98 lines (78 loc) · 2.06 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
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
package main
import (
"flag"
"html/template"
"log"
"net/http"
"github.com/nathandao/http2status/Godeps/_workspace/src/github.com/gorilla/csrf"
"github.com/nathandao/http2status/Godeps/_workspace/src/github.com/gorilla/mux"
. "github.com/nathandao/http2status/http2status"
)
var header = `
<html>
<head>
<meta charset='utf-8'>
<title>⚡</title>
</head>
<body>
<p>⚡⚡⚡⚡⚡⚡⚡⚡</p>
<p>⚡ HTML/2 checker ⚡</p>
<p>⚡⚡⚡⚡⚡⚡⚡⚡</p>
`
var footer = `
</body>
</html>
`
var form = `
<form method="POST" action="/" accept-charset="UTF-8">
<input type="text" name="url" placeholder="yoursite.com">
{{ .csrfField }}
<input type="submit" value="Check for HTTP2 status!">
</form>
`
var result = `
<p>{{ .err }}</p>
<h2>{{ .siteUrl }}</h2>
<h2>{{ .status }}</h2>
<p>{{ .response }}</p>
`
var tForm = template.Must(template.New("form.tmpl").Parse(form))
var tResult = template.Must(template.New("result.tmpl").Parse(result))
var tHeader = template.Must(template.New("header.tmpl").Parse(header))
var tFooter = template.Must(template.New("footer.tmpl").Parse(footer))
var port string
func main() {
flag.StringVar(&port, "p", "8000", "port to run the server on")
flag.Parse()
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
log.Println("Listen and serving at 127.0.0.1:" + port)
// Add csrf middleware.
http.ListenAndServe(":"+port,
csrf.Protect([]byte("32-byte-long-auth-key"), csrf.Secure(false))(r))
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
tHeader.Execute(w, nil)
tForm.Execute(w, map[string]interface{}{
csrf.TemplateTag: csrf.TemplateField(r),
})
//url := r.URL.Query().Get("url")
url := r.PostFormValue("url")
if url != "" {
obj := map[string]interface{}{}
isH2, res, sanitizedUrl, err := Http2Status(url)
if err != nil {
obj["err"] = err
} else {
if !isH2 {
obj["status"] = "Nope. It's not 1984 anymore, time to upgrade to http2."
} else {
obj["status"] = "You're on HTTP2!"
}
}
obj["response"] = res
obj["siteUrl"] = sanitizedUrl
tResult.Execute(w, obj)
}
tFooter.Execute(w, nil)
}