Skip to content

Commit aa87789

Browse files
committed
std.Uri: make scheme non-optional
1 parent 646a911 commit aa87789

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

lib/std/Uri.zig

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Uri = @This();
55
const std = @import("std.zig");
66
const testing = std.testing;
77

8-
scheme: ?[]const u8,
8+
scheme: []const u8,
99
user: ?[]const u8,
1010
password: ?[]const u8,
1111
host: ?[]const u8,
@@ -98,8 +98,9 @@ pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort };
9898
/// The return value will contain unescaped strings pointing into the
9999
/// original `text`. Each component that is provided, will be non-`null`.
100100
pub fn parse(text: []const u8) ParseError!Uri {
101+
var reader = SliceReader{ .slice = text };
101102
var uri = Uri{
102-
.scheme = null,
103+
.scheme = reader.readWhile(isSchemeChar),
103104
.user = null,
104105
.password = null,
105106
.host = null,
@@ -109,10 +110,6 @@ pub fn parse(text: []const u8) ParseError!Uri {
109110
.fragment = null,
110111
};
111112

112-
var reader = SliceReader{ .slice = text };
113-
114-
uri.scheme = reader.readWhile(isSchemeChar);
115-
116113
// after the scheme, a ':' must appear
117114
if (reader.get()) |c| {
118115
if (c != ':')
@@ -296,15 +293,15 @@ fn isQuerySeparator(c: u8) bool {
296293

297294
test "basic" {
298295
const parsed = try parse("https://ziglang.org/download");
299-
try testing.expectEqualStrings("https", parsed.scheme orelse return error.UnexpectedNull);
296+
try testing.expectEqualStrings("https", parsed.scheme);
300297
try testing.expectEqualStrings("ziglang.org", parsed.host orelse return error.UnexpectedNull);
301298
try testing.expectEqualStrings("/download", parsed.path);
302299
try testing.expectEqual(@as(?u16, null), parsed.port);
303300
}
304301

305302
test "with port" {
306303
const parsed = try parse("http://example:1337/");
307-
try testing.expectEqualStrings("http", parsed.scheme orelse return error.UnexpectedNull);
304+
try testing.expectEqualStrings("http", parsed.scheme);
308305
try testing.expectEqualStrings("example", parsed.host orelse return error.UnexpectedNull);
309306
try testing.expectEqualStrings("/", parsed.path);
310307
try testing.expectEqual(@as(?u16, 1337), parsed.port);
@@ -315,12 +312,12 @@ test "should fail gracefully" {
315312
}
316313

317314
test "scheme" {
318-
try std.testing.expectEqualSlices(u8, "http", (try parse("http:_")).scheme.?);
319-
try std.testing.expectEqualSlices(u8, "scheme-mee", (try parse("scheme-mee:_")).scheme.?);
320-
try std.testing.expectEqualSlices(u8, "a.b.c", (try parse("a.b.c:_")).scheme.?);
321-
try std.testing.expectEqualSlices(u8, "ab+", (try parse("ab+:_")).scheme.?);
322-
try std.testing.expectEqualSlices(u8, "X+++", (try parse("X+++:_")).scheme.?);
323-
try std.testing.expectEqualSlices(u8, "Y+-.", (try parse("Y+-.:_")).scheme.?);
315+
try std.testing.expectEqualSlices(u8, "http", (try parse("http:_")).scheme);
316+
try std.testing.expectEqualSlices(u8, "scheme-mee", (try parse("scheme-mee:_")).scheme);
317+
try std.testing.expectEqualSlices(u8, "a.b.c", (try parse("a.b.c:_")).scheme);
318+
try std.testing.expectEqualSlices(u8, "ab+", (try parse("ab+:_")).scheme);
319+
try std.testing.expectEqualSlices(u8, "X+++", (try parse("X+++:_")).scheme);
320+
try std.testing.expectEqualSlices(u8, "Y+-.", (try parse("Y+-.:_")).scheme);
324321
}
325322

326323
test "authority" {

lib/std/http/Client.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,10 +735,9 @@ pub fn connect(client: *Client, host: []const u8, port: u16, protocol: Connectio
735735
}
736736

737737
pub fn request(client: *Client, uri: Uri, headers: Request.Headers, options: Request.Options) !Request {
738-
const scheme = uri.scheme orelse return error.UnsupportedUrlScheme;
739-
const protocol: Connection.Protocol = if (mem.eql(u8, scheme, "http"))
738+
const protocol: Connection.Protocol = if (mem.eql(u8, uri.scheme, "http"))
740739
.plain
741-
else if (mem.eql(u8, scheme, "https"))
740+
else if (mem.eql(u8, uri.scheme, "https"))
742741
.tls
743742
else
744743
return error.UnsupportedUrlScheme;

0 commit comments

Comments
 (0)