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

fix async redirection #11

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/std/http/Client.zig
Original file line number Diff line number Diff line change
@@ -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) {
8 changes: 8 additions & 0 deletions src/tests.zig
Original file line number Diff line number Diff line change
@@ -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()) {