Skip to content

Commit 083d3db

Browse files
authored
net.http: Use a full url when using a proxy, instead of only the path (#25228)
1 parent 08a739d commit 083d3db

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

vlib/net/http/http_proxy.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ fn (pr &HttpProxy) build_proxy_headers(host string) string {
8787
fn (pr &HttpProxy) http_do(host urllib.URL, method Method, path string, req &Request) !Response {
8888
host_name, port := net.split_address(host.hostname())!
8989

90-
s := req.build_request_headers(req.method, host_name, port, path)
90+
port_part := if port == 80 || port == 0 { '' } else { ':${port}' }
91+
92+
s := req.build_request_headers(req.method, host_name, port, '${host.scheme}://${host_name}${port_part}${path}')
9193
if host.scheme == 'https' {
9294
mut client := pr.ssl_dial('${host.host}:443')!
9395

vlib/net/http/http_proxy_test.v

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module http
22

33
import encoding.base64
4+
import net.urllib
5+
import os
46

57
const sample_proxy_url = 'https://localhost'
68
const sample_auth_proxy_url = 'http://user:pass@localhost:8888'
@@ -46,3 +48,29 @@ fn test_proxy_headers_authenticated() ? {
4648
assert headers == 'CONNECT 127.0.0.1:1337 HTTP/1.1\r\n' + 'Host: 127.0.0.1\r\n' +
4749
'Proxy-Connection: Keep-Alive\r\nProxy-Authorization: Basic ${auth_token}\r\n\r\n'
4850
}
51+
52+
fn test_http_proxy_do() {
53+
env := os.environ()
54+
mut env_proxy := ''
55+
56+
for envvar in ['http_proxy', 'HTTP_PROXY', 'https_proxy', 'HTTPS_PROXY'] {
57+
prox_val := env[envvar] or { continue }
58+
if prox_val != '' {
59+
env_proxy = env[envvar]
60+
}
61+
}
62+
if env_proxy != '' {
63+
println('Has usable proxy env vars')
64+
proxy := new_http_proxy(env_proxy)!
65+
mut header := new_header(key: .user_agent, value: 'vlib')
66+
header.add_custom('X-Vlang-Test', 'proxied')!
67+
res := proxy.http_do(urllib.parse('http://httpbin.org/headers')!, Method.get,
68+
'/headers', &Request{ proxy: proxy, header: header })!
69+
println(res.status_code)
70+
println('he4aders ${res.header}')
71+
assert res.status_code == 200
72+
// assert res.header.data['X-Vlang-Test'] == 'proxied'
73+
} else {
74+
println('Proxy env vars (HTTP_PROXY or HTTPS_PROXY) not set. Skipping test.')
75+
}
76+
}

0 commit comments

Comments
 (0)