@@ -17,26 +17,14 @@ import (
17
17
// the target request will be for /base/dir.
18
18
//
19
19
// Relative to httputil.NewSingleHostReverseProxy with some additions.
20
+ //
21
+ // Look `ProxyHandlerRemote` too.
20
22
func ProxyHandler (target * url.URL ) * httputil.ReverseProxy {
21
- targetQuery := target .RawQuery
22
23
director := func (req * http.Request ) {
23
- req .URL .Scheme = target .Scheme
24
- req .URL .Host = target .Host
25
- req .Host = target .Host
26
-
24
+ modifyProxiedRequest (req , target )
27
25
req .URL .Path = path .Join (target .Path , req .URL .Path )
28
-
29
- if targetQuery == "" || req .URL .RawQuery == "" {
30
- req .URL .RawQuery = targetQuery + req .URL .RawQuery
31
- } else {
32
- req .URL .RawQuery = targetQuery + "&" + req .URL .RawQuery
33
- }
34
-
35
- if _ , ok := req .Header ["User-Agent" ]; ! ok {
36
- // explicitly disable User-Agent so it's not set to default value
37
- req .Header .Set ("User-Agent" , "" )
38
- }
39
26
}
27
+
40
28
p := & httputil.ReverseProxy {Director : director }
41
29
42
30
if netutil .IsLoopbackHost (target .Host ) {
@@ -49,37 +37,41 @@ func ProxyHandler(target *url.URL) *httputil.ReverseProxy {
49
37
return p
50
38
}
51
39
40
+ func modifyProxiedRequest (req * http.Request , target * url.URL ) {
41
+ req .URL .Scheme = target .Scheme
42
+ req .URL .Host = target .Host
43
+ req .Host = target .Host
44
+
45
+ if target .RawQuery == "" || req .URL .RawQuery == "" {
46
+ req .URL .RawQuery = target .RawQuery + req .URL .RawQuery
47
+ } else {
48
+ req .URL .RawQuery = target .RawQuery + "&" + req .URL .RawQuery
49
+ }
50
+
51
+ if _ , ok := req .Header ["User-Agent" ]; ! ok {
52
+ // explicitly disable User-Agent so it's not set to default value
53
+ req .Header .Set ("User-Agent" , "" )
54
+ }
55
+ }
56
+
52
57
// ProxyHandlerRemote returns a new ReverseProxy that rewrites
53
58
// URLs to the scheme, host, and path provided in target.
54
59
// Case 1: req.Host == target.Host
55
60
// behavior same as ProxyHandler
56
61
// Case 2: req.Host != target.Host
57
62
// the target request will be forwarded to the target's url
58
- // insecureSkipVerify indicates enable ssl certificate verification or not
63
+ // insecureSkipVerify indicates enable ssl certificate verification or not.
64
+ //
65
+ // Look `ProxyHandler` too.
59
66
func ProxyHandlerRemote (target * url.URL , insecureSkipVerify bool ) * httputil.ReverseProxy {
60
- targetQuery := target .RawQuery
61
67
director := func (req * http.Request ) {
62
- req . URL . Scheme = target . Scheme
68
+ modifyProxiedRequest ( req , target )
63
69
64
70
if req .Host != target .Host {
65
71
req .URL .Path = target .Path
66
72
} else {
67
73
req .URL .Path = path .Join (target .Path , req .URL .Path )
68
74
}
69
-
70
- req .URL .Host = target .Host
71
- req .Host = target .Host
72
-
73
- if targetQuery == "" || req .URL .RawQuery == "" {
74
- req .URL .RawQuery = targetQuery + req .URL .RawQuery
75
- } else {
76
- req .URL .RawQuery = targetQuery + "&" + req .URL .RawQuery
77
- }
78
-
79
- if _ , ok := req .Header ["User-Agent" ]; ! ok {
80
- // explicitly disable User-Agent so it's not set to default value
81
- req .Header .Set ("User-Agent" , "" )
82
- }
83
75
}
84
76
p := & httputil.ReverseProxy {Director : director }
85
77
0 commit comments