From 99a5de9dbb0a3a30f09056558f715f7607b1a20a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 16 Feb 2024 00:56:13 -0700 Subject: [PATCH] git fetching: fix redirect handling I mistakenly thought this was dead code in an earlier commit in this branch. This commit restores the proper behavior. --- lib/std/http/Client.zig | 5 +++++ src/Package/Fetch/git.zig | 11 ++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/std/http/Client.zig b/lib/std/http/Client.zig index 9731952afd1855397da79671529e874396580772..a8e2b5e98b092214c3757617d49c276777380f97 100644 --- a/lib/std/http/Client.zig +++ b/lib/std/http/Client.zig @@ -639,6 +639,11 @@ pub const Request = struct { _ => rb.* = @enumFromInt(@intFromEnum(rb.*) - 1), } } + + pub fn remaining(rb: RedirectBehavior) u16 { + assert(rb != .unhandled); + return @intFromEnum(rb); + } }; /// Frees all resources associated with the request. diff --git a/src/Package/Fetch/git.zig b/src/Package/Fetch/git.zig index 9fbb881417880d44bfaea06fd662ba7e88cb15dd..dc0c844d1dafad3e0f0022e845d58d74345d67f7 100644 --- a/src/Package/Fetch/git.zig +++ b/src/Package/Fetch/git.zig @@ -530,8 +530,9 @@ pub const Session = struct { info_refs_uri.query = "service=git-upload-pack"; info_refs_uri.fragment = null; + const max_redirects = 3; var request = try session.transport.open(.GET, info_refs_uri, .{ - .redirect_behavior = @enumFromInt(3), + .redirect_behavior = @enumFromInt(max_redirects), .server_header_buffer = http_headers_buffer, .extra_headers = &.{ .{ .name = "Git-Protocol", .value = "version=2" }, @@ -543,12 +544,8 @@ pub const Session = struct { try request.wait(); if (request.response.status != .ok) return error.ProtocolError; - // Pretty sure this is dead code - in order for a redirect to occur, the status - // code would need to be in the 300s and then it would not be "OK" which is checked - // on the line above. - var runtime_false = false; - _ = &runtime_false; - if (runtime_false) { + const any_redirects_occurred = request.redirect_behavior.remaining() < max_redirects; + if (any_redirects_occurred) { if (!mem.endsWith(u8, request.uri.path, "/info/refs")) return error.UnparseableRedirect; var new_uri = request.uri; new_uri.path = new_uri.path[0 .. new_uri.path.len - "/info/refs".len]; -- 2.54.0