forked from knative/serving
/
httpproxy.go
104 lines (87 loc) · 2.66 KB
/
httpproxy.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
/*
Copyright 2018 The Knative 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
https://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 main
import (
"flag"
"fmt"
"log"
"os"
"net/http"
"net/http/httputil"
"net/url"
"knative.dev/serving/pkg/network"
"knative.dev/serving/test"
)
const (
targetHostEnv = "TARGET_HOST"
gatewayHostEnv = "GATEWAY_HOST"
portEnv = "PORT" // Allow port to be customized / randomly assigned by tests
defaultPort = "8080"
)
var (
httpProxy *httputil.ReverseProxy
)
func handler(w http.ResponseWriter, r *http.Request) {
log.Print("HTTP proxy received a request.")
// Reverse proxy does not automatically reset the Host header.
// We need to manually reset it.
r.Host = getTargetHostEnv()
httpProxy.ServeHTTP(w, r)
}
func getPort() string {
value := os.Getenv(portEnv)
if value == "" {
return defaultPort
}
return value
}
func getTargetHostEnv() string {
value := os.Getenv(targetHostEnv)
if value == "" {
log.Fatalf("No env %v provided.", targetHostEnv)
}
return value
}
func initialHTTPProxy(proxyURL string) *httputil.ReverseProxy {
target, err := url.Parse(proxyURL)
if err != nil {
log.Fatalf("Failed to parse url %v", proxyURL)
}
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.ErrorHandler = func(w http.ResponseWriter, req *http.Request, err error) {
log.Printf("error reverse proxying request: %v", err)
http.Error(w, err.Error(), http.StatusBadGateway)
}
return proxy
}
func main() {
flag.Parse()
log.Print("HTTP Proxy app started.")
targetHost := getTargetHostEnv()
port := getPort()
// Gateway is an optional value. It is used only when resolvable domain is not set
// for external access test, as xip.io is flaky.
// ref: https://github.com/knative/serving/issues/5389
gateway := os.Getenv(gatewayHostEnv)
if gateway != "" {
targetHost = gateway
}
targetURL := fmt.Sprintf("http://%s", targetHost)
log.Print("target is " + targetURL)
httpProxy = initialHTTPProxy(targetURL)
address := fmt.Sprintf(":%s", port)
log.Printf("Listening on address: %s", address)
// Handle forwarding requests which uses "K-Network-Hash" header.
probeHandler := network.NewProbeHandler(http.HandlerFunc(handler)).ServeHTTP
test.ListenAndServeGracefully(address, probeHandler)
}