Skip to content

Commit f382648

Browse files
committed
chore: better name for context
1 parent 21878cd commit f382648

8 files changed

Lines changed: 40 additions & 41 deletions

File tree

README.org

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#+TITLE: zig-curl
22
#+DATE: 2023-09-16T23:16:15+0800
3-
#+LASTMOD: 2025-07-27T19:36:36+0800
3+
#+LASTMOD: 2025-07-27T20:02:08+0800
44
#+OPTIONS: toc:nil num:nil
55
#+STARTUP: content
66

@@ -44,7 +44,7 @@ pub fn main() !void {
4444
});
4545
defer easy.deinit();
4646

47-
var ctx = curl.DynamicContext.init(allocator);
47+
var ctx = curl.ResizableWriteContext.init(allocator);
4848
defer ctx.deinit();
4949
const resp = try easy.fetch("https://httpbin.org/anything", .{}, &ctx);
5050

examples/advanced.zig

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

44-
var writeContext = curl.DynamicContext.init(allocator);
44+
var writeContext = curl.ResizableWriteContext.init(allocator);
4545
defer writeContext.deinit();
46-
try easy.setWriteContext(&writeContext, curl.DynamicContext.write);
46+
try easy.setWriteContext(&writeContext, curl.ResizableWriteContext.write);
4747

