URLComponents queryItems在变异时丢失百分比编码

时间:2017-12-21 20:23:30

标签: swift foundation nsurlcomponents

使用URLComponents queryItems时,我发现如果您的查询项的值包含一些百分比编码字符,则在我的情况下为/被编码为%2F,然后如果您从包含此类查询项的URLComponents URL构造String对象,则改变URLComponents对象的查询项列表,如果您尝试通过调用URL对象上的.url来获取URLComponents,则查询项会丢失其编码百分比。

这是我在游乐场测试过的代码:

import UIKit

// --- Part 1 ---
print("--- Part 1 ---\n")

let startURL = "https://test.com/test.jpg?X-Test-Token=FQdzEPH%2F%2F%2F"
var components = URLComponents(string: startURL)!

if let compURL = components.url {
    print(URL(string: startURL)! == compURL) // True
    print(startURL)
    print(compURL)
}

// --- Part 2 ---
print("\n--- Part 2 ---\n")

let startURLTwo = "https://test.com/test.jpg?X-Test-Token=FQdzEPH%2F%2F%2F"
let finalURL = "https://test.com/test.jpg?X-Test-Token=FQdzEPH%2F%2F%2F&foo=bar"
var componentsTwo = URLComponents(string: startURLTwo)!

let extraQueryItem = URLQueryItem(name: "foo", value: "bar")
componentsTwo.queryItems!.append(extraQueryItem)

if let compURLTwo = componentsTwo.url {
    print(URL(string: finalURL)! == compURLTwo) // False
    print(finalURL)
    print(compURLTwo)
}

这是一张图片,如果这样可以更容易理解正在发生的事情:

enter image description here

2 个答案:

答案 0 :(得分:1)

RFC 3986明确指出URL查询可能包含/字符。它不需要进行百分比编码。当您专门修改任何查询参数时,URLComponents只是遵循标准并将%2F解除为/

在第一种情况下,您根本不会修改任何内容,因此URL保持不变。在第二个中,您将修改组件的查询参数属性。所以URLComponents从更新的查询参数数组中构建一个新的查询字符串。在此过程中,如果对所有这些进行规范化,则会删除不必要的百分比编码。

答案 1 :(得分:1)

如果您的查询已经是百分比编码,则应使用percentEncodedQuery

let startURL = "https://test.com/test.jpg"
var components = URLComponents(string: startURL)!
components.percentEncodedQuery = "X-Test-Token=FQdzEPH%2F%2F%2F"

if let compURL = components.url {
    print(compURL)
}

或者您可以将其指定为非转义(并且将其保留为非转义状态,因为不必在查询中转义/个字符):

let startURL = "https://test.com/test.jpg"
var components = URLComponents(string: startURL)!
components.queryItems = [URLQueryItem(name: "X-Test-Token", value: "FQdzEPH///")]

if let compURL = components.url {
    print(compURL)
}

如果您需要更新queryItems,请务必在最后设置percentEncodedQuery

let startURL = "https://test.com/test.jpg"
let encodedQuery = "X-Test-Token=FQdzEPH%2F%2F%2F"
var components = URLComponents(string: startURL)!
components.queryItems = [URLQueryItem(name: "foo", value: "bar, baz, & qux")]
if let query = components.percentEncodedQuery {
    components.percentEncodedQuery = query + "&" + encodedQuery
} else {
    components.percentEncodedQuery = encodedQuery
}

if let compURL = components.url {
    print(compURL)
}