Skip to content

Commit 0504a3d

Browse files
authored
refactor: use Reader to do upload (#39)
1 parent 1a23d31 commit 0504a3d

13 files changed

Lines changed: 117 additions & 124 deletions

File tree

.github/workflows/CI.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
name: CI
22

33
on:
4-
schedule:
5-
- cron: '10 20 * * *'
64
workflow_dispatch:
75
pull_request:
86
paths:
@@ -23,15 +21,9 @@ jobs:
2321
fail-fast: false
2422
matrix:
2523
os: [ubuntu-latest, macos-latest]
26-
zig-version: [0.15.1]
2724
steps:
2825
- uses: actions/checkout@v4
2926
- uses: mlugg/setup-zig@v2
30-
with:
31-
version: ${{ matrix.zig-version }}
32-
- uses: actions/setup-go@v5
33-
with:
34-
go-version: 'stable'
3527
- name: Run tests
3628
run: |
3729
make test
@@ -61,12 +53,9 @@ jobs:
6153
fail-fast: false
6254
matrix:
6355
os: [ubuntu-latest]
64-
zig-version: [0.15.1]
6556
steps:
6657
- uses: actions/checkout@v4
6758
- uses: mlugg/setup-zig@v2
68-
with:
69-
version: ${{ matrix.zig-version }}
7059
- name: Set Environment Variables
7160
run: |
7261
echo "ZIG_ARGS='-Dlink_vendor=false'" >> $GITHUB_ENV

.github/workflows/docs.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ jobs:
2626
- name: Setup Pages
2727
uses: actions/configure-pages@v3
2828
- uses: mlugg/setup-zig@v2
29-
with:
30-
version: 0.15.1
3129
- name: Generate docs
3230
run: |
3331
make docs

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
ARGS = ${ZIG_ARGS}
33

44
run:
5-
zig build run-hello -freference-trace $(ARGS)
65
zig build run-basic -freference-trace $(ARGS)
6+
zig build run-post -freference-trace $(ARGS)
7+
zig build run-upload -freference-trace $(ARGS)
78
zig build run-advanced -freference-trace $(ARGS)
89
zig build run-multi -freference-trace $(ARGS)
910
zig build run-header -freference-trace $(ARGS)

README.org

Lines changed: 28 additions & 16 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-09-14T21:23:40+0800
3+
#+LASTMOD: 2025-10-02T10:13:24+0800
44
#+OPTIONS: toc:nil num:nil
55
#+STARTUP: content
66

@@ -18,38 +18,50 @@ 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
23-
cat examples/hello.zig
23+
cat examples/basic.zig
2424
#+end_src
2525

2626
#+RESULTS:
2727
#+begin_src zig
2828
const std = @import("std");
29+
const println = @import("util.zig").println;
30+
const mem = std.mem;
31+
const Allocator = mem.Allocator;
2932
const curl = @import("curl");
33+
const Easy = curl.Easy;
3034

3135
pub fn main() !void {
32-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
36+
var gpa: std.heap.DebugAllocator(.{}) = .init;
3337
defer if (gpa.deinit() != .ok) @panic("leak");
3438
const allocator = gpa.allocator();
3539

3640
const ca_bundle = try curl.allocCABundle(allocator);
3741
defer ca_bundle.deinit();
38-
const easy = try curl.Easy.init(.{
42+
const easy = try Easy.init(.{
3943
.ca_bundle = ca_bundle,
4044
});
4145
defer easy.deinit();
42-
43-
var writer = std.Io.Writer.Allocating.init(allocator);
44-
defer writer.deinit();
45-
const resp = try easy.fetch("https://httpbin.org/anything", .{
46-
.writer = &writer.writer,
47-
});
48-
49-
std.debug.print("Status code: {d}\nBody: {s}\n", .{
50-
resp.status_code,
51-
writer.writer.buffered(),
52-
});
46+
{
47+
println("GET without write context");
48+
const resp = try easy.fetch("https://httpbin.org/anything", .{});
49+
50+
std.debug.print("Status code: {d}\n", .{resp.status_code});
51+
}
52+
53+
{
54+
println("GET with fixed buffer");
55+
var buffer: [1024]u8 = undefined;
56+
var writer = std.Io.Writer.fixed(&buffer);
57+
const resp = try easy.fetch("https://httpbin.org/anything", .{
58+
.writer = &writer,
59+
});
60+
std.debug.print("Status code: {d}\nBody: {s}\n", .{
61+
resp.status_code,
62+
writer.buffered(),
63+
});
64+
}
5365
}
5466
#+end_src
5567

build.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ const Build = std.Build;
33
const Step = Build.Step;
44
const Module = Build.Module;
55
const Allocator = std.mem.Allocator;
6+
const SanitizeC = std.zig.SanitizeC;
67

78
const MODULE_NAME = "curl";
89

910
pub fn build(b: *Build) !void {
1011
const target = b.standardTargetOptions(.{});
1112
const optimize = b.standardOptimizeOption(.{});
1213
const link_vendor = b.option(bool, "link_vendor", "Whether link to vendored libcurl (default: true)") orelse true;
13-
const sanitize_c: ?std.zig.SanitizeC = b.option(std.zig.SanitizeC, "sanitize_c", "Enable compiler sanitizers (default: null)") orelse null;
14+
const sanitize_c = b.option(SanitizeC, "sanitize_c", "Enable compiler sanitizers (default: null)");
1415

1516
const module = b.addModule(MODULE_NAME, .{
1617
.root_source_file = b.path("src/root.zig"),
@@ -35,7 +36,7 @@ pub fn build(b: *Build) !void {
3536
}
3637
}
3738

38-
inline for (.{ "hello", "basic", "advanced", "multi", "header" }) |name| {
39+
inline for (.{ "basic", "post", "upload", "advanced", "multi", "header" }) |name| {
3940
try addExample(b, name, module, libcurl, target, optimize);
4041
}
4142

build.zig.zon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
.{
22
.name = .curl,
33
.fingerprint = 0x3e01b4de1538b3f,
4-
.version = "0.3.1",
4+
.version = "0.3.2",
5+
.minimum_zig_version = "0.15.1",
56
.paths = .{
67
"src",
78
"libs",

examples/advanced.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn postMultiPart(allocator: Allocator, easy: Easy) !void {
113113
}
114114

115115
pub fn main() !void {
116-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
116+
var gpa = std.heap.DebugAllocator(.{}){};
117117
defer if (gpa.deinit() != .ok) @panic("leak");
118118
const allocator = gpa.allocator();
119119

examples/basic.zig

Lines changed: 10 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ const Allocator = mem.Allocator;
55
const curl = @import("curl");
66
const Easy = curl.Easy;
77

8-
const LOCAL_SERVER_ADDR = "http://localhost:8182";
8+
pub fn main() !void {
9+
var gpa: std.heap.DebugAllocator(.{}) = .init;
10+
defer if (gpa.deinit() != .ok) @panic("leak");
11+
const allocator = gpa.allocator();
912

10-
fn get(easy: Easy) !void {
13+
const ca_bundle = try curl.allocCABundle(allocator);
14+
defer ca_bundle.deinit();
15+
const easy = try Easy.init(.{
16+
.ca_bundle = ca_bundle,
17+
});
18+
defer easy.deinit();
1119
{
1220
println("GET without write context");
1321
const resp = try easy.fetch("https://httpbin.org/anything", .{});
@@ -28,67 +36,3 @@ fn get(easy: Easy) !void {
2836
});
2937
}
3038
}
31-
32-
fn post(allocator: Allocator, easy: Easy) !void {
33-
const payload =
34-
\\{"name": "John", "age": 15}
35-
;
36-
37-
var writer = std.Io.Writer.Allocating.init(allocator);
38-
defer writer.deinit();
39-
const resp = try easy.fetch(
40-
"https://httpbin.org/anything",
41-
.{
42-
.method = .POST,
43-
.body = payload,
44-
.headers = &.{
45-
"Content-Type: application/json",
46-
},
47-
.writer = &writer.writer,
48-
},
49-
);
50-
51-
std.debug.print("Status code: {d}\nBody: {s}\n", .{
52-
resp.status_code,
53-
writer.writer.buffered(),
54-
});
55-
}
56-
57-
fn upload(allocator: Allocator, easy: Easy) !void {
58-
const path = "LICENSE";
59-
var writer = std.Io.Writer.Allocating.init(allocator);
60-
61-
defer writer.deinit();
62-
63-
const resp = try easy.upload(LOCAL_SERVER_ADDR ++ "/anything", path, &writer.writer);
64-
65-
std.debug.print("Status code: {d}\nBody: {s}\n", .{
66-
resp.status_code,
67-
writer.writer.buffered(),
68-
});
69-
}
70-
71-
pub fn main() !void {
72-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
73-
defer if (gpa.deinit() != .ok) @panic("leak");
74-
const allocator = gpa.allocator();
75-
76-
const ca_bundle = try curl.allocCABundle(allocator);
77-
defer ca_bundle.deinit();
78-
const easy = try Easy.init(.{
79-
.ca_bundle = ca_bundle,
80-
});
81-
defer easy.deinit();
82-
83-
try easy.setVerbose(false);
84-
println("GET demo");
85-
try get(easy);
86-
87-
println("POST demo");
88-
easy.reset();
89-
try post(allocator, easy);
90-
91-
println("Upload demo");
92-
easy.reset();
93-
try upload(allocator, easy);
94-
}

examples/header.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn iterateRedirectedHeaders(easy: Easy) !void {
5858
}
5959

6060
pub fn main() !void {
61-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
61+
var gpa = std.heap.DebugAllocator(.{}){};
6262
defer if (gpa.deinit() != .ok) @panic("leak");
6363
const allocator = gpa.allocator();
6464

examples/multi.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn newEasy(writer: *Writer, url: [:0]const u8) !Easy {
2121
}
2222

2323
pub fn main() !void {
24-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
24+
var gpa = std.heap.DebugAllocator(.{}){};
2525
defer if (gpa.deinit() != .ok) @panic("leak");
2626
const allocator = gpa.allocator();
2727

0 commit comments

Comments
 (0)