Skip to content

Commit 2a1916c

Browse files
committed
response_writer use new Writer API
1 parent e07a92b commit 2a1916c

13 files changed

Lines changed: 54 additions & 145 deletions

File tree

.fdignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

.gitattributes

Lines changed: 0 additions & 3 deletions
This file was deleted.

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
zig 0.14.1
1+
zig 0.15.1

README.org

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#+TITLE: zig-curl
22
#+DATE: 2023-09-16T23:16:15+0800
3-
#+LASTMOD: 2025-08-24T10:24:23+0800
3+
#+LASTMOD: 2025-09-14T21:23:40+0800
44
#+OPTIONS: toc:nil num:nil
55
#+STARTUP: content
66

7-
[[https://img.shields.io/badge/zig%20version-0.15.0-blue.svg]]
7+
[[https://img.shields.io/badge/zig%20version-0.15.1-blue.svg]]
88
[[https://img.shields.io/badge/zig%20version-master-blue.svg]]
99
[[https://github.com/jiacai2050/zig-curl/actions/workflows/CI.yml][https://github.com/jiacai2050/zig-curl/actions/workflows/CI.yml/badge.svg]]
1010
[[https://ci.codeberg.org/repos/13257][https://ci.codeberg.org/api/badges/13257/status.svg]]
@@ -18,7 +18,7 @@ The vendored libraries consist of:
1818
| zlib | [[https://github.com/madler/zlib/tree/v1.3.1][1.3.1]] |
1919
| mbedtls | [[https://github.com/Mbed-TLS/mbedtls/tree/v3.6.0][3.6.0]] |
2020

21-
* Usage
21+
- Usage
2222
#+begin_src bash :results verbatim :exports results :wrap src zig
2323
cat examples/hello.zig
2424
#+end_src
@@ -40,15 +40,15 @@ pub fn main() !void {
4040
});
4141
defer easy.deinit();
4242

43-
var writer = curl.ResizableResponseWriter.init(allocator);
43+
var writer = std.Io.Writer.Allocating.init(allocator);
4444
defer writer.deinit();
4545
const resp = try easy.fetch("https://httpbin.org/anything", .{
46-
.response_writer = writer.asAny(),
46+
.writer = &writer.writer,
4747
});
4848

4949
std.debug.print("Status code: {d}\nBody: {s}\n", .{
5050
resp.status_code,
51-
writer.asSlice(),
51+
writer.writer.buffered(),
5252
});
5353
}
5454
#+end_src

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
22
.name = .curl,
33
.fingerprint = 0x3e01b4de1538b3f,
4-
.version = "0.3.0",
4+
.version = "0.3.1",
55
.paths = .{
66
"src",
77
"libs",

examples/advanced.zig

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ fn putWithCustomHeader(allocator: Allocator, easy: Easy) !void {
4141
try easy.setVerbose(true);
4242
try easy.setPostFields(body);
4343

44-
var writer = curl.ResizableResponseWriter.init(allocator);
44+
var writer = std.Io.Writer.Allocating.init(allocator);
4545
defer writer.deinit();
46-
try easy.setAnyWriter(&writer.asAny());
46+
try easy.setWriter(&writer.writer);
4747

4848
const resp = try easy.perform();
4949
std.debug.print("Status code: {d}\nBody: {s}\n", .{
5050
resp.status_code,
51-
writer.asSlice(),
51+
writer.writer.buffered(),
5252
});
5353

5454
const parsed = try std.json.parseFromSlice(
5555
Response,
5656
allocator,
57-
writer.asSlice(),
57+
writer.writer.buffered(),
5858
.{ .ignore_unknown_fields = true },
5959
);
6060
defer parsed.deinit();
@@ -103,12 +103,13 @@ fn postMultiPart(allocator: Allocator, easy: Easy) !void {
103103
try easy.setMultiPart(multi_part);
104104
try easy.setVerbose(true);
105105

106-
var writer = curl.ResizableResponseWriter.init(allocator);
106+
var writer = std.Io.Writer.Allocating.init(allocator);
107107
defer writer.deinit();
108-
try easy.setAnyWriter(&writer.asAny());
108+
109+
try easy.setWriter(&writer.writer);
109110

110111
const resp = try easy.perform();
111-
std.debug.print("code: {d}, resp:{s}\n", .{ resp.status_code, writer.asSlice() });
112+
std.debug.print("code: {d}, resp:{s}\n", .{ resp.status_code, writer.writer.buffered() });
112113
}
113114

114115
pub fn main() !void {

examples/basic.zig

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ fn get(easy: Easy) !void {
1818
{
1919
println("GET with fixed buffer");
2020
var buffer: [1024]u8 = undefined;
21-
var writer = curl.FixedResponseWriter.init(&buffer);
21+
var writer = std.Io.Writer.fixed(&buffer);
2222
const resp = try easy.fetch("https://httpbin.org/anything", .{
23-
.response_writer = writer.asAny(),
23+
.writer = &writer,
2424
});
2525
std.debug.print("Status code: {d}\nBody: {s}\n", .{
2626
resp.status_code,
27-
writer.asSlice(),
27+
writer.buffered(),
2828
});
2929
}
3030
}
@@ -33,7 +33,8 @@ fn post(allocator: Allocator, easy: Easy) !void {
3333
const payload =
3434
\\{"name": "John", "age": 15}
3535
;
36-
var writer = curl.ResizableResponseWriter.init(allocator);
36+
37+
var writer = std.Io.Writer.Allocating.init(allocator);
3738
defer writer.deinit();
3839
const resp = try easy.fetch(
3940
"https://httpbin.org/anything",
@@ -43,26 +44,27 @@ fn post(allocator: Allocator, easy: Easy) !void {
4344
.headers = &.{
4445
"Content-Type: application/json",
4546
},
46-
.response_writer = writer.asAny(),
47+
.writer = &writer.writer,
4748
},
4849
);
4950

5051
std.debug.print("Status code: {d}\nBody: {s}\n", .{
5152
resp.status_code,
52-
writer.asSlice(),
53+
writer.writer.buffered(),
5354
});
5455
}
5556

5657
fn upload(allocator: Allocator, easy: Easy) !void {
5758
const path = "LICENSE";
58-
var writer = curl.ResizableResponseWriter.init(allocator);
59+
var writer = std.Io.Writer.Allocating.init(allocator);
60+
5961
defer writer.deinit();
6062

61-
const resp = try easy.upload(LOCAL_SERVER_ADDR ++ "/anything", path, writer.asAny());
63+
const resp = try easy.upload(LOCAL_SERVER_ADDR ++ "/anything", path, &writer.writer);
6264

6365
std.debug.print("Status code: {d}\nBody: {s}\n", .{
6466
resp.status_code,
65-
writer.asSlice(),
67+
writer.writer.buffered(),
6668
});
6769
}
6870

examples/hello.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ pub fn main() !void {
1313
});
1414
defer easy.deinit();
1515

16-
var writer = curl.ResizableResponseWriter.init(allocator);
16+
var writer = std.Io.Writer.Allocating.init(allocator);
1717
defer writer.deinit();
1818
const resp = try easy.fetch("https://httpbin.org/anything", .{
19-
.response_writer = writer.asAny(),
19+
.writer = &writer.writer,
2020
});
2121

2222
std.debug.print("Status code: {d}\nBody: {s}\n", .{
2323
resp.status_code,
24-
writer.asSlice(),
24+
writer.writer.buffered(),
2525
});
2626
}

examples/multi.zig

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const Easy = curl.Easy;
77
const Multi = curl.Multi;
88
const c = curl.libcurl;
99
const checkCode = curl.checkCode;
10-
const AnyWriter = std.io.AnyWriter;
10+
const Writer = std.io.Writer;
1111

12-
fn newEasy(writer: *curl.ResizableResponseWriter, any_writer: *const AnyWriter, url: [:0]const u8) !Easy {
12+
fn newEasy(writer: *Writer, url: [:0]const u8) !Easy {
1313
const easy = try Easy.init(.{});
1414
try easy.setUrl(url);
15-
try easy.setAnyWriter(any_writer);
15+
try easy.setWriter(writer);
1616
// CURLOPT_PRIVATE allows us to store a pointer to the ctx in the easy handle
1717
// so we can retrieve it later in the callback.
1818
try easy.setPrivate(writer);
@@ -28,15 +28,13 @@ pub fn main() !void {
2828
const multi = try Multi.init();
2929
defer multi.deinit();
3030

31-
var wtr1 = curl.ResizableResponseWriter.init(allocator);
31+
var wtr1 = std.Io.Writer.Allocating.init(allocator);
3232
defer wtr1.deinit();
33-
const any_writer1: AnyWriter = wtr1.asAny();
34-
var wtr2 = curl.ResizableResponseWriter.init(allocator);
33+
var wtr2 = std.Io.Writer.Allocating.init(allocator);
3534
defer wtr2.deinit();
36-
const any_writer2: AnyWriter = wtr2.asAny();
3735

38-
try multi.addHandle(try newEasy(&wtr1, &any_writer1, "http://httpbin.org/headers"));
39-
try multi.addHandle(try newEasy(&wtr2, &any_writer2, "http://httpbin.org/ip"));
36+
try multi.addHandle(try newEasy(&wtr1.writer, "http://httpbin.org/headers"));
37+
try multi.addHandle(try newEasy(&wtr2.writer, "http://httpbin.org/ip"));
4038

4139
var keep_running = true;
4240
while (keep_running) {
@@ -72,8 +70,8 @@ pub fn main() !void {
7270
// Get the private data (buffer) associated with this handle
7371
var private_data: ?*anyopaque = null;
7472
try checkCode(c.curl_easy_getinfo(easy_handle, c.CURLINFO_PRIVATE, &private_data));
75-
const writer: *curl.ResizableResponseWriter = @ptrCast(@alignCast(private_data.?));
73+
const writer: *Writer = @ptrCast(@alignCast(private_data.?));
7674

77-
std.debug.print("Response body: {s}\n", .{writer.asSlice()});
75+
std.debug.print("Response body: {s}\n", .{writer.buffered()});
7876
}
7977
}

libs/curl.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub fn create(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.bui
88
.target = target,
99
.optimize = optimize,
1010
.link_libc = true,
11-
.root_source_file = null,
1211
}),
1312
});
1413
const curl_dep = b.lazyDependency("curl", .{

0 commit comments

Comments
 (0)