-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
424b8fd
commit 504d540
Showing
6 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/libs/curl | ||
/libs/mbedtls | ||
/libs/zlib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const std = @import("std"); | ||
const println = @import("util.zig").println; | ||
const mem = std.mem; | ||
const Allocator = mem.Allocator; | ||
const curl = @import("curl"); | ||
const Easy = curl.Easy; | ||
const Multi = curl.Multi; | ||
const c = curl.libcurl; | ||
|
||
pub fn main() !void { | ||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
defer arena.deinit(); | ||
const allocator = arena.allocator(); | ||
|
||
const easy = try Easy.init(allocator, .{}); | ||
try easy.set_url("http://httpbin.org/headers"); | ||
defer easy.deinit(); | ||
|
||
const multi = try Multi.init(); | ||
// defer multi.deinit(); | ||
|
||
try multi.addHandle(easy.handle); | ||
|
||
var running = true; | ||
const transfer = try multi.perform(); | ||
std.debug.print("num of transfer {any}\n", .{transfer}); | ||
|
||
while (running) { | ||
const info = try multi.readInfo(); | ||
running = info.msgs_in_queue != 0; | ||
try multi.removeHandle(info.msg.easy_handle.?); | ||
c.curl_easy_cleanup(info.msg.easy_handle.?); | ||
std.debug.print("{any}\n", .{info}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const std = @import("std"); | ||
const errors = @import("errors.zig"); | ||
const util = @import("util.zig"); | ||
const Easy = @import("easy.zig"); | ||
const c = util.c; | ||
|
||
const mem = std.mem; | ||
const fmt = std.fmt; | ||
const Allocator = mem.Allocator; | ||
const checkMCode = errors.checkMCode; | ||
const Self = @This(); | ||
|
||
multi: *c.CURLM, | ||
|
||
pub fn init() !Self { | ||
const core = c.curl_multi_init(); | ||
if (core == null) { | ||
return error.InitMulti; | ||
} | ||
return .{ .multi = core.? }; | ||
} | ||
|
||
pub fn deinit() void {} | ||
|
||
/// Adds the easy handle to the multi_handle. | ||
/// https://curl.se/libcurl/c/curl_multi_add_handle.html | ||
pub fn addHandle(self: Self, handle: *c.CURL) !void { | ||
return checkMCode(c.curl_multi_add_handle(self.multi, handle)); | ||
} | ||
|
||
/// Removes a given easy_handle from the multi_handle. | ||
/// https://curl.se/libcurl/c/curl_multi_remove_handle.html | ||
pub fn removeHandle(self: Self, handle: *c.CURL) !void { | ||
return checkMCode(c.curl_multi_remove_handle(self.multi, handle)); | ||
} | ||
|
||
/// This function performs transfers on all the added handles that need attention in a non-blocking fashion. | ||
/// Returns the number of handles that still transfer data. When that reaches zero, all transfers are done. | ||
/// https://curl.se/libcurl/c/curl_multi_perform.html | ||
pub fn perform(self: Self) !c_int { | ||
var still_running: c_int = undefined; | ||
try checkMCode(c.curl_multi_perform(self.multi, &still_running)); | ||
|
||
return still_running; | ||
} | ||
|
||
const Info = struct { | ||
msgs_in_queue: c_int, | ||
msg: *c.CURLMsg, | ||
}; | ||
|
||
pub fn readInfo(self: Self) !Info { | ||
var msgs_in_queue: c_int = undefined; | ||
|
||
const msg = c.curl_multi_info_read(self.multi, &msgs_in_queue); | ||
if (msg == null) { | ||
return error.OutOfStruct; | ||
} | ||
|
||
return Info{ | ||
.msg = msg.?, | ||
.msgs_in_queue = msgs_in_queue, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters