Skip to content

Commit

Permalink
fix async redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
krichprollsch committed Nov 29, 2024
1 parent 07f0e0e commit 6f0b781
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/std/http/Client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.* = .{
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit 6f0b781

Please sign in to comment.