-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
easy.zig
410 lines (344 loc) · 13.1 KB
/
easy.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
const c = @import("c.zig").c;
const std = @import("std");
const errors = @import("errors.zig");
const mem = std.mem;
const fmt = std.fmt;
const Allocator = mem.Allocator;
const checkCode = errors.checkCode;
const has_parse_header_support = @import("c.zig").has_parse_header_support;
const Self = @This();
allocator: Allocator,
handle: *c.CURL,
/// The maximum time in milliseconds that the entire transfer operation to take.
timeout_ms: usize = 30_000,
default_user_agent: []const u8 = "zig-curl/0.1.0",
ca_bundle: ?[]const u8,
pub const HEADER_CONTENT_TYPE: []const u8 = "Content-Type";
pub const HEADER_USER_AGENT: []const u8 = "User-Agent";
pub const Method = enum {
GET,
POST,
PUT,
HEAD,
PATCH,
DELETE,
fn asString(self: @This()) [:0]const u8 {
return @tagName(self);
}
};
pub const RequestHeader = struct {
entries: std.StringHashMap([]const u8),
allocator: Allocator,
pub fn init(allocator: Allocator) @This() {
return .{
.entries = std.StringHashMap([]const u8).init(allocator),
.allocator = allocator,
};
}
pub fn deinit(self: *@This()) void {
self.entries.deinit();
}
pub fn add(self: *@This(), k: []const u8, v: []const u8) !void {
try self.entries.put(k, v);
}
// Note: Caller should free returned list (after usage) with `freeCHeader`.
fn asCHeader(self: @This(), ua: []const u8) !?*c.struct_curl_slist {
if (self.entries.count() == 0) {
return null;
}
var lst: ?*c.struct_curl_slist = null;
var it = self.entries.iterator();
var has_ua = false;
while (it.next()) |entry| {
if (!has_ua and std.ascii.eqlIgnoreCase(entry.key_ptr.*, HEADER_USER_AGENT)) {
has_ua = true;
}
const kv = try fmt.allocPrintZ(self.allocator, "{s}: {s}", .{ entry.key_ptr.*, entry.value_ptr.* });
defer self.allocator.free(kv);
lst = c.curl_slist_append(lst, kv);
}
if (!has_ua) {
const kv = try fmt.allocPrintZ(self.allocator, "{s}: {s}", .{ HEADER_USER_AGENT, ua });
defer self.allocator.free(kv);
lst = c.curl_slist_append(lst, kv);
}
return lst;
}
fn freeCHeader(lst: *c.struct_curl_slist) void {
c.curl_slist_free_all(lst);
}
};
pub const RequestArgs = struct {
method: Method = .GET,
header: ?RequestHeader = null,
multi_part: ?MultiPart = null,
verbose: bool = false,
/// Redirection limit, 0 refuse any redirect, -1 for an infinite number of redirects.
redirects: i32 = 10,
/// Max body size, default 128M.
max_body_size: usize = 128 * 1024 * 1024,
};
pub fn Request(comptime ReaderType: type) type {
return struct {
url: []const u8,
/// body is io.Reader or void
body: ReaderType,
args: RequestArgs,
pub fn init(url: []const u8, body: ReaderType, args: RequestArgs) @This() {
return .{
.url = url,
.body = body,
.args = args,
};
}
pub fn deinit(self: *@This()) void {
if (self.args.header) |*h| {
h.deinit();
}
if (self.args.multi_part) |mp| {
mp.deinit();
}
}
fn getVerbose(self: @This()) c_long {
return if (self.args.verbose) 1 else 0;
}
fn getBody(self: @This(), allocator: Allocator) !?[]u8 {
if (@TypeOf(self.body) == void) {
return null;
}
return try self.body.readAllAlloc(allocator, self.args.max_body_size);
}
};
}
pub const Buffer = std.ArrayList(u8);
pub const Response = struct {
body: Buffer,
status_code: i32,
handle: *c.CURL,
allocator: Allocator,
pub fn deinit(self: @This()) void {
self.body.deinit();
}
pub const Header = struct {
c_header: polyfill_struct_curl_header(),
name: []const u8,
/// Get the first value associated with the given key.
/// Applications need to copy the data if it wants to keep it around.
pub fn get(self: @This()) []const u8 {
return mem.sliceTo(self.c_header.value, 0);
}
};
/// Gets the header associated with the given name.
pub fn get_header(self: @This(), name: []const u8) errors.HeaderError!?Header {
if (comptime !has_parse_header_support()) {
return error.NoCurlHeaderSupport;
}
const c_name = try fmt.allocPrintZ(self.allocator, "{s}", .{name});
defer self.allocator.free(c_name);
var header: ?*c.struct_curl_header = null;
const code = c.curl_easy_header(self.handle, name.ptr, 0, c.CURLH_HEADER, -1, &header);
return if (errors.headerErrorFrom(code)) |err|
switch (err) {
error.Missing => null,
else => err,
}
else
Header{
.c_header = header.?,
.name = name,
};
}
};
pub const MultiPart = struct {
mime_handle: *c.curl_mime,
allocator: Allocator,
pub const DataSource = union(enum) {
/// Set a mime part's body content from memory data.
/// Data will get copied when send request.
/// Setting large data is memory consuming: one might consider using `data_callback` in such a case.
data: []const u8,
/// Set a mime part's body data from a file contents.
file: []const u8,
// TODO: https://curl.se/libcurl/c/curl_mime_data_cb.html
// data_callback: u8,
};
pub fn deinit(self: @This()) void {
c.curl_mime_free(self.mime_handle);
}
pub fn add_part(self: @This(), name: []const u8, source: DataSource) !void {
const part = if (c.curl_mime_addpart(self.mime_handle)) |part| part else return error.MimeAddPart;
const namez = try fmt.allocPrintZ(self.allocator, "{s}", .{name});
defer self.allocator.free(namez);
try checkCode(c.curl_mime_name(part, namez));
switch (source) {
.data => |slice| {
try checkCode(c.curl_mime_data(part, slice.ptr, slice.len));
},
.file => |filepath| {
const filepathz = try std.fmt.allocPrintZ(self.allocator, "{s}", .{filepath});
defer self.allocator.free(filepathz);
try checkCode(c.curl_mime_filedata(part, filepathz));
},
}
}
};
/// Init options for Easy handle
pub const EasyOptions = struct {
/// Use zig's std.crypto.Certificate.Bundle for TLS instead of libcurl's default.
/// Note that the builtin libcurl is compiled with mbedtls and does not include a CA bundle.
use_std_crypto_ca_bundle: bool = true,
};
pub fn init(allocator: Allocator, options: EasyOptions) !Self {
const ca_bundle = blk: {
if (options.use_std_crypto_ca_bundle) {
var bundle = std.crypto.Certificate.Bundle{};
defer bundle.deinit(allocator);
try bundle.rescan(allocator);
var blob = std.ArrayListUnmanaged(u8){};
const base64 = std.base64.standard.Encoder;
var iter = bundle.map.iterator();
while (iter.next()) |entry| {
const der = try std.crypto.Certificate.der.Element.parse(bundle.bytes.items, entry.value_ptr.*);
const cert = bundle.bytes.items[entry.value_ptr.*..der.slice.end];
const begin_marker = "-----BEGIN CERTIFICATE-----";
const end_marker = "\n-----END CERTIFICATE-----\n";
const encoded_sz = base64.calcSize(cert.len);
try blob.ensureTotalCapacity(allocator, blob.items.len + encoded_sz);
var encoded = try allocator.alloc(u8, encoded_sz + begin_marker.len + end_marker.len);
defer allocator.free(encoded);
_ = base64.encode(encoded[0..], cert);
try blob.appendSlice(allocator, begin_marker);
for (encoded, 0..) |char, n| {
if (n % 64 == 0) try blob.append(allocator, '\n');
try blob.append(allocator, char);
}
try blob.appendSlice(allocator, end_marker);
}
break :blk try blob.toOwnedSlice(allocator);
} else {
break :blk null;
}
};
return if (c.curl_easy_init()) |h|
.{
.allocator = allocator,
.handle = h,
.ca_bundle = ca_bundle,
}
else
error.CurlInit;
}
pub fn deinit(self: Self) void {
if (self.ca_bundle) |bundle| self.allocator.free(bundle);
c.curl_easy_cleanup(self.handle);
}
pub fn add_multi_part(self: Self) !MultiPart {
return if (c.curl_mime_init(self.handle)) |h|
.{
.allocator = self.allocator,
.mime_handle = h,
}
else
error.MimeInit;
}
/// Do sends an HTTP request and returns an HTTP response.
pub fn do(self: Self, req: anytype) !Response {
// Re-initializes all options previously set on a specified CURL handle to the default values.
defer c.curl_easy_reset(self.handle);
try self.set_common_opts();
const url = try fmt.allocPrintZ(self.allocator, "{s}", .{req.url});
defer self.allocator.free(url);
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_URL, url.ptr));
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_MAXREDIRS, @as(c_long, req.args.redirects)));
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_CUSTOMREQUEST, req.args.method.asString().ptr));
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_VERBOSE, req.getVerbose()));
if (self.ca_bundle) |bundle| {
const blob = c.curl_blob {
.data = @constCast(bundle.ptr),
.len = bundle.len,
.flags = c.CURL_BLOB_NOCOPY,
};
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_CAINFO_BLOB, blob));
}
const body = try req.getBody(self.allocator);
defer if (body) |b| {
self.allocator.free(b);
};
if (body) |b| {
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_POSTFIELDS, b.ptr));
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_POSTFIELDSIZE, b.len));
}
var mime_handle: ?*c.curl_mime = null;
if (req.args.multi_part) |mp| {
mime_handle = mp.mime_handle;
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_MIMEPOST, mime_handle));
}
var header: ?*c.struct_curl_slist = null;
if (req.args.header) |h| {
header = try h.asCHeader(self.default_user_agent);
}
defer if (header) |h| RequestHeader.freeCHeader(h);
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_HTTPHEADER, header));
var resp_buffer = Buffer.init(self.allocator);
errdefer resp_buffer.deinit();
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_WRITEDATA, &resp_buffer));
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_WRITEFUNCTION, write_callback));
try checkCode(c.curl_easy_perform(self.handle));
var status_code: c_long = 0;
try checkCode(c.curl_easy_getinfo(self.handle, c.CURLINFO_RESPONSE_CODE, &status_code));
return .{
.status_code = @truncate(status_code),
.body = resp_buffer,
.handle = self.handle,
.allocator = self.allocator,
};
}
/// Get issues a GET to the specified URL.
pub fn get(self: Self, url: []const u8) !Response {
var req = Request(void).init(url, {}, .{});
defer req.deinit();
return self.do(req);
}
/// Head issues a HEAD to the specified URL.
pub fn head(self: Self, url: []const u8) !Response {
var req = Request(void).init(url, {}, .{ .method = .HEAD });
defer req.deinit();
return self.do(req);
}
/// Post issues a POST to the specified URL.
pub fn post(self: Self, url: []const u8, content_type: []const u8, body: anytype) !Response {
const header = blk: {
var h = RequestHeader.init(self.allocator);
errdefer h.deinit();
try h.add(HEADER_CONTENT_TYPE, content_type);
break :blk h;
};
var req = Request(@TypeOf(body)).init(url, body, .{
.method = .POST,
.header = header,
});
defer req.deinit();
return self.do(req);
}
/// Used for https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html
// size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);
fn write_callback(ptr: [*c]c_char, size: c_uint, nmemb: c_uint, user_data: *anyopaque) callconv(.C) c_uint {
const real_size = size * nmemb;
var buffer: *Buffer = @alignCast(@ptrCast(user_data));
var typed_data: [*]u8 = @ptrCast(ptr);
buffer.appendSlice(typed_data[0..real_size]) catch return 0;
return real_size;
}
fn set_common_opts(self: Self) !void {
try checkCode(c.curl_easy_setopt(self.handle, c.CURLOPT_TIMEOUT_MS, self.timeout_ms));
}
pub fn polyfill_struct_curl_header() type {
if (has_parse_header_support()) {
return *c.struct_curl_header;
} else {
// return a dummy struct to make it compile on old version.
return struct {
value: [:0]const u8,
};
}
}