4848
const resp = try easy.perform();
4949
std.debug.print("Status code: {d}\nBody: {s}\n", .{
@@ -103,9 +103,9 @@ fn postMultiPart(allocator: Allocator, easy: Easy) !void {
103103
try easy.setMultiPart(multi_part);
104104
try easy.setVerbose(true);
105105

106-
var writeContext = curl.DynamicContext.init(allocator);
106+
var writeContext = curl.ResizableWriteContext.init(allocator);
107107
defer writeContext.deinit();
108-
try easy.setWriteContext(&writeContext, curl.DynamicContext.write);
108+
try easy.setWriteContext(&writeContext, curl.ResizableWriteContext.write);
109109

110110
const resp = try easy.perform();
111111
std.debug.print("code: {d}, resp:{s}\n", .{ resp.status_code, writeContext.asSlice() });

examples/basic.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn get(easy: Easy) !void {
1818
{
1919
println("GET with fixed buffer");
2020
var buffer: [1024]u8 = undefined;
21-
var writeContext = curl.StaticContext.init(&buffer);
21+
var writeContext = curl.FixedWriteContext.init(&buffer);
2222
const resp = try easy.fetch("https://httpbin.org/anything", .{}, &writeContext);
2323
std.debug.print("Status code: {d}\nBody: {s}\n", .{
2424
resp.status_code,
@@ -31,7 +31,7 @@ fn post(allocator: Allocator, easy: Easy) !void {
3131
const payload =
3232
\\{"name": "John", "age": 15}
3333
;
34-
var writeContext = curl.DynamicContext.init(allocator);
34+
var writeContext = curl.ResizableWriteContext.init(allocator);
3535
defer writeContext.deinit();
3636
const resp = try easy.fetch(
3737
"https://httpbin.org/anything",
@@ -53,7 +53,7 @@ fn post(allocator: Allocator, easy: Easy) !void {
5353

5454
fn upload(allocator: Allocator, easy: Easy) !void {
5555
const path = "LICENSE";
56-
var writeContext = curl.DynamicContext.init(allocator);
56+
var writeContext = curl.ResizableWriteContext.init(allocator);
5757
defer writeContext.deinit();
5858

5959
const resp = try easy.upload(LOCAL_SERVER_ADDR ++ "/anything", path, &writeContext);

examples/hello.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn main() !void {
1313
});
1414
defer easy.deinit();
1515

16-
var ctx = curl.DynamicContext.init(allocator);
16+
var ctx = curl.ResizableWriteContext.init(allocator);
1717
defer ctx.deinit();
1818
const resp = try easy.fetch("https://httpbin.org/anything", .{}, &ctx);
1919

examples/multi.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const Multi = curl.Multi;
88
const c = curl.libcurl;
99
const checkCode = curl.checkCode;
1010

11-
fn newEasy(ctx: *curl.DynamicContext, url: [:0]const u8) !Easy {
11+
fn newEasy(ctx: *curl.ResizableWriteContext, url: [:0]const u8) !Easy {
1212
const easy = try Easy.init(.{});
1313
try easy.setUrl(url);
14-
try easy.setWriteContext(ctx, curl.DynamicContext.write);
14+
try easy.setWriteContext(ctx, curl.ResizableWriteContext.write);
1515
// CURLOPT_PRIVATE allows us to store a pointer to the ctx in the easy handle
1616
// so we can retrieve it later in the callback.
1717
try easy.setPrivate(ctx);
@@ -27,9 +27,9 @@ pub fn main() !void {
2727
const multi = try Multi.init();
2828
defer multi.deinit();
2929

30-
var ctx1 = curl.DynamicContext.init(allocator);
30+
var ctx1 = curl.ResizableWriteContext.init(allocator);
3131
defer ctx1.deinit();
32-
var ctx2 = curl.DynamicContext.init(allocator);
32+
var ctx2 = curl.ResizableWriteContext.init(allocator);
3333
defer ctx2.deinit();
3434

3535
try multi.addHandle(try newEasy(&ctx1, "http://httpbin.org/headers"));
@@ -69,7 +69,7 @@ pub fn main() !void {
6969
// Get the private data (buffer) associated with this handle
7070
var private_data: ?*anyopaque = null;
7171
try checkCode(c.curl_easy_getinfo(easy_handle, c.CURLINFO_PRIVATE, &private_data));
72-
const ctx: *curl.DynamicContext = @ptrCast(@alignCast(private_data.?));
72+
const ctx: *curl.ResizableWriteContext = @ptrCast(@alignCast(private_data.?));
7373

7474
std.debug.print("Response body: {s}\n", .{ctx.asSlice()});
7575
}

src/Easy.zig

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ const mem = std.mem;
77
const fmt = std.fmt;
88
const Allocator = mem.Allocator;
99
const checkCode = errors.checkCode;
10-
const DynamicBuffer = util.DynamicBuffer;
11-
const StaticBuffer = util.StaticBuffer;
10+
const ResizableBuffer = util.ResizableBuffer;
1211

1312
const hasParseHeaderSupport = @import("util.zig").hasParseHeaderSupport;
1413

@@ -17,7 +16,7 @@ const Self = @This();
1716
handle: *c.CURL,
1817
timeout_ms: usize,
1918
user_agent: [:0]const u8,
20-
ca_bundle: ?DynamicBuffer,
19+
ca_bundle: ?ResizableBuffer,
2120

2221
pub const Method = enum {
2322
GET,
@@ -256,7 +255,7 @@ pub const Options = struct {
256255
// Note that the vendored libcurl is compiled with mbedtls and does not include a CA bundle,
257256
// so this should be set when link with vendored libcurl, otherwise https
258257
// requests will fail.
259-
ca_bundle: ?DynamicBuffer = null,
258+
ca_bundle: ?ResizableBuffer = null,
260259
/// The maximum time in milliseconds that the entire transfer operation to take.
261260
default_timeout_ms: usize = 30_000,
262261
default_user_agent: [:0]const u8 = "zig-curl/" ++ @import("build_info").version,
@@ -457,8 +456,8 @@ pub fn head(self: Self, url: [:0]const u8) !Response {
457456
/// Fetch issues a request to the specified URL.
458457
///
459458
/// `writeContext` is used to write the response body. There are two built-in write contexts:
460-
/// 1. `DynamicContext` - a dynamic buffer that grows as needed.
461-
/// 2. `StaticContext` - a fixed-size buffer that must be large enough to hold the response body.
459+
/// 1. `ResizableWriteContext` - a resizable context that grows as needed to hold the response body.
460+
/// 2. `FixedWriteContext` - a fixed-size one that must be large enough to hold the response body.
462461
/// Or it can be `void`, which means you don't care about the response body.
463462
pub fn fetch(
464463
self: Self,

src/root.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pub const checkCode = @import("errors.zig").checkCode;
44

55
pub const Easy = @import("Easy.zig");
66
pub const Multi = @import("Multi.zig");
7-
pub const DynamicContext = util.DynamicContext;
8-
pub const StaticContext = util.StaticContext;
7+
pub const ResizableWriteContext = util.ResizableWriteContext;
8+
pub const FixedWriteContext = util.FixedWriteContext;
99

1010
pub const printLibcurlVersion = util.printLibcurlVersion;
1111
pub const hasParseHeaderSupport = util.hasParseHeaderSupport;

src/util.zig

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@ pub const c = @cImport({
44
});
55
const Allocator = std.mem.Allocator;
66
const Encoder = std.base64.standard.Encoder;
7-
pub const DynamicBuffer = std.ArrayList(u8);
8-
pub const StaticBuffer = struct {
7+
pub const ResizableBuffer = std.ArrayList(u8);
8+
pub const FixedBuffer = struct {
99
data: []u8,
1010
// How many bytes are used in the buffer.
1111
size: usize,
1212

13-
pub fn init(data: []u8) StaticBuffer {
13+
pub fn init(data: []u8) FixedBuffer {
1414
return .{ .data = data, .size = 0 };
1515
}
1616

17-
pub fn asSlice(self: *StaticBuffer) []const u8 {
17+
pub fn asSlice(self: *FixedBuffer) []const u8 {
1818
return self.data[0..self.size];
1919
}
2020
};
2121

22-
pub const StaticContext = struct {
23-
buffer: StaticBuffer,
22+
pub const FixedWriteContext = struct {
23+
buffer: FixedBuffer,
2424

25-
pub fn init(buffer: []u8) StaticContext {
25+
pub fn init(buffer: []u8) FixedWriteContext {
2626
return .{ .buffer = .init(buffer) };
2727
}
2828

2929
pub fn write(
30-
ctx: *StaticContext,
30+
ctx: *FixedWriteContext,
3131
data: []const u8,
3232
) usize {
3333
if (ctx.buffer.size + data.len > ctx.buffer.data.len) {
@@ -39,24 +39,24 @@ pub const StaticContext = struct {
3939
return data.len;
4040
}
4141

42-
pub fn asSlice(self: *StaticContext) []const u8 {
42+
pub fn asSlice(self: *FixedWriteContext) []const u8 {
4343
return self.buffer.asSlice();
4444
}
4545
};
4646

47-
pub const DynamicContext = struct {
48-
buffer: DynamicBuffer,
47+
pub const ResizableWriteContext = struct {
48+
buffer: ResizableBuffer,
4949

50-
pub fn init(allocator: Allocator) DynamicContext {
51-
return .{ .buffer = DynamicBuffer.init(allocator) };
50+
pub fn init(allocator: Allocator) ResizableWriteContext {
51+
return .{ .buffer = ResizableBuffer.init(allocator) };
5252
}
5353

54-
pub fn deinit(self: *DynamicContext) void {
54+
pub fn deinit(self: *ResizableWriteContext) void {
5555
self.buffer.deinit();
5656
}
5757

5858
pub fn write(
59-
ctx: *DynamicContext,
59+
ctx: *ResizableWriteContext,
6060
data: []const u8,
6161
) usize {
6262
ctx.buffer.appendSlice(data) catch {
@@ -67,7 +67,7 @@ pub const DynamicContext = struct {
6767
return data.len;
6868
}
6969

70-
pub fn asSlice(self: *DynamicContext) []const u8 {
70+
pub fn asSlice(self: *ResizableWriteContext) []const u8 {
7171
return self.buffer.items;
7272
}
7373
};
@@ -159,11 +159,11 @@ test "url encode" {
159159
const CERT_MARKER_BEGIN = "-----BEGIN CERTIFICATE-----";
160160
const CERT_MARKER_END = "\n-----END CERTIFICATE-----\n";
161161

162-
pub fn allocCABundle(allocator: std.mem.Allocator) !DynamicBuffer {
162+
pub fn allocCABundle(allocator: std.mem.Allocator) !ResizableBuffer {
163163
var bundle: std.crypto.Certificate.Bundle = .{};
164164
defer bundle.deinit(allocator);
165165

166-
var blob = DynamicBuffer.init(allocator);
166+
var blob = ResizableBuffer.init(allocator);
167167
try bundle.rescan(allocator);
168168
var iter = bundle.map.iterator();
169169
while (iter.next()) |entry| {

0 commit comments

Comments
 (0)