| ... | ... | @@ -1,156 +1,157 @@ |
| 1 | 1 | //! Uniform Resource Identifier (URI) parsing roughly adhering to <https://tools.ietf.org/html/rfc3986>. |
| 2 | 2 | //! Does not do perfect grammar and character class checking, but should be robust against URIs in the wild. |
| 3 | 3 | |
| 4 | | const Uri = @This(); |
| 5 | | const std = @import("std.zig"); |
| 6 | | const testing = std.testing; |
| 7 | | const Allocator = std.mem.Allocator; |
| 8 | | |
| 9 | 4 | scheme: []const u8, |
| 10 | | user: ?[]const u8 = null, |
| 11 | | password: ?[]const u8 = null, |
| 12 | | host: ?[]const u8 = null, |
| 5 | user: ?Component = null, |
| 6 | password: ?Component = null, |
| 7 | host: ?Component = null, |
| 13 | 8 | port: ?u16 = null, |
| 14 | | path: []const u8, |
| 15 | | query: ?[]const u8 = null, |
| 16 | | fragment: ?[]const u8 = null, |
| 17 | | |
| 18 | | /// Applies URI encoding and replaces all reserved characters with their respective %XX code. |
| 19 | | pub fn escapeString(allocator: Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 20 | | return escapeStringWithFn(allocator, input, isUnreserved); |
| 21 | | } |
| 22 | | |
| 23 | | pub fn escapePath(allocator: Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 24 | | return escapeStringWithFn(allocator, input, isPathChar); |
| 25 | | } |
| 26 | | |
| 27 | | pub fn escapeQuery(allocator: Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 28 | | return escapeStringWithFn(allocator, input, isQueryChar); |
| 29 | | } |
| 30 | | |
| 31 | | pub fn writeEscapedString(writer: anytype, input: []const u8) !void { |
| 32 | | return writeEscapedStringWithFn(writer, input, isUnreserved); |
| 33 | | } |
| 34 | | |
| 35 | | pub fn writeEscapedPath(writer: anytype, input: []const u8) !void { |
| 36 | | return writeEscapedStringWithFn(writer, input, isPathChar); |
| 37 | | } |
| 38 | | |
| 39 | | pub fn writeEscapedQuery(writer: anytype, input: []const u8) !void { |
| 40 | | return writeEscapedStringWithFn(writer, input, isQueryChar); |
| 41 | | } |
| 42 | | |
| 43 | | pub fn escapeStringWithFn(allocator: Allocator, input: []const u8, comptime keepUnescaped: fn (c: u8) bool) Allocator.Error![]u8 { |
| 44 | | var outsize: usize = 0; |
| 45 | | for (input) |c| { |
| 46 | | outsize += if (keepUnescaped(c)) @as(usize, 1) else 3; |
| 9 | path: Component = Component.empty, |
| 10 | query: ?Component = null, |
| 11 | fragment: ?Component = null, |
| 12 | |
| 13 | pub const Component = union(enum) { |
| 14 | /// Invalid characters in this component must be percent encoded |
| 15 | /// before being printed as part of a URI. |
| 16 | raw: []const u8, |
| 17 | /// This component is already percent-encoded, it can be printed |
| 18 | /// directly as part of a URI. |
| 19 | percent_encoded: []const u8, |
| 20 | |
| 21 | pub const empty: Component = .{ .percent_encoded = "" }; |
| 22 | |
| 23 | pub fn isEmpty(component: Component) bool { |
| 24 | return switch (component) { |
| 25 | .raw, .percent_encoded => |string| string.len == 0, |
| 26 | }; |
| 47 | 27 | } |
| 48 | | var output = try allocator.alloc(u8, outsize); |
| 49 | | var outptr: usize = 0; |
| 50 | 28 | |
| 51 | | for (input) |c| { |
| 52 | | if (keepUnescaped(c)) { |
| 53 | | output[outptr] = c; |
| 54 | | outptr += 1; |
| 55 | | } else { |
| 56 | | var buf: [2]u8 = undefined; |
| 57 | | _ = std.fmt.bufPrint(&buf, "{X:0>2}", .{c}) catch unreachable; |
| 58 | | |
| 59 | | output[outptr + 0] = '%'; |
| 60 | | output[outptr + 1] = buf[0]; |
| 61 | | output[outptr + 2] = buf[1]; |
| 62 | | outptr += 3; |
| 63 | | } |
| 29 | /// Allocates the result with `arena` only if needed, so the result should not be freed. |
| 30 | pub fn toRawMaybeAlloc( |
| 31 | component: Component, |
| 32 | arena: std.mem.Allocator, |
| 33 | ) std.mem.Allocator.Error![]const u8 { |
| 34 | return switch (component) { |
| 35 | .raw => |raw| raw, |
| 36 | .percent_encoded => |percent_encoded| if (std.mem.indexOfScalar(u8, percent_encoded, '%')) |_| |
| 37 | try std.fmt.allocPrint(arena, "{raw}", .{component}) |
| 38 | else |
| 39 | percent_encoded, |
| 40 | }; |
| 64 | 41 | } |
| 65 | | return output; |
| 66 | | } |
| 67 | 42 | |
| 68 | | pub fn writeEscapedStringWithFn(writer: anytype, input: []const u8, comptime keepUnescaped: fn (c: u8) bool) @TypeOf(writer).Error!void { |
| 69 | | for (input) |c| { |
| 70 | | if (keepUnescaped(c)) { |
| 71 | | try writer.writeByte(c); |
| 72 | | } else { |
| 73 | | try writer.print("%{X:0>2}", .{c}); |
| 74 | | } |
| 43 | pub fn format( |
| 44 | component: Component, |
| 45 | comptime fmt_str: []const u8, |
| 46 | _: std.fmt.FormatOptions, |
| 47 | writer: anytype, |
| 48 | ) @TypeOf(writer).Error!void { |
| 49 | if (fmt_str.len == 0) { |
| 50 | try writer.print("std.Uri.Component{{ .{s} = \"{}\" }}", .{ |
| 51 | @tagName(component), |
| 52 | std.zig.fmtEscapes(switch (component) { |
| 53 | .raw, .percent_encoded => |string| string, |
| 54 | }), |
| 55 | }); |
| 56 | } else if (comptime std.mem.eql(u8, fmt_str, "raw")) switch (component) { |
| 57 | .raw => |raw| try writer.writeAll(raw), |
| 58 | .percent_encoded => |percent_encoded| { |
| 59 | var start: usize = 0; |
| 60 | var index: usize = 0; |
| 61 | while (std.mem.indexOfScalarPos(u8, percent_encoded, index, '%')) |percent| { |
| 62 | index = percent + 1; |
| 63 | if (percent_encoded.len - index < 2) continue; |
| 64 | const percent_encoded_char = |
| 65 | std.fmt.parseInt(u8, percent_encoded[index..][0..2], 16) catch continue; |
| 66 | try writer.print("{s}{c}", .{ |
| 67 | percent_encoded[start..percent], |
| 68 | percent_encoded_char, |
| 69 | }); |
| 70 | start = percent + 3; |
| 71 | index = percent + 3; |
| 72 | } |
| 73 | try writer.writeAll(percent_encoded[start..]); |
| 74 | }, |
| 75 | } else if (comptime std.mem.eql(u8, fmt_str, "%")) switch (component) { |
| 76 | .raw => |raw| try percentEncode(writer, raw, isUnreserved), |
| 77 | .percent_encoded => |percent_encoded| try writer.writeAll(percent_encoded), |
| 78 | } else if (comptime std.mem.eql(u8, fmt_str, "user")) switch (component) { |
| 79 | .raw => |raw| try percentEncode(writer, raw, isUserChar), |
| 80 | .percent_encoded => |percent_encoded| try writer.writeAll(percent_encoded), |
| 81 | } else if (comptime std.mem.eql(u8, fmt_str, "password")) switch (component) { |
| 82 | .raw => |raw| try percentEncode(writer, raw, isPasswordChar), |
| 83 | .percent_encoded => |percent_encoded| try writer.writeAll(percent_encoded), |
| 84 | } else if (comptime std.mem.eql(u8, fmt_str, "host")) switch (component) { |
| 85 | .raw => |raw| try percentEncode(writer, raw, isHostChar), |
| 86 | .percent_encoded => |percent_encoded| try writer.writeAll(percent_encoded), |
| 87 | } else if (comptime std.mem.eql(u8, fmt_str, "path")) switch (component) { |
| 88 | .raw => |raw| try percentEncode(writer, raw, isPathChar), |
| 89 | .percent_encoded => |percent_encoded| try writer.writeAll(percent_encoded), |
| 90 | } else if (comptime std.mem.eql(u8, fmt_str, "query")) switch (component) { |
| 91 | .raw => |raw| try percentEncode(writer, raw, isQueryChar), |
| 92 | .percent_encoded => |percent_encoded| try writer.writeAll(percent_encoded), |
| 93 | } else if (comptime std.mem.eql(u8, fmt_str, "fragment")) switch (component) { |
| 94 | .raw => |raw| try percentEncode(writer, raw, isFragmentChar), |
| 95 | .percent_encoded => |percent_encoded| try writer.writeAll(percent_encoded), |
| 96 | } else @compileError("invalid format string '" ++ fmt_str ++ "'"); |
| 75 | 97 | } |
| 76 | | } |
| 77 | 98 | |
| 78 | | /// Parses a URI string and unescapes all %XX where XX is a valid hex number. Otherwise, verbatim copies |
| 79 | | /// them to the output. |
| 80 | | pub fn unescapeString(allocator: Allocator, input: []const u8) error{OutOfMemory}![]u8 { |
| 81 | | var outsize: usize = 0; |
| 82 | | var inptr: usize = 0; |
| 83 | | while (inptr < input.len) { |
| 84 | | if (input[inptr] == '%') { |
| 85 | | inptr += 1; |
| 86 | | if (inptr + 2 <= input.len) { |
| 87 | | _ = std.fmt.parseInt(u8, input[inptr..][0..2], 16) catch { |
| 88 | | outsize += 3; |
| 89 | | inptr += 2; |
| 90 | | continue; |
| 91 | | }; |
| 92 | | inptr += 2; |
| 93 | | outsize += 1; |
| 94 | | } else { |
| 95 | | outsize += 1; |
| 96 | | } |
| 97 | | } else { |
| 98 | | inptr += 1; |
| 99 | | outsize += 1; |
| 99 | pub fn percentEncode( |
| 100 | writer: anytype, |
| 101 | raw: []const u8, |
| 102 | comptime isValidChar: fn (u8) bool, |
| 103 | ) @TypeOf(writer).Error!void { |
| 104 | var start: usize = 0; |
| 105 | for (raw, 0..) |char, index| { |
| 106 | if (isValidChar(char)) continue; |
| 107 | try writer.print("{s}%{X:0>2}", .{ raw[start..index], char }); |
| 108 | start = index + 1; |
| 100 | 109 | } |
| 110 | try writer.writeAll(raw[start..]); |
| 101 | 111 | } |
| 112 | }; |
| 102 | 113 | |
| 103 | | var output = try allocator.alloc(u8, outsize); |
| 104 | | var outptr: usize = 0; |
| 105 | | inptr = 0; |
| 106 | | while (inptr < input.len) { |
| 107 | | if (input[inptr] == '%') { |
| 108 | | inptr += 1; |
| 109 | | if (inptr + 2 <= input.len) { |
| 110 | | const value = std.fmt.parseInt(u8, input[inptr..][0..2], 16) catch { |
| 111 | | output[outptr + 0] = input[inptr + 0]; |
| 112 | | output[outptr + 1] = input[inptr + 1]; |
| 113 | | inptr += 2; |
| 114 | | outptr += 2; |
| 114 | /// Percent decodes all %XX where XX is a valid hex number. |
| 115 | /// `output` may alias `input` if `output.ptr <= input.ptr`. |
| 116 | /// Mutates and returns a subslice of `output`. |
| 117 | pub fn percentDecodeBackwards(output: []u8, input: []const u8) []u8 { |
| 118 | var input_index = input.len; |
| 119 | var output_index = output.len; |
| 120 | while (input_index > 0) { |
| 121 | if (input_index >= 3) { |
| 122 | const maybe_percent_encoded = input[input_index - 3 ..][0..3]; |
| 123 | if (maybe_percent_encoded[0] == '%') { |
| 124 | if (std.fmt.parseInt(u8, maybe_percent_encoded[1..], 16)) |percent_encoded_char| { |
| 125 | input_index -= maybe_percent_encoded.len; |
| 126 | output_index -= 1; |
| 127 | output[output_index] = percent_encoded_char; |
| 115 | 128 | continue; |
| 116 | | }; |
| 117 | | |
| 118 | | output[outptr] = value; |
| 119 | | |
| 120 | | inptr += 2; |
| 121 | | outptr += 1; |
| 122 | | } else { |
| 123 | | output[outptr] = input[inptr - 1]; |
| 124 | | outptr += 1; |
| 129 | } else |_| {} |
| 125 | 130 | } |
| 126 | | } else { |
| 127 | | output[outptr] = input[inptr]; |
| 128 | | inptr += 1; |
| 129 | | outptr += 1; |
| 130 | 131 | } |
| 132 | input_index -= 1; |
| 133 | output_index -= 1; |
| 134 | output[output_index] = input[input_index]; |
| 131 | 135 | } |
| 132 | | return output; |
| 136 | return output[output_index..]; |
| 137 | } |
| 138 | |
| 139 | /// Percent decodes all %XX where XX is a valid hex number. |
| 140 | /// Mutates and returns a subslice of `buffer`. |
| 141 | pub fn percentDecodeInPlace(buffer: []u8) []u8 { |
| 142 | return percentDecodeBackwards(buffer, buffer); |
| 133 | 143 | } |
| 134 | 144 | |
| 135 | 145 | pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort }; |
| 136 | 146 | |
| 137 | 147 | /// Parses the URI or returns an error. This function is not compliant, but is required to parse |
| 138 | 148 | /// some forms of URIs in the wild, such as HTTP Location headers. |
| 139 | | /// The return value will contain unescaped strings pointing into the |
| 140 | | /// original `text`. Each component that is provided, will be non-`null`. |
| 141 | | pub fn parseWithoutScheme(text: []const u8) ParseError!Uri { |
| 149 | /// The return value will contain strings pointing into the original `text`. |
| 150 | /// Each component that is provided, will be non-`null`. |
| 151 | pub fn parseAfterScheme(scheme: []const u8, text: []const u8) ParseError!Uri { |
| 142 | 152 | var reader = SliceReader{ .slice = text }; |
| 143 | 153 | |
| 144 | | var uri = Uri{ |
| 145 | | .scheme = "", |
| 146 | | .user = null, |
| 147 | | .password = null, |
| 148 | | .host = null, |
| 149 | | .port = null, |
| 150 | | .path = "", // path is always set, but empty by default. |
| 151 | | .query = null, |
| 152 | | .fragment = null, |
| 153 | | }; |
| 154 | var uri: Uri = .{ .scheme = scheme, .path = undefined }; |
| 154 | 155 | |
| 155 | 156 | if (reader.peekPrefix("//")) a: { // authority part |
| 156 | 157 | std.debug.assert(reader.get().? == '/'); |
| ... | ... | @@ -167,12 +168,12 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri { |
| 167 | 168 | const user_info = authority[0..index]; |
| 168 | 169 | |
| 169 | 170 | if (std.mem.indexOf(u8, user_info, ":")) |idx| { |
| 170 | | uri.user = user_info[0..idx]; |
| 171 | uri.user = .{ .percent_encoded = user_info[0..idx] }; |
| 171 | 172 | if (idx < user_info.len - 1) { // empty password is also "no password" |
| 172 | | uri.password = user_info[idx + 1 ..]; |
| 173 | uri.password = .{ .percent_encoded = user_info[idx + 1 ..] }; |
| 173 | 174 | } |
| 174 | 175 | } else { |
| 175 | | uri.user = user_info; |
| 176 | uri.user = .{ .percent_encoded = user_info }; |
| 176 | 177 | uri.password = null; |
| 177 | 178 | } |
| 178 | 179 | } |
| ... | ... | @@ -205,19 +206,19 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri { |
| 205 | 206 | } |
| 206 | 207 | |
| 207 | 208 | if (start_of_host >= end_of_host) return error.InvalidFormat; |
| 208 | | uri.host = authority[start_of_host..end_of_host]; |
| 209 | uri.host = .{ .percent_encoded = authority[start_of_host..end_of_host] }; |
| 209 | 210 | } |
| 210 | 211 | |
| 211 | | uri.path = reader.readUntil(isPathSeparator); |
| 212 | uri.path = .{ .percent_encoded = reader.readUntil(isPathSeparator) }; |
| 212 | 213 | |
| 213 | 214 | if ((reader.peek() orelse 0) == '?') { // query part |
| 214 | 215 | std.debug.assert(reader.get().? == '?'); |
| 215 | | uri.query = reader.readUntil(isQuerySeparator); |
| 216 | uri.query = .{ .percent_encoded = reader.readUntil(isQuerySeparator) }; |
| 216 | 217 | } |
| 217 | 218 | |
| 218 | 219 | if ((reader.peek() orelse 0) == '#') { // fragment part |
| 219 | 220 | std.debug.assert(reader.get().? == '#'); |
| 220 | | uri.fragment = reader.readUntilEof(); |
| 221 | uri.fragment = .{ .percent_encoded = reader.readUntilEof() }; |
| 221 | 222 | } |
| 222 | 223 | |
| 223 | 224 | return uri; |
| ... | ... | @@ -241,9 +242,6 @@ pub const WriteToStreamOptions = struct { |
| 241 | 242 | |
| 242 | 243 | /// When true, include the fragment part of the URI. Ignored when `path` is false. |
| 243 | 244 | fragment: bool = false, |
| 244 | | |
| 245 | | /// When true, do not escape any part of the URI. |
| 246 | | raw: bool = false, |
| 247 | 245 | }; |
| 248 | 246 | |
| 249 | 247 | pub fn writeToStream( |
| ... | ... | @@ -252,80 +250,51 @@ pub fn writeToStream( |
| 252 | 250 | writer: anytype, |
| 253 | 251 | ) @TypeOf(writer).Error!void { |
| 254 | 252 | if (options.scheme) { |
| 255 | | try writer.writeAll(uri.scheme); |
| 256 | | try writer.writeAll(":"); |
| 257 | | |
| 253 | try writer.print("{s}:", .{uri.scheme}); |
| 258 | 254 | if (options.authority and uri.host != null) { |
| 259 | 255 | try writer.writeAll("//"); |
| 260 | 256 | } |
| 261 | 257 | } |
| 262 | | |
| 263 | 258 | if (options.authority) { |
| 264 | 259 | if (options.authentication and uri.host != null) { |
| 265 | 260 | if (uri.user) |user| { |
| 266 | | try writer.writeAll(user); |
| 261 | try writer.print("{user}", .{user}); |
| 267 | 262 | if (uri.password) |password| { |
| 268 | | try writer.writeAll(":"); |
| 269 | | try writer.writeAll(password); |
| 263 | try writer.print(":{password}", .{password}); |
| 270 | 264 | } |
| 271 | | try writer.writeAll("@"); |
| 265 | try writer.writeByte('@'); |
| 272 | 266 | } |
| 273 | 267 | } |
| 274 | | |
| 275 | 268 | if (uri.host) |host| { |
| 276 | | try writer.writeAll(host); |
| 277 | | |
| 278 | | if (uri.port) |port| { |
| 279 | | try writer.writeAll(":"); |
| 280 | | try std.fmt.formatInt(port, 10, .lower, .{}, writer); |
| 281 | | } |
| 269 | try writer.print("{host}", .{host}); |
| 270 | if (uri.port) |port| try writer.print(":{d}", .{port}); |
| 282 | 271 | } |
| 283 | 272 | } |
| 284 | | |
| 285 | 273 | if (options.path) { |
| 286 | | if (uri.path.len == 0) { |
| 287 | | try writer.writeAll("/"); |
| 288 | | } else if (options.raw) { |
| 289 | | try writer.writeAll(uri.path); |
| 290 | | } else { |
| 291 | | try writeEscapedPath(writer, uri.path); |
| 274 | try writer.print("{path}", .{ |
| 275 | if (uri.path.isEmpty()) Uri.Component{ .percent_encoded = "/" } else uri.path, |
| 276 | }); |
| 277 | if (options.query) { |
| 278 | if (uri.query) |query| try writer.print("?{query}", .{query}); |
| 279 | } |
| 280 | if (options.fragment) { |
| 281 | if (uri.fragment) |fragment| try writer.print("#{fragment}", .{fragment}); |
| 292 | 282 | } |
| 293 | | |
| 294 | | if (options.query) if (uri.query) |q| { |
| 295 | | try writer.writeAll("?"); |
| 296 | | if (options.raw) { |
| 297 | | try writer.writeAll(q); |
| 298 | | } else { |
| 299 | | try writeEscapedQuery(writer, q); |
| 300 | | } |
| 301 | | }; |
| 302 | | |
| 303 | | if (options.fragment) if (uri.fragment) |f| { |
| 304 | | try writer.writeAll("#"); |
| 305 | | if (options.raw) { |
| 306 | | try writer.writeAll(f); |
| 307 | | } else { |
| 308 | | try writeEscapedQuery(writer, f); |
| 309 | | } |
| 310 | | }; |
| 311 | 283 | } |
| 312 | 284 | } |
| 313 | 285 | |
| 314 | 286 | pub fn format( |
| 315 | 287 | uri: Uri, |
| 316 | | comptime fmt: []const u8, |
| 317 | | options: std.fmt.FormatOptions, |
| 288 | comptime fmt_str: []const u8, |
| 289 | _: std.fmt.FormatOptions, |
| 318 | 290 | writer: anytype, |
| 319 | 291 | ) @TypeOf(writer).Error!void { |
| 320 | | _ = options; |
| 321 | | |
| 322 | | const scheme = comptime std.mem.indexOf(u8, fmt, ";") != null or fmt.len == 0; |
| 323 | | const authentication = comptime std.mem.indexOf(u8, fmt, "@") != null or fmt.len == 0; |
| 324 | | const authority = comptime std.mem.indexOf(u8, fmt, "+") != null or fmt.len == 0; |
| 325 | | const path = comptime std.mem.indexOf(u8, fmt, "/") != null or fmt.len == 0; |
| 326 | | const query = comptime std.mem.indexOf(u8, fmt, "?") != null or fmt.len == 0; |
| 327 | | const fragment = comptime std.mem.indexOf(u8, fmt, "#") != null or fmt.len == 0; |
| 328 | | const raw = comptime std.mem.indexOf(u8, fmt, "r") != null or fmt.len == 0; |
| 292 | const scheme = comptime std.mem.indexOfScalar(u8, fmt_str, ';') != null or fmt_str.len == 0; |
| 293 | const authentication = comptime std.mem.indexOfScalar(u8, fmt_str, '@') != null or fmt_str.len == 0; |
| 294 | const authority = comptime std.mem.indexOfScalar(u8, fmt_str, '+') != null or fmt_str.len == 0; |
| 295 | const path = comptime std.mem.indexOfScalar(u8, fmt_str, '/') != null or fmt_str.len == 0; |
| 296 | const query = comptime std.mem.indexOfScalar(u8, fmt_str, '?') != null or fmt_str.len == 0; |
| 297 | const fragment = comptime std.mem.indexOfScalar(u8, fmt_str, '#') != null or fmt_str.len == 0; |
| 329 | 298 | |
| 330 | 299 | return writeToStream(uri, .{ |
| 331 | 300 | .scheme = scheme, |
| ... | ... | @@ -334,12 +303,11 @@ pub fn format( |
| 334 | 303 | .path = path, |
| 335 | 304 | .query = query, |
| 336 | 305 | .fragment = fragment, |
| 337 | | .raw = raw, |
| 338 | 306 | }, writer); |
| 339 | 307 | } |
| 340 | 308 | |
| 341 | 309 | /// Parses the URI or returns an error. |
| 342 | | /// The return value will contain unescaped strings pointing into the |
| 310 | /// The return value will contain strings pointing into the |
| 343 | 311 | /// original `text`. Each component that is provided, will be non-`null`. |
| 344 | 312 | pub fn parse(text: []const u8) ParseError!Uri { |
| 345 | 313 | var reader: SliceReader = .{ .slice = text }; |
| ... | ... | @@ -353,42 +321,32 @@ pub fn parse(text: []const u8) ParseError!Uri { |
| 353 | 321 | return error.InvalidFormat; |
| 354 | 322 | } |
| 355 | 323 | |
| 356 | | var uri = try parseWithoutScheme(reader.readUntilEof()); |
| 357 | | uri.scheme = scheme; |
| 358 | | |
| 359 | | return uri; |
| 324 | return parseAfterScheme(scheme, reader.readUntilEof()); |
| 360 | 325 | } |
| 361 | 326 | |
| 362 | | pub const ResolveInplaceError = ParseError || error{OutOfMemory}; |
| 327 | pub const ResolveInPlaceError = ParseError || error{NoSpaceLeft}; |
| 363 | 328 | |
| 364 | 329 | /// Resolves a URI against a base URI, conforming to RFC 3986, Section 5. |
| 365 | | /// Copies `new` to the beginning of `aux_buf`, allowing the slices to overlap, |
| 330 | /// Copies `new` to the beginning of `aux_buf.*`, allowing the slices to overlap, |
| 366 | 331 | /// then parses `new` as a URI, and then resolves the path in place. |
| 367 | 332 | /// If a merge needs to take place, the newly constructed path will be stored |
| 368 | | /// in `aux_buf` just after the copied `new`. |
| 369 | | pub fn resolve_inplace(base: Uri, new: []const u8, aux_buf: []u8) ResolveInplaceError!Uri { |
| 370 | | std.mem.copyForwards(u8, aux_buf, new); |
| 333 | /// in `aux_buf.*` just after the copied `new`, and `aux_buf.*` will be modified |
| 334 | /// to only contain the remaining unused space. |
| 335 | pub fn resolve_inplace(base: Uri, new: []const u8, aux_buf: *[]u8) ResolveInPlaceError!Uri { |
| 336 | std.mem.copyForwards(u8, aux_buf.*, new); |
| 371 | 337 | // At this point, new is an invalid pointer. |
| 372 | | const new_mut = aux_buf[0..new.len]; |
| 373 | | |
| 374 | | const new_parsed, const has_scheme = p: { |
| 375 | | break :p .{ |
| 376 | | parse(new_mut) catch |first_err| { |
| 377 | | break :p .{ |
| 378 | | parseWithoutScheme(new_mut) catch return first_err, |
| 379 | | false, |
| 380 | | }; |
| 381 | | }, |
| 382 | | true, |
| 383 | | }; |
| 384 | | }; |
| 338 | const new_mut = aux_buf.*[0..new.len]; |
| 339 | aux_buf.* = aux_buf.*[new.len..]; |
| 385 | 340 | |
| 341 | const new_parsed = parse(new_mut) catch |err| |
| 342 | (parseAfterScheme("", new_mut) catch return err); |
| 386 | 343 | // As you can see above, `new_mut` is not a const pointer. |
| 387 | | const new_path: []u8 = @constCast(new_parsed.path); |
| 344 | const new_path: []u8 = @constCast(new_parsed.path.percent_encoded); |
| 388 | 345 | |
| 389 | | if (has_scheme) return .{ |
| 346 | if (new_parsed.scheme.len > 0) return .{ |
| 390 | 347 | .scheme = new_parsed.scheme, |
| 391 | 348 | .user = new_parsed.user, |
| 349 | .password = new_parsed.password, |
| 392 | 350 | .host = new_parsed.host, |
| 393 | 351 | .port = new_parsed.port, |
| 394 | 352 | .path = remove_dot_segments(new_path), |
| ... | ... | @@ -399,6 +357,7 @@ pub fn resolve_inplace(base: Uri, new: []const u8, aux_buf: []u8) ResolveInplace |
| 399 | 357 | if (new_parsed.host) |host| return .{ |
| 400 | 358 | .scheme = base.scheme, |
| 401 | 359 | .user = new_parsed.user, |
| 360 | .password = new_parsed.password, |
| 402 | 361 | .host = host, |
| 403 | 362 | .port = new_parsed.port, |
| 404 | 363 | .path = remove_dot_segments(new_path), |
| ... | ... | @@ -406,28 +365,21 @@ pub fn resolve_inplace(base: Uri, new: []const u8, aux_buf: []u8) ResolveInplace |
| 406 | 365 | .fragment = new_parsed.fragment, |
| 407 | 366 | }; |
| 408 | 367 | |
| 409 | | const path, const query = b: { |
| 410 | | if (new_path.len == 0) |
| 411 | | break :b .{ |
| 412 | | base.path, |
| 413 | | new_parsed.query orelse base.query, |
| 414 | | }; |
| 415 | | |
| 416 | | if (new_path[0] == '/') |
| 417 | | break :b .{ |
| 418 | | remove_dot_segments(new_path), |
| 419 | | new_parsed.query, |
| 420 | | }; |
| 421 | | |
| 422 | | break :b .{ |
| 423 | | try merge_paths(base.path, new_path, aux_buf[new_mut.len..]), |
| 424 | | new_parsed.query, |
| 425 | | }; |
| 368 | const path, const query = if (new_path.len == 0) .{ |
| 369 | base.path, |
| 370 | new_parsed.query orelse base.query, |
| 371 | } else if (new_path[0] == '/') .{ |
| 372 | remove_dot_segments(new_path), |
| 373 | new_parsed.query, |
| 374 | } else .{ |
| 375 | try merge_paths(base.path, new_path, aux_buf), |
| 376 | new_parsed.query, |
| 426 | 377 | }; |
| 427 | 378 | |
| 428 | 379 | return .{ |
| 429 | 380 | .scheme = base.scheme, |
| 430 | 381 | .user = base.user, |
| 382 | .password = base.password, |
| 431 | 383 | .host = base.host, |
| 432 | 384 | .port = base.port, |
| 433 | 385 | .path = path, |
| ... | ... | @@ -437,7 +389,7 @@ pub fn resolve_inplace(base: Uri, new: []const u8, aux_buf: []u8) ResolveInplace |
| 437 | 389 | } |
| 438 | 390 | |
| 439 | 391 | /// In-place implementation of RFC 3986, Section 5.2.4. |
| 440 | | fn remove_dot_segments(path: []u8) []u8 { |
| 392 | fn remove_dot_segments(path: []u8) Component { |
| 441 | 393 | var in_i: usize = 0; |
| 442 | 394 | var out_i: usize = 0; |
| 443 | 395 | while (in_i < path.len) { |
| ... | ... | @@ -476,28 +428,28 @@ fn remove_dot_segments(path: []u8) []u8 { |
| 476 | 428 | } |
| 477 | 429 | } |
| 478 | 430 | } |
| 479 | | return path[0..out_i]; |
| 431 | return .{ .percent_encoded = path[0..out_i] }; |
| 480 | 432 | } |
| 481 | 433 | |
| 482 | 434 | test remove_dot_segments { |
| 483 | 435 | { |
| 484 | 436 | var buffer = "/a/b/c/./../../g".*; |
| 485 | | try std.testing.expectEqualStrings("/a/g", remove_dot_segments(&buffer)); |
| 437 | try std.testing.expectEqualStrings("/a/g", remove_dot_segments(&buffer).percent_encoded); |
| 486 | 438 | } |
| 487 | 439 | } |
| 488 | 440 | |
| 489 | 441 | /// 5.2.3. Merge Paths |
| 490 | | fn merge_paths(base: []const u8, new: []u8, aux: []u8) error{OutOfMemory}![]u8 { |
| 491 | | if (aux.len < base.len + 1 + new.len) return error.OutOfMemory; |
| 492 | | if (base.len == 0) { |
| 493 | | aux[0] = '/'; |
| 494 | | @memcpy(aux[1..][0..new.len], new); |
| 495 | | return remove_dot_segments(aux[0 .. new.len + 1]); |
| 442 | fn merge_paths(base: Component, new: []u8, aux_buf: *[]u8) error{NoSpaceLeft}!Component { |
| 443 | var aux = std.io.fixedBufferStream(aux_buf.*); |
| 444 | if (!base.isEmpty()) { |
| 445 | try aux.writer().print("{path}", .{base}); |
| 446 | aux.pos = std.mem.lastIndexOfScalar(u8, aux.getWritten(), '/') orelse |
| 447 | return remove_dot_segments(new); |
| 496 | 448 | } |
| 497 | | const pos = std.mem.lastIndexOfScalar(u8, base, '/') orelse return remove_dot_segments(new); |
| 498 | | @memcpy(aux[0 .. pos + 1], base[0 .. pos + 1]); |
| 499 | | @memcpy(aux[pos + 1 ..][0..new.len], new); |
| 500 | | return remove_dot_segments(aux[0 .. pos + 1 + new.len]); |
| 449 | try aux.writer().print("/{s}", .{new}); |
| 450 | const merged_path = remove_dot_segments(aux.getWritten()); |
| 451 | aux_buf.* = aux_buf.*[merged_path.percent_encoded.len..]; |
| 452 | return merged_path; |
| 501 | 453 | } |
| 502 | 454 | |
| 503 | 455 | const SliceReader = struct { |
| ... | ... | @@ -561,13 +513,6 @@ fn isSchemeChar(c: u8) bool { |
| 561 | 513 | }; |
| 562 | 514 | } |
| 563 | 515 | |
| 564 | | fn isAuthoritySeparator(c: u8) bool { |
| 565 | | return switch (c) { |
| 566 | | '/', '?', '#' => true, |
| 567 | | else => false, |
| 568 | | }; |
| 569 | | } |
| 570 | | |
| 571 | 516 | /// reserved = gen-delims / sub-delims |
| 572 | 517 | fn isReserved(c: u8) bool { |
| 573 | 518 | return isGenLimit(c) or isSubLimit(c); |
| ... | ... | @@ -598,19 +543,40 @@ fn isUnreserved(c: u8) bool { |
| 598 | 543 | }; |
| 599 | 544 | } |
| 600 | 545 | |
| 601 | | fn isPathSeparator(c: u8) bool { |
| 602 | | return switch (c) { |
| 603 | | '?', '#' => true, |
| 604 | | else => false, |
| 605 | | }; |
| 546 | fn isUserChar(c: u8) bool { |
| 547 | return isUnreserved(c) or isSubLimit(c); |
| 548 | } |
| 549 | |
| 550 | fn isPasswordChar(c: u8) bool { |
| 551 | return isUserChar(c) or c == ':'; |
| 552 | } |
| 553 | |
| 554 | fn isHostChar(c: u8) bool { |
| 555 | return isPasswordChar(c) or c == '[' or c == ']'; |
| 606 | 556 | } |
| 607 | 557 | |
| 608 | 558 | fn isPathChar(c: u8) bool { |
| 609 | | return isUnreserved(c) or isSubLimit(c) or c == '/' or c == ':' or c == '@'; |
| 559 | return isUserChar(c) or c == '/' or c == ':' or c == '@'; |
| 610 | 560 | } |
| 611 | 561 | |
| 612 | 562 | fn isQueryChar(c: u8) bool { |
| 613 | | return isPathChar(c) or c == '?' or c == '%'; |
| 563 | return isPathChar(c) or c == '?'; |
| 564 | } |
| 565 | |
| 566 | const isFragmentChar = isQueryChar; |
| 567 | |
| 568 | fn isAuthoritySeparator(c: u8) bool { |
| 569 | return switch (c) { |
| 570 | '/', '?', '#' => true, |
| 571 | else => false, |
| 572 | }; |
| 573 | } |
| 574 | |
| 575 | fn isPathSeparator(c: u8) bool { |
| 576 | return switch (c) { |
| 577 | '?', '#' => true, |
| 578 | else => false, |
| 579 | }; |
| 614 | 580 | } |
| 615 | 581 | |
| 616 | 582 | fn isQuerySeparator(c: u8) bool { |
| ... | ... | @@ -623,92 +589,92 @@ fn isQuerySeparator(c: u8) bool { |
| 623 | 589 | test "basic" { |
| 624 | 590 | const parsed = try parse("https://ziglang.org/download"); |
| 625 | 591 | try testing.expectEqualStrings("https", parsed.scheme); |
| 626 | | try testing.expectEqualStrings("ziglang.org", parsed.host orelse return error.UnexpectedNull); |
| 627 | | try testing.expectEqualStrings("/download", parsed.path); |
| 592 | try testing.expectEqualStrings("ziglang.org", parsed.host.?.percent_encoded); |
| 593 | try testing.expectEqualStrings("/download", parsed.path.percent_encoded); |
| 628 | 594 | try testing.expectEqual(@as(?u16, null), parsed.port); |
| 629 | 595 | } |
| 630 | 596 | |
| 631 | 597 | test "with port" { |
| 632 | 598 | const parsed = try parse("http://example:1337/"); |
| 633 | 599 | try testing.expectEqualStrings("http", parsed.scheme); |
| 634 | | try testing.expectEqualStrings("example", parsed.host orelse return error.UnexpectedNull); |
| 635 | | try testing.expectEqualStrings("/", parsed.path); |
| 600 | try testing.expectEqualStrings("example", parsed.host.?.percent_encoded); |
| 601 | try testing.expectEqualStrings("/", parsed.path.percent_encoded); |
| 636 | 602 | try testing.expectEqual(@as(?u16, 1337), parsed.port); |
| 637 | 603 | } |
| 638 | 604 | |
| 639 | 605 | test "should fail gracefully" { |
| 640 | | try std.testing.expectEqual(@as(ParseError!Uri, error.InvalidFormat), parse("foobar://")); |
| 606 | try std.testing.expectError(error.InvalidFormat, parse("foobar://")); |
| 641 | 607 | } |
| 642 | 608 | |
| 643 | 609 | test "file" { |
| 644 | 610 | const parsed = try parse("file:///"); |
| 645 | | try std.testing.expectEqualSlices(u8, "file", parsed.scheme); |
| 646 | | try std.testing.expectEqual(@as(?[]const u8, null), parsed.host); |
| 647 | | try std.testing.expectEqualSlices(u8, "/", parsed.path); |
| 611 | try std.testing.expectEqualStrings("file", parsed.scheme); |
| 612 | try std.testing.expectEqual(@as(?Component, null), parsed.host); |
| 613 | try std.testing.expectEqualStrings("/", parsed.path.percent_encoded); |
| 648 | 614 | |
| 649 | 615 | const parsed2 = try parse("file:///an/absolute/path/to/something"); |
| 650 | | try std.testing.expectEqualSlices(u8, "file", parsed2.scheme); |
| 651 | | try std.testing.expectEqual(@as(?[]const u8, null), parsed2.host); |
| 652 | | try std.testing.expectEqualSlices(u8, "/an/absolute/path/to/something", parsed2.path); |
| 616 | try std.testing.expectEqualStrings("file", parsed2.scheme); |
| 617 | try std.testing.expectEqual(@as(?Component, null), parsed2.host); |
| 618 | try std.testing.expectEqualStrings("/an/absolute/path/to/something", parsed2.path.percent_encoded); |
| 653 | 619 | |
| 654 | 620 | const parsed3 = try parse("file://localhost/an/absolute/path/to/another/thing/"); |
| 655 | | try std.testing.expectEqualSlices(u8, "file", parsed3.scheme); |
| 656 | | try std.testing.expectEqualSlices(u8, "localhost", parsed3.host.?); |
| 657 | | try std.testing.expectEqualSlices(u8, "/an/absolute/path/to/another/thing/", parsed3.path); |
| 621 | try std.testing.expectEqualStrings("file", parsed3.scheme); |
| 622 | try std.testing.expectEqualStrings("localhost", parsed3.host.?.percent_encoded); |
| 623 | try std.testing.expectEqualStrings("/an/absolute/path/to/another/thing/", parsed3.path.percent_encoded); |
| 658 | 624 | } |
| 659 | 625 | |
| 660 | 626 | test "scheme" { |
| 661 | | try std.testing.expectEqualSlices(u8, "http", (try parse("http:_")).scheme); |
| 662 | | try std.testing.expectEqualSlices(u8, "scheme-mee", (try parse("scheme-mee:_")).scheme); |
| 663 | | try std.testing.expectEqualSlices(u8, "a.b.c", (try parse("a.b.c:_")).scheme); |
| 664 | | try std.testing.expectEqualSlices(u8, "ab+", (try parse("ab+:_")).scheme); |
| 665 | | try std.testing.expectEqualSlices(u8, "X+++", (try parse("X+++:_")).scheme); |
| 666 | | try std.testing.expectEqualSlices(u8, "Y+-.", (try parse("Y+-.:_")).scheme); |
| 627 | try std.testing.expectEqualStrings("http", (try parse("http:_")).scheme); |
| 628 | try std.testing.expectEqualStrings("scheme-mee", (try parse("scheme-mee:_")).scheme); |
| 629 | try std.testing.expectEqualStrings("a.b.c", (try parse("a.b.c:_")).scheme); |
| 630 | try std.testing.expectEqualStrings("ab+", (try parse("ab+:_")).scheme); |
| 631 | try std.testing.expectEqualStrings("X+++", (try parse("X+++:_")).scheme); |
| 632 | try std.testing.expectEqualStrings("Y+-.", (try parse("Y+-.:_")).scheme); |
| 667 | 633 | } |
| 668 | 634 | |
| 669 | 635 | test "authority" { |
| 670 | | try std.testing.expectEqualSlices(u8, "hostname", (try parse("scheme://hostname")).host.?); |
| 636 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://hostname")).host.?.percent_encoded); |
| 671 | 637 | |
| 672 | | try std.testing.expectEqualSlices(u8, "hostname", (try parse("scheme://userinfo@hostname")).host.?); |
| 673 | | try std.testing.expectEqualSlices(u8, "userinfo", (try parse("scheme://userinfo@hostname")).user.?); |
| 674 | | try std.testing.expectEqual(@as(?[]const u8, null), (try parse("scheme://userinfo@hostname")).password); |
| 675 | | try std.testing.expectEqual(@as(?[]const u8, null), (try parse("scheme://userinfo@")).host); |
| 638 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://userinfo@hostname")).host.?.percent_encoded); |
| 639 | try std.testing.expectEqualStrings("userinfo", (try parse("scheme://userinfo@hostname")).user.?.percent_encoded); |
| 640 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://userinfo@hostname")).password); |
| 641 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://userinfo@")).host); |
| 676 | 642 | |
| 677 | | try std.testing.expectEqualSlices(u8, "hostname", (try parse("scheme://user:password@hostname")).host.?); |
| 678 | | try std.testing.expectEqualSlices(u8, "user", (try parse("scheme://user:password@hostname")).user.?); |
| 679 | | try std.testing.expectEqualSlices(u8, "password", (try parse("scheme://user:password@hostname")).password.?); |
| 643 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://user:password@hostname")).host.?.percent_encoded); |
| 644 | try std.testing.expectEqualStrings("user", (try parse("scheme://user:password@hostname")).user.?.percent_encoded); |
| 645 | try std.testing.expectEqualStrings("password", (try parse("scheme://user:password@hostname")).password.?.percent_encoded); |
| 680 | 646 | |
| 681 | | try std.testing.expectEqualSlices(u8, "hostname", (try parse("scheme://hostname:0")).host.?); |
| 647 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://hostname:0")).host.?.percent_encoded); |
| 682 | 648 | try std.testing.expectEqual(@as(u16, 1234), (try parse("scheme://hostname:1234")).port.?); |
| 683 | 649 | |
| 684 | | try std.testing.expectEqualSlices(u8, "hostname", (try parse("scheme://userinfo@hostname:1234")).host.?); |
| 650 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://userinfo@hostname:1234")).host.?.percent_encoded); |
| 685 | 651 | try std.testing.expectEqual(@as(u16, 1234), (try parse("scheme://userinfo@hostname:1234")).port.?); |
| 686 | | try std.testing.expectEqualSlices(u8, "userinfo", (try parse("scheme://userinfo@hostname:1234")).user.?); |
| 687 | | try std.testing.expectEqual(@as(?[]const u8, null), (try parse("scheme://userinfo@hostname:1234")).password); |
| 652 | try std.testing.expectEqualStrings("userinfo", (try parse("scheme://userinfo@hostname:1234")).user.?.percent_encoded); |
| 653 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://userinfo@hostname:1234")).password); |
| 688 | 654 | |
| 689 | | try std.testing.expectEqualSlices(u8, "hostname", (try parse("scheme://user:password@hostname:1234")).host.?); |
| 655 | try std.testing.expectEqualStrings("hostname", (try parse("scheme://user:password@hostname:1234")).host.?.percent_encoded); |
| 690 | 656 | try std.testing.expectEqual(@as(u16, 1234), (try parse("scheme://user:password@hostname:1234")).port.?); |
| 691 | | try std.testing.expectEqualSlices(u8, "user", (try parse("scheme://user:password@hostname:1234")).user.?); |
| 692 | | try std.testing.expectEqualSlices(u8, "password", (try parse("scheme://user:password@hostname:1234")).password.?); |
| 657 | try std.testing.expectEqualStrings("user", (try parse("scheme://user:password@hostname:1234")).user.?.percent_encoded); |
| 658 | try std.testing.expectEqualStrings("password", (try parse("scheme://user:password@hostname:1234")).password.?.percent_encoded); |
| 693 | 659 | } |
| 694 | 660 | |
| 695 | 661 | test "authority.password" { |
| 696 | | try std.testing.expectEqualSlices(u8, "username", (try parse("scheme://username@a")).user.?); |
| 697 | | try std.testing.expectEqual(@as(?[]const u8, null), (try parse("scheme://username@a")).password); |
| 662 | try std.testing.expectEqualStrings("username", (try parse("scheme://username@a")).user.?.percent_encoded); |
| 663 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://username@a")).password); |
| 698 | 664 | |
| 699 | | try std.testing.expectEqualSlices(u8, "username", (try parse("scheme://username:@a")).user.?); |
| 700 | | try std.testing.expectEqual(@as(?[]const u8, null), (try parse("scheme://username:@a")).password); |
| 665 | try std.testing.expectEqualStrings("username", (try parse("scheme://username:@a")).user.?.percent_encoded); |
| 666 | try std.testing.expectEqual(@as(?Component, null), (try parse("scheme://username:@a")).password); |
| 701 | 667 | |
| 702 | | try std.testing.expectEqualSlices(u8, "username", (try parse("scheme://username:password@a")).user.?); |
| 703 | | try std.testing.expectEqualSlices(u8, "password", (try parse("scheme://username:password@a")).password.?); |
| 668 | try std.testing.expectEqualStrings("username", (try parse("scheme://username:password@a")).user.?.percent_encoded); |
| 669 | try std.testing.expectEqualStrings("password", (try parse("scheme://username:password@a")).password.?.percent_encoded); |
| 704 | 670 | |
| 705 | | try std.testing.expectEqualSlices(u8, "username", (try parse("scheme://username::@a")).user.?); |
| 706 | | try std.testing.expectEqualSlices(u8, ":", (try parse("scheme://username::@a")).password.?); |
| 671 | try std.testing.expectEqualStrings("username", (try parse("scheme://username::@a")).user.?.percent_encoded); |
| 672 | try std.testing.expectEqualStrings(":", (try parse("scheme://username::@a")).password.?.percent_encoded); |
| 707 | 673 | } |
| 708 | 674 | |
| 709 | 675 | fn testAuthorityHost(comptime hostlist: anytype) !void { |
| 710 | 676 | inline for (hostlist) |hostname| { |
| 711 | | try std.testing.expectEqualSlices(u8, hostname, (try parse("scheme://" ++ hostname)).host.?); |
| 677 | try std.testing.expectEqualStrings(hostname, (try parse("scheme://" ++ hostname)).host.?.percent_encoded); |
| 712 | 678 | } |
| 713 | 679 | } |
| 714 | 680 | |
| ... | ... | @@ -761,11 +727,11 @@ test "RFC example 1" { |
| 761 | 727 | .scheme = uri[0..3], |
| 762 | 728 | .user = null, |
| 763 | 729 | .password = null, |
| 764 | | .host = uri[6..17], |
| 730 | .host = .{ .percent_encoded = uri[6..17] }, |
| 765 | 731 | .port = 8042, |
| 766 | | .path = uri[22..33], |
| 767 | | .query = uri[34..45], |
| 768 | | .fragment = uri[46..50], |
| 732 | .path = .{ .percent_encoded = uri[22..33] }, |
| 733 | .query = .{ .percent_encoded = uri[34..45] }, |
| 734 | .fragment = .{ .percent_encoded = uri[46..50] }, |
| 769 | 735 | }, try parse(uri)); |
| 770 | 736 | } |
| 771 | 737 | |
| ... | ... | @@ -777,7 +743,7 @@ test "RFC example 2" { |
| 777 | 743 | .password = null, |
| 778 | 744 | .host = null, |
| 779 | 745 | .port = null, |
| 780 | | .path = uri[4..], |
| 746 | .path = .{ .percent_encoded = uri[4..] }, |
| 781 | 747 | .query = null, |
| 782 | 748 | .fragment = null, |
| 783 | 749 | }, try parse(uri)); |
| ... | ... | @@ -838,55 +804,60 @@ test "Special test" { |
| 838 | 804 | _ = try parse("https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtu.be&t=0"); |
| 839 | 805 | } |
| 840 | 806 | |
| 841 | | test "URI escaping" { |
| 842 | | const input = "\\ö/ äöß ~~.adas-https://canvas:123/#ads&&sad"; |
| 843 | | const expected = "%5C%C3%B6%2F%20%C3%A4%C3%B6%C3%9F%20~~.adas-https%3A%2F%2Fcanvas%3A123%2F%23ads%26%26sad"; |
| 807 | test "URI percent encoding" { |
| 808 | try std.testing.expectFmt( |
| 809 | "%5C%C3%B6%2F%20%C3%A4%C3%B6%C3%9F%20~~.adas-https%3A%2F%2Fcanvas%3A123%2F%23ads%26%26sad", |
| 810 | "{%}", |
| 811 | .{Component{ .raw = "\\ö/ äöß ~~.adas-https://canvas:123/#ads&&sad" }}, |
| 812 | ); |
| 813 | } |
| 844 | 814 | |
| 845 | | const actual = try escapeString(std.testing.allocator, input); |
| 846 | | defer std.testing.allocator.free(actual); |
| 815 | test "URI percent decoding" { |
| 816 | { |
| 817 | const expected = "\\ö/ äöß ~~.adas-https://canvas:123/#ads&&sad"; |
| 818 | var input = "%5C%C3%B6%2F%20%C3%A4%C3%B6%C3%9F%20~~.adas-https%3A%2F%2Fcanvas%3A123%2F%23ads%26%26sad".*; |
| 847 | 819 | |
| 848 | | try std.testing.expectEqualSlices(u8, expected, actual); |
| 849 | | } |
| 820 | try std.testing.expectFmt(expected, "{raw}", .{Component{ .percent_encoded = &input }}); |
| 821 | |
| 822 | var output: [expected.len]u8 = undefined; |
| 823 | try std.testing.expectEqualStrings(percentDecodeBackwards(&output, &input), expected); |
| 824 | |
| 825 | try std.testing.expectEqualStrings(expected, percentDecodeInPlace(&input)); |
| 826 | } |
| 850 | 827 | |
| 851 | | test "URI unescaping" { |
| 852 | | const input = "%5C%C3%B6%2F%20%C3%A4%C3%B6%C3%9F%20~~.adas-https%3A%2F%2Fcanvas%3A123%2F%23ads%26%26sad"; |
| 853 | | const expected = "\\ö/ äöß ~~.adas-https://canvas:123/#ads&&sad"; |
| 828 | { |
| 829 | const expected = "/abc%"; |
| 830 | var input = expected.*; |
| 854 | 831 | |
| 855 | | const actual = try unescapeString(std.testing.allocator, input); |
| 856 | | defer std.testing.allocator.free(actual); |
| 832 | try std.testing.expectFmt(expected, "{raw}", .{Component{ .percent_encoded = &input }}); |
| 857 | 833 | |
| 858 | | try std.testing.expectEqualSlices(u8, expected, actual); |
| 834 | var output: [expected.len]u8 = undefined; |
| 835 | try std.testing.expectEqualStrings(percentDecodeBackwards(&output, &input), expected); |
| 859 | 836 | |
| 860 | | const decoded = try unescapeString(std.testing.allocator, "/abc%"); |
| 861 | | defer std.testing.allocator.free(decoded); |
| 862 | | try std.testing.expectEqualStrings("/abc%", decoded); |
| 837 | try std.testing.expectEqualStrings(expected, percentDecodeInPlace(&input)); |
| 838 | } |
| 863 | 839 | } |
| 864 | 840 | |
| 865 | | test "URI query escaping" { |
| 841 | test "URI query encoding" { |
| 866 | 842 | const address = "https://objects.githubusercontent.com/?response-content-type=application%2Foctet-stream"; |
| 867 | 843 | const parsed = try Uri.parse(address); |
| 868 | 844 | |
| 869 | | // format the URI to escape it |
| 870 | | const formatted_uri = try std.fmt.allocPrint(std.testing.allocator, "{/?}", .{parsed}); |
| 871 | | defer std.testing.allocator.free(formatted_uri); |
| 872 | | try std.testing.expectEqualStrings("/?response-content-type=application%2Foctet-stream", formatted_uri); |
| 845 | // format the URI to percent encode it |
| 846 | try std.testing.expectFmt("/?response-content-type=application%2Foctet-stream", "{/?}", .{parsed}); |
| 873 | 847 | } |
| 874 | 848 | |
| 875 | 849 | test "format" { |
| 876 | | const uri = Uri{ |
| 850 | const uri: Uri = .{ |
| 877 | 851 | .scheme = "file", |
| 878 | 852 | .user = null, |
| 879 | 853 | .password = null, |
| 880 | 854 | .host = null, |
| 881 | 855 | .port = null, |
| 882 | | .path = "/foo/bar/baz", |
| 856 | .path = .{ .raw = "/foo/bar/baz" }, |
| 883 | 857 | .query = null, |
| 884 | 858 | .fragment = null, |
| 885 | 859 | }; |
| 886 | | var buf = std.ArrayList(u8).init(std.testing.allocator); |
| 887 | | defer buf.deinit(); |
| 888 | | try buf.writer().print("{;/?#}", .{uri}); |
| 889 | | try std.testing.expectEqualSlices(u8, "file:/foo/bar/baz", buf.items); |
| 860 | try std.testing.expectFmt("file:/foo/bar/baz", "{;/?#}", .{uri}); |
| 890 | 861 | } |
| 891 | 862 | |
| 892 | 863 | test "URI malformed input" { |
| ... | ... | @@ -894,3 +865,7 @@ test "URI malformed input" { |
| 894 | 865 | try std.testing.expectError(error.InvalidFormat, std.Uri.parse("http://]@[")); |
| 895 | 866 | try std.testing.expectError(error.InvalidFormat, std.Uri.parse("http://lo]s\x85hc@[/8\x10?0Q")); |
| 896 | 867 | } |
| 868 | |
| 869 | const std = @import("std.zig"); |
| 870 | const testing = std.testing; |
| 871 | const Uri = @This(); |