diff --git a/src/std/http/Client.zig b/src/std/http/Client.zig index 895802a..2bab4e7 100644 --- a/src/std/http/Client.zig +++ b/src/std/http/Client.zig @@ -868,7 +868,7 @@ pub const Request = struct { fn onRedirectConnect(ctx: *Ctx, res: anyerror!void) !void { res catch |err| return ctx.pop(err); // re-send request - ctx.req.prepareSend(.{}) catch |err| return ctx.pop(err); + ctx.req.prepareSend() catch |err| return ctx.pop(err); ctx.req.connection.?.async_flush(ctx, onRedirectSend) catch |err| return ctx.pop(err); } @@ -896,6 +896,8 @@ pub const Request = struct { req.privileged_headers = &.{}; } + try ctx.push(onRedirectConnect); + // create a new connection for the redirected URI ctx.data.conn = try req.client.allocator.create(Connection); ctx.data.conn.* = .{ @@ -1490,20 +1492,18 @@ pub const Request = struct { const location = req.response.location orelse return error.HttpRedirectLocationMissing; - // This mutates the beginning of header_bytes_buffer and uses that - // for the backing memory of the returned Uri. - try req.redirect(req.uri.resolve_inplace( - location, - &req.response.parser.header_bytes_buffer, - ) catch |err| switch (err) { - error.UnexpectedCharacter, - error.InvalidFormat, - error.InvalidPort, - => return error.HttpRedirectLocationInvalid, - error.NoSpaceLeft => return error.HttpHeadersOversize, - }); - - return .{ .redirect_uri = req.uri }; + return .{ + .redirect_uri = req.uri.resolve_inplace( + location, + &req.response.parser.header_bytes_buffer, + ) catch |err| switch (err) { + error.UnexpectedCharacter, + error.InvalidFormat, + error.InvalidPort, + => return error.HttpRedirectLocationInvalid, + error.NoSpaceLeft => return error.HttpHeadersOversize, + }, + }; } else { req.response.skip = false; if (!req.response.parser.done) { diff --git a/src/tests.zig b/src/tests.zig index 748884f..307d631 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -55,6 +55,14 @@ test "TLS example.com" { try do(&urls); } +test "redirection" { + var urls = [_][]const u8{ + "https://httpbin.io/links/1", + "https://httpbin.io/absolute-redirect/3", + }; + try do(&urls); +} + fn do(urls: [][]const u8) !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer switch (gpa.deinit()) {