Skip to content

Commit b220077

Browse files
xydonejiacai2050Copilot
authored
feat: curl diagnostics (#46)
closes #45 --------- Co-authored-by: Jiacai Liu <dev@liujiacai.net> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 86ea61a commit b220077

12 files changed

Lines changed: 170 additions & 104 deletions

File tree

README.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn main() !void {
3838

3939
const ca_bundle = try curl.allocCABundle(allocator);
4040
defer ca_bundle.deinit();
41-
const easy = try curl.Easy.init(.{
41+
var easy = try curl.Easy.init(.{
4242
.ca_bundle = ca_bundle,
4343
});
4444
defer easy.deinit();

examples/advanced.zig

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const Response = struct {
2020
url: []const u8,
2121
};
2222

23-
fn putWithCustomHeader(allocator: Allocator, easy: Easy) !void {
23+
fn putWithCustomHeader(allocator: Allocator, easy: *Easy) !void {
2424
const body =
2525
\\ {"name": "John", "age": 15}
2626
;
@@ -87,7 +87,7 @@ fn putWithCustomHeader(allocator: Allocator, easy: Easy) !void {
8787
}
8888
}
8989

90-
fn postMultiPart(allocator: Allocator, easy: Easy) !void {
90+
fn postMultiPart(allocator: Allocator, easy: *Easy) !void {
9191
// Reset old options, e.g. headers.
9292
easy.reset();
9393

@@ -118,10 +118,37 @@ pub fn main() !void {
118118

119119
const ca_bundle = try curl.allocCABundle(allocator);
120120
defer ca_bundle.deinit();
121-
const easy = try Easy.init(.{ .ca_bundle = ca_bundle });
121+
122+
var easy = try Easy.init(.{
123+
.ca_bundle = ca_bundle,
124+
});
122125
defer easy.deinit();
123126

124127
println("PUT with custom header demo");
125-
try putWithCustomHeader(allocator, easy);
126-
try postMultiPart(allocator, easy);
128+
putWithCustomHeader(allocator, &easy) catch |err| {
129+
if (easy.diagnostics.error_code) |error_code| {
130+
switch (error_code) {
131+
.code => |c| {
132+
std.log.err("putWithCustomHeader encountered a curl error! error code: {}", .{c});
133+
},
134+
.m_code => |mc| {
135+
std.log.err("putWithCustomHeader encountered a curl multi error! error code: {}", .{mc});
136+
},
137+
}
138+
}
139+
return err;
140+
};
141+
postMultiPart(allocator, &easy) catch |err| {
142+
if (easy.diagnostics.error_code) |error_code| {
143+
switch (error_code) {
144+
.code => |c| {
145+
std.log.err("postMultipart encountered a curl error! error code: {}", .{c});
146+
},
147+
.m_code => |mc| {
148+
std.log.err("postMultipart encountered a curl multi error! error code: {}", .{mc});
149+
},
150+
}
151+
}
152+
return err;
153+
};
127154
}

examples/basic.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn main() !void {
1010

1111
const ca_bundle = try curl.allocCABundle(allocator);
1212
defer ca_bundle.deinit();
13-
const easy = try curl.Easy.init(.{
13+
var easy = try curl.Easy.init(.{
1414
.ca_bundle = ca_bundle,
1515
});
1616
defer easy.deinit();

examples/header.zig

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const curl = @import("curl");
33
const Easy = curl.Easy;
44
const println = @import("util.zig").println;
55

6-
fn iterateHeaders(easy: Easy) !void {
6+
fn iterateHeaders(easy: *Easy) !void {
77
// Reset old options, e.g. headers.
88
easy.reset();
99

@@ -29,11 +29,12 @@ fn iterateHeaders(easy: Easy) !void {
2929
}
3030
}
3131

32-
fn iterateRedirectedHeaders(easy: Easy) !void {
32+
fn iterateRedirectedHeaders(easy: *Easy) !void {
3333
try easy.setFollowLocation(true);
3434
const resp = try easy.fetch("https://edgebin.liujiacai.net/redirect/2", .{});
3535

36-
const redirects = try resp.getRedirectCount();
36+
var diagnostics: Easy.Diagnostics = .{};
37+
const redirects = try resp.getRedirectCount(&diagnostics);
3738
try std.testing.expectEqual(redirects, 2);
3839

3940
for (0..redirects + 1) |i| {
@@ -64,7 +65,7 @@ pub fn main() !void {
6465

6566
const ca_bundle = try curl.allocCABundle(allocator);
6667
defer ca_bundle.deinit();
67-
const easy = try Easy.init(.{ .ca_bundle = ca_bundle });
68+
var easy = try Easy.init(.{ .ca_bundle = ca_bundle });
6869
defer easy.deinit();
6970

7071
if (comptime !curl.hasParseHeaderSupport()) {
@@ -73,11 +74,11 @@ pub fn main() !void {
7374
}
7475

7576
println("Iterate headers demo");
76-
try iterateHeaders(easy);
77+
try iterateHeaders(&easy);
7778

7879
// Reset old options, e.g. headers.
7980
easy.reset();
8081

8182
println("Redirected headers demo");
82-
try iterateRedirectedHeaders(easy);
83+
try iterateRedirectedHeaders(&easy);
8384
}

examples/multi.zig

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const checkCode = curl.checkCode;
1010
const Writer = std.io.Writer;
1111

1212
fn newEasy(writer: *Writer, url: [:0]const u8) !Easy {
13-
const easy = try Easy.init(.{});
13+
var easy = try Easy.init(.{});
1414
try easy.setUrl(url);
1515
try easy.setWriter(writer);
1616
// CURLOPT_PRIVATE allows us to store a pointer to the ctx in the easy handle
@@ -25,16 +25,23 @@ pub fn main() !void {
2525
defer if (gpa.deinit() != .ok) @panic("leak");
2626
const allocator = gpa.allocator();
2727

28-
const multi = try Multi.init();
28+
var diagnostics: Multi.Diagnostics = .{};
29+
30+
const multi = try Multi.init(&diagnostics);
2931
defer multi.deinit();
3032

3133
var wtr1 = std.Io.Writer.Allocating.init(allocator);
3234
defer wtr1.deinit();
3335
var wtr2 = std.Io.Writer.Allocating.init(allocator);
3436
defer wtr2.deinit();
3537

36-
try multi.addHandle(try newEasy(&wtr1.writer, "http://edgebin.liujiacai.net/headers"));
37-
try multi.addHandle(try newEasy(&wtr2.writer, "http://edgebin.liujiacai.net/ip"));
38+
var easy1 = try newEasy(&wtr1.writer, "http://edgebin.liujiacai.net/headers");
39+
defer easy1.deinit();
40+
var easy2 = try newEasy(&wtr2.writer, "http://edgebin.liujiacai.net/ip");
41+
defer easy2.deinit();
42+
43+
try multi.addHandle(&easy1);
44+
try multi.addHandle(&easy2);
3845

3946
var keep_running = true;
4047
while (keep_running) {
@@ -60,16 +67,16 @@ pub fn main() !void {
6067
}
6168

6269
// check that the request was successful
63-
try checkCode(info.msg.data.result);
70+
try checkCode(info.msg.data.result, &diagnostics);
6471

6572
// Read the HTTP status code
6673
var status_code: c_long = 0;
67-
try checkCode(c.curl_easy_getinfo(easy_handle, c.CURLINFO_RESPONSE_CODE, &status_code));
74+
try checkCode(c.curl_easy_getinfo(easy_handle, c.CURLINFO_RESPONSE_CODE, &status_code), &diagnostics);
6875
std.debug.print("Response Code: {any}\n", .{status_code});
6976

7077
// Get the private data (buffer) associated with this handle
7178
var private_data: ?*anyopaque = null;
72-
try checkCode(c.curl_easy_getinfo(easy_handle, c.CURLINFO_PRIVATE, &private_data));
79+
try checkCode(c.curl_easy_getinfo(easy_handle, c.CURLINFO_PRIVATE, &private_data), &diagnostics);
7380
const writer: *Writer = @ptrCast(@alignCast(private_data.?));
7481

7582
std.debug.print("Response body: {s}\n", .{writer.buffered()});

examples/post.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn main() !void {
88

99
const ca_bundle = try curl.allocCABundle(allocator);
1010
defer ca_bundle.deinit();
11-
const easy = try curl.Easy.init(.{
11+
var easy = try curl.Easy.init(.{
1212
.ca_bundle = ca_bundle,
1313
});
1414
defer easy.deinit();

examples/upload.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn main() !void {
1313

1414
const ca_bundle = try curl.allocCABundle(allocator);
1515
defer ca_bundle.deinit();
16-
const easy = try curl.Easy.init(.{
16+
var easy = try curl.Easy.init(.{
1717
.ca_bundle = ca_bundle,
1818
});
1919
defer easy.deinit();
@@ -37,10 +37,10 @@ pub fn main() !void {
3737
easy.reset();
3838

3939
// Upload with multipart/form-data
40-
try multipartUpload(allocator, easy);
40+
try multipartUpload(allocator, &easy);
4141
}
4242

43-
fn multipartUpload(allocator: Allocator, easy: curl.Easy) !void {
43+
fn multipartUpload(allocator: Allocator, easy: *curl.Easy) !void {
4444
std.debug.print("Multipart upload demo\n", .{});
4545
try easy.setUrl(URL);
4646
var multi_part = try easy.createMultiPart();

0 commit comments

Comments
 (0)