Skip to content

Commit

Permalink
Support file:/// URIs and relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamGoertz committed Jul 12, 2023
1 parent 6bc9c4f commit 7d279c9
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 136 deletions.
2 changes: 2 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ pub fn build(b: *std.Build) !void {
artifact.linkSystemLibrary("version");
artifact.linkSystemLibrary("uuid");
artifact.linkSystemLibrary("ole32");
artifact.linkSystemLibrary("shlwapi");
}
}
}
Expand Down Expand Up @@ -670,6 +671,7 @@ fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {
exe.linkSystemLibrary("version");
exe.linkSystemLibrary("uuid");
exe.linkSystemLibrary("ole32");
exe.linkSystemLibrary("shlwapi");
}
}

Expand Down
45 changes: 40 additions & 5 deletions lib/std/Uri.zig
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort };
/// original `text`. Each component that is provided, will be non-`null`.
pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
var reader = SliceReader{ .slice = text };

var uri = Uri{
.scheme = "",
.user = null,
Expand All @@ -145,13 +146,14 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
.fragment = null,
};

if (reader.peekPrefix("//")) { // authority part
if (reader.peekPrefix("//")) a: { // authority part
std.debug.assert(reader.get().? == '/');
std.debug.assert(reader.get().? == '/');

const authority = reader.readUntil(isAuthoritySeparator);
if (authority.len == 0)
return error.InvalidFormat;
var authority = reader.readUntil(isAuthoritySeparator);
if (authority.len == 0) {
if (reader.peekPrefix("/")) break :a else return error.InvalidFormat;
}

var start_of_host: usize = 0;
if (std.mem.indexOf(u8, authority, "@")) |index| {
Expand Down Expand Up @@ -223,7 +225,6 @@ pub fn format(
try writer.writeAll(":");
if (uri.host) |host| {
try writer.writeAll("//");

if (uri.user) |user| {
try writer.writeAll(user);
if (uri.password) |password| {
Expand Down Expand Up @@ -473,6 +474,23 @@ test "should fail gracefully" {
try std.testing.expectEqual(@as(ParseError!Uri, error.InvalidFormat), parse("foobar://"));
}

test "file" {
const parsed = try parse("file:///");
try std.testing.expectEqualSlices(u8, "file", parsed.scheme);
try std.testing.expectEqual(@as(?[]const u8, null), parsed.host);
try std.testing.expectEqualSlices(u8, "/", parsed.path);

const parsed2 = try parse("file:///an/absolute/path/to/something");
try std.testing.expectEqualSlices(u8, "file", parsed2.scheme);
try std.testing.expectEqual(@as(?[]const u8, null), parsed2.host);
try std.testing.expectEqualSlices(u8, "/an/absolute/path/to/something", parsed2.path);

const parsed3 = try parse("file://localhost/an/absolute/path/to/another/thing/");
try std.testing.expectEqualSlices(u8, "file", parsed3.scheme);
try std.testing.expectEqualSlices(u8, "localhost", parsed3.host.?);
try std.testing.expectEqualSlices(u8, "/an/absolute/path/to/another/thing/", parsed3.path);
}

test "scheme" {
try std.testing.expectEqualSlices(u8, "http", (try parse("http:_")).scheme);
try std.testing.expectEqualSlices(u8, "scheme-mee", (try parse("scheme-mee:_")).scheme);
Expand Down Expand Up @@ -672,3 +690,20 @@ test "URI unescaping" {

try std.testing.expectEqualSlices(u8, expected, actual);
}

test "format" {
const uri = Uri{
.scheme = "file",
.user = null,
.password = null,
.host = null,
.port = null,
.path = "/foo/bar/baz",
.query = null,
.fragment = null,
};
var buf = std.ArrayList(u8).init(std.testing.allocator);
defer buf.deinit();
try uri.format("+/", .{}, buf.writer());
try std.testing.expectEqualSlices(u8, "file:/foo/bar/baz", buf.items);
}
1 change: 1 addition & 0 deletions lib/std/os/windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub const gdi32 = @import("windows/gdi32.zig");
pub const winmm = @import("windows/winmm.zig");
pub const crypt32 = @import("windows/crypt32.zig");
pub const nls = @import("windows/nls.zig");
pub const shlwapi = @import("windows/shlwapi.zig");

pub const self_process_handle = @as(HANDLE, @ptrFromInt(maxInt(usize)));

Expand Down
13 changes: 13 additions & 0 deletions lib/std/os/windows/shlwapi.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const std = @import("../../std.zig");
const windows = std.os.windows;

const DWORD = windows.DWORD;
const WINAPI = windows.WINAPI;
const HRESULT = windows.HRESULT;
const LPCSTR = windows.LPCSTR;
const LPSTR = windows.LPSTR;
const LPWSTR = windows.LPWSTR;
const LPCWSTR = windows.LPCWSTR;

pub extern "shlwapi" fn PathCreateFromUrlW(pszUrl: LPCWSTR, pszPath: LPWSTR, pcchPath: *DWORD, dwFlags: DWORD) callconv(WINAPI) HRESULT;
pub extern "shlwapi" fn PathCreateFromUrlA(pszUrl: LPCSTR, pszPath: LPSTR, pcchPath: *DWORD, dwFlags: DWORD) callconv(WINAPI) HRESULT;
44 changes: 32 additions & 12 deletions src/Manifest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ pub const basename = "build.zig.zon";
pub const Hash = std.crypto.hash.sha2.Sha256;

pub const Dependency = struct {
url: []const u8,
url_tok: Ast.TokenIndex,
location: union(enum) {
url: []const u8,
path: []const u8,
},
location_tok: Ast.TokenIndex,
hash: ?[]const u8,
hash_tok: Ast.TokenIndex,
};
Expand Down Expand Up @@ -218,12 +221,12 @@ const Parse = struct {
};

var dep: Dependency = .{
.url = undefined,
.url_tok = undefined,
.location = undefined,
.location_tok = undefined,
.hash = null,
.hash_tok = undefined,
};
var have_url = false;
var has_location = false;

for (struct_init.ast.fields) |field_init| {
const name_token = ast.firstToken(field_init) - 2;
Expand All @@ -232,12 +235,29 @@ const Parse = struct {
// things manually provides an opportunity to do any additional verification
// that is desirable on a per-field basis.
if (mem.eql(u8, field_name, "url")) {
dep.url = parseString(p, field_init) catch |err| switch (err) {
error.ParseFailure => continue,
else => |e| return e,
if (has_location) {
return fail(p, main_tokens[field_init], "dependency should specify only one of 'url' and 'path' fields.", .{});
}
dep.location = .{
.url = parseString(p, field_init) catch |err| switch (err) {
error.ParseFailure => continue,
else => |e| return e,
},
};
has_location = true;
dep.location_tok = main_tokens[field_init];
} else if (mem.eql(u8, field_name, "path")) {
if (has_location) {
return fail(p, main_tokens[field_init], "dependency should specify only one of 'url' and 'path' fields.", .{});
}
dep.location = .{
.path = parseString(p, field_init) catch |err| switch (err) {
error.ParseFailure => continue,
else => |e| return e,
},
};
dep.url_tok = main_tokens[field_init];
have_url = true;
has_location = true;
dep.location_tok = main_tokens[field_init];
} else if (mem.eql(u8, field_name, "hash")) {
dep.hash = parseHash(p, field_init) catch |err| switch (err) {
error.ParseFailure => continue,
Expand All @@ -250,8 +270,8 @@ const Parse = struct {
}
}

if (!have_url) {
try appendError(p, main_tokens[node], "dependency is missing 'url' field", .{});
if (!has_location) {
try appendError(p, main_tokens[node], "dependency requires location field, one of 'url' or 'path'.", .{});
}

return dep;
Expand Down
Loading

0 comments on commit 7d279c9

Please sign in to comment.