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 --- README.org | 1 - build.zig | 3 --- build.zig.zon | 4 ---- src/EasyHttp.zig | 32 ++++++++++++++++++++++++++++++++ src/Installation.zig | 22 +++++++++++++++------- src/install.zig | 30 ++++++++---------------------- src/main.zig | 1 + 7 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 src/EasyHttp.zig diff --git a/README.org b/README.org index f680594..66bec38 100644 --- a/README.org +++ b/README.org @@ -9,7 +9,6 @@ ** System libraries - libarchive -- libcurl ** Zig version diff --git a/build.zig b/build.zig index b953275..762eb67 100644 --- a/build.zig +++ b/build.zig @@ -14,7 +14,6 @@ pub fn build(b: *Build) void { config.addOption(SemanticVersion, "version", version); const clap = b.dependency("clap", .{}); - const curl = b.dependency("curl", .{}); const libarchive = b.dependency("libarchive", .{}); const xdg = b.dependency("xdg", .{}); @@ -27,12 +26,10 @@ pub fn build(b: *Build) void { }); exe.addOptions("zup-config", config); exe.addModule("clap", clap.module("clap")); - exe.addModule("curl", curl.module("curl")); exe.addModule("libarchive", libarchive.module("libarchive")); exe.addModule("xdg", xdg.module("xdg")); exe.linkLibC(); exe.linkSystemLibrary("libarchive"); - exe.linkSystemLibrary("libcurl"); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); diff --git a/build.zig.zon b/build.zig.zon index 151d3f2..31a3ad8 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -7,10 +7,6 @@ .url = "https://github.com/Hejsil/zig-clap/archive/bdb5853b678d68f342ec65b04a6785af522ca6c9.tar.gz", .hash = "12202af04ec78191f2018458a7be29f54e0d9118f7688e7a226857acf754d68b8473", }, - .curl = .{ - .url = "https://git.sr.ht/~ukko/zig-curl/archive/aa55a3d061fceebf737c0af25a6df2d05f83bcad.tar.gz", - .hash = "12208fc84cbd67383b6fe7e3f65a965cead4577ca27cd59660a2812c9f4edc545c5e", - }, .libarchive = .{ .url = "https://git.sr.ht/~ukko/zig-libarchive/archive/b70c51b8555e68e73c36b0e691dff65c0092baf6.tar.gz", .hash = "12206a8120d9a7550112f2fcc908c46900d6882c95156f900d5e0b39fcdb6a64a065", 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