Skip to content

Commit 6e37e2d

Browse files
committed
Support file:/// URIs and relative paths
1 parent 2adb932 commit 6e37e2d

File tree

6 files changed

+498
-208
lines changed

6 files changed

+498
-208
lines changed

build.zig

+2
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ pub fn build(b: *std.Build) !void {
336336
artifact.linkSystemLibrary("version");
337337
artifact.linkSystemLibrary("uuid");
338338
artifact.linkSystemLibrary("ole32");
339+
artifact.linkSystemLibrary("shlwapi");
339340
}
340341
}
341342
}
@@ -712,6 +713,7 @@ fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {
712713
exe.linkSystemLibrary("version");
713714
exe.linkSystemLibrary("uuid");
714715
exe.linkSystemLibrary("ole32");
716+
exe.linkSystemLibrary("shlwapi");
715717
}
716718
}
717719

lib/std/Uri.zig

+40-5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort };
134134
/// original `text`. Each component that is provided, will be non-`null`.
135135
pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
136136
var reader = SliceReader{ .slice = text };
137+
137138
var uri = Uri{
138139
.scheme = "",
139140
.user = null,
@@ -145,13 +146,14 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
145146
.fragment = null,
146147
};
147148

148-
if (reader.peekPrefix("//")) { // authority part
149+
if (reader.peekPrefix("//")) a: { // authority part
149150
std.debug.assert(reader.get().? == '/');
150151
std.debug.assert(reader.get().? == '/');
151152

152-
const authority = reader.readUntil(isAuthoritySeparator);
153-
if (authority.len == 0)
154-
return error.InvalidFormat;
153+
var authority = reader.readUntil(isAuthoritySeparator);
154+
if (authority.len == 0) {
155+
if (reader.peekPrefix("/")) break :a else return error.InvalidFormat;
156+
}
155157

156158
var start_of_host: usize = 0;
157159
if (std.mem.indexOf(u8, authority, "@")) |index| {
@@ -223,7 +225,6 @@ pub fn format(
223225
try writer.writeAll(":");
224226
if (uri.host) |host| {
225227
try writer.writeAll("//");
226-
227228
if (uri.user) |user| {
228229
try writer.writeAll(user);
229230
if (uri.password) |password| {
@@ -473,6 +474,23 @@ test "should fail gracefully" {
473474
try std.testing.expectEqual(@as(ParseError!Uri, error.InvalidFormat), parse("foobar://"));
474475
}
475476

477+
test "file" {
478+
const parsed = try parse("file:///");
479+
try std.testing.expectEqualSlices(u8, "file", parsed.scheme);
480+
try std.testing.expectEqual(@as(?[]const u8, null), parsed.host);
481+
try std.testing.expectEqualSlices(u8, "/", parsed.path);
482+
483+
const parsed2 = try parse("file:///an/absolute/path/to/something");
484+
try std.testing.expectEqualSlices(u8, "file", parsed2.scheme);
485+
try std.testing.expectEqual(@as(?[]const u8, null), parsed2.host);
486+
try std.testing.expectEqualSlices(u8, "/an/absolute/path/to/something", parsed2.path);
487+
488+
const parsed3 = try parse("file://localhost/an/absolute/path/to/another/thing/");
489+
try std.testing.expectEqualSlices(u8, "file", parsed3.scheme);
490+
try std.testing.expectEqualSlices(u8, "localhost", parsed3.host.?);
491+
try std.testing.expectEqualSlices(u8, "/an/absolute/path/to/another/thing/", parsed3.path);
492+
}
493+
476494
test "scheme" {
477495
try std.testing.expectEqualSlices(u8, "http", (try parse("http:_")).scheme);
478496
try std.testing.expectEqualSlices(u8, "scheme-mee", (try parse("scheme-mee:_")).scheme);
@@ -682,3 +700,20 @@ test "URI query escaping" {
682700
defer std.testing.allocator.free(formatted_uri);
683701
try std.testing.expectEqualStrings("/?response-content-type=application%2Foctet-stream", formatted_uri);
684702
}
703+
704+
test "format" {
705+
const uri = Uri{
706+
.scheme = "file",
707+
.user = null,
708+
.password = null,
709+
.host = null,
710+
.port = null,
711+
.path = "/foo/bar/baz",
712+
.query = null,
713+
.fragment = null,
714+
};
715+
var buf = std.ArrayList(u8).init(std.testing.allocator);
716+
defer buf.deinit();
717+
try uri.format("+/", .{}, buf.writer());
718+
try std.testing.expectEqualSlices(u8, "file:/foo/bar/baz", buf.items);
719+
}

lib/std/os/windows.zig

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub const gdi32 = @import("windows/gdi32.zig");
3030
pub const winmm = @import("windows/winmm.zig");
3131
pub const crypt32 = @import("windows/crypt32.zig");
3232
pub const nls = @import("windows/nls.zig");
33+
pub const shlwapi = @import("windows/shlwapi.zig");
3334

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

lib/std/os/windows/shlwapi.zig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const std = @import("../../std.zig");
2+
const windows = std.os.windows;
3+
4+
const DWORD = windows.DWORD;
5+
const WINAPI = windows.WINAPI;
6+
const HRESULT = windows.HRESULT;
7+
const LPCSTR = windows.LPCSTR;
8+
const LPSTR = windows.LPSTR;
9+
const LPWSTR = windows.LPWSTR;
10+
const LPCWSTR = windows.LPCWSTR;
11+
12+
pub extern "shlwapi" fn PathCreateFromUrlW(pszUrl: LPCWSTR, pszPath: LPWSTR, pcchPath: *DWORD, dwFlags: DWORD) callconv(WINAPI) HRESULT;
13+
pub extern "shlwapi" fn PathCreateFromUrlA(pszUrl: LPCSTR, pszPath: LPSTR, pcchPath: *DWORD, dwFlags: DWORD) callconv(WINAPI) HRESULT;

src/Manifest.zig

+32-12
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ pub const basename = "build.zig.zon";
22
pub const Hash = std.crypto.hash.sha2.Sha256;
33

44
pub const Dependency = struct {
5-
url: []const u8,
6-
url_tok: Ast.TokenIndex,
5+
location: union(enum) {
6+
url: []const u8,
7+
path: []const u8,
8+
},
9+
location_tok: Ast.TokenIndex,
710
hash: ?[]const u8,
811
hash_tok: Ast.TokenIndex,
912
};
@@ -218,12 +221,12 @@ const Parse = struct {
218221
};
219222

220223
var dep: Dependency = .{
221-
.url = undefined,
222-
.url_tok = undefined,
224+
.location = undefined,
225+
.location_tok = undefined,
223226
.hash = null,
224227
.hash_tok = undefined,
225228
};
226-
var have_url = false;
229+
var has_location = false;
227230

228231
for (struct_init.ast.fields) |field_init| {
229232
const name_token = ast.firstToken(field_init) - 2;
@@ -232,12 +235,29 @@ const Parse = struct {
232235
// things manually provides an opportunity to do any additional verification
233236
// that is desirable on a per-field basis.
234237
if (mem.eql(u8, field_name, "url")) {
235-
dep.url = parseString(p, field_init) catch |err| switch (err) {
236-
error.ParseFailure => continue,
237-
else => |e| return e,
238+
if (has_location) {
239+
return fail(p, main_tokens[field_init], "dependency should specify only one of 'url' and 'path' fields.", .{});
240+
}
241+
dep.location = .{
242+
.url = parseString(p, field_init) catch |err| switch (err) {
243+
error.ParseFailure => continue,
244+
else => |e| return e,
245+
},
246+
};
247+
has_location = true;
248+
dep.location_tok = main_tokens[field_init];
249+
} else if (mem.eql(u8, field_name, "path")) {
250+
if (has_location) {
251+
return fail(p, main_tokens[field_init], "dependency should specify only one of 'url' and 'path' fields.", .{});
252+
}
253+
dep.location = .{
254+
.path = parseString(p, field_init) catch |err| switch (err) {
255+
error.ParseFailure => continue,
256+
else => |e| return e,
257+
},
238258
};
239-
dep.url_tok = main_tokens[field_init];
240-
have_url = true;
259+
has_location = true;
260+
dep.location_tok = main_tokens[field_init];
241261
} else if (mem.eql(u8, field_name, "hash")) {
242262
dep.hash = parseHash(p, field_init) catch |err| switch (err) {
243263
error.ParseFailure => continue,
@@ -250,8 +270,8 @@ const Parse = struct {
250270
}
251271
}
252272

253-
if (!have_url) {
254-
try appendError(p, main_tokens[node], "dependency is missing 'url' field", .{});
273+
if (!has_location) {
274+
try appendError(p, main_tokens[node], "dependency requires location field, one of 'url' or 'path'.", .{});
255275
}
256276

257277
return dep;

0 commit comments

Comments
 (0)