| 1 | //! Uniform Resource Identifier (URI) parsing roughly adhering to |
| 2 | //! <https://tools.ietf.org/html/rfc3986>. Does not do perfect grammar and |
| 3 | //! character class checking, but should be robust against URIs in the wild. |
| 4 | |
| 5 | const std = @import("std.zig"); |
| 6 | const testing = std.testing; |
| 7 | const Uri = @This(); |
| 8 | const Allocator = std.mem.Allocator; |
| 9 | const Writer = std.Io.Writer; |
| 10 | |
| 11 | scheme: []const u8, |
| 12 | user: ?Component = null, |
| 13 | password: ?Component = null, |
| 14 | host: ?Component = null, |
| 15 | port: ?u16 = null, |
| 16 | path: Component = Component.empty, |
| 17 | query: ?Component = null, |
| 18 | fragment: ?Component = null, |
| 19 | |
| 20 | pub const getHost = @compileError("This function has been moved to std.Io.net.HostName.fromUri"); |
| 21 | pub const getHostAlloc = @compileError("This function has been deleted. See std.Io.net.HostName.fromUri instead"); |
| 22 | |
| 23 | pub const Component = union(enum) { |
| 24 | /// Invalid characters in this component must be percent encoded |
| 25 | /// before being printed as part of a URI. |
| 26 | raw: []const u8, |
| 27 | /// This component is already percent-encoded, it can be printed |
| 28 | /// directly as part of a URI. |
| 29 | percent_encoded: []const u8, |
| 30 | |
| 31 | pub const empty: Component = .{ .percent_encoded = "" }; |
| 32 | |
| 33 | pub fn isEmpty(component: Component) bool { |
| 34 | return switch (component) { |
| 35 | .raw, .percent_encoded => |string| string.len == 0, |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | /// Returned value may point into `buffer` or be the original string. |
| 40 | pub fn toRaw(component: Component, buffer: []u8) error{NoSpaceLeft}![]const u8 { |
| 41 | return switch (component) { |
| 42 | .raw => |raw| raw, |
| 43 | .percent_encoded => |percent_encoded| if (std.mem.findScalar(u8, percent_encoded, '%')) |_| |
| 44 | try std.mem.print(buffer, "{f}", .{std.fmt.alt(component, .formatRaw)}) |
| 45 | else |
| 46 | percent_encoded, |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | /// Allocates the result with `arena` only if needed, so the result should not be freed. |
| 51 | pub fn toRawMaybeAlloc(component: Component, arena: Allocator) Allocator.Error![]const u8 { |
| 52 | return switch (component) { |
| 53 | .raw => |raw| raw, |
| 54 | .percent_encoded => |percent_encoded| if (std.mem.findScalar(u8, percent_encoded, '%')) |_| |
| 55 | try arena.print("{f}", .{std.fmt.alt(component, .formatRaw)}) |
| 56 | else |
| 57 | percent_encoded, |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | pub fn formatRaw(component: Component, w: *Writer) Writer.Error!void { |
| 62 | switch (component) { |
| 63 | .raw => |raw| try w.writeAll(raw), |
| 64 | .percent_encoded => |percent_encoded| { |
| 65 | var start: usize = 0; |
| 66 | var index: usize = 0; |
| 67 | while (std.mem.findScalarPos(u8, percent_encoded, index, '%')) |percent| { |
| 68 | index = percent + 1; |
| 69 | if (percent_encoded.len - index < 2) continue; |
| 70 | const percent_encoded_char = |
| 71 | std.fmt.parseInt(u8, percent_encoded[index..][0..2], 16) catch continue; |
| 72 | try w.print("{s}{c}", .{ |
| 73 | percent_encoded[start..percent], |
| 74 | percent_encoded_char, |
| 75 | }); |
| 76 | start = percent + 3; |
| 77 | index = percent + 3; |
| 78 | } |
| 79 | try w.writeAll(percent_encoded[start..]); |
| 80 | }, |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pub fn formatEscaped(component: Component, w: *Writer) Writer.Error!void { |
| 85 | switch (component) { |
| 86 | .raw => |raw| try percentEncode(w, raw, isUnreserved), |
| 87 | .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded), |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | pub fn formatUser(component: Component, w: *Writer) Writer.Error!void { |
| 92 | switch (component) { |
| 93 | .raw => |raw| try percentEncode(w, raw, isUserChar), |
| 94 | .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub fn formatPassword(component: Component, w: *Writer) Writer.Error!void { |
| 99 | switch (component) { |
| 100 | .raw => |raw| try percentEncode(w, raw, isPasswordChar), |
| 101 | .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded), |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | pub fn formatHost(component: Component, w: *Writer) Writer.Error!void { |
| 106 | switch (component) { |
| 107 | .raw => |raw| try percentEncode(w, raw, isHostChar), |
| 108 | .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded), |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | pub fn formatPath(component: Component, w: *Writer) Writer.Error!void { |
| 113 | switch (component) { |
| 114 | .raw => |raw| try percentEncode(w, raw, isPathChar), |
| 115 | .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded), |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | pub fn formatQuery(component: Component, w: *Writer) Writer.Error!void { |
| 120 | switch (component) { |
| 121 | .raw => |raw| try percentEncode(w, raw, isQueryChar), |
| 122 | .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded), |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | pub fn formatFragment(component: Component, w: *Writer) Writer.Error!void { |
| 127 | switch (component) { |
| 128 | .raw => |raw| try percentEncode(w, raw, isFragmentChar), |
| 129 | .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded), |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | pub fn percentEncode(w: *Writer, raw: []const u8, comptime isValidChar: fn (u8) bool) Writer.Error!void { |
| 134 | var start: usize = 0; |
| 135 | for (raw, 0..) |char, index| { |
| 136 | if (isValidChar(char)) continue; |
| 137 | try w.print("{s}%{X:0>2}", .{ raw[start..index], char }); |
| 138 | start = index + 1; |
| 139 | } |
| 140 | try w.writeAll(raw[start..]); |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | /// Percent decodes all %XX where XX is a valid hex number. |
| 145 | /// `output` may alias `input` if `output.ptr <= input.ptr`. |
| 146 | /// Mutates and returns a subslice of `output`. |
| 147 | pub fn percentDecodeBackwards(output: []u8, input: []const u8) []u8 { |
| 148 | var input_index = input.len; |
| 149 | var output_index = output.len; |
| 150 | while (input_index > 0) { |
| 151 | if (input_index >= 3) { |
| 152 | const maybe_percent_encoded = input[input_index - 3 ..][0..3]; |
| 153 | if (maybe_percent_encoded[0] == '%') { |
| 154 | if (std.fmt.parseInt(u8, maybe_percent_encoded[1..], 16)) |percent_encoded_char| { |
| 155 | input_index -= maybe_percent_encoded.len; |
| 156 | output_index -= 1; |
| 157 | output[output_index] = percent_encoded_char; |
| 158 | continue; |
| 159 | } else |_| {} |
| 160 | } |
| 161 | } |
| 162 | input_index -= 1; |
| 163 | output_index -= 1; |
| 164 | output[output_index] = input[input_index]; |
| 165 | } |
| 166 | return output[output_index..]; |
| 167 | } |
| 168 | |
| 169 | /// Percent decodes all %XX where XX is a valid hex number. |
| 170 | /// Mutates and returns a subslice of `buffer`. |
| 171 | pub fn percentDecodeInPlace(buffer: []u8) []u8 { |
| 172 | return percentDecodeBackwards(buffer, buffer); |
| 173 | } |
| 174 | |
| 175 | pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort, InvalidHostName }; |
| 176 | |
| 177 | /// Parses the URI or returns an error. This function is not compliant, but is required to parse |
| 178 | /// some forms of URIs in the wild, such as HTTP Location headers. |
| 179 | /// The return value will contain strings pointing into the original `text`. |
| 180 | /// Each component that is provided, will be non-`null`. |
| 181 | pub fn parseAfterScheme(scheme: []const u8, text: []const u8) ParseError!Uri { |
| 182 | var uri: Uri = .{ .scheme = scheme, .path = undefined }; |
| 183 | var i: usize = 0; |
| 184 | |
| 185 | if (std.mem.startsWith(u8, text, "//")) a: { |
| 186 | i = std.mem.findAnyPos(u8, text, 2, &authority_sep) orelse text.len; |
| 187 | const authority = text[2..i]; |
| 188 | if (authority.len == 0) { |
| 189 | if (!std.mem.startsWith(u8, text[2..], "/")) return error.InvalidFormat; |
| 190 | break :a; |
| 191 | } |
| 192 | |
| 193 | var start_of_host: usize = 0; |
| 194 | // Use findLast to handle unencoded @ in userinfo gracefully, |
| 195 | // e.g. scheme://user:p@ssword@hostname. This deviates from RFC3986 |
| 196 | // (which requires @ to be percent-encoded as %40 in userinfo) but |
| 197 | // is more robust against URIs in the wild. |
| 198 | if (std.mem.findLast(u8, authority, "@")) |index| { |
| 199 | start_of_host = index + 1; |
| 200 | const user_info = authority[0..index]; |
| 201 | |
| 202 | if (std.mem.find(u8, user_info, ":")) |idx| { |
| 203 | uri.user = .{ .percent_encoded = user_info[0..idx] }; |
| 204 | if (idx < user_info.len - 1) { // empty password is also "no password" |
| 205 | uri.password = .{ .percent_encoded = user_info[idx + 1 ..] }; |
| 206 | } |
| 207 | } else { |
| 208 | uri.user = .{ .percent_encoded = user_info }; |
| 209 | uri.password = null; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // only possible if uri consists of only `userinfo@` |
| 214 | if (start_of_host >= authority.len) break :a; |
| 215 | |
| 216 | var end_of_host: usize = authority.len; |
| 217 | |
| 218 | // if we see `]` first without `@` |
| 219 | if (authority[start_of_host] == ']') { |
| 220 | return error.InvalidFormat; |
| 221 | } |
| 222 | |
| 223 | if (authority.len > start_of_host and authority[start_of_host] == '[') { // IPv6 |
| 224 | end_of_host = std.mem.findLast(u8, authority, "]") orelse return error.InvalidFormat; |
| 225 | end_of_host += 1; |
| 226 | |
| 227 | if (std.mem.findLast(u8, authority, ":")) |index| { |
| 228 | if (index >= end_of_host) { // if not part of the V6 address field |
| 229 | end_of_host = @min(end_of_host, index); |
| 230 | uri.port = std.fmt.parseInt(u16, authority[index + 1 ..], 10) catch return error.InvalidPort; |
| 231 | } |
| 232 | } |
| 233 | } else if (std.mem.findLast(u8, authority, ":")) |index| { |
| 234 | if (index >= start_of_host) { // if not part of the userinfo field |
| 235 | end_of_host = @min(end_of_host, index); |
| 236 | uri.port = std.fmt.parseInt(u16, authority[index + 1 ..], 10) catch return error.InvalidPort; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (start_of_host >= end_of_host) return error.InvalidFormat; |
| 241 | const host = authority[start_of_host..end_of_host]; |
| 242 | if (host.len > std.Io.net.HostName.max_len) return error.InvalidHostName; |
| 243 | uri.host = .{ .percent_encoded = host }; |
| 244 | } |
| 245 | |
| 246 | const path_start = i; |
| 247 | i = std.mem.findAnyPos(u8, text, path_start, &path_sep) orelse text.len; |
| 248 | uri.path = .{ .percent_encoded = text[path_start..i] }; |
| 249 | |
| 250 | if (std.mem.startsWith(u8, text[i..], "?")) { |
| 251 | const query_start = i + 1; |
| 252 | i = std.mem.findScalarPos(u8, text, query_start, '#') orelse text.len; |
| 253 | uri.query = .{ .percent_encoded = text[query_start..i] }; |
| 254 | } |
| 255 | |
| 256 | if (std.mem.startsWith(u8, text[i..], "#")) { |
| 257 | uri.fragment = .{ .percent_encoded = text[i + 1 ..] }; |
| 258 | } |
| 259 | |
| 260 | return uri; |
| 261 | } |
| 262 | |
| 263 | pub fn format(uri: *const Uri, writer: *Writer) Writer.Error!void { |
| 264 | return writeToStream(uri, writer, .all); |
| 265 | } |
| 266 | |
| 267 | pub fn writeToStream(uri: *const Uri, writer: *Writer, flags: Format.Flags) Writer.Error!void { |
| 268 | if (flags.scheme) { |
| 269 | try writer.print("{s}:", .{uri.scheme}); |
| 270 | if (flags.authority and uri.host != null) { |
| 271 | try writer.writeAll("//"); |
| 272 | } |
| 273 | } |
| 274 | if (flags.authority) { |
| 275 | if (flags.authentication and uri.host != null) { |
| 276 | if (uri.user) |user| { |
| 277 | try user.formatUser(writer); |
| 278 | if (uri.password) |password| { |
| 279 | try writer.writeByte(':'); |
| 280 | try password.formatPassword(writer); |
| 281 | } |
| 282 | try writer.writeByte('@'); |
| 283 | } |
| 284 | } |
| 285 | if (uri.host) |host| { |
| 286 | try host.formatHost(writer); |
| 287 | if (flags.port) { |
| 288 | if (uri.port) |port| try writer.print(":{d}", .{port}); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | if (flags.path) { |
| 293 | const uri_path: Component = if (uri.path.isEmpty()) .{ .percent_encoded = "/" } else uri.path; |
| 294 | try uri_path.formatPath(writer); |
| 295 | if (flags.query) { |
| 296 | if (uri.query) |query| { |
| 297 | try writer.writeByte('?'); |
| 298 | try query.formatQuery(writer); |
| 299 | } |
| 300 | } |
| 301 | if (flags.fragment) { |
| 302 | if (uri.fragment) |fragment| { |
| 303 | try writer.writeByte('#'); |
| 304 | try fragment.formatFragment(writer); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | pub const Format = struct { |
| 311 | uri: *const Uri, |
| 312 | flags: Flags = .{}, |
| 313 | |
| 314 | pub const Flags = struct { |
| 315 | /// When true, include the scheme part of the URI. |
| 316 | scheme: bool = false, |
| 317 | /// When true, include the user and password part of the URI. Ignored if `authority` is false. |
| 318 | authentication: bool = false, |
| 319 | /// When true, include the authority part of the URI. |
| 320 | authority: bool = false, |
| 321 | /// When true, include the path part of the URI. |
| 322 | path: bool = false, |
| 323 | /// When true, include the query part of the URI. Ignored when `path` is false. |
| 324 | query: bool = false, |
| 325 | /// When true, include the fragment part of the URI. Ignored when `path` is false. |
| 326 | fragment: bool = false, |
| 327 | /// When true, include the port part of the URI. Ignored when `port` is null. |
| 328 | port: bool = true, |
| 329 | |
| 330 | pub const all: Flags = .{ |
| 331 | .scheme = true, |
| 332 | .authentication = true, |
| 333 | .authority = true, |
| 334 | .path = true, |
| 335 | .query = true, |
| 336 | .fragment = true, |
| 337 | .port = true, |
| 338 | }; |
| 339 | }; |
| 340 | |
| 341 | pub fn default(f: Format, writer: *Writer) Writer.Error!void { |
| 342 | return writeToStream(f.uri, writer, f.flags); |
| 343 | } |
| 344 | }; |
| 345 | |
| 346 | pub fn fmt(uri: *const Uri, flags: Format.Flags) std.fmt.Alt(Format, Format.default) { |
| 347 | return .{ .data = .{ .uri = uri, .flags = flags } }; |
| 348 | } |
| 349 | |
| 350 | /// The return value will contain strings pointing into the original `text`. |
| 351 | /// Each component that is provided will be non-`null`. |
| 352 | pub fn parse(text: []const u8) ParseError!Uri { |
| 353 | const scheme, const rest = std.mem.cutScalar(u8, text, ':') orelse |
| 354 | return error.InvalidFormat; |
| 355 | if (!isValidScheme(scheme)) |
| 356 | return error.UnexpectedCharacter; |
| 357 | return parseAfterScheme(scheme, rest); |
| 358 | } |
| 359 | |
| 360 | pub const ResolveInPlaceError = ParseError || error{NoSpaceLeft}; |
| 361 | |
| 362 | /// Resolves a URI against a base URI, conforming to |
| 363 | /// [RFC 3986, Section 5](https://www.rfc-editor.org/rfc/rfc3986#section-5) |
| 364 | /// |
| 365 | /// Assumes new location is already copied to the beginning of `aux_buf.*`. |
| 366 | /// Parses that new location as a URI, and then resolves the path in place. |
| 367 | /// |
| 368 | /// If a merge needs to take place, the newly constructed path will be stored |
| 369 | /// in `aux_buf.*` just after the copied location, and `aux_buf.*` will be |
| 370 | /// modified to only contain the remaining unused space. |
| 371 | pub fn resolveInPlace(base: Uri, new_len: usize, aux_buf: *[]u8) ResolveInPlaceError!Uri { |
| 372 | const new = aux_buf.*[0..new_len]; |
| 373 | const new_parsed = parse(new) catch |err| (parseAfterScheme("", new) catch return err); |
| 374 | aux_buf.* = aux_buf.*[new_len..]; |
| 375 | // As you can see above, `new` is not a const pointer. |
| 376 | const new_path: []u8 = @constCast(new_parsed.path.percent_encoded); |
| 377 | |
| 378 | if (new_parsed.scheme.len > 0) return .{ |
| 379 | .scheme = new_parsed.scheme, |
| 380 | .user = new_parsed.user, |
| 381 | .password = new_parsed.password, |
| 382 | .host = new_parsed.host, |
| 383 | .port = new_parsed.port, |
| 384 | .path = remove_dot_segments(new_path), |
| 385 | .query = new_parsed.query, |
| 386 | .fragment = new_parsed.fragment, |
| 387 | }; |
| 388 | |
| 389 | if (new_parsed.host) |host| return .{ |
| 390 | .scheme = base.scheme, |
| 391 | .user = new_parsed.user, |
| 392 | .password = new_parsed.password, |
| 393 | .host = host, |
| 394 | .port = new_parsed.port, |
| 395 | .path = remove_dot_segments(new_path), |
| 396 | .query = new_parsed.query, |
| 397 | .fragment = new_parsed.fragment, |
| 398 | }; |
| 399 | |
| 400 | const path, const query = if (new_path.len == 0) .{ |
| 401 | base.path, |
| 402 | new_parsed.query orelse base.query, |
| 403 | } else if (new_path[0] == '/') .{ |
| 404 | remove_dot_segments(new_path), |
| 405 | new_parsed.query, |
| 406 | } else .{ |
| 407 | try merge_paths(base.path, new_path, aux_buf), |
| 408 | new_parsed.query, |
| 409 | }; |
| 410 | |
| 411 | return .{ |
| 412 | .scheme = base.scheme, |
| 413 | .user = base.user, |
| 414 | .password = base.password, |
| 415 | .host = base.host, |
| 416 | .port = base.port, |
| 417 | .path = path, |
| 418 | .query = query, |
| 419 | .fragment = new_parsed.fragment, |
| 420 | }; |
| 421 | } |
| 422 | |
| 423 | /// In-place implementation of RFC 3986, Section 5.2.4. |
| 424 | fn remove_dot_segments(path: []u8) Component { |
| 425 | var in_i: usize = 0; |
| 426 | var out_i: usize = 0; |
| 427 | while (in_i < path.len) { |
| 428 | if (std.mem.startsWith(u8, path[in_i..], "./")) { |
| 429 | in_i += 2; |
| 430 | } else if (std.mem.startsWith(u8, path[in_i..], "../")) { |
| 431 | in_i += 3; |
| 432 | } else if (std.mem.startsWith(u8, path[in_i..], "/./")) { |
| 433 | in_i += 2; |
| 434 | } else if (std.mem.eql(u8, path[in_i..], "/.")) { |
| 435 | in_i += 1; |
| 436 | path[in_i] = '/'; |
| 437 | } else if (std.mem.startsWith(u8, path[in_i..], "/../")) { |
| 438 | in_i += 3; |
| 439 | while (out_i > 0) { |
| 440 | out_i -= 1; |
| 441 | if (path[out_i] == '/') break; |
| 442 | } |
| 443 | } else if (std.mem.eql(u8, path[in_i..], "/..")) { |
| 444 | in_i += 2; |
| 445 | path[in_i] = '/'; |
| 446 | while (out_i > 0) { |
| 447 | out_i -= 1; |
| 448 | if (path[out_i] == '/') break; |
| 449 | } |
| 450 | } else if (std.mem.eql(u8, path[in_i..], ".")) { |
| 451 | in_i += 1; |
| 452 | } else if (std.mem.eql(u8, path[in_i..], "..")) { |
| 453 | in_i += 2; |
| 454 | } else { |
| 455 | while (true) { |
| 456 | path[out_i] = path[in_i]; |
| 457 | out_i += 1; |
| 458 | in_i += 1; |
| 459 | if (in_i >= path.len or path[in_i] == '/') break; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | return .{ .percent_encoded = path[0..out_i] }; |
| 464 | } |
| 465 | |
| 466 | test remove_dot_segments { |
| 467 | { |
| 468 | var buffer = "/a/b/c/./../../g".*; |
| 469 | try std.testing.expectEqualStrings("/a/g", remove_dot_segments(&buffer).percent_encoded); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /// 5.2.3. Merge Paths |
| 474 | fn merge_paths(base: Component, new: []u8, aux_buf: *[]u8) error{NoSpaceLeft}!Component { |
| 475 | var aux: Writer = .fixed(aux_buf.*); |
| 476 | if (!base.isEmpty()) { |
| 477 | base.formatPath(&aux) catch return error.NoSpaceLeft; |
| 478 | aux.end = std.mem.findScalarLast(u8, aux.buffered(), '/') orelse return remove_dot_segments(new); |
| 479 | } |
| 480 | aux.print("/{s}", .{new}) catch return error.NoSpaceLeft; |
| 481 | const merged_path = remove_dot_segments(aux.buffered()); |
| 482 | aux_buf.* = aux_buf.*[merged_path.percent_encoded.len..]; |
| 483 | return merged_path; |
| 484 | } |
| 485 | |
| 486 | /// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) |
| 487 | fn isValidScheme(scheme: []const u8) bool { |
| 488 | if (scheme.len == 0) return false; |
| 489 | if (!std.ascii.isAlphabetic(scheme[0])) return false; |
| 490 | for (scheme[1..]) |byte| { |
| 491 | switch (byte) { |
| 492 | 'A'...'Z', 'a'...'z', '0'...'9', '+', '-', '.' => continue, |
| 493 | else => return false, |
| 494 | } |
| 495 | } |
| 496 | return true; |
| 497 | } |
| 498 | |
| 499 | /// sub-delims = "!" / "$" / "&" / "'" / "(" / ")" |
| 500 | /// / "*" / "+" / "," / ";" / "=" |
| 501 | fn isSubLimit(c: u8) bool { |
| 502 | return switch (c) { |
| 503 | '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=' => true, |
| 504 | else => false, |
| 505 | }; |
| 506 | } |
| 507 | |
| 508 | /// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
| 509 | fn isUnreserved(c: u8) bool { |
| 510 | return switch (c) { |
| 511 | 'A'...'Z', 'a'...'z', '0'...'9', '-', '.', '_', '~' => true, |
| 512 | else => false, |
| 513 | }; |
| 514 | } |
| 515 | |
| 516 | fn isUserChar(c: u8) bool { |
| 517 | return isUnreserved(c) or isSubLimit(c); |
| 518 | } |
| 519 | |
| 520 | fn isPasswordChar(c: u8) bool { |
| 521 | return isUserChar(c) or c == ':'; |
| 522 | } |
| 523 | |
| 524 | fn isHostChar(c: u8) bool { |
| 525 | return isPasswordChar(c) or c == '[' or c == ']'; |
| 526 | } |
| 527 | |
| 528 | fn isPathChar(c: u8) bool { |
| 529 | return isUserChar(c) or c == '/' or c == ':' or c == '@'; |
| 530 | } |
| 531 | |
| 532 | fn isQueryChar(c: u8) bool { |
| 533 | return isPathChar(c) or c == '?'; |
| 534 | } |
| 535 | |
| 536 | const isFragmentChar = isQueryChar; |
| 537 | |
| 538 | const authority_sep: [3]u8 = .{ '/', '?', '#' }; |
| 539 | const path_sep: [2]u8 = .{ '?', '#' }; |
| 540 | |
| 541 | test "basic" { |
| 542 | const parsed = try parse("https://ziglang.org/download"); |
| 543 | try testing.expectEqualStrings("https", parsed.scheme); |
| 544 | try testing.expectEqualStrings("ziglang.org", parsed.host.?.percent_encoded); |
| 545 | try testing.expectEqualStrings("/download", parsed.path.percent_encoded); |
| 546 | try testing.expectEqual(@as(?u16, null), parsed.port); |
| 547 | } |
| 548 | |
| 549 | test "with port" { |
| 550 | const parsed = try parse("http://example:1337/"); |
| 551 | try testing.expectEqualStrings("http", parsed.scheme); |
| 552 | try testing.expectEqualStrings("example", parsed.host.?.percent_encoded); |
| 553 | try testing.expectEqualStrings("/", parsed.path.percent_encoded); |
| 554 | try testing.expectEqual(@as(?u16, 1337), parsed.port); |
| 555 | } |
| 556 | |
| 557 | test "should fail gracefully" { |
| 558 | try std.testing.expectError(error.InvalidFormat, parse("foobar://")); |
| 559 | } |
| 560 | |
| 561 | test "parse name too long" { |
| 562 | const uri = "http://" ++ @as([std.Io.net.HostName.max_len + 1]u8, @splat('Z')); |
| 563 | try std.testing.expectError(error.InvalidHostName, parse(uri)); |
| 564 | } |
| 565 | |
| 566 | test "file" { |
| 567 | const parsed = try parse("file:///"); |
| 568 | try std.testing.expectEqualStrings("file", parsed.scheme); |
| 569 | try std.testing.expectEqual(@as(?Component, null), parsed.host); |
| 570 | try std.testing.expectEqualStrings("/", parsed.path.percent_encoded); |
| 571 | |
| 572 | const parsed2 = try parse("file:///an/absolute/path/to/something"); |
| 573 | try std.testing.expectEqualStrings("file", parsed2.scheme); |
| 574 | try std.testing.expectEqual(@as(?Component, null), parsed2.host); |
| 575 | try std.testing.expectEqualStrings("/an/absolute/path/to/something", parsed2.path.percent_encoded); |
| 576 | |
| 577 | const parsed3 = try parse("file://localhost/an/absolute/path/to/another/thing/"); |
| 578 | try std.testing.expectEqualStrings("file", parsed3.scheme); |
| 579 | try std.testing.expectEqualStrings("localhost", parsed3.host.?.percent_encoded); |
| 580 | try std.testing.expectEqualStrings("/an/absolute/path/to/another/thing/", parsed3.path.percent_encoded); |
| 581 | |
| 582 | const parsed4 = try parse("file:/an/absolute/path"); |
| 583 | try std.testing.expectEqualStrings("file", parsed4.scheme); |
| 584 | try std.testing.expectEqual(@as(?Component, null), parsed4.host); |
| 585 | try std.testing.expectEqualStrings("/an/absolute/path", parsed4.path.percent_encoded); |
| 586 | } |
| 587 | |
| 588 | test "scheme" { |
| 589 | try std.testing.expectEqualStrings("http", (try parse("http:_")).scheme); |
| 590 | try std.testing.expectEqualStrings("scheme-mee", (try parse("scheme-mee:_")).scheme); |
| 591 | try std.testing.expectEqualStrings("a.b.c", (try parse("a.b.c:_")).scheme); |
| 592 | try std.testing.expectEqualStrings("ab+", (try parse("ab+:_")).scheme); |
| 593 | try std.testing.expectEqualStrings("X+++", (try parse("X+++:_")).scheme); |
| 594 | try std.testing.expectEqualStrings("Y+-.", (try parse("Y+-.:_")).scheme); |
| 595 | |
| 596 | try std.testing.expectError(error.InvalidFormat, parse("")); |
| 597 | try std.testing.expectError(error.UnexpectedCharacter, parse(":")); |
| 598 | try std.testing.expectError(error.UnexpectedCharacter, parse("-:")); |
| 599 | try std.testing.expectError(error.UnexpectedCharacter, parse("+hTTp:")); |
| 600 | try std.testing.expectError(error.UnexpectedCharacter, parse("$:")); |
| 601 | try std.testing.expectError(error.UnexpectedCharacter, parse("h$:")); |
| 602 | } |
| 603 | |
| 604 | test "authority" { |
| 605 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://hostname")).host.?.percent_encoded); |
| 606 | |
| 607 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://userinfo@hostname")).host.?.percent_encoded); |
| 608 | try std.testing.expectEqualStrings("userinfo", (try parse("scheme://userinfo@hostname")).user.?.percent_encoded); |
| 609 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://userinfo@hostname")).password); |
| 610 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://userinfo@")).host); |
| 611 | |
| 612 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://user:password@hostname")).host.?.percent_encoded); |
| 613 | try std.testing.expectEqualStrings("user", (try parse("scheme://user:password@hostname")).user.?.percent_encoded); |
| 614 | try std.testing.expectEqualStrings("password", (try parse("scheme://user:password@hostname")).password.?.percent_encoded); |
| 615 | |
| 616 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://hostname:0")).host.?.percent_encoded); |
| 617 | try std.testing.expectEqual(@as(u16, 1234), (try parse("scheme://hostname:1234")).port.?); |
| 618 | |
| 619 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://userinfo@hostname:1234")).host.?.percent_encoded); |
| 620 | try std.testing.expectEqual(@as(u16, 1234), (try parse("scheme://userinfo@hostname:1234")).port.?); |
| 621 | try std.testing.expectEqualStrings("userinfo", (try parse("scheme://userinfo@hostname:1234")).user.?.percent_encoded); |
| 622 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://userinfo@hostname:1234")).password); |
| 623 | |
| 624 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://user:password@hostname:1234")).host.?.percent_encoded); |
| 625 | try std.testing.expectEqual(@as(u16, 1234), (try parse("scheme://user:password@hostname:1234")).port.?); |
| 626 | try std.testing.expectEqualStrings("user", (try parse("scheme://user:password@hostname:1234")).user.?.percent_encoded); |
| 627 | try std.testing.expectEqualStrings("password", (try parse("scheme://user:password@hostname:1234")).password.?.percent_encoded); |
| 628 | |
| 629 | try std.testing.expectEqualStrings("user", (try parse("scheme://user:p@ssword@hostname:1234")).user.?.percent_encoded); |
| 630 | try std.testing.expectEqualStrings("p@ssword", (try parse("scheme://user:p@ssword@hostname:1234")).password.?.percent_encoded); |
| 631 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://user:p@ssword@hostname:1234")).host.?.percent_encoded); |
| 632 | |
| 633 | try std.testing.expectEqualStrings("user", (try parse("scheme://user:p@@@word@hostname:1234")).user.?.percent_encoded); |
| 634 | try std.testing.expectEqualStrings("p@@@word", (try parse("scheme://user:p@@@word@hostname:1234")).password.?.percent_encoded); |
| 635 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://user:p@@@word@hostname:1234")).host.?.percent_encoded); |
| 636 | |
| 637 | try std.testing.expectEqualStrings("user@name", (try parse("scheme://user@name@hostname")).user.?.percent_encoded); |
| 638 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://user@name@hostname")).password); |
| 639 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://user@name@hostname")).host.?.percent_encoded); |
| 640 | |
| 641 | try std.testing.expectEqualStrings("user@name", (try parse("scheme://user@name@hostname:1234")).user.?.percent_encoded); |
| 642 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://user@name@hostname:1234")).password); |
| 643 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://user@name@hostname:1234")).host.?.percent_encoded); |
| 644 | try std.testing.expectEqual(@as(u16, 1234), (try parse("scheme://user@name@hostname:1234")).port.?); |
| 645 | } |
| 646 | |
| 647 | test "authority.password" { |
| 648 | try std.testing.expectEqualStrings("username", (try parse("scheme://username@a")).user.?.percent_encoded); |
| 649 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://username@a")).password); |
| 650 | |
| 651 | try std.testing.expectEqualStrings("username", (try parse("scheme://username:@a")).user.?.percent_encoded); |
| 652 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://username:@a")).password); |
| 653 | |
| 654 | try std.testing.expectEqualStrings("username", (try parse("scheme://username:password@a")).user.?.percent_encoded); |
| 655 | try std.testing.expectEqualStrings("password", (try parse("scheme://username:password@a")).password.?.percent_encoded); |
| 656 | |
| 657 | try std.testing.expectEqualStrings("username", (try parse("scheme://username::@a")).user.?.percent_encoded); |
| 658 | try std.testing.expectEqualStrings(":", (try parse("scheme://username::@a")).password.?.percent_encoded); |
| 659 | } |
| 660 | |
| 661 | fn testAuthorityHost(comptime hostlist: anytype) !void { |
| 662 | inline for (hostlist) |hostname| { |
| 663 | try std.testing.expectEqualStrings(hostname, (try parse("scheme://" ++ hostname)).host.?.percent_encoded); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | test "authority.dns-names" { |
| 668 | try testAuthorityHost(.{ |
| 669 | "a", |
| 670 | "a.b", |
| 671 | "example.com", |
| 672 | "www.example.com", |
| 673 | "example.org.", |
| 674 | "www.example.org.", |
| 675 | "xn--nw2a.xn--j6w193g", // internationalized URI: 見.香港 |
| 676 | "fe80--1ff-fe23-4567-890as3.ipv6-literal.net", |
| 677 | }); |
| 678 | } |
| 679 | |
| 680 | test "authority.IPv4" { |
| 681 | try testAuthorityHost(.{ |
| 682 | "127.0.0.1", |
| 683 | "255.255.255.255", |
| 684 | "0.0.0.0", |
| 685 | "8.8.8.8", |
| 686 | "1.2.3.4", |
| 687 | "192.168.0.1", |
| 688 | "10.42.0.0", |
| 689 | }); |
| 690 | } |
| 691 | |
| 692 | test "authority.IPv6" { |
| 693 | try testAuthorityHost(.{ |
| 694 | "[2001:db8:0:0:0:0:2:1]", |
| 695 | "[2001:db8::2:1]", |
| 696 | "[2001:db8:0000:1:1:1:1:1]", |
| 697 | "[2001:db8:0:1:1:1:1:1]", |
| 698 | "[0:0:0:0:0:0:0:0]", |
| 699 | "[0:0:0:0:0:0:0:1]", |
| 700 | "[::1]", |
| 701 | "[::]", |
| 702 | "[2001:db8:85a3:8d3:1319:8a2e:370:7348]", |
| 703 | "[fe80::1ff:fe23:4567:890a%25eth2]", |
| 704 | "[fe80::1ff:fe23:4567:890a]", |
| 705 | "[fe80::1ff:fe23:4567:890a%253]", |
| 706 | "[fe80:3::1ff:fe23:4567:890a]", |
| 707 | }); |
| 708 | } |
| 709 | |
| 710 | test "RFC example 1" { |
| 711 | const uri = "foo://example.com:8042/over/there?name=ferret#nose"; |
| 712 | try std.testing.expectEqual(Uri{ |
| 713 | .scheme = uri[0..3], |
| 714 | .user = null, |
| 715 | .password = null, |
| 716 | .host = .{ .percent_encoded = uri[6..17] }, |
| 717 | .port = 8042, |
| 718 | .path = .{ .percent_encoded = uri[22..33] }, |
| 719 | .query = .{ .percent_encoded = uri[34..45] }, |
| 720 | .fragment = .{ .percent_encoded = uri[46..50] }, |
| 721 | }, try parse(uri)); |
| 722 | } |
| 723 | |
| 724 | test "RFC example 2" { |
| 725 | const uri = "urn:example:animal:ferret:nose"; |
| 726 | try std.testing.expectEqual(Uri{ |
| 727 | .scheme = uri[0..3], |
| 728 | .user = null, |
| 729 | .password = null, |
| 730 | .host = null, |
| 731 | .port = null, |
| 732 | .path = .{ .percent_encoded = uri[4..] }, |
| 733 | .query = null, |
| 734 | .fragment = null, |
| 735 | }, try parse(uri)); |
| 736 | } |
| 737 | |
| 738 | // source: |
| 739 | // https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#Examples |
| 740 | test "Examples from wikipedia" { |
| 741 | const list = [_][]const u8{ |
| 742 | "https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top", |
| 743 | "ldap://[2001:db8::7]/c=GB?objectClass?one", |
| 744 | "mailto:John.Doe@example.com", |
| 745 | "news:comp.infosystems.www.servers.unix", |
| 746 | "tel:+1-816-555-1212", |
| 747 | "telnet://192.0.2.16:80/", |
| 748 | "urn:oasis:names:specification:docbook:dtd:xml:4.1.2", |
| 749 | "http://a/b/c/d;p?q", |
| 750 | }; |
| 751 | for (list) |uri| { |
| 752 | _ = try parse(uri); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | // source: |
| 757 | // https://tools.ietf.org/html/rfc3986#section-5.4.1 |
| 758 | test "Examples from RFC3986" { |
| 759 | const list = [_][]const u8{ |
| 760 | "http://a/b/c/g", |
| 761 | "http://a/b/c/g", |
| 762 | "http://a/b/c/g/", |
| 763 | "http://a/g", |
| 764 | "http://g", |
| 765 | "http://a/b/c/d;p?y", |
| 766 | "http://a/b/c/g?y", |
| 767 | "http://a/b/c/d;p?q#s", |
| 768 | "http://a/b/c/g#s", |
| 769 | "http://a/b/c/g?y#s", |
| 770 | "http://a/b/c/;x", |
| 771 | "http://a/b/c/g;x", |
| 772 | "http://a/b/c/g;x?y#s", |
| 773 | "http://a/b/c/d;p?q", |
| 774 | "http://a/b/c/", |
| 775 | "http://a/b/c/", |
| 776 | "http://a/b/", |
| 777 | "http://a/b/", |
| 778 | "http://a/b/g", |
| 779 | "http://a/", |
| 780 | "http://a/", |
| 781 | "http://a/g", |
| 782 | }; |
| 783 | for (list) |uri| { |
| 784 | _ = try parse(uri); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | test "Special test" { |
| 789 | // This is for all of you code readers ♥ |
| 790 | _ = try parse("https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtu.be&t=0"); |
| 791 | } |
| 792 | |
| 793 | test "URI percent encoding" { |
| 794 | try std.testing.expectFmt( |
| 795 | "%5C%C3%B6%2F%20%C3%A4%C3%B6%C3%9F%20~~.adas-https%3A%2F%2Fcanvas%3A123%2F%23ads%26%26sad", |
| 796 | "{f}", |
| 797 | .{std.fmt.alt( |
| 798 | @as(Component, .{ .raw = "\\ö/ äöß ~~.adas-https://canvas:123/#ads&&sad" }), |
| 799 | .formatEscaped, |
| 800 | )}, |
| 801 | ); |
| 802 | } |
| 803 | |
| 804 | test "URI percent decoding" { |
| 805 | { |
| 806 | const expected = "\\ö/ äöß ~~.adas-https://canvas:123/#ads&&sad"; |
| 807 | var input = "%5C%C3%B6%2F%20%C3%A4%C3%B6%C3%9F%20~~.adas-https%3A%2F%2Fcanvas%3A123%2F%23ads%26%26sad".*; |
| 808 | |
| 809 | try std.testing.expectFmt(expected, "{f}", .{std.fmt.alt( |
| 810 | @as(Component, .{ .percent_encoded = &input }), |
| 811 | .formatRaw, |
| 812 | )}); |
| 813 | |
| 814 | var output: [expected.len]u8 = undefined; |
| 815 | try std.testing.expectEqualStrings(percentDecodeBackwards(&output, &input), expected); |
| 816 | |
| 817 | try std.testing.expectEqualStrings(expected, percentDecodeInPlace(&input)); |
| 818 | } |
| 819 | |
| 820 | { |
| 821 | const expected = "/abc%"; |
| 822 | var input = expected.*; |
| 823 | |
| 824 | try std.testing.expectFmt(expected, "{f}", .{std.fmt.alt( |
| 825 | @as(Component, .{ .percent_encoded = &input }), |
| 826 | .formatRaw, |
| 827 | )}); |
| 828 | |
| 829 | var output: [expected.len]u8 = undefined; |
| 830 | try std.testing.expectEqualStrings(percentDecodeBackwards(&output, &input), expected); |
| 831 | |
| 832 | try std.testing.expectEqualStrings(expected, percentDecodeInPlace(&input)); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | test "URI query encoding" { |
| 837 | const address = "https://objects.githubusercontent.com/?response-content-type=application%2Foctet-stream"; |
| 838 | const parsed = try Uri.parse(address); |
| 839 | |
| 840 | // format the URI to percent encode it |
| 841 | try std.testing.expectFmt("/?response-content-type=application%2Foctet-stream", "{f}", .{ |
| 842 | parsed.fmt(.{ .path = true, .query = true }), |
| 843 | }); |
| 844 | } |
| 845 | |
| 846 | test "format" { |
| 847 | const uri: Uri = .{ |
| 848 | .scheme = "file", |
| 849 | .user = null, |
| 850 | .password = null, |
| 851 | .host = null, |
| 852 | .port = null, |
| 853 | .path = .{ .raw = "/foo/bar/baz" }, |
| 854 | .query = null, |
| 855 | .fragment = null, |
| 856 | }; |
| 857 | try std.testing.expectFmt("file:/foo/bar/baz", "{f}", .{ |
| 858 | uri.fmt(.{ .scheme = true, .path = true, .query = true, .fragment = true }), |
| 859 | }); |
| 860 | } |
| 861 | |
| 862 | test "URI malformed input" { |
| 863 | try std.testing.expectError(error.InvalidFormat, std.Uri.parse("http://][")); |
| 864 | try std.testing.expectError(error.InvalidFormat, std.Uri.parse("http://]@[")); |
| 865 | try std.testing.expectError(error.InvalidFormat, std.Uri.parse("http://lo]s\x85hc@[/8\x10?0Q")); |
| 866 | } |