Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit b42bb23

Browse files
committed
fix async redirection
1 parent ea422e0 commit b42bb23

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/std/http/Client.zig

+15-15
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ pub const Request = struct {
868868
fn onRedirectConnect(ctx: *Ctx, res: anyerror!void) !void {
869869
res catch |err| return ctx.pop(err);
870870
// re-send request
871-
ctx.req.prepareSend(.{}) catch |err| return ctx.pop(err);
871+
ctx.req.prepareSend() catch |err| return ctx.pop(err);
872872
ctx.req.connection.?.async_flush(ctx, onRedirectSend) catch |err| return ctx.pop(err);
873873
}
874874

@@ -896,6 +896,8 @@ pub const Request = struct {
896896
req.privileged_headers = &.{};
897897
}
898898

899+
try ctx.push(onRedirectConnect);
900+
899901
// create a new connection for the redirected URI
900902
ctx.data.conn = try req.client.allocator.create(Connection);
901903
ctx.data.conn.* = .{
@@ -1490,20 +1492,18 @@ pub const Request = struct {
14901492
const location = req.response.location orelse
14911493
return error.HttpRedirectLocationMissing;
14921494

1493-
// This mutates the beginning of header_bytes_buffer and uses that
1494-
// for the backing memory of the returned Uri.
1495-
try req.redirect(req.uri.resolve_inplace(
1496-
location,
1497-
&req.response.parser.header_bytes_buffer,
1498-
) catch |err| switch (err) {
1499-
error.UnexpectedCharacter,
1500-
error.InvalidFormat,
1501-
error.InvalidPort,
1502-
=> return error.HttpRedirectLocationInvalid,
1503-
error.NoSpaceLeft => return error.HttpHeadersOversize,
1504-
});
1505-
1506-
return .{ .redirect_uri = req.uri };
1495+
return .{
1496+
.redirect_uri = req.uri.resolve_inplace(
1497+
location,
1498+
&req.response.parser.header_bytes_buffer,
1499+
) catch |err| switch (err) {
1500+
error.UnexpectedCharacter,
1501+
error.InvalidFormat,
1502+
error.InvalidPort,
1503+
=> return error.HttpRedirectLocationInvalid,
1504+
error.NoSpaceLeft => return error.HttpHeadersOversize,
1505+
},
1506+
};
15071507
} else {
15081508
req.response.skip = false;
15091509
if (!req.response.parser.done) {

src/tests.zig

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ test "TLS example.com" {
5555
try do(&urls);
5656
}
5757

58+
test "redirection" {
59+
var urls = [_][]const u8{
60+
"https://httpbin.io/links/1",
61+
"https://httpbin.io/absolute-redirect/3",
62+
};
63+
try do(&urls);
64+
}
65+
5866
fn do(urls: [][]const u8) !void {
5967
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
6068
defer switch (gpa.deinit()) {

0 commit comments

Comments
 (0)