refactor(endPointFromUri): parse Uri using standard library
All checks were successful
AWS-Zig Build / build-zig-amd64-host (push) Successful in 7m13s

This commit is contained in:
Simon Hartcher 2025-04-23 10:44:56 +10:00
parent f4c306a2df
commit 3cae1882f7

View file

@ -463,41 +463,19 @@ fn s3BucketFromPath(path: []const u8) []const u8 {
/// allocator: Will be used only to construct the EndPoint struct /// allocator: Will be used only to construct the EndPoint struct
/// uri: string constructed in such a way that deallocation is needed /// uri: string constructed in such a way that deallocation is needed
fn endPointFromUri(allocator: std.mem.Allocator, uri: []const u8, path: []const u8) !EndPoint { fn endPointFromUri(allocator: std.mem.Allocator, uri: []const u8, path: []const u8) !EndPoint {
var scheme: []const u8 = ""; const parsed_uri = try std.Uri.parse(uri);
var host: []const u8 = "";
var port: u16 = 443; const scheme = parsed_uri.scheme;
var host_start: usize = 0; const host = try allocator.dupe(u8, parsed_uri.host.?.percent_encoded);
var host_end: usize = 0; const port: u16 = blk: {
for (uri, 0..) |ch, i| { if (parsed_uri.port) |port| break :blk port;
switch (ch) { if (std.mem.eql(u8, scheme, "http")) break :blk 80;
':' => { if (std.mem.eql(u8, scheme, "https")) break :blk 443;
if (!std.mem.eql(u8, scheme, "")) { break :blk 0;
// here to end is port - this is likely a bug if ipv6 address used };
const rest_of_uri = uri[i + 1 ..];
port = try std.fmt.parseUnsigned(u16, rest_of_uri, 10);
host_end = i;
}
},
'/' => {
if (host_start == 0) {
host_start = i + 2;
scheme = uri[0 .. i - 1];
if (std.mem.eql(u8, scheme, "http")) {
port = 80;
} else {
port = 443;
}
}
},
else => continue,
}
}
if (host_end == 0) {
host_end = uri.len;
}
host = try allocator.dupe(u8, uri[host_start..host_end]);
log.debug("host: {s}, scheme: {s}, port: {}", .{ host, scheme, port }); log.debug("host: {s}, scheme: {s}, port: {}", .{ host, scheme, port });
return EndPoint{ return EndPoint{
.uri = uri, .uri = uri,
.host = host, .host = host,