From f81f97955ad30830cde97a1693a309c87ce2ae21 Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Tue, 26 Sep 2023 02:02:54 +0300 Subject: Remove curl --- src/EasyHttp.zig | 32 ++++++++++++++++++++++++++++++++ src/Installation.zig | 22 +++++++++++++++------- src/install.zig | 30 ++++++++---------------------- src/main.zig | 1 + 4 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 src/EasyHttp.zig (limited to 'src') diff --git a/src/EasyHttp.zig b/src/EasyHttp.zig new file mode 100644 index 0000000..197cf9d --- /dev/null +++ b/src/EasyHttp.zig @@ -0,0 +1,32 @@ +const std = @import("std"); + +const Allocator = std.mem.Allocator; +const ArenaAllocator = std.heap.ArenaAllocator; +const Client = std.http.Client; +const Headers = std.http.Headers; +const Reader = Request.Reader; +const Request = Client.Request; +const Uri = std.Uri; + +pub fn get(parent_allocator: Allocator, uri: Uri) ![]u8 { + var arena = ArenaAllocator.init(parent_allocator); + defer arena.deinit(); + + const allocator = arena.allocator(); + + var client = Client{ + .allocator = allocator, + }; + defer client.deinit(); + + var headers = Headers.init(allocator); + defer headers.deinit(); + + var request = try client.request(.GET, uri, headers, .{}); + defer request.deinit(); + + try request.start(); + try request.wait(); + + return request.reader().readAllAlloc(parent_allocator, std.math.maxInt(usize)); +} diff --git a/src/Installation.zig b/src/Installation.zig index 54b870c..8218f23 100644 --- a/src/Installation.zig +++ b/src/Installation.zig @@ -1,4 +1,3 @@ -const curl = @import("curl"); const std = @import("std"); const xdg = @import("xdg"); const zup = @import("root"); @@ -7,9 +6,11 @@ const Allocator = std.mem.Allocator; const ArenaAllocator = std.heap.ArenaAllocator; const Config = zup.Config; const ChildProcess = std.ChildProcess; +const EasyHttp = zup.EasyHttp; const JsonValue = std.json.Value; const SemanticVersion = std.SemanticVersion; const StringHashMap = std.StringHashMap; +const Uri = std.Uri; const Installation = @This(); @@ -18,11 +19,12 @@ pub const Installations = StringHashMap(Installation); allocator: Allocator, ver_str: []u8, version: SemanticVersion, -url: ?[:0]u8, +url_str: ?[]u8, +url: ?Uri, pub fn deinit(self: *Installation) void { self.allocator.free(self.ver_str); - if (self.url) |url| self.allocator.free(url); + if (self.url_str) |str| self.allocator.free(str); self.* = undefined; } @@ -118,6 +120,7 @@ pub fn getInstalledList(allocator: Allocator) !Installations { .allocator = allocator, .ver_str = res.stdout, .version = version, + .url_str = null, .url = null, }); } @@ -133,18 +136,21 @@ pub fn isInstalled(allocator: Allocator, name: []const u8) !bool { return true; } +const index_json_uri = Uri.parse("https://ziglang.org/download/index.json") catch unreachable; + pub fn getAvailableList(config: Config) !Installations { var arena = ArenaAllocator.init(config.allocator); defer arena.deinit(); const allocator = arena.allocator(); - const json_str = try curl.easyDownload(allocator, "https://ziglang.org/download/index.json"); + var data = try EasyHttp.get(allocator, index_json_uri); + defer allocator.free(data); const parsed = try std.json.parseFromSliceLeaky( JsonValue, allocator, - json_str, + data, .{}, ); @@ -203,8 +209,9 @@ fn parseInstallation(config: Config, name: []const u8, value: JsonValue) !?Insta if (url_src != .string) { return error.JsonSchema; } - var url = try allocator.dupeZ(u8, url_src.string); - errdefer allocator.free(url); + var url_str = try allocator.dupe(u8, url_src.string); + errdefer allocator.free(url_str); + const url = try Uri.parse(url_str); const version_src = if (map.get("version")) |ver| blk: { if (ver != .string) { @@ -223,6 +230,7 @@ fn parseInstallation(config: Config, name: []const u8, value: JsonValue) !?Insta .allocator = allocator, .ver_str = ver_str, .version = version, + .url_str = url_str, .url = url, }; } diff --git a/src/install.zig b/src/install.zig index 2e3efab..a459aa6 100644 --- a/src/install.zig +++ b/src/install.zig @@ -1,12 +1,13 @@ const libarchive = @import("libarchive"); -const curl = @import("curl"); const std = @import("std"); const xdg = @import("xdg"); const zup = @import("root"); const Allocator = std.mem.Allocator; -const Config = zup.Config; const ArchiveRead = libarchive.Read; +const ArenaAllocator = std.heap.ArenaAllocator; +const Config = zup.Config; +const EasyHttp = zup.EasyHttp; const Installation = zup.Installation; const Installations = zup.Installations; @@ -51,26 +52,11 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I defer allocator.free(installation_dir); std.log.info("Installing {s}, version {}", .{ name, installation.version }); - const filename = std.fs.path.basename(installation.url.?); - - // TODO: Platform-agnostic tempfile creation - var tmpname = try std.fmt.allocPrintZ(allocator, "/tmp/{s}", .{filename}); - defer allocator.free(tmpname); - - var tmpdir = try std.fs.openDirAbsolute(std.fs.path.dirname(tmpname).?, .{}); - defer tmpdir.close(); - - var tmpfile = try tmpdir.createFile(filename, .{}); - defer { - tmpfile.close(); - std.log.info("Deleting /tmp/{s}...", .{filename}); - tmpdir.deleteFile(filename) catch |err| { - std.log.warn("Failed to delete /tmp/{s}: {}", .{ filename, err }); - }; - } - std.log.info("Downloading to /tmp/{s}...", .{filename}); - try curl.easyDownloadToFile(&tmpfile, installation.url.?); + std.log.info("Downloading from {s}...", .{installation.url_str.?}); + + var data = try EasyHttp.get(allocator, installation.url.?); + defer allocator.free(data); std.log.info("Extracting...", .{}); var archive = try ArchiveRead.init(); @@ -78,7 +64,7 @@ pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: I try archive.supportFilter(.all); try archive.supportFormat(.all); - try archive.openFilename(tmpname, 10240); + try archive.openMemory(data); while (try archive.nextHeader()) |entry_c| { var entry = entry_c; diff --git a/src/main.zig b/src/main.zig index 93cd5d4..5522915 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,4 +1,5 @@ pub const Config = @import("Config.zig"); +pub const EasyHttp = @import("EasyHttp.zig"); pub const Installation = @import("Installation.zig"); pub const Installations = Installation.Installations; pub const SubCommand = @import("subcommand.zig").SubCommand; -- cgit v1.2.3