Skip to content

Commit

Permalink
fix wrong request when filter header by name
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Jul 26, 2024
1 parent 75502f8 commit c18d706
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
12 changes: 12 additions & 0 deletions examples/header.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ fn iterateRedirectedHeaders(easy: Easy) !void {
std.debug.print(" {s}: {s}\n", .{ header.name, header.get() });
}
}

// Iterating content-type only
const expected_values = [_][]const u8{ "text/html; charset=utf-8", "application/json" };
var count: usize = 0;
for (0..redirects + 1) |i| {
var iter = try resp.iterateHeaders(.{ .request = i, .name = "Content-Type" });
while (try iter.next()) |header| {
try std.testing.expectEqualStrings(header.get(), expected_values[count]);
count += 1;
}
}
try std.testing.expectEqual(count, 2);
}

pub fn main() !void {
Expand Down
28 changes: 17 additions & 11 deletions src/Easy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,21 @@ pub const Response = struct {
}

var header: ?*c.struct_curl_header = null;
return Response.getHeaderInner(self.handle, name, &header);
return Response.getHeaderInner(
self.handle,
name,
-1, // request, -1 means last request
&header,
);
}

fn getHeaderInner(easy: ?*c.CURL, name: [:0]const u8, hout: *?*c.struct_curl_header) errors.HeaderError!?Header {
fn getHeaderInner(easy: ?*c.CURL, name: [:0]const u8, request: c_int, hout: *?*c.struct_curl_header) errors.HeaderError!?Header {
const code = c.curl_easy_header(
easy,
name.ptr,
0, // index, 0 means first header
c.CURLH_HEADER,
-1, // request, -1 means last request
request,
hout,
);
return if (errors.headerErrorFrom(code)) |err| switch (err) {
Expand Down Expand Up @@ -140,17 +145,16 @@ pub const Response = struct {
return null;
}
} else {
return Response.getHeaderInner(self.handle, filter_name, &self.c_header);
return Response.getHeaderInner(self.handle, filter_name, request, &self.c_header);
}
}

while (true) {
const c_header = c.curl_easy_nextheader(
self.handle,
c.CURLH_HEADER,
request,
self.c_header,
) orelse return null;
while (c.curl_easy_nextheader(
self.handle,
c.CURLH_HEADER,
request,
self.c_header,
)) |c_header| {
self.c_header = c_header;

const name = std.mem.sliceTo(c_header.*.name, 0);
Expand All @@ -164,6 +168,8 @@ pub const Response = struct {
.c_header = c_header,
.name = name,
};
} else {
return null;
}
}
};
Expand Down

0 comments on commit c18d706

Please sign in to comment.