mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-07 14:24:43 +00:00
std.Uri: make scheme non-optional
This commit is contained in:
parent
646a911c19
commit
aa87789c29
2 changed files with 13 additions and 17 deletions
|
|
@ -5,7 +5,7 @@ const Uri = @This();
|
||||||
const std = @import("std.zig");
|
const std = @import("std.zig");
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
|
|
||||||
scheme: ?[]const u8,
|
scheme: []const u8,
|
||||||
user: ?[]const u8,
|
user: ?[]const u8,
|
||||||
password: ?[]const u8,
|
password: ?[]const u8,
|
||||||
host: ?[]const u8,
|
host: ?[]const u8,
|
||||||
|
|
@ -98,8 +98,9 @@ pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort };
|
||||||
/// The return value will contain unescaped strings pointing into the
|
/// The return value will contain unescaped strings pointing into the
|
||||||
/// original `text`. Each component that is provided, will be non-`null`.
|
/// original `text`. Each component that is provided, will be non-`null`.
|
||||||
pub fn parse(text: []const u8) ParseError!Uri {
|
pub fn parse(text: []const u8) ParseError!Uri {
|
||||||
|
var reader = SliceReader{ .slice = text };
|
||||||
var uri = Uri{
|
var uri = Uri{
|
||||||
.scheme = null,
|
.scheme = reader.readWhile(isSchemeChar),
|
||||||
.user = null,
|
.user = null,
|
||||||
.password = null,
|
.password = null,
|
||||||
.host = null,
|
.host = null,
|
||||||
|
|
@ -109,10 +110,6 @@ pub fn parse(text: []const u8) ParseError!Uri {
|
||||||
.fragment = null,
|
.fragment = null,
|
||||||
};
|
};
|
||||||
|
|
||||||
var reader = SliceReader{ .slice = text };
|
|
||||||
|
|
||||||
uri.scheme = reader.readWhile(isSchemeChar);
|
|
||||||
|
|
||||||
// after the scheme, a ':' must appear
|
// after the scheme, a ':' must appear
|
||||||
if (reader.get()) |c| {
|
if (reader.get()) |c| {
|
||||||
if (c != ':')
|
if (c != ':')
|
||||||
|
|
@ -296,7 +293,7 @@ fn isQuerySeparator(c: u8) bool {
|
||||||
|
|
||||||
test "basic" {
|
test "basic" {
|
||||||
const parsed = try parse("https://ziglang.org/download");
|
const parsed = try parse("https://ziglang.org/download");
|
||||||
try testing.expectEqualStrings("https", parsed.scheme orelse return error.UnexpectedNull);
|
try testing.expectEqualStrings("https", parsed.scheme);
|
||||||
try testing.expectEqualStrings("ziglang.org", parsed.host orelse return error.UnexpectedNull);
|
try testing.expectEqualStrings("ziglang.org", parsed.host orelse return error.UnexpectedNull);
|
||||||
try testing.expectEqualStrings("/download", parsed.path);
|
try testing.expectEqualStrings("/download", parsed.path);
|
||||||
try testing.expectEqual(@as(?u16, null), parsed.port);
|
try testing.expectEqual(@as(?u16, null), parsed.port);
|
||||||
|
|
@ -304,7 +301,7 @@ test "basic" {
|
||||||
|
|
||||||
test "with port" {
|
test "with port" {
|
||||||
const parsed = try parse("http://example:1337/");
|
const parsed = try parse("http://example:1337/");
|
||||||
try testing.expectEqualStrings("http", parsed.scheme orelse return error.UnexpectedNull);
|
try testing.expectEqualStrings("http", parsed.scheme);
|
||||||
try testing.expectEqualStrings("example", parsed.host orelse return error.UnexpectedNull);
|
try testing.expectEqualStrings("example", parsed.host orelse return error.UnexpectedNull);
|
||||||
try testing.expectEqualStrings("/", parsed.path);
|
try testing.expectEqualStrings("/", parsed.path);
|
||||||
try testing.expectEqual(@as(?u16, 1337), parsed.port);
|
try testing.expectEqual(@as(?u16, 1337), parsed.port);
|
||||||
|
|
@ -315,12 +312,12 @@ test "should fail gracefully" {
|
||||||
}
|
}
|
||||||
|
|
||||||
test "scheme" {
|
test "scheme" {
|
||||||
try std.testing.expectEqualSlices(u8, "http", (try parse("http:_")).scheme.?);
|
try std.testing.expectEqualSlices(u8, "http", (try parse("http:_")).scheme);
|
||||||
try std.testing.expectEqualSlices(u8, "scheme-mee", (try parse("scheme-mee:_")).scheme.?);
|
try std.testing.expectEqualSlices(u8, "scheme-mee", (try parse("scheme-mee:_")).scheme);
|
||||||
try std.testing.expectEqualSlices(u8, "a.b.c", (try parse("a.b.c:_")).scheme.?);
|
try std.testing.expectEqualSlices(u8, "a.b.c", (try parse("a.b.c:_")).scheme);
|
||||||
try std.testing.expectEqualSlices(u8, "ab+", (try parse("ab+:_")).scheme.?);
|
try std.testing.expectEqualSlices(u8, "ab+", (try parse("ab+:_")).scheme);
|
||||||
try std.testing.expectEqualSlices(u8, "X+++", (try parse("X+++:_")).scheme.?);
|
try std.testing.expectEqualSlices(u8, "X+++", (try parse("X+++:_")).scheme);
|
||||||
try std.testing.expectEqualSlices(u8, "Y+-.", (try parse("Y+-.:_")).scheme.?);
|
try std.testing.expectEqualSlices(u8, "Y+-.", (try parse("Y+-.:_")).scheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "authority" {
|
test "authority" {
|
||||||
|
|
|
||||||
|
|
@ -735,10 +735,9 @@ pub fn connect(client: *Client, host: []const u8, port: u16, protocol: Connectio
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn request(client: *Client, uri: Uri, headers: Request.Headers, options: Request.Options) !Request {
|
pub fn request(client: *Client, uri: Uri, headers: Request.Headers, options: Request.Options) !Request {
|
||||||
const scheme = uri.scheme orelse return error.UnsupportedUrlScheme;
|
const protocol: Connection.Protocol = if (mem.eql(u8, uri.scheme, "http"))
|
||||||
const protocol: Connection.Protocol = if (mem.eql(u8, scheme, "http"))
|
|
||||||
.plain
|
.plain
|
||||||
else if (mem.eql(u8, scheme, "https"))
|
else if (mem.eql(u8, uri.scheme, "https"))
|
||||||
.tls
|
.tls
|
||||||
else
|
else
|
||||||
return error.UnsupportedUrlScheme;
|
return error.UnsupportedUrlScheme;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue