-
-
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.
polyfill struct_curl_header for old libcurl version
- Loading branch information
1 parent
73ef123
commit 79799fe
Showing
6 changed files
with
197 additions
and
85 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
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,67 @@ | ||
const std = @import("std"); | ||
pub const c = @cImport({ | ||
@cInclude("curl/curl.h"); | ||
}); | ||
|
||
pub fn print_libcurl_version() void { | ||
const v = c.curl_version_info(c.CURLVERSION_NOW); | ||
std.debug.print( | ||
\\Libcurl build info | ||
\\Host: {s} | ||
\\Version: {s} | ||
\\SSL version: {s} | ||
\\Libz version: {s} | ||
\\Protocols: | ||
, .{ | ||
v.*.host, | ||
v.*.version, | ||
v.*.ssl_version, | ||
v.*.libz_version, | ||
}); | ||
var i: usize = 0; | ||
while (v.*.protocols[i] != null) { | ||
std.debug.print(" {s}", .{ | ||
v.*.protocols[i], | ||
}); | ||
i += 1; | ||
} else { | ||
std.debug.print("\n", .{}); | ||
} | ||
|
||
// feature_names is introduced in 7.87.0 | ||
if (@hasField(c.struct_curl_version_info_data, "feature_names")) { | ||
std.debug.print("Features:", .{}); | ||
i = 0; | ||
while (v.*.feature_names[i] != null) { | ||
std.debug.print(" {s}", .{ | ||
v.*.feature_names[i], | ||
}); | ||
i += 1; | ||
} else { | ||
std.debug.print("\n", .{}); | ||
} | ||
} | ||
} | ||
|
||
pub fn polyfill_struct_curl_header() type { | ||
if (has_curl_header()) { | ||
return *c.struct_curl_header; | ||
} else { | ||
// return a dummy struct to make it compile on old version. | ||
return struct { | ||
value: [:0]const u8, | ||
}; | ||
} | ||
} | ||
|
||
pub fn has_curl_header() bool { | ||
// `curl_header` is officially supported since 7.84.0. | ||
// https://curl.se/libcurl/c/curl_easy_header.html | ||
return c.CURL_AT_LEAST_VERSION(7, 84, 0); | ||
} | ||
|
||
// comptime { | ||
// if (!c.CURL_AT_LEAST_VERSION(7, 10, 0)) { | ||
// @compileError("Must use libcurl >= 7.10.0"); | ||
// } | ||
// } |
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,31 @@ | ||
const c = @import("c.zig").c; | ||
const assert = @import("std").debug.assert; | ||
|
||
pub const HeaderError = error{ | ||
BadIndex, | ||
Missing, | ||
NoHeaders, | ||
NoRequest, | ||
OutOfMemory, | ||
BadArgument, | ||
NotBuiltIn, | ||
|
||
UnknownHeaderError, | ||
/// This means there is no `curl_easy_header` method in current libcurl. | ||
NoCurlHeaderSupport, | ||
}; | ||
|
||
pub fn headerErrorFrom(code: c.CURLHcode) ?HeaderError { | ||
// https://curl.se/libcurl/c/libcurl-errors.html | ||
return switch (code) { | ||
c.CURLHE_OK => null, | ||
c.CURLHE_BADINDEX => error.BadIndex, | ||
c.CURLHE_MISSING => error.Missing, | ||
c.CURLHE_NOHEADERS => error.NoHeaders, | ||
c.CURLHE_NOREQUEST => error.NoRequest, | ||
c.CURLHE_OUT_OF_MEMORY => error.OutOfMemory, | ||
c.CURLHE_BAD_ARGUMENT => error.BadArgument, | ||
c.CURLHE_NOT_BUILT_IN => error.NotBuiltIn, | ||
else => error.UnknownHeaderError, | ||
}; | ||
} |
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