-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
133 lines (116 loc) · 11.9 KB
/
server.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package mse6
import (
"crypto/tls"
"fmt"
"github.com/rs/zerolog/log"
"net/http"
"time"
)
const waitDuration = time.Second * 3
var Version = "v0.5.1"
var Port int
var Prefix string
var rc = 0
const idletimeoutSeconds = 600
type ServerHandler struct {
Methods []string
Pattern string
Handler http.HandlerFunc
}
var handlers []ServerHandler
func addHandlerFunc(methods []string, pattern string, f http.HandlerFunc) {
h := ServerHandler{
Methods: methods,
Pattern: Prefix + pattern,
Handler: f,
}
handlers = append(handlers, h)
http.HandleFunc(h.Pattern, f)
}
func Bootstrap(port int, prefix string, tlsMode bool) {
Port = port
Prefix = prefix
mode := "http"
if tlsMode {
mode = "tls"
}
log.Info().Msgf("mse6 %s starting %s server on port %d with prefix '%s'", Version, mode, Port, Prefix)
addHandlerFunc([]string{"GET"}, "badcontentlength", badcontentlength)
addHandlerFunc([]string{"GET"}, "badgzip", badgzipf)
addHandlerFunc([]string{"GET"}, "brotli", brotlif)
addHandlerFunc([]string{"CONNECT"}, "connect", connect)
addHandlerFunc([]string{"GET"}, "choose", chooseaef)
addHandlerFunc([]string{"GET"}, "chunked", chunked)
addHandlerFunc([]string{"DELETE"}, "delete", delete)
addHandlerFunc([]string{"GET"}, "deflate", deflatef)
addHandlerFunc([]string{"GET"}, "echoheader", echoheader)
addHandlerFunc([]string{"GET"}, "echoquery", echoquery)
addHandlerFunc([]string{"GET"}, "echoport", echoport)
addHandlerFunc([]string{"GET"}, "formget", formget)
addHandlerFunc([]string{"POST"}, "formpost", formpost)
addHandlerFunc([]string{"GET"}, "get", get)
addHandlerFunc([]string{"GET", "HEAD"}, "getorhead", getorhead)
addHandlerFunc([]string{"GET"}, "gzip", gzipf)
addHandlerFunc([]string{"GET"}, "hangupduringheader", hangupConnDuringHeadersSend)
addHandlerFunc([]string{"GET"}, "hangupafterheader", hangupConnAfterHeadersSent)
addHandlerFunc([]string{"GET"}, "hangupduringbody", hangupConnDuringBodySend)
addHandlerFunc([]string{"GET"}, "jwks", jwks)
addHandlerFunc([]string{"GET"}, "jwkses256", jwkses256)
addHandlerFunc([]string{"GET"}, "jwksbad", jwksbad)
addHandlerFunc([]string{"GET"}, "jwksmix", jwksmix)
addHandlerFunc([]string{"GET"}, "jwksrotate", jwksrotate)
addHandlerFunc([]string{"GET"}, "jwksbadrotate", jwksbadrotate)
addHandlerFunc([]string{"GET"}, "nocontentenc", nocontentenc)
addHandlerFunc([]string{"OPTIONS"}, "options", options)
addHandlerFunc([]string{"PATCH"}, "patch", patch)
addHandlerFunc([]string{"POST"}, "post", post)
addHandlerFunc([]string{"PUT"}, "put", put)
addHandlerFunc([]string{"GET"}, "redirected", redirected)
addHandlerFunc([]string{"GET"}, "send", send)
addHandlerFunc([]string{"GET"}, "slowheader", slowheader)
addHandlerFunc([]string{"GET"}, "slowbody", slowbody)
addHandlerFunc([]string{"TRACE"}, "trace", trace)
addHandlerFunc([]string{"GET"}, "tiny", tinyidentityf)
addHandlerFunc([]string{"GET"}, "tinygzip", tinygzipf)
addHandlerFunc([]string{"GET"}, "unknowncontentenc", unknowncontentenc)
addHandlerFunc([]string{"GET"}, "websocket", websocket)
//catchall. Matches everything that wasn't previously matched.
http.HandleFunc("/", index)
var err error
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
IdleTimeout: time.Duration(idletimeoutSeconds * time.Second),
}
if tlsMode == false {
err = server.ListenAndServe()
} else {
chain, _ := getCert()
server.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{chain},
}
err = server.ListenAndServeTLS("", "")
}
if err != nil {
panic(err.Error())
}
}
func index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "mse6 "+Version)
w.Header().Set("Content-Encoding", "identity")
w.WriteHeader(200)
w.Write([]byte("mse6 " + Version))
for _, v := range handlers {
w.Write([]byte(fmt.Sprintf("\n%v %s", v.Methods, v.Pattern)))
}
log.Info().Msgf("served %v request with X-Request-Id %s", r.URL.Path, getXRequestId(r))
}
func getCert() (tls.Certificate, error) {
certPem := "-----BEGIN CERTIFICATE-----\nMIIFtDCCA5ygAwIBAgICEAIwDQYJKoZIhvcNAQELBQAwdjELMAkGA1UEBhMCQVUx\nDDAKBgNVBAgMA05TVzENMAsGA1UECgwEbXljYTEaMBgGA1UECwwRbXljYSBpbnRl\ncm1lZGlhdGUxLjAsBgNVBAMMJW15IGNlcnRpZmljYXRlIGF1dGhvcml0eSBpbnRl\ncm1lZGlhdGUwIBcNMjAwOTE5MDQxNjAwWhgPMjEwMjExMDkwNDE2MDBaMGIxCzAJ\nBgNVBAYTAkFVMQwwCgYDVQQIDANOU1cxDzANBgNVBAcMBlN5ZG5leTEOMAwGA1UE\nCgwFY2VydDIxETAPBgNVBAsMCGNlcnQyIG91MREwDwYDVQQDDAhjZXJ0MiBjbjCC\nASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAN3FFHDc3fWIyxukMDRriEbY\ntVA41EeiQiwf7RLdDxh+N2VAazUbbxUJ06nKAslX2+6ZmJrMlS+ionX1BvPhPy3s\nnuZI1movXcvH6ZV5yUGZyJDocjOTHHqNwPSDOAQX87tLjQbCa8Rw//B488GoPbaZ\nlWYDvZQ0Mw5rasiu0B+OI6PL8+Vnc2jXdPlc3tiNoIVXRZ14TNei7bUDA3O1y593\nift2tQ/TZxlY7fylZWhTV4sUm/9yk/zob+dyzro795Jy8vThlePAN//tZGLWFzG7\na8o9Mx36BPncSZ0v+EfEv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ojMXft1TnwYKb8H0ce6qcsBHZmd/M7IXf\nhyedRkcm7RuN7ayNjFA+44pwAr4jMMNklBQDpGD5yYt1jsltiYoYX5bwZdn2I/rr\nwNQ/FfNSp8rJWqtBhaEt0VI+snHuy0Gdx1eQGf4bJNzvsDLjjJuQ32VUjaCOzsd1\n7d8jR/yjR3Sq20oFEu3HqFSC9OCH2QORTqf6i2IkaUeJbkVTa8+uVceDDbRs3CwY\nVgk/4WcOzcrz0F2BJPpFQ4knrSuHgUbElPHPVuZcn3XZ0n1KBXZdNVCIyLVRowdr\nI+gNEgWE3670Osx55QWg7depP7hU30nQlC1cm2ej2MxM48ddbAL4Zqs8/W1gm+Xb\nDkTsfh81QZQaw6qFVGHJNRIyfMT68ekFB8AgqntulIFR2RJTr/3QJBMhGHKQkmcT\nsa0z0ZrmS/ieurRUjaCsud10Y5VbY5Y8ll5kPsuRWuyijftjcPFqHBzLSSdLacO9\nlVIGkTA3ARCGgym3v5+ZZJ4DeLOJRz9c9OCIASlCkNFFEm1aJ8oagynh2tYqe5TK\nCva1MX8QW5OjHbrm1xvQ8uZOSj55yuBQWKH47GF4QxiojzKikLv4Cpv2Tk5SR9qv\nq3C4t8B26KurNb4z99eo5XhW5XXvQdKZTQC9BqZDN7xhQlwm5lbRSuhZMBJJaQOS\n-----END CERTIFICATE-----\n"
cB := []byte(certPem + interCAPem + rootCAPem)
kB := []byte(keyPem)
chain, err1 := tls.X509KeyPair(cB, kB)
return chain, err1
}