mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
net.http: Use a full url when using a proxy, instead of only the path (#25228)
This commit is contained in:
parent
08a739d793
commit
083d3dba38
2 changed files with 31 additions and 1 deletions
|
@ -87,7 +87,9 @@ fn (pr &HttpProxy) build_proxy_headers(host string) string {
|
||||||
fn (pr &HttpProxy) http_do(host urllib.URL, method Method, path string, req &Request) !Response {
|
fn (pr &HttpProxy) http_do(host urllib.URL, method Method, path string, req &Request) !Response {
|
||||||
host_name, port := net.split_address(host.hostname())!
|
host_name, port := net.split_address(host.hostname())!
|
||||||
|
|
||||||
s := req.build_request_headers(req.method, host_name, port, path)
|
port_part := if port == 80 || port == 0 { '' } else { ':${port}' }
|
||||||
|
|
||||||
|
s := req.build_request_headers(req.method, host_name, port, '${host.scheme}://${host_name}${port_part}${path}')
|
||||||
if host.scheme == 'https' {
|
if host.scheme == 'https' {
|
||||||
mut client := pr.ssl_dial('${host.host}:443')!
|
mut client := pr.ssl_dial('${host.host}:443')!
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
module http
|
module http
|
||||||
|
|
||||||
import encoding.base64
|
import encoding.base64
|
||||||
|
import net.urllib
|
||||||
|
import os
|
||||||
|
|
||||||
const sample_proxy_url = 'https://localhost'
|
const sample_proxy_url = 'https://localhost'
|
||||||
const sample_auth_proxy_url = 'http://user:pass@localhost:8888'
|
const sample_auth_proxy_url = 'http://user:pass@localhost:8888'
|
||||||
|
@ -46,3 +48,29 @@ fn test_proxy_headers_authenticated() ? {
|
||||||
assert headers == 'CONNECT 127.0.0.1:1337 HTTP/1.1\r\n' + 'Host: 127.0.0.1\r\n' +
|
assert headers == 'CONNECT 127.0.0.1:1337 HTTP/1.1\r\n' + 'Host: 127.0.0.1\r\n' +
|
||||||
'Proxy-Connection: Keep-Alive\r\nProxy-Authorization: Basic ${auth_token}\r\n\r\n'
|
'Proxy-Connection: Keep-Alive\r\nProxy-Authorization: Basic ${auth_token}\r\n\r\n'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_http_proxy_do() {
|
||||||
|
env := os.environ()
|
||||||
|
mut env_proxy := ''
|
||||||
|
|
||||||
|
for envvar in ['http_proxy', 'HTTP_PROXY', 'https_proxy', 'HTTPS_PROXY'] {
|
||||||
|
prox_val := env[envvar] or { continue }
|
||||||
|
if prox_val != '' {
|
||||||
|
env_proxy = env[envvar]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if env_proxy != '' {
|
||||||
|
println('Has usable proxy env vars')
|
||||||
|
proxy := new_http_proxy(env_proxy)!
|
||||||
|
mut header := new_header(key: .user_agent, value: 'vlib')
|
||||||
|
header.add_custom('X-Vlang-Test', 'proxied')!
|
||||||
|
res := proxy.http_do(urllib.parse('http://httpbin.org/headers')!, Method.get,
|
||||||
|
'/headers', &Request{ proxy: proxy, header: header })!
|
||||||
|
println(res.status_code)
|
||||||
|
println('he4aders ${res.header}')
|
||||||
|
assert res.status_code == 200
|
||||||
|
// assert res.header.data['X-Vlang-Test'] == 'proxied'
|
||||||
|
} else {
|
||||||
|
println('Proxy env vars (HTTP_PROXY or HTTPS_PROXY) not set. Skipping test.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue