authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-16 00:56:13-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-23 02:37:11-07:00
log99a5de9dbb0a3a30f09056558f715f7607b1a20a
treeeedca0be26422424b81c38d8c2c8b1ae48275f59
parentb6ca89fa7c0c4c766229586f1977babfb20d782a

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.

2 files changed, 9 insertions(+), 7 deletions(-)

lib/std/http/Client.zig+5
...@@ -639,6 +639,11 @@ pub const Request = struct {...@@ -639,6 +639,11 @@ pub const Request = struct {
639 _ => rb.* = @enumFromInt(@intFromEnum(rb.*) - 1),639 _ => rb.* = @enumFromInt(@intFromEnum(rb.*) - 1),
640 }640 }
641 }641 }
642
643 pub fn remaining(rb: RedirectBehavior) u16 {
644 assert(rb != .unhandled);
645 return @intFromEnum(rb);
646 }
642 };647 };
643648
644 /// Frees all resources associated with the request.649 /// Frees all resources associated with the request.
src/Package/Fetch/git.zig+4-7
...@@ -530,8 +530,9 @@ pub const Session = struct {...@@ -530,8 +530,9 @@ pub const Session = struct {
530 info_refs_uri.query = "service=git-upload-pack";530 info_refs_uri.query = "service=git-upload-pack";
531 info_refs_uri.fragment = null;531 info_refs_uri.fragment = null;
532532
533 const max_redirects = 3;
533 var request = try session.transport.open(.GET, info_refs_uri, .{534 var request = try session.transport.open(.GET, info_refs_uri, .{
534 .redirect_behavior = @enumFromInt(3),535 .redirect_behavior = @enumFromInt(max_redirects),
535 .server_header_buffer = http_headers_buffer,536 .server_header_buffer = http_headers_buffer,
536 .extra_headers = &.{537 .extra_headers = &.{
537 .{ .name = "Git-Protocol", .value = "version=2" },538 .{ .name = "Git-Protocol", .value = "version=2" },
...@@ -543,12 +544,8 @@ pub const Session = struct {...@@ -543,12 +544,8 @@ pub const Session = struct {
543544
544 try request.wait();545 try request.wait();
545 if (request.response.status != .ok) return error.ProtocolError;546 if (request.response.status != .ok) return error.ProtocolError;
546 // Pretty sure this is dead code - in order for a redirect to occur, the status547 const any_redirects_occurred = request.redirect_behavior.remaining() < max_redirects;
547 // code would need to be in the 300s and then it would not be "OK" which is checked548 if (any_redirects_occurred) {
548 // on the line above.
549 var runtime_false = false;
550 _ = &runtime_false;
551 if (runtime_false) {
552 if (!mem.endsWith(u8, request.uri.path, "/info/refs")) return error.UnparseableRedirect;549 if (!mem.endsWith(u8, request.uri.path, "/info/refs")) return error.UnparseableRedirect;
553 var new_uri = request.uri;550 var new_uri = request.uri;
554 new_uri.path = new_uri.path[0 .. new_uri.path.len - "/info/refs".len];551 new_uri.path = new_uri.path[0 .. new_uri.path.len - "/info/refs".len];