From 6d796798eb8923f5c72ffb9ee953208183c30246 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 1 Jun 2026 22:08:29 +0300 Subject: [PATCH] ci: fix vfmt and http redirect failures (#27319) --- cmd/tools/vfmt.v | 5 +++- vlib/net/http/http_test.v | 50 ++++++++++++++++++++++++++++++++++++++- vlib/net/http/request.v | 17 ++++++------- 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/cmd/tools/vfmt.v b/cmd/tools/vfmt.v index ab926e9b0..961b2b69c 100644 --- a/cmd/tools/vfmt.v +++ b/cmd/tools/vfmt.v @@ -81,6 +81,9 @@ fn main() { eprintln('vfmt env_vflags_and_os_args: ' + args.str()) eprintln('vfmt possible_files: ' + possible_files.str()) } + if '-help' in args || '--help' in args { + help.print_and_exit('fmt') + } files := util.find_all_v_files(possible_files) or { verror(err.msg()) return @@ -89,7 +92,7 @@ fn main() { foptions.format_pipe() exit(0) } - if files.len == 0 || '-help' in args || '--help' in args { + if files.len == 0 { help.print_and_exit('fmt') } mut cli_args_no_files := []string{} diff --git a/vlib/net/http/http_test.v b/vlib/net/http/http_test.v index b13ede40c..f656ed1d8 100644 --- a/vlib/net/http/http_test.v +++ b/vlib/net/http/http_test.v @@ -1,5 +1,6 @@ import net import net.http +import time fn test_https_get() { $if !network ? { @@ -69,11 +70,58 @@ fn test_https_public_servers() { } } +struct RelativeRedirectHandler {} + +fn (mut handler RelativeRedirectHandler) handle(req http.Request) http.Response { + mut res := http.Response{} + match req.url { + '/relative-redirect/3?abc=xyz' { + res.header = http.new_header(key: .location, value: '/relative-redirect/2?abc=xyz') + res.set_status(.found) + } + '/relative-redirect/2?abc=xyz' { + res.header = http.new_header(key: .location, value: '/relative-redirect/1?abc=xyz') + res.set_status(.found) + } + '/relative-redirect/1?abc=xyz' { + res.header = http.new_header(key: .location, value: '/get?abc=xyz') + res.set_status(.found) + } + '/get?abc=xyz' { + res.body = '{"args": {"abc": "xyz"}}' + res.set_status(.ok) + } + else { + res.body = req.url + res.set_status(.not_found) + } + } + + res.set_version(req.version) + return res +} + fn test_relative_redirects() { $if !network ? { return } - res := http.get('https://httpbin.org/relative-redirect/3?abc=xyz') or { panic(err) } + mut server := &http.Server{ + accept_timeout: 100 * time.millisecond + handler: RelativeRedirectHandler{} + addr: '127.0.0.1:0' + show_startup_message: false + } + t := spawn server.listen_and_serve() + server.wait_till_running() or { + server.stop() + t.wait() + panic(err) + } + defer { + server.stop() + t.wait() + } + res := http.get('http://${server.addr}/relative-redirect/3?abc=xyz') or { panic(err) } assert res.status() == .ok assert res.body != '' assert res.body.contains('"abc": "xyz"') diff --git a/vlib/net/http/request.v b/vlib/net/http/request.v index 85a73cf88..ef88a3187 100644 --- a/vlib/net/http/request.v +++ b/vlib/net/http/request.v @@ -161,8 +161,7 @@ pub fn (req &Request) cookie(name string) ?Cookie { // do will send the HTTP request and returns `http.Response` as soon as the response is received pub fn (req &Request) do() !Response { - mut url := urllib.parse(req.url) or { return error('http.Request.do: invalid url ${req.url}') } - mut rurl := url + mut rurl := urllib.parse(req.url) or { return error('http.Request.do: invalid url ${req.url}') } mut resp := Response{} mut method := req.method mut data := req.data @@ -184,18 +183,20 @@ pub fn (req &Request) do() !Response { } // follow any redirects mut redirect_url := resp.header.get(.location) or { '' } + mut qrurl := urllib.URL{} if redirect_url.len > 0 && redirect_url[0] == `/` { - url.set_path(redirect_url) or { - return error('http.request.do: invalid path in redirect: "${redirect_url}"') + qrurl = rurl.parse(redirect_url) or { + return error('http.request.do: invalid URL in redirect "${redirect_url}"') + } + redirect_url = qrurl.str() + } else { + qrurl = urllib.parse(redirect_url) or { + return error('http.request.do: invalid URL in redirect "${redirect_url}"') } - redirect_url = url.str() } if req.on_redirect != unsafe { nil } { req.on_redirect(req, nredirects, redirect_url)! } - qrurl := urllib.parse(redirect_url) or { - return error('http.request.do: invalid URL in redirect "${redirect_url}"') - } method, data, header = redirected_request_parts(method, status, data, header) rurl = qrurl nredirects++ -- 2.39.5