diff options
| author | Jacob Young <jacobly0@users.noreply.github.com> | 2024-04-09 22:22:08 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2024-04-10 02:11:54 -0700 |
| commit | c4587dc9f46e15d4fb875a7675bc1aa22138c1ab (patch) | |
| tree | fff3520b8a9ba7492b7ac1d35a5290c535c50ba5 /src/Package/Fetch/git.zig | |
| parent | 215de3ee67f75e2405c177b262cb5c1cd8c8e343 (diff) | |
| download | zig-c4587dc9f46e15d4fb875a7675bc1aa22138c1ab.tar.gz zig-c4587dc9f46e15d4fb875a7675bc1aa22138c1ab.zip | |
Uri: propagate per-component encoding
This allows `std.Uri.resolve_inplace` to properly preserve the fact
that `new` is already escaped but `base` may not be. I originally tried
just moving `raw_uri` around, but it made uri resolution unmanagably
complicated, so I instead added per-component information to `Uri` which
allows extra allocations to be avoided when constructing uris with
components from different sources, and in some cases, deferring the work
all the way to when the uri is printed, where an allocator may not even
be needed.
Closes #19587
Diffstat (limited to 'src/Package/Fetch/git.zig')
| -rw-r--r-- | src/Package/Fetch/git.zig | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/src/Package/Fetch/git.zig b/src/Package/Fetch/git.zig index d7cdd8483c..a8c106412e 100644 --- a/src/Package/Fetch/git.zig +++ b/src/Package/Fetch/git.zig @@ -540,9 +540,13 @@ pub const Session = struct { http_headers_buffer: []u8, ) !CapabilityIterator { var info_refs_uri = session.uri; - info_refs_uri.path = try std.fs.path.resolvePosix(allocator, &.{ "/", session.uri.path, "info/refs" }); - defer allocator.free(info_refs_uri.path); - info_refs_uri.query = "service=git-upload-pack"; + { + const session_uri_path = try std.fmt.allocPrint(allocator, "{path}", .{session.uri.path}); + defer allocator.free(session_uri_path); + info_refs_uri.path = .{ .percent_encoded = try std.fs.path.resolvePosix(allocator, &.{ "/", session_uri_path, "info/refs" }) }; + } + defer allocator.free(info_refs_uri.path.percent_encoded); + info_refs_uri.query = .{ .percent_encoded = "service=git-upload-pack" }; info_refs_uri.fragment = null; const max_redirects = 3; @@ -554,16 +558,18 @@ pub const Session = struct { }, }); errdefer request.deinit(); - try request.send(.{}); + try request.send(); try request.finish(); try request.wait(); if (request.response.status != .ok) return error.ProtocolError; 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; + const request_uri_path = try std.fmt.allocPrint(allocator, "{path}", .{request.uri.path}); + defer allocator.free(request_uri_path); + 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]; + new_uri.path = .{ .percent_encoded = request_uri_path[0 .. request_uri_path.len - "/info/refs".len] }; new_uri.query = null; redirect_uri.* = try std.fmt.allocPrint(allocator, "{+/}", .{new_uri}); return error.Redirected; @@ -645,8 +651,12 @@ pub const Session = struct { /// Returns an iterator over refs known to the server. pub fn listRefs(session: Session, allocator: Allocator, options: ListRefsOptions) !RefIterator { var upload_pack_uri = session.uri; - upload_pack_uri.path = try std.fs.path.resolvePosix(allocator, &.{ "/", session.uri.path, "git-upload-pack" }); - defer allocator.free(upload_pack_uri.path); + { + const session_uri_path = try std.fmt.allocPrint(allocator, "{path}", .{session.uri.path}); + defer allocator.free(session_uri_path); + upload_pack_uri.path = .{ .percent_encoded = try std.fs.path.resolvePosix(allocator, &.{ "/", session_uri_path, "git-upload-pack" }) }; + } + defer allocator.free(upload_pack_uri.path.percent_encoded); upload_pack_uri.query = null; upload_pack_uri.fragment = null; @@ -681,7 +691,7 @@ pub const Session = struct { }); errdefer request.deinit(); request.transfer_encoding = .{ .content_length = body.items.len }; - try request.send(.{}); + try request.send(); try request.writeAll(body.items); try request.finish(); @@ -748,8 +758,12 @@ pub const Session = struct { http_headers_buffer: []u8, ) !FetchStream { var upload_pack_uri = session.uri; - upload_pack_uri.path = try std.fs.path.resolvePosix(allocator, &.{ "/", session.uri.path, "git-upload-pack" }); - defer allocator.free(upload_pack_uri.path); + { + const session_uri_path = try std.fmt.allocPrint(allocator, "{path}", .{session.uri.path}); + defer allocator.free(session_uri_path); + upload_pack_uri.path = .{ .percent_encoded = try std.fs.path.resolvePosix(allocator, &.{ "/", session_uri_path, "git-upload-pack" }) }; + } + defer allocator.free(upload_pack_uri.path.percent_encoded); upload_pack_uri.query = null; upload_pack_uri.fragment = null; @@ -786,7 +800,7 @@ pub const Session = struct { }); errdefer request.deinit(); request.transfer_encoding = .{ .content_length = body.items.len }; - try request.send(.{}); + try request.send(); try request.writeAll(body.items); try request.finish(); |
