| ... | @@ -197,12 +197,7 @@ pub fn percentDecodeInPlace(buffer: []u8) []u8 { | ... | @@ -197,12 +197,7 @@ pub fn percentDecodeInPlace(buffer: []u8) []u8 { |
| 197 | return percentDecodeBackwards(buffer, buffer); | 197 | return percentDecodeBackwards(buffer, buffer); |
| 198 | } | 198 | } |
| 199 | | 199 | |
| 200 | pub const ParseError = error{ | 200 | pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort, InvalidHostName }; |
| 201 | UnexpectedCharacter, | | |
| 202 | InvalidFormat, | | |
| 203 | InvalidPort, | | |
| 204 | InvalidHostName, | | |
| 205 | }; | | |
| 206 | | 201 | |
| 207 | /// Parses the URI or returns an error. This function is not compliant, but is required to parse | 202 | /// Parses the URI or returns an error. This function is not compliant, but is required to parse |
| 208 | /// some forms of URIs in the wild, such as HTTP Location headers. | 203 | /// some forms of URIs in the wild, such as HTTP Location headers. |
| ... | @@ -264,7 +259,9 @@ pub fn parseAfterScheme(scheme: []const u8, text: []const u8) ParseError!Uri { | ... | @@ -264,7 +259,9 @@ pub fn parseAfterScheme(scheme: []const u8, text: []const u8) ParseError!Uri { |
| 264 | } | 259 | } |
| 265 | | 260 | |
| 266 | if (start_of_host >= end_of_host) return error.InvalidFormat; | 261 | if (start_of_host >= end_of_host) return error.InvalidFormat; |
| 267 | uri.host = .{ .percent_encoded = authority[start_of_host..end_of_host] }; | 262 | const host = authority[start_of_host..end_of_host]; |
| | 263 | if (host.len > HostName.max_len) return error.InvalidHostName; |
| | 264 | uri.host = .{ .percent_encoded = host }; |
| 268 | } | 265 | } |
| 269 | | 266 | |
| 270 | const path_start = i; | 267 | const path_start = i; |
| ... | @@ -594,6 +591,11 @@ test "should fail gracefully" { | ... | @@ -594,6 +591,11 @@ test "should fail gracefully" { |
| 594 | try std.testing.expectError(error.InvalidFormat, parse("foobar://")); | 591 | try std.testing.expectError(error.InvalidFormat, parse("foobar://")); |
| 595 | } | 592 | } |
| 596 | | 593 | |
| | 594 | test "parse name too long" { |
| | 595 | const uri = "http://" ++ @as([HostName.max_len + 1]u8, @splat('Z')); |
| | 596 | try std.testing.expectError(error.InvalidHostName, parse(uri)); |
| | 597 | } |
| | 598 | |
| 597 | test "file" { | 599 | test "file" { |
| 598 | const parsed = try parse("file:///"); | 600 | const parsed = try parse("file:///"); |
| 599 | try std.testing.expectEqualStrings("file", parsed.scheme); | 601 | try std.testing.expectEqualStrings("file", parsed.scheme); |