Registry: don't loose query parameters on URL absolutization (#47)

* Registry: don't loose query parameters on URL absolutization

* Registry: throw a proper exception when Location header parsing fails
This commit is contained in:
Nikolay Edigaryev 2022-05-06 16:03:58 +03:00 committed by GitHub
parent 29e08220e4
commit a4db60d656
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 9 deletions

View File

@ -61,15 +61,11 @@ class Registry {
throw RegistryError.MissingLocationHeader
}
var uploadLocation = URL(string: uploadLocationRaw)!
// If the URL provided in the Location header
// is relative make it absolute.
if uploadLocation.absoluteString == uploadLocation.relativeString {
uploadLocation = URL(string: uploadLocation.path, relativeTo: baseURL)!
guard let uploadLocation = URL(string: uploadLocationRaw) else {
throw RegistryError.MalformedHeader(why: "Location header contains invalid URL: \"\(uploadLocationRaw)\"")
}
return URLComponents(url: uploadLocation, resolvingAgainstBaseURL: true)!
return URLComponents(url: uploadLocation.absolutize(baseURL), resolvingAgainstBaseURL: true)!
}
public func pushBlob(fromData: Data, chunkSize: Int = 5 * 1024 * 1024) async throws -> String {
@ -137,8 +133,8 @@ class Registry {
) async throws -> (Data, HTTPURLResponse) {
var urlComponents = urlComponents
if !parameters.isEmpty {
urlComponents.queryItems = Array()
if urlComponents.queryItems == nil {
urlComponents.queryItems = []
}
urlComponents.queryItems?.append(contentsOf: parameters.map { key, value -> URLQueryItem in
URLQueryItem(name: key, value: value)

View File

@ -0,0 +1,7 @@
import Foundation
extension URL {
func absolutize(_ baseURL: URL) -> Self {
URL(string: absoluteString, relativeTo: baseURL)!
}
}

View File

@ -0,0 +1,18 @@
import XCTest
@testable import tart
final class URLAbsolutizationTets: XCTestCase {
func testNeedsAbsolutization() throws {
let url = URL(string: "/v2/some/path?some=query")!
.absolutize(URL(string: "https://example.com/v2/")!)
XCTAssertEqual(url.absoluteString, "https://example.com/v2/some/path?some=query")
}
func testDoesntNeedAbsolutization() throws {
let url = URL(string: "https://example.org/v2/some/path?some=query")!
.absolutize(URL(string: "https://example.com/v2/")!)
XCTAssertEqual(url.absoluteString, "https://example.org/v2/some/path?some=query")
}
}