Skip to content

[SR-3329] URLQueryItems are not getting encoded in to the URL #719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions CoreFoundation/URL.subproj/CFURLComponents.c
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,8 @@ CF_EXPORT CFArrayRef _CFURLComponentsCopyQueryItems(CFURLComponentsRef component
else {
valueString = CFSTR("");
}
CFTypeRef keys[] = {CFSTR("name"), CFSTR("value")};
CFStringRef name = CFSTR("name");
CFTypeRef keys[] = {name, CFSTR("value")};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To work-around SR-2462 compiler bug.

CFTypeRef values[] = {nameString, valueString};
CFDictionaryRef entry = CFDictionaryCreate(kCFAllocatorSystemDefault, keys, values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFArrayAppendValue(intermediateResult, entry);
Expand All @@ -1097,7 +1098,8 @@ CF_EXPORT CFArrayRef _CFURLComponentsCopyQueryItems(CFURLComponentsRef component
else {
nameString = CFSTR("");
}
CFTypeRef keys[] = {CFSTR("name")};
CFStringRef name = CFSTR("name");
CFTypeRef keys[] = {name};
CFTypeRef values[] = {nameString};
CFDictionaryRef entry = CFDictionaryCreate(kCFAllocatorSystemDefault, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFArrayAppendValue(intermediateResult, entry);
Expand Down Expand Up @@ -1126,7 +1128,8 @@ CF_EXPORT CFArrayRef _CFURLComponentsCopyQueryItems(CFURLComponentsRef component
else {
valueString = CFSTR("");
}
CFTypeRef keys[] = {CFSTR("name"), CFSTR("value")};
CFStringRef name = CFSTR("name");
CFTypeRef keys[] = {name, CFSTR("value")};
CFTypeRef values[] = {nameString, valueString};
CFDictionaryRef entry = CFDictionaryCreate(kCFAllocatorSystemDefault, keys, values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFArrayAppendValue(intermediateResult, entry);
Expand All @@ -1148,7 +1151,8 @@ CF_EXPORT CFArrayRef _CFURLComponentsCopyQueryItems(CFURLComponentsRef component
else {
nameString = CFSTR("");
}
CFTypeRef keys[] = {CFSTR("name")};
CFStringRef name = CFSTR("name");
CFTypeRef keys[] = {name};
CFTypeRef values[] = {nameString};
CFDictionaryRef entry = CFDictionaryCreate(kCFAllocatorSystemDefault, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFArrayAppendValue(intermediateResult, entry);
Expand Down Expand Up @@ -1206,7 +1210,7 @@ CF_EXPORT void _CFURLComponentsSetQueryItems(CFURLComponentsRef components, CFAr
chars[0] = '=';
CFStringAppendCharactersToAppendBuffer(&buf, chars, 1);
CFStringRef stringWithPercentEncoding = _CFStringCreateByAddingPercentEncodingWithAllowedCharacters(kCFAllocatorSystemDefault, value, queryNameValueAllowed);
CFStringAppendStringToAppendBuffer(&buf, value);
CFStringAppendStringToAppendBuffer(&buf, stringWithPercentEncoding);
CFRelease(stringWithPercentEncoding);
}
// else the query item string will be simply "name"
Expand Down
5 changes: 3 additions & 2 deletions Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1233,8 +1233,9 @@ open class NSURLComponents: NSObject, NSCopying {

return (0..<count).map { idx in
let oneEntry = unsafeBitCast(CFArrayGetValueAtIndex(queryArray, idx), to: NSDictionary.self)
let entryName = oneEntry.object(forKey: "name"._cfObject) as! String
let entryValue = oneEntry.object(forKey: "value"._cfObject) as? String
let swiftEntry = oneEntry._swiftObject
let entryName = swiftEntry["name"] as! String
let entryValue = swiftEntry["value"] as? String
return URLQueryItem(name: entryName, value: entryValue)
}
} else {
Expand Down
19 changes: 18 additions & 1 deletion TestFoundation/TestNSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,8 @@ class TestNSURLComponents : XCTestCase {
("test_string", test_string),
("test_port", test_portSetter),
("test_url", test_url),
("test_copy", test_copy)
("test_copy", test_copy),
("test_createURLWithComponents", test_createURLWithComponents)
]
}

Expand Down Expand Up @@ -517,4 +518,20 @@ class TestNSURLComponents : XCTestCase {
/* Assert that NSURLComponents.copy is actually a copy of NSURLComponents */
XCTAssertTrue(copy.isEqual(urlComponent))
}

func test_createURLWithComponents() {
let urlComponents = NSURLComponents()
urlComponents.scheme = "https";
urlComponents.host = "com.test.swift";
urlComponents.path = "/test/path";
let date = Date()
let query1 = URLQueryItem(name: "date", value: date.description)
let query2 = URLQueryItem(name: "simpleDict", value: "false")
let query3 = URLQueryItem(name: "checkTest", value: "false")
let query4 = URLQueryItem(name: "someKey", value: "afsdjhfgsdkf^fhdjgf")
urlComponents.queryItems = [query1, query2, query3, query4]
XCTAssertNotNil(urlComponents.url?.query)
XCTAssertEqual(urlComponents.queryItems?.count, 4)
}

}