Skip to content

Commit

Permalink
Merge pull request #9 from SwiftPackageIndex/handle-malformed-urls
Browse files Browse the repository at this point in the history
Handle malformed urls
  • Loading branch information
finestructure authored Dec 29, 2023
2 parents 1588003 + 50dff14 commit 0652109
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/CanonicalPackageURL/CanonicalPackageURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ extension CanonicalPackageURL {
extension CanonicalPackageURL {
static var hostname: some Parser<Substring, String> {
Parse {
Skip {
// skip leading slashes that might be left over from parsing malformed http:////host input
Optionally { Many { "/" } }
}
OneOf {
PrefixUpTo(":")
PrefixUpTo("/")
Expand Down
16 changes: 16 additions & 0 deletions Tests/CanonicalPackageURLTests/CanonicalPackageURLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ final class CanonicalPackageURLsTests: XCTestCase {
.init(prefix: .https, hostname: "gitee.com", path: "zexu007/Kingfisher"))
}

func test_parser_malformedInput() throws {
// triple slash
XCTAssertEqual(try CanonicalPackageURL.parser.parse("https:///github.com/apple/swift-system.git"),
.init(prefix: .https, hostname: "github.com", path: "apple/swift-system"))
XCTAssertEqual(try CanonicalPackageURL.parser.parse("http:///github.com/apple/swift-system.git"),
.init(prefix: .http, hostname: "github.com", path: "apple/swift-system"))
// quadruple slash
XCTAssertEqual(try CanonicalPackageURL.parser.parse("https:////github.com/apple/swift-system.git"),
.init(prefix: .https, hostname: "github.com", path: "apple/swift-system"))
XCTAssertEqual(try CanonicalPackageURL.parser.parse("http:////github.com/apple/swift-system.git"),
.init(prefix: .http, hostname: "github.com", path: "apple/swift-system"))
// bad (but salvageable) git@ url
XCTAssertEqual(try CanonicalPackageURL.parser.parse("git@/github.com/apple/swift-system.git"),
.init(prefix: .gitAt, hostname: "github.com", path: "apple/swift-system"))
}

}

0 comments on commit 0652109

Please sign in to comment.