Function parseAfterScheme [src]
Parses the URI or returns an error. This function is not compliant, but is required to parse
some forms of URIs in the wild, such as HTTP Location headers.
The return value will contain strings pointing into the original text.
Each component that is provided, will be non-null.
Prototype
pub fn parseAfterScheme(scheme: []const u8, text: []const u8) ParseError!Uri
Parameters
scheme: []const u8
text: []const u8
Possible Errors
Source
pub fn parseAfterScheme(scheme: []const u8, text: []const u8) ParseError!Uri {
var reader = SliceReader{ .slice = text };
var uri: Uri = .{ .scheme = scheme, .path = undefined };
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) {
if (reader.peekPrefix("/")) break :a else return error.InvalidFormat;
}
var start_of_host: usize = 0;
if (std.mem.indexOf(u8, authority, "@")) |index| {
start_of_host = index + 1;
const user_info = authority[0..index];
if (std.mem.indexOf(u8, user_info, ":")) |idx| {
uri.user = .{ .percent_encoded = user_info[0..idx] };
if (idx < user_info.len - 1) { // empty password is also "no password"
uri.password = .{ .percent_encoded = user_info[idx + 1 ..] };
}
} else {
uri.user = .{ .percent_encoded = user_info };
uri.password = null;
}
}
// only possible if uri consists of only `userinfo@`
if (start_of_host >= authority.len) break :a;
var end_of_host: usize = authority.len;
// if we see `]` first without `@`
if (authority[start_of_host] == ']') {
return error.InvalidFormat;
}
if (authority.len > start_of_host and authority[start_of_host] == '[') { // IPv6
end_of_host = std.mem.lastIndexOf(u8, authority, "]") orelse return error.InvalidFormat;
end_of_host += 1;
if (std.mem.lastIndexOf(u8, authority, ":")) |index| {
if (index >= end_of_host) { // if not part of the V6 address field
end_of_host = @min(end_of_host, index);
uri.port = std.fmt.parseInt(u16, authority[index + 1 ..], 10) catch return error.InvalidPort;
}
}
} else if (std.mem.lastIndexOf(u8, authority, ":")) |index| {
if (index >= start_of_host) { // if not part of the userinfo field
end_of_host = @min(end_of_host, index);
uri.port = std.fmt.parseInt(u16, authority[index + 1 ..], 10) catch return error.InvalidPort;
}
}
if (start_of_host >= end_of_host) return error.InvalidFormat;
uri.host = .{ .percent_encoded = authority[start_of_host..end_of_host] };
}
uri.path = .{ .percent_encoded = reader.readUntil(isPathSeparator) };
if ((reader.peek() orelse 0) == '?') { // query part
std.debug.assert(reader.get().? == '?');
uri.query = .{ .percent_encoded = reader.readUntil(isQuerySeparator) };
}
if ((reader.peek() orelse 0) == '#') { // fragment part
std.debug.assert(reader.get().? == '#');
uri.fragment = .{ .percent_encoded = reader.readUntilEof() };
}
return uri;
}