Skip to content

Commit c4646c6

Browse files
author
WilliamHeaven
committed
Feature issue gorilla#479
1 parent b65e629 commit c4646c6

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

client.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ type Dialer struct {
9999
// If Jar is nil, cookies are not sent in requests and ignored
100100
// in responses.
101101
Jar http.CookieJar
102+
103+
// custom proxy connect header
104+
ProxyConnectHeader http.Header
102105
}
103106

104107
// Dial creates a new client connection by calling DialContext with a background context.
@@ -274,7 +277,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
274277
return nil, nil, err
275278
}
276279
if proxyURL != nil {
277-
dialer, err := proxy_FromURL(proxyURL, netDialerFunc(netDial))
280+
dialer, err := proxy_FromURL(proxyURL, &netDialer{d.ProxyConnectHeader,netDial})
278281
if err != nil {
279282
return nil, nil, err
280283
}

client_server_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ func TestProxyDial(t *testing.T) {
156156

157157
cstDialer := cstDialer // make local copy for modification on next line.
158158
cstDialer.Proxy = http.ProxyURL(surl)
159+
cstDialer.ProxyConnectHeader = map[string][]string{
160+
"User-Agents": {"xxx"},
161+
}
159162

160163
connect := false
161164
origHandler := s.Server.Config.Handler
@@ -166,6 +169,10 @@ func TestProxyDial(t *testing.T) {
166169
if r.Method == "CONNECT" {
167170
connect = true
168171
w.WriteHeader(http.StatusOK)
172+
if r.Header.Get("User-Agents") != "xxx" {
173+
t.Log("xxx not found in the request header")
174+
http.Error(w, "header xxx not found", http.StatusMethodNotAllowed)
175+
}
169176
return
170177
}
171178

proxy.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,32 @@ import (
1414
"strings"
1515
)
1616

17-
type netDialerFunc func(network, addr string) (net.Conn, error)
17+
// type netDialerFunc func(network, addr string) (net.Conn, error)
18+
//
19+
// func (fn netDialerFunc) Dial(network, addr string) (net.Conn, error) {
20+
// return fn(network, addr)
21+
// }
22+
type netDialer struct {
23+
proxyHeader http.Header
24+
f func(network, addr string) (net.Conn, error)
25+
}
1826

19-
func (fn netDialerFunc) Dial(network, addr string) (net.Conn, error) {
20-
return fn(network, addr)
27+
func (n netDialer) Dial(network, addr string) (net.Conn, error) {
28+
return n.f(network, addr)
2129
}
2230

2331
func init() {
2432
proxy_RegisterDialerType("http", func(proxyURL *url.URL, forwardDialer proxy_Dialer) (proxy_Dialer, error) {
25-
return &httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDialer.Dial}, nil
33+
p, _ := forwardDialer.(*netDialer)
34+
return &httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDialer.Dial, proxyHeader: p.proxyHeader}, nil
35+
// return &httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDialer.Dial}, nil
2636
})
2737
}
2838

2939
type httpProxyDialer struct {
3040
proxyURL *url.URL
3141
forwardDial func(network, addr string) (net.Conn, error)
42+
proxyHeader http.Header
3243
}
3344

3445
func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error) {
@@ -47,6 +58,10 @@ func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error)
4758
}
4859
}
4960

61+
for k, v := range hpd.proxyHeader {
62+
connectHeader[k] = v
63+
}
64+
5065
connectReq := &http.Request{
5166
Method: "CONNECT",
5267
URL: &url.URL{Opaque: addr},

0 commit comments

Comments
 (0)