@@ -19,6 +19,11 @@ import (
19
19
20
20
var redirectRegex = regexp .MustCompile (`^(201|30([1278]))$` )
21
21
22
+ type (
23
+ ctxTargetKey struct {}
24
+ ctxPrefixKey struct {}
25
+ )
26
+
22
27
// Server field TargetForReq should be non-nil
23
28
type Server struct {
24
29
Secure bool // verify SSL certificate
@@ -66,7 +71,7 @@ func (s *Server) Handler() http.HandlerFunc {
66
71
67
72
// rewrite request url, pr.Out and pr.In share the same context
68
73
func (s * Server ) rewrite (pr * httputil.ProxyRequest ) {
69
- target := pr .In .Context ().Value ("target" ).(* url.URL )
74
+ target := pr .In .Context ().Value (ctxTargetKey {} ).(* url.URL )
70
75
71
76
// clean path if not prependPath before SetURL
72
77
if ! s .PrependPath {
@@ -100,7 +105,7 @@ func (s *Server) rewrite(pr *httputil.ProxyRequest) {
100
105
}
101
106
102
107
func (s * Server ) modifyResponse (r * http.Response ) error {
103
- prefix := r .Request .Context ().Value ("prefix" ).(string )
108
+ prefix := r .Request .Context ().Value (ctxPrefixKey {} ).(string )
104
109
if r .StatusCode < 300 {
105
110
s .updateLastActivity (prefix )
106
111
} else {
@@ -159,8 +164,8 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request) {
159
164
ctx , cancel = context .WithTimeout (ctx , time .Duration (s .ProxyTimeout )* time .Millisecond )
160
165
defer cancel ()
161
166
}
162
- ctx = context .WithValue (ctx , "target" , targetUrl )
163
- ctx = context .WithValue (ctx , "prefix" , targetInfo .Prefix )
167
+ ctx = context .WithValue (ctx , ctxTargetKey {} , targetUrl )
168
+ ctx = context .WithValue (ctx , ctxPrefixKey {} , targetInfo .Prefix )
164
169
165
170
rn := r .WithContext (ctx )
166
171
s .handleHttp (w , rn )
0 commit comments