forked from istio/istio
/
http_server.go
180 lines (159 loc) · 4.71 KB
/
http_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package env
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strconv"
"sync"
"time"
)
// If HTTP header has non empty FailHeader,
// HTTP server will fail the request with 400 with FailBody in the response body.
const (
FailHeader = "x-istio-backend-fail"
FailBody = "Bad request from backend."
)
const publicKey = `
{
"keys": [
{
"alg": "RS256",
"e": "AQAB",
"kid": "62a93512c9ee4c7f8067b5a216dade2763d32a47",
"kty": "RSA",
"n": "` +
"0YWnm_eplO9BFtXszMRQNL5UtZ8HJdTH2jK7vjs4XdLkPW7YBkkm_2xNgcaVpkW0VT2l4mU3KftR-6" +
"s3Oa5Rnz5BrWEUkCTVVolR7VYksfqIB2I_x5yZHdOiomMTcm3DheUUCgbJRv5OKRnNqszA4xHn3tA3" +
"Ry8VO3X7BgKZYAUh9fyZTFLlkeAh0-bLK5zvqCmKW5QgDIXSxUTJxPjZCgfx1vmAfGqaJb-nvmrORX" +
"Q6L284c73DUL7mnt6wj3H6tVqPKA27j56N0TB1Hfx4ja6Slr8S4EB3F1luYhATa1PKUSH8mYDW11Ho" +
"lzZmTQpRoLV8ZoHbHEaTfqX_aYahIw" +
`",
"use": "sig"
},
{
"alg": "RS256",
"e": "AQAB",
"kid": "b3319a147514df7ee5e4bcdee51350cc890cc89e",
"kty": "RSA",
"n": "` +
"qDi7Tx4DhNvPQsl1ofxxc2ePQFcs-L0mXYo6TGS64CY_2WmOtvYlcLNZjhuddZVV2X88m0MfwaSA16w" +
"E-RiKM9hqo5EY8BPXj57CMiYAyiHuQPp1yayjMgoE1P2jvp4eqF-BTillGJt5W5RuXti9uqfMtCQdag" +
"B8EC3MNRuU_KdeLgBy3lS3oo4LOYd-74kRBVZbk2wnmmb7IhP9OoLc1-7-9qU1uhpDxmE6JwBau0mDS" +
"wMnYDS4G_ML17dC-ZDtLd1i24STUw39KH0pcSdfFbL2NtEZdNeam1DDdk0iUtJSPZliUHJBI_pj8M-2" +
"Mn_oA8jBuI8YKwBqYkZCN1I95Q" +
`",
"use": "sig"
}
]
}
`
// HTTPServer stores data for a HTTP server.
type HTTPServer struct {
port uint16
lis net.Listener
reqHeaders http.Header
mu sync.Mutex
}
func pubkeyHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%v", publicKey)
}
// handle handles a request and sends response. If ?delay=n is in request URL, then sleeps for
// n second and sends response.
func (s *HTTPServer) handle(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Fail if there is such header.
if r.Header.Get(FailHeader) != "" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(FailBody))
return
}
// echo back the Content-Type and Content-Length in the response
for _, k := range []string{"Content-Type", "Content-Length"} {
if v := r.Header.Get(k); v != "" {
w.Header().Set(k, v)
}
}
if delay := r.URL.Query().Get("delay"); delay != "" {
delaySeconds, err := strconv.ParseInt(delay, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("Bad request parameter: delay"))
return
}
time.Sleep(time.Duration(delaySeconds) * time.Second)
}
w.WriteHeader(http.StatusOK)
reqHeaders := make(http.Header)
reqHeaders[":method"] = []string{r.Method}
reqHeaders[":authority"] = []string{r.Host}
reqHeaders[":path"] = []string{r.URL.String()}
for name, headers := range r.Header {
for _, h := range headers {
reqHeaders[name] = append(reqHeaders[name], h)
}
}
s.mu.Lock()
s.reqHeaders = reqHeaders
s.mu.Unlock()
w.Write(body)
}
// NewHTTPServer creates a new HTTP server.
func NewHTTPServer(port uint16) (*HTTPServer, error) {
log.Printf("Http server listening on port %v\n", port)
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatal(err)
return nil, err
}
return &HTTPServer{
port: port,
lis: lis,
}, nil
}
// Start starts the server
func (s *HTTPServer) Start() <-chan error {
errCh := make(chan error)
go func() {
http.HandleFunc("/", s.handle)
http.HandleFunc("/pubkey", pubkeyHandler)
errCh <- http.Serve(s.lis, nil)
}()
go func() {
url := fmt.Sprintf("http://localhost:%v/echo", s.port)
errCh <- WaitForHTTPServer(url)
}()
return errCh
}
// Stop shutdown the server
func (s *HTTPServer) Stop() {
log.Printf("Close HTTP server\n")
_ = s.lis.Close()
log.Printf("Close HTTP server -- Done\n")
}
// LastRequestHeaders returns the headers from the last request and clears the value
func (s *HTTPServer) LastRequestHeaders() http.Header {
s.mu.Lock()
out := s.reqHeaders
s.reqHeaders = nil
s.mu.Unlock()
return out
}