From 3531bd1b5c0a774660dfa4abed3566dd15148d74 Mon Sep 17 00:00:00 2001 From: melhindi Date: Wed, 17 Jan 2024 17:23:42 +0100 Subject: [PATCH] Fix compilation issues on zig master --- Package.zig | 15 +++++---- build.zig | 26 ++++++--------- build.zig.zon | 7 ++-- codegen/src/Hasher.zig | 10 +++--- codegen/src/main.zig | 34 +++++++++---------- src/aws.zig | 8 ++--- src/aws_credentials.zig | 59 ++++++++++++++------------------- src/aws_http.zig | 16 ++++----- src/aws_signing.zig | 16 ++++----- src/date.zig | 16 ++++----- src/http_client_17015_issue.zig | 6 ++-- src/json.zig | 18 +++++----- src/main.zig | 11 +++--- src/xml.zig | 2 +- src/xml_shaper.zig | 4 +-- 15 files changed, 120 insertions(+), 128 deletions(-) diff --git a/Package.zig b/Package.zig index eea3212..6cb36b9 100644 --- a/Package.zig +++ b/Package.zig @@ -33,7 +33,7 @@ pub fn fetchOneAndUnpack( var http_client: std.http.Client = .{ .allocator = allocator }; defer http_client.deinit(); - var global_cache_directory: std.Build.Cache.Directory = .{ + const global_cache_directory: std.Build.Cache.Directory = .{ .handle = try std.fs.cwd().makeOpenPath(cache_directory, .{}), .path = cache_directory, }; @@ -111,12 +111,12 @@ pub fn fetchAndUnpack( const path = try global_cache_directory.join(gpa, &.{tmp_dir_sub_path}); errdefer gpa.free(path); - const iterable_dir = try global_cache_directory.handle.makeOpenPathIterable(tmp_dir_sub_path, .{}); + const iterable_dir = try global_cache_directory.handle.makeOpenPath(tmp_dir_sub_path, .{}); errdefer iterable_dir.close(); break :d .{ .path = path, - .handle = iterable_dir.dir, + .handle = iterable_dir, }; }; defer tmp_directory.closeAndFree(gpa); @@ -124,10 +124,10 @@ pub fn fetchAndUnpack( var h = std.http.Headers{ .allocator = gpa }; defer h.deinit(); - var req = try http_client.request(.GET, uri, h, .{}); + var req = try http_client.open(.GET, uri, h, .{}); defer req.deinit(); - try req.start(); + try req.send(.{}); try req.wait(); if (req.response.status != .ok) { @@ -202,7 +202,8 @@ pub fn fetchAndUnpack( // Of course, if the ignore rules above omit the file from the package, then everything // is fine and no error should be raised. - break :a try Hasher.computeDirectoryHash(thread_pool, .{ .dir = tmp_directory.handle }, &.{}); + var options = .{}; + break :a try Hasher.computeDirectoryHash(thread_pool, tmp_directory.handle, &options); }; const pkg_dir_sub_path = "p" ++ s ++ Hasher.hexDigest(actual_hash); @@ -464,7 +465,7 @@ test "fetch and unpack" { var http_client: std.http.Client = .{ .allocator = alloc }; defer http_client.deinit(); - var global_cache_directory: std.Build.Cache.Directory = .{ + const global_cache_directory: std.Build.Cache.Directory = .{ .handle = try std.fs.cwd().makeOpenPath("test-pkg", .{}), .path = "test-pkg", }; diff --git a/build.zig b/build.zig index 5081721..dab1b0c 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); -const Builder = @import("std").build.Builder; +const Builder = @import("std").Build; const Package = @import("Package.zig"); const models_url = "https://github.com/aws/aws-sdk-go-v2/archive/7502ff360b1c3b79cbe117437327f6ff5fb89f65.tar.gz"; @@ -79,22 +79,18 @@ pub fn build(b: *Builder) !void { .optimize = optimize, }); const smithy_module = smithy_dep.module("smithy"); - exe.addModule("smithy", smithy_module); // not sure this should be here... + exe.root_module.addImport("smithy", smithy_module); // not sure this should be here... // Expose module to others _ = b.addModule("aws", .{ - .source_file = .{ .path = "src/aws.zig" }, - .dependencies = &[_]std.build.ModuleDependency{ - .{ .name = "smithy", .module = smithy_module }, - }, + .root_source_file = .{ .path = "src/aws.zig" }, + .imports = &.{.{ .name = "smithy", .module = smithy_module }}, }); // Expose module to others _ = b.addModule("aws-signing", .{ - .source_file = .{ .path = "src/aws_signing.zig" }, - .dependencies = &[_]std.build.ModuleDependency{ - .{ .name = "smithy", .module = smithy_module }, - }, + .root_source_file = .{ .path = "src/aws_signing.zig" }, + .imports = &.{.{ .name = "smithy", .module = smithy_module }}, }); // TODO: This does not work correctly due to https://github.com/ziglang/zig/issues/16354 // @@ -128,10 +124,10 @@ pub fn build(b: *Builder) !void { .name = "codegen", .root_source_file = .{ .path = "codegen/src/main.zig" }, // We need this generated for the host, not the real target - // .target = target, + .target = b.host, .optimize = if (b.verbose) .Debug else .ReleaseSafe, }); - cg_exe.addModule("smithy", smithy_dep.module("smithy")); + cg_exe.root_module.addImport("smithy", smithy_dep.module("smithy")); var cg_cmd = b.addRunArtifact(cg_exe); cg_cmd.addArg("--models"); cg_cmd.addArg(try std.fs.path.join( @@ -139,7 +135,7 @@ pub fn build(b: *Builder) !void { &[_][]const u8{ b.global_cache_root.path.?, models_dir }, )); cg_cmd.addArg("--output"); - cg_cmd.addDirectoryArg(std.Build.FileSource.relative("src/models")); + cg_cmd.addDirectoryArg(std.Build.LazyPath.relative("src/models")); if (b.verbose) cg_cmd.addArg("--verbose"); cg_cmd.step.dependOn(&fetch_step.step); @@ -173,10 +169,10 @@ pub fn build(b: *Builder) !void { // but does not run it. const unit_tests = b.addTest(.{ .root_source_file = .{ .path = "src/aws.zig" }, - .target = t, + .target = b.resolveTargetQuery(t), .optimize = optimize, }); - unit_tests.addModule("smithy", smithy_dep.module("smithy")); + unit_tests.root_module.addImport("smithy", smithy_dep.module("smithy")); unit_tests.step.dependOn(gen_step); const run_unit_tests = b.addRunArtifact(unit_tests); diff --git a/build.zig.zon b/build.zig.zon index f025b01..41e3a27 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,11 +1,14 @@ .{ .name = "aws-zig", .version = "0.0.1", + .paths = .{""}, .dependencies = .{ .smithy = .{ - .url = "https://git.lerch.org/lobo/smithy/archive/41b61745d25a65817209dd5dddbb5f9b66896a99.tar.gz", - .hash = "122087deb0ae309b2258d59b40d82fe5921fdfc35b420bb59033244851f7f276fa34", + .url = "https://github.com/melhindi/smithy/archive/refs/heads/master.tar.gz", + .hash = "12209568cc5c0bad2fcf6a62dbf6792fbbf3727214da98b21c390fc399f6dd743d3a", + // .url = "https://git.lerch.org/lobo/smithy/archive/41b61745d25a65817209dd5dddbb5f9b66896a99.tar.gz", + // .hash = "122087deb0ae309b2258d59b40d82fe5921fdfc35b420bb59033244851f7f276fa34", }, }, } diff --git a/codegen/src/Hasher.zig b/codegen/src/Hasher.zig index 4d08819..95ebf1f 100644 --- a/codegen/src/Hasher.zig +++ b/codegen/src/Hasher.zig @@ -77,13 +77,13 @@ pub fn hex64(x: u64) [16]u8 { return result; } -pub const walkerFn = *const fn (std.fs.IterableDir.Walker.WalkerEntry) bool; +pub const walkerFn = *const fn (std.fs.Dir.Walker.WalkerEntry) bool; -fn included(entry: std.fs.IterableDir.Walker.WalkerEntry) bool { +fn included(entry: std.fs.Dir.Walker.WalkerEntry) bool { _ = entry; return true; } -fn excluded(entry: std.fs.IterableDir.Walker.WalkerEntry) bool { +fn excluded(entry: std.fs.Dir.Walker.WalkerEntry) bool { _ = entry; return false; } @@ -96,7 +96,7 @@ pub const ComputeDirectoryOptions = struct { pub fn computeDirectoryHash( thread_pool: *std.Thread.Pool, - dir: std.fs.IterableDir, + dir: std.fs.Dir, options: *ComputeDirectoryOptions, ) ![Hash.digest_length]u8 { const gpa = thread_pool.allocator; @@ -138,7 +138,7 @@ pub fn computeDirectoryHash( .failure = undefined, // to be populated by the worker }; wait_group.start(); - try thread_pool.spawn(workerHashFile, .{ dir.dir, hashed_file, &wait_group }); + try thread_pool.spawn(workerHashFile, .{ dir, hashed_file, &wait_group }); try all_files.append(hashed_file); } diff --git a/codegen/src/main.zig b/codegen/src/main.zig index 3fcc457..948dfdc 100644 --- a/codegen/src/main.zig +++ b/codegen/src/main.zig @@ -17,7 +17,7 @@ pub fn main() anyerror!void { var output_dir = std.fs.cwd(); defer if (output_dir.fd > 0) output_dir.close(); - var models_dir: ?std.fs.IterableDir = null; + var models_dir: ?std.fs.Dir = null; defer if (models_dir) |*m| m.close(); for (args, 0..) |arg, i| { if (std.mem.eql(u8, "--help", arg) or @@ -31,13 +31,13 @@ pub fn main() anyerror!void { if (std.mem.eql(u8, "--output", arg)) output_dir = try output_dir.makeOpenPath(args[i + 1], .{}); if (std.mem.eql(u8, "--models", arg)) - models_dir = try std.fs.cwd().openIterableDir(args[i + 1], .{}); + models_dir = try std.fs.cwd().openDir(args[i + 1], .{ .iterate = true }); } // TODO: Seems like we should remove this in favor of a package try output_dir.writeFile("json.zig", json_zig); // TODO: We need a different way to handle this file... - var manifest_file_started = false; + const manifest_file_started = false; var manifest_file: std.fs.File = undefined; defer if (manifest_file_started) manifest_file.close(); var manifest: std.fs.File.Writer = undefined; @@ -71,11 +71,11 @@ pub fn main() anyerror!void { // this is our normal mode of operation and where initial optimizations // can be made if (models_dir) |m| { - var cwd = try std.fs.cwd().openDir(".", .{}); + var cwd = try std.fs.cwd().openDir(".", .{ .iterate = true }); defer cwd.close(); defer cwd.setAsCwd() catch unreachable; - try m.dir.setAsCwd(); + try m.setAsCwd(); try processDirectories(m, output_dir); } } @@ -87,7 +87,7 @@ const OutputManifest = struct { model_dir_hash_digest: [Hasher.hex_multihash_len]u8, output_dir_hash_digest: [Hasher.hex_multihash_len]u8, }; -fn processDirectories(models_dir: std.fs.IterableDir, output_dir: std.fs.Dir) !void { +fn processDirectories(models_dir: std.fs.Dir, output_dir: std.fs.Dir) !void { // Let's get ready to hash!! var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); @@ -131,15 +131,15 @@ fn processDirectories(models_dir: std.fs.IterableDir, output_dir: std.fs.Dir) !v } var model_digest: ?[Hasher.hex_multihash_len]u8 = null; -fn calculateDigests(models_dir: std.fs.IterableDir, output_dir: std.fs.Dir, thread_pool: *std.Thread.Pool) !OutputManifest { +fn calculateDigests(models_dir: std.fs.Dir, output_dir: std.fs.Dir, thread_pool: *std.Thread.Pool) !OutputManifest { const model_hash = if (model_digest) |m| m[0..Hasher.digest_len].* else try Hasher.computeDirectoryHash(thread_pool, models_dir, @constCast(&Hasher.ComputeDirectoryOptions{ .isIncluded = struct { - pub fn include(entry: std.fs.IterableDir.Walker.WalkerEntry) bool { + pub fn include(entry: std.fs.Dir.Walker.WalkerEntry) bool { return std.mem.endsWith(u8, entry.basename, ".json"); } }.include, .isExcluded = struct { - pub fn exclude(entry: std.fs.IterableDir.Walker.WalkerEntry) bool { + pub fn exclude(entry: std.fs.Dir.Walker.WalkerEntry) bool { _ = entry; return false; } @@ -148,14 +148,14 @@ fn calculateDigests(models_dir: std.fs.IterableDir, output_dir: std.fs.Dir, thre })); if (verbose) std.log.info("Model directory hash: {s}", .{model_digest orelse Hasher.hexDigest(model_hash)}); - const output_hash = try Hasher.computeDirectoryHash(thread_pool, try output_dir.openIterableDir(".", .{}), @constCast(&Hasher.ComputeDirectoryOptions{ + const output_hash = try Hasher.computeDirectoryHash(thread_pool, try output_dir.openDir(".", .{ .iterate = true }), @constCast(&Hasher.ComputeDirectoryOptions{ .isIncluded = struct { - pub fn include(entry: std.fs.IterableDir.Walker.WalkerEntry) bool { + pub fn include(entry: std.fs.Dir.Walker.WalkerEntry) bool { return std.mem.endsWith(u8, entry.basename, ".zig"); } }.include, .isExcluded = struct { - pub fn exclude(entry: std.fs.IterableDir.Walker.WalkerEntry) bool { + pub fn exclude(entry: std.fs.Dir.Walker.WalkerEntry) bool { _ = entry; return false; } @@ -201,7 +201,7 @@ fn processFile(file_name: []const u8, output_dir: std.fs.Dir, manifest: anytype) defer allocator.free(output_file_name); for (service_names) |name| { const seperator = if (output_file_name.len > 0) "-" else ""; - var new_output_file_name = try std.fmt.allocPrint( + const new_output_file_name = try std.fmt.allocPrint( allocator, "{s}{s}{s}", .{ output_file_name, seperator, name }, @@ -211,7 +211,7 @@ fn processFile(file_name: []const u8, output_dir: std.fs.Dir, manifest: anytype) } { // append .zig on to the file name - var new_output_file_name = try std.fmt.allocPrint( + const new_output_file_name = try std.fmt.allocPrint( allocator, "{s}.zig", .{output_file_name}, @@ -337,7 +337,7 @@ fn generateServices(allocator: std.mem.Allocator, comptime _: []const u8, file: var generated = std.StringHashMap(void).init(allocator); defer generated.deinit(); - var state = FileGenerationState{ + const state = FileGenerationState{ .shape_references = shape_references, .additional_types_to_generate = &unresolved, .additional_types_generated = &generated, @@ -345,8 +345,8 @@ fn generateServices(allocator: std.mem.Allocator, comptime _: []const u8, file: }; for (services.items) |service| { var sdk_id: []const u8 = undefined; - var version: []const u8 = service.shape.service.version; - var name: []const u8 = service.name; + const version: []const u8 = service.shape.service.version; + const name: []const u8 = service.name; var arn_namespace: []const u8 = undefined; var sigv4_name: []const u8 = undefined; var endpoint_prefix: []const u8 = undefined; diff --git a/src/aws.zig b/src/aws.zig index fcf2bbb..73b032f 100644 --- a/src/aws.zig +++ b/src/aws.zig @@ -31,7 +31,7 @@ pub const services = servicemodel.services; pub const Services = servicemodel.Services; pub const ClientOptions = struct { - proxy: ?std.http.Client.HttpProxy = null, + proxy: ?std.http.Client.Proxy = null, }; pub const Client = struct { allocator: std.mem.Allocator, @@ -365,7 +365,7 @@ pub fn Request(comptime request_action: anytype) type { .raw_parsed = .{ .raw = .{} }, .allocator = options.client.allocator, }; - var body_field = @field(rc.response, action.Response.http_payload); + const body_field = @field(rc.response, action.Response.http_payload); const BodyField = @TypeOf(body_field); if (BodyField == []const u8 or BodyField == ?[]const u8) { expected_body_field_len = 0; @@ -875,7 +875,7 @@ fn FullResponse(comptime action: anytype) type { } } if (@hasDecl(Response, "http_payload")) { - var body_field = @field(self.response, Response.http_payload); + const body_field = @field(self.response, Response.http_payload); const BodyField = @TypeOf(body_field); if (BodyField == []const u8) { self.allocator.free(body_field); @@ -1465,7 +1465,7 @@ fn processRequest(options: *TestOptions, server: *std.http.Server) !void { else res.transfer_encoding = .chunked; - try res.do(); + try res.send(); _ = try res.writer().writeAll(response_bytes); try res.finish(); log.debug( diff --git a/src/aws_credentials.zig b/src/aws_credentials.zig index f3fc8ab..95e8dcf 100644 --- a/src/aws_credentials.zig +++ b/src/aws_credentials.zig @@ -126,9 +126,9 @@ fn getContainerCredentials(allocator: std.mem.Allocator) !?auth.Credentials { defer empty_headers.deinit(); var cl = std.http.Client{ .allocator = allocator }; defer cl.deinit(); // I don't belive connection pooling would help much here as it's non-ssl and local - var req = try cl.request(.GET, try std.Uri.parse(container_uri), empty_headers, .{}); + var req = try cl.open(.GET, try std.Uri.parse(container_uri), empty_headers, .{}); defer req.deinit(); - try req.start(); + try req.send(.{}); try req.wait(); if (req.response.status != .ok and req.response.status != .not_found) { log.warn("Bad status code received from container credentials endpoint: {}", .{@intFromEnum(req.response.status)}); @@ -140,7 +140,7 @@ fn getContainerCredentials(allocator: std.mem.Allocator) !?auth.Credentials { var resp_payload = try std.ArrayList(u8).initCapacity(allocator, @intCast(req.response.content_length.?)); defer resp_payload.deinit(); try resp_payload.resize(@intCast(req.response.content_length.?)); - var response_data = try resp_payload.toOwnedSlice(); + const response_data = try resp_payload.toOwnedSlice(); defer allocator.free(response_data); _ = try req.readAll(response_data); log.debug("Read {d} bytes from container credentials endpoint", .{response_data.len}); @@ -185,9 +185,9 @@ fn getImdsv2Credentials(allocator: std.mem.Allocator) !?auth.Credentials { var headers = std.http.Headers.init(allocator); defer headers.deinit(); try headers.append("X-aws-ec2-metadata-token-ttl-seconds", "21600"); - var req = try cl.request(.PUT, try std.Uri.parse("http://169.254.169.254/latest/api/token"), headers, .{}); + var req = try cl.open(.PUT, try std.Uri.parse("http://169.254.169.254/latest/api/token"), headers, .{}); defer req.deinit(); - try req.start(); + try req.send(.{}); try req.wait(); if (req.response.status != .ok) { log.warn("Bad status code received from IMDS v2: {}", .{@intFromEnum(req.response.status)}); @@ -228,10 +228,10 @@ fn getImdsRoleName(allocator: std.mem.Allocator, client: *std.http.Client, imds_ defer headers.deinit(); try headers.append("X-aws-ec2-metadata-token", imds_token); - var req = try client.request(.GET, try std.Uri.parse("http://169.254.169.254/latest/meta-data/iam/info"), headers, .{}); + var req = try client.open(.GET, try std.Uri.parse("http://169.254.169.254/latest/meta-data/iam/info"), headers, .{}); defer req.deinit(); - try req.start(); + try req.send(.{}); try req.wait(); if (req.response.status != .ok and req.response.status != .not_found) { @@ -243,7 +243,7 @@ fn getImdsRoleName(allocator: std.mem.Allocator, client: *std.http.Client, imds_ log.warn("Unexpected empty response from IMDS endpoint post token", .{}); return null; } - var resp = try allocator.alloc(u8, @intCast(req.response.content_length.?)); + const resp = try allocator.alloc(u8, @intCast(req.response.content_length.?)); defer allocator.free(resp); _ = try req.readAll(resp); @@ -281,10 +281,10 @@ fn getImdsCredentials(allocator: std.mem.Allocator, client: *std.http.Client, ro const url = try std.fmt.allocPrint(allocator, "http://169.254.169.254/latest/meta-data/iam/security-credentials/{s}/", .{role_name}); defer allocator.free(url); - var req = try client.request(.GET, try std.Uri.parse(url), headers, .{}); + var req = try client.open(.GET, try std.Uri.parse(url), headers, .{}); defer req.deinit(); - try req.start(); + try req.send(.{}); try req.wait(); if (req.response.status != .ok and req.response.status != .not_found) { @@ -296,7 +296,7 @@ fn getImdsCredentials(allocator: std.mem.Allocator, client: *std.http.Client, ro log.warn("Unexpected empty response from IMDS role endpoint", .{}); return null; } - var resp = try allocator.alloc(u8, @intCast(req.response.content_length.?)); + const resp = try allocator.alloc(u8, @intCast(req.response.content_length.?)); defer allocator.free(resp); _ = try req.readAll(resp); @@ -455,7 +455,7 @@ const LineIterator = struct { pub fn next(self: *Self) ?[]const u8 { if (self.inx >= self.text.len) return null; var current = self.inx; - var start = self.inx; + const start = self.inx; for (self.text[self.inx..], 0..) |c, i| { if (c == '\n') { // log.debug("got \\n: {d}", .{i}); @@ -571,7 +571,7 @@ const EvaluatedPath = struct { evaluated_path: []const u8, }; fn getDefaultPath(allocator: std.mem.Allocator, home_dir: ?[]const u8, dir: []const u8, file: []const u8) !EvaluatedPath { - var home = home_dir orelse try getHomeDir(allocator); + const home = home_dir orelse try getHomeDir(allocator); log.debug("Home directory: {s}", .{home}); const rc = try std.fs.path.join(allocator, &[_][]const u8{ home, dir, file }); log.debug("Path evaluated as: {s}", .{rc}); @@ -581,29 +581,18 @@ fn getDefaultPath(allocator: std.mem.Allocator, home_dir: ?[]const u8, dir: []co fn getHomeDir(allocator: std.mem.Allocator) ![]const u8 { switch (builtin.os.tag) { .windows => { - var dir_path_ptr: [*:0]u16 = undefined; // https://docs.microsoft.com/en-us/windows/win32/shell/knownfolderid - const FOLDERID_Profile = std.os.windows.GUID.parse("{5E6C858F-0E22-4760-9AFE-EA3317B67173}"); - switch (std.os.windows.shell32.SHGetKnownFolderPath( - &FOLDERID_Profile, - std.os.windows.KF_FLAG_CREATE, - null, - &dir_path_ptr, - )) { - std.os.windows.S_OK => { - defer std.os.windows.ole32.CoTaskMemFree(@as(*anyopaque, @ptrCast(dir_path_ptr))); - const global_dir = std.unicode.utf16leToUtf8Alloc(allocator, std.mem.sliceTo(dir_path_ptr, 0)) catch |err| switch (err) { - error.UnexpectedSecondSurrogateHalf => return error.HomeDirUnavailable, - error.ExpectedSecondSurrogateHalf => return error.HomeDirUnavailable, - error.DanglingSurrogateHalf => return error.HomeDirUnavailable, - error.OutOfMemory => return error.OutOfMemory, - }; - return global_dir; - // defer allocator.free(global_dir); - }, - std.os.windows.E_OUTOFMEMORY => return error.OutOfMemory, - else => return error.HomeDirUnavailable, - } + //const FOLDERID_Profile = std.os.windows.GUID.parse("{5E6C858F-0E22-4760-9AFE-EA3317B67173}"); + + const local_app_data_dir = std.process.getEnvVarOwned(allocator, "LOCALAPPDATA") catch |err| switch (err) { + error.InvalidUtf8 => return error.HomeDirUnavailable, + error.EnvironmentVariableNotFound => return error.HomeDirUnavailable, + error.OutOfMemory => return error.OutOfMemory, + }; + + //defer allocator.free(local_app_data_dir); + //return std.fs.path.join(allocator, &[_][]const u8{ local_app_data_dir, appname }); + return local_app_data_dir; }, .macos, .linux, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris => { const home_dir = std.os.getenv("HOME") orelse { diff --git a/src/aws_http.zig b/src/aws_http.zig index b3f886e..5be6c71 100644 --- a/src/aws_http.zig +++ b/src/aws_http.zig @@ -64,11 +64,11 @@ const EndPoint = struct { }; pub const AwsHttp = struct { allocator: std.mem.Allocator, - proxy: ?std.http.Client.HttpProxy, + proxy: ?std.http.Client.Proxy, const Self = @This(); - pub fn init(allocator: std.mem.Allocator, proxy: ?std.http.Client.HttpProxy) Self { + pub fn init(allocator: std.mem.Allocator, proxy: ?std.http.Client.Proxy) Self { return Self{ .allocator = allocator, .proxy = proxy, @@ -175,7 +175,7 @@ pub const AwsHttp = struct { const url = try std.fmt.allocPrint(self.allocator, "{s}{s}{s}", .{ endpoint.uri, request_cp.path, request_cp.query }); defer self.allocator.free(url); log.debug("Request url: {s}", .{url}); - var cl = std.http.Client{ .allocator = self.allocator, .proxy = self.proxy }; + var cl = std.http.Client{ .allocator = self.allocator, .http_proxy = self.proxy }; defer cl.deinit(); // TODO: Connection pooling // // var req = try zfetch.Request.init(self.allocator, url, self.trust_chain); @@ -209,11 +209,11 @@ pub const AwsHttp = struct { // // const unescaped_url = try std.Uri.unescapeString(self.allocator, url); // defer self.allocator.free(unescaped_url); - var req = try cl.request(method, try std.Uri.parse(url), headers, .{}); + var req = try cl.open(method, try std.Uri.parse(url), headers, .{}); defer req.deinit(); if (request_cp.body.len > 0) req.transfer_encoding = .{ .content_length = request_cp.body.len }; - try @import("http_client_17015_issue.zig").start(&req); + try @import("http_client_17015_issue.zig").send(&req); // try req.start(); if (request_cp.body.len > 0) { // Workaround for https://github.com/ziglang/zig/issues/15626 @@ -250,12 +250,12 @@ pub const AwsHttp = struct { content_length = std.fmt.parseInt(usize, h.value, 10) catch 0; } - var response_data: []u8 = - if (req.response.transfer_encoding) |_| // the only value here is "chunked" + const response_data: []u8 = + if (req.response.transfer_encoding == .none) // the only value here is "chunked" try req.reader().readAllAlloc(self.allocator, std.math.maxInt(usize)) else blk: { // content length - var tmp_data = try self.allocator.alloc(u8, content_length); + const tmp_data = try self.allocator.alloc(u8, content_length); errdefer self.allocator.free(tmp_data); _ = try req.readAll(tmp_data); break :blk tmp_data; diff --git a/src/aws_signing.zig b/src/aws_signing.zig index 4286bd9..e8083e7 100644 --- a/src/aws_signing.zig +++ b/src/aws_signing.zig @@ -177,7 +177,7 @@ pub fn signRequest(allocator: std.mem.Allocator, request: base.Request, config: }; } errdefer freeSignedRequest(allocator, &rc, config); - std.mem.copy(base.Header, newheaders, oldheaders); + @memcpy(newheaders[0..oldheaders.len], oldheaders); newheaders[newheaders.len - 3] = base.Header{ .name = "X-Amz-Date", .value = signing_iso8601, @@ -364,7 +364,7 @@ fn verifyParsedAuthorization( const service = credential_iterator.next().?; const aws4_request = credential_iterator.next().?; if (!std.mem.eql(u8, aws4_request, "aws4_request")) return error.UnexpectedCredentialValue; - var config = Config{ + const config = Config{ .service = service, .credentials = credentials, .region = region, @@ -444,7 +444,7 @@ fn getSigningKey(allocator: std.mem.Allocator, signing_date: []const u8, config: \\ region: {s} \\ service: {s} , .{ signing_date, config.region, config.service }); - var secret = try std.fmt.allocPrint(allocator, "AWS4{s}", .{config.credentials.secret_key}); + const secret = try std.fmt.allocPrint(allocator, "AWS4{s}", .{config.credentials.secret_key}); defer { // secureZero avoids compiler optimizations that may say // "WTF are you doing this thing? Looks like nothing to me. It's silly and we will remove it" @@ -716,7 +716,7 @@ fn canonicalQueryString(allocator: std.mem.Allocator, path: []const u8) ![]const for (sort_me.items) |i| { if (!first) try normalized.append('&'); first = false; - var first_equals = std.mem.indexOf(u8, i, "="); + const first_equals = std.mem.indexOf(u8, i, "="); if (first_equals == null) { // Rare. This is "foo=" const normed_item = try encodeUri(allocator, i); @@ -744,7 +744,7 @@ fn canonicalQueryString(allocator: std.mem.Allocator, path: []const u8) ![]const } fn replace(allocator: std.mem.Allocator, haystack: []const u8, needle: []const u8, replacement_value: []const u8) ![]const u8 { - var buffer = try allocator.alloc(u8, std.mem.replacementSize(u8, haystack, needle, replacement_value)); + const buffer = try allocator.alloc(u8, std.mem.replacementSize(u8, haystack, needle, replacement_value)); _ = std.mem.replace(u8, haystack, needle, replacement_value, buffer); return buffer; } @@ -837,7 +837,7 @@ fn canonicalHeaders(allocator: std.mem.Allocator, headers: []base.Header, servic fn canonicalHeaderValue(allocator: std.mem.Allocator, value: []const u8) ![]const u8 { var started = false; - var in_quote = false; + const in_quote = false; var start: usize = 0; const rc = try allocator.alloc(u8, value.len); var rc_inx: usize = 0; @@ -1002,7 +1002,7 @@ test "can sign" { try headers.append(.{ .name = "Content-Type", .value = "application/x-www-form-urlencoded; charset=utf-8" }); try headers.append(.{ .name = "Content-Length", .value = "13" }); try headers.append(.{ .name = "Host", .value = "example.amazonaws.com" }); - var req = base.Request{ + const req = base.Request{ .path = "/", .query = "", .body = "Param1=value1", @@ -1071,7 +1071,7 @@ test "can verify server request" { var buf = "bar".*; var fis = std.io.fixedBufferStream(&buf); - var request = std.http.Server.Request{ + const request = std.http.Server.Request{ .method = std.http.Method.PUT, .target = "/mysfitszj3t6webstack-hostingbucketa91a61fe-1ep3ezkgwpxr0/i/am/a/teapot/foo?x-id=PutObject", .version = .@"HTTP/1.1", diff --git a/src/date.zig b/src/date.zig index cd81843..aec04e7 100644 --- a/src/date.zig +++ b/src/date.zig @@ -21,7 +21,7 @@ pub fn timestampToDateTime(timestamp: i64) DateTime { const DAY_NUMBER_ADJUSTED_1970_01_01 = 719468; //* Day number relates to March 1st */ var dayN: u64 = DAY_NUMBER_ADJUSTED_1970_01_01 + unixtime / SECONDS_PER_DAY; - var seconds_since_midnight: u64 = unixtime % SECONDS_PER_DAY; + const seconds_since_midnight: u64 = unixtime % SECONDS_PER_DAY; var temp: u64 = 0; // Leap year rules for Gregorian Calendars @@ -37,7 +37,7 @@ pub fn timestampToDateTime(timestamp: i64) DateTime { // dayN calculates the days of the year in relation to March 1 var month = @as(u8, @intCast((5 * dayN + 2) / 153)); - var day = @as(u8, @intCast(dayN - (@as(u64, @intCast(month)) * 153 + 2) / 5 + 1)); + const day = @as(u8, @intCast(dayN - (@as(u64, @intCast(month)) * 153 + 2) / 5 + 1)); // 153 = 31+30+31+30+31 Days for the 5 months from March through July // 153 = 31+30+31+30+31 Days for the 5 months from August through December // 31+28 Days for January and February (see below) @@ -50,9 +50,9 @@ pub fn timestampToDateTime(timestamp: i64) DateTime { year += 1; } - var hours = @as(u8, @intCast(seconds_since_midnight / 3600)); - var minutes = @as(u8, @intCast(seconds_since_midnight % 3600 / 60)); - var seconds = @as(u8, @intCast(seconds_since_midnight % 60)); + const hours = @as(u8, @intCast(seconds_since_midnight / 3600)); + const minutes = @as(u8, @intCast(seconds_since_midnight % 3600 / 60)); + const seconds = @as(u8, @intCast(seconds_since_midnight % 60)); return DateTime{ .day = day, .month = month, .year = year, .hour = hours, .minute = minutes, .second = seconds }; } @@ -275,10 +275,10 @@ fn secondsBetween(start: DateTime, end: DateTime) DateTimeToTimestampError!i64 { return (try secondsBetween(new_start, end)) - seconds_into_start_year; } const leap_years_between = leapYearsBetween(start.year, end.year); - var add_days: u1 = 0; + const add_days: u1 = 0; const years_diff = end.year - start.year; // log.debug("Years from epoch: {d}, Leap years: {d}", .{ years_diff, leap_years_between }); - var days_diff: i32 = (years_diff * DAYS_PER_YEAR) + leap_years_between + add_days; + const days_diff: i32 = (years_diff * DAYS_PER_YEAR) + leap_years_between + add_days; // log.debug("Days with leap year, without month: {d}", .{days_diff}); const seconds_into_year = secondsFromBeginningOfYear( @@ -306,7 +306,7 @@ fn secondsFromBeginningOfYear(year: u16, month: u8, day: u8, hour: u8, minute: u const normal_days_per_month: [12]u5 = .{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; const days_per_month = if (current_year_is_leap_year) leap_year_days_per_month else normal_days_per_month; var current_month: usize = 1; - var end_month = month; + const end_month = month; var days_diff: u32 = 0; while (current_month != end_month) { days_diff += days_per_month[current_month - 1]; // months are 1-based vs array is 0-based diff --git a/src/http_client_17015_issue.zig b/src/http_client_17015_issue.zig index 3427f74..73ce4ff 100644 --- a/src/http_client_17015_issue.zig +++ b/src/http_client_17015_issue.zig @@ -10,8 +10,8 @@ const Uri = std.Uri; /// only the two w.print lines for req.uri 16 and 18 lines down from this comment /////////////////////////////////////////////////////////////////////////// /// Send the request to the server. -pub fn start(req: *std.http.Client.Request) std.http.Client.Request.StartError!void { - var buffered = std.io.bufferedWriter(req.connection.?.data.writer()); +pub fn send(req: *std.http.Client.Request) std.http.Client.Request.SendError!void { + var buffered = std.io.bufferedWriter(req.connection.?.writer()); const w = buffered.writer(); try w.writeAll(@tagName(req.method)); @@ -21,7 +21,7 @@ pub fn start(req: *std.http.Client.Request) std.http.Client.Request.StartError!v try w.writeAll(req.uri.host.?); try w.writeByte(':'); try w.print("{}", .{req.uri.port.?}); - } else if (req.connection.?.data.proxied) { + } else if (req.connection.?.proxied) { // proxied connections require the full uri try format(req.uri, "+/", .{}, w); } else { diff --git a/src/json.zig b/src/json.zig index 83429c3..efdb924 100644 --- a/src/json.zig +++ b/src/json.zig @@ -1762,7 +1762,7 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options: var r: T = undefined; const source_slice = stringToken.slice(tokens.slice, tokens.i - 1); switch (stringToken.escapes) { - .None => mem.copy(u8, &r, source_slice), + .None => @memcpy(&r, source_slice), .Some => try unescapeValidString(&r, source_slice), } return r; @@ -2019,7 +2019,7 @@ test "parse into tagged union" { } { // failing allocations should be bubbled up instantly without trying next member - var fail_alloc = testing.FailingAllocator.init(testing.allocator, 0); + var fail_alloc = testing.FailingAllocator.init(testing.allocator, .{}); const options = ParseOptions{ .allocator = fail_alloc.allocator() }; const T = union(enum) { // both fields here match the input @@ -2067,7 +2067,7 @@ test "parse union bubbles up AllocatorRequired" { } test "parseFree descends into tagged union" { - var fail_alloc = testing.FailingAllocator.init(testing.allocator, 1); + var fail_alloc = testing.FailingAllocator.init(testing.allocator, .{}); const options = ParseOptions{ .allocator = fail_alloc.allocator() }; const T = union(enum) { int: i32, @@ -2299,7 +2299,7 @@ pub const Parser = struct { }, .ObjectValue => { var object = &p.stack.items[p.stack.items.len - 2].Object; - var key = p.stack.items[p.stack.items.len - 1].String; + const key = p.stack.items[p.stack.items.len - 1].String; switch (token) { .ObjectBegin => { @@ -2827,14 +2827,14 @@ pub fn stringify( } }, .Enum => { - if (comptime std.meta.trait.hasFn("jsonStringify")(T)) { + if (comptime std.meta.hasFn(T, "jsonStringify")) { return value.jsonStringify(options, out_stream); } @compileError("Unable to stringify enum '" ++ @typeName(T) ++ "'"); }, .Union => { - if (comptime std.meta.trait.hasFn("jsonStringify")(T)) { + if (comptime std.meta.hasFn(T, "jsonStringify")) { return value.jsonStringify(options, out_stream); } @@ -2850,7 +2850,7 @@ pub fn stringify( } }, .Struct => |S| { - if (comptime std.meta.trait.hasFn("jsonStringify")(T)) { + if (comptime std.meta.hasFn(T, "jsonStringify")) { return value.jsonStringify(options, out_stream); } @@ -2874,11 +2874,11 @@ pub fn stringify( try child_whitespace.outputIndent(out_stream); } var field_written = false; - if (comptime std.meta.trait.hasFn("jsonStringifyField")(T)) + if (comptime std.meta.hasFn(T, "jsonStringifyField")) field_written = try value.jsonStringifyField(Field.name, child_options, out_stream); if (!field_written) { - if (comptime std.meta.trait.hasFn("fieldNameFor")(T)) { + if (comptime std.meta.hasFn(T, "fieldNameFor")) { const name = value.fieldNameFor(Field.name); try stringify(name, options, out_stream); } else { diff --git a/src/main.zig b/src/main.zig index 3aad5c9..1f3f31a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -71,7 +71,7 @@ pub fn main() anyerror!void { defer bw.flush() catch unreachable; const stdout = bw.writer(); var arg0: ?[]const u8 = null; - var proxy: ?std.http.Client.HttpProxy = null; + var proxy: ?std.http.Client.Proxy = null; while (args.next()) |arg| { if (arg0 == null) arg0 = arg; if (std.mem.eql(u8, "-h", arg) or std.mem.eql(u8, "--help", arg)) { @@ -87,7 +87,7 @@ pub fn main() anyerror!void { return; } if (std.mem.eql(u8, "-x", arg) or std.mem.eql(u8, "--proxy", arg)) { - proxy = try proxyFromString(args.next().?); // parse stuff + proxy = try proxyFromString(allocator, args.next().?); // parse stuff continue; } if (std.mem.startsWith(u8, arg, "-v")) { @@ -353,10 +353,13 @@ pub fn main() anyerror!void { std.log.info("===== Tests complete =====", .{}); } -fn proxyFromString(string: []const u8) !std.http.Client.HttpProxy { - var rc = std.http.Client.HttpProxy{ +fn proxyFromString(allocator: std.mem.Allocator, string: []const u8) !std.http.Client.Proxy { + var rc = std.http.Client.Proxy{ + .headers = std.http.Headers{ .allocator = allocator }, + .allocator = allocator, .protocol = undefined, .host = undefined, + .port = undefined, }; var remaining: []const u8 = string; if (std.mem.startsWith(u8, string, "http://")) { diff --git a/src/xml.zig b/src/xml.zig index d554c6d..ca6fda4 100644 --- a/src/xml.zig +++ b/src/xml.zig @@ -260,7 +260,7 @@ const ParseContext = struct { begin = prev_nl + 1; } - var end = mem.indexOfScalarPos(u8, self.source, self.offset, '\n') orelse self.source.len; + const end = mem.indexOfScalarPos(u8, self.source, self.offset, '\n') orelse self.source.len; return self.source[begin..end]; } }; diff --git a/src/xml_shaper.zig b/src/xml_shaper.zig index fd51594..0939b0e 100644 --- a/src/xml_shaper.zig +++ b/src/xml_shaper.zig @@ -214,9 +214,9 @@ fn parseInternal(comptime T: type, element: *xml.Element, options: ParseOptions) log.debug("Processing fields in struct: {s}", .{@typeName(T)}); inline for (struct_info.fields, 0..) |field, i| { - var name = field.name; + var name: []const u8 = field.name; var found_value = false; - if (comptime std.meta.trait.hasFn("fieldNameFor")(T)) + if (comptime std.meta.hasFn(T, "fieldNameFor")) name = r.fieldNameFor(field.name); log.debug("Field name: {s}, Element: {s}, Adjusted field name: {s}", .{ field.name, element.tag, name }); var iterator = element.findChildrenByTag(name);