Skip to content

Commit 807bd72

Browse files
saiHemak“saiHemak”
authored andcommitted
[SR-3329] URLQueryItems are not getting encoded in to the URL
1 parent afd53fc commit 807bd72

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

CoreFoundation/URL.subproj/CFURLComponents.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,8 @@ CF_EXPORT CFArrayRef _CFURLComponentsCopyQueryItems(CFURLComponentsRef component
10731073
else {
10741074
valueString = CFSTR("");
10751075
}
1076-
CFTypeRef keys[] = {CFSTR("name"), CFSTR("value")};
1076+
CFStringRef name = CFSTR("name");
1077+
CFTypeRef keys[] = {name, CFSTR("value")};
10771078
CFTypeRef values[] = {nameString, valueString};
10781079
CFDictionaryRef entry = CFDictionaryCreate(kCFAllocatorSystemDefault, keys, values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
10791080
CFArrayAppendValue(intermediateResult, entry);
@@ -1097,7 +1098,8 @@ CF_EXPORT CFArrayRef _CFURLComponentsCopyQueryItems(CFURLComponentsRef component
10971098
else {
10981099
nameString = CFSTR("");
10991100
}
1100-
CFTypeRef keys[] = {CFSTR("name")};
1101+
CFStringRef name = CFSTR("name");
1102+
CFTypeRef keys[] = {name};
11011103
CFTypeRef values[] = {nameString};
11021104
CFDictionaryRef entry = CFDictionaryCreate(kCFAllocatorSystemDefault, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
11031105
CFArrayAppendValue(intermediateResult, entry);
@@ -1126,7 +1128,8 @@ CF_EXPORT CFArrayRef _CFURLComponentsCopyQueryItems(CFURLComponentsRef component
11261128
else {
11271129
valueString = CFSTR("");
11281130
}
1129-
CFTypeRef keys[] = {CFSTR("name"), CFSTR("value")};
1131+
CFStringRef name = CFSTR("name");
1132+
CFTypeRef keys[] = {name, CFSTR("value")};
11301133
CFTypeRef values[] = {nameString, valueString};
11311134
CFDictionaryRef entry = CFDictionaryCreate(kCFAllocatorSystemDefault, keys, values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
11321135
CFArrayAppendValue(intermediateResult, entry);
@@ -1148,7 +1151,8 @@ CF_EXPORT CFArrayRef _CFURLComponentsCopyQueryItems(CFURLComponentsRef component
11481151
else {
11491152
nameString = CFSTR("");
11501153
}
1151-
CFTypeRef keys[] = {CFSTR("name")};
1154+
CFStringRef name = CFSTR("name");
1155+
CFTypeRef keys[] = {name};
11521156
CFTypeRef values[] = {nameString};
11531157
CFDictionaryRef entry = CFDictionaryCreate(kCFAllocatorSystemDefault, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
11541158
CFArrayAppendValue(intermediateResult, entry);
@@ -1206,7 +1210,7 @@ CF_EXPORT void _CFURLComponentsSetQueryItems(CFURLComponentsRef components, CFAr
12061210
chars[0] = '=';
12071211
CFStringAppendCharactersToAppendBuffer(&buf, chars, 1);
12081212
CFStringRef stringWithPercentEncoding = _CFStringCreateByAddingPercentEncodingWithAllowedCharacters(kCFAllocatorSystemDefault, value, queryNameValueAllowed);
1209-
CFStringAppendStringToAppendBuffer(&buf, value);
1213+
CFStringAppendStringToAppendBuffer(&buf, stringWithPercentEncoding);
12101214
CFRelease(stringWithPercentEncoding);
12111215
}
12121216
// else the query item string will be simply "name"

Foundation/NSURL.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,8 +1233,9 @@ open class NSURLComponents: NSObject, NSCopying {
12331233

12341234
return (0..<count).map { idx in
12351235
let oneEntry = unsafeBitCast(CFArrayGetValueAtIndex(queryArray, idx), to: NSDictionary.self)
1236-
let entryName = oneEntry.object(forKey: "name"._cfObject) as! String
1237-
let entryValue = oneEntry.object(forKey: "value"._cfObject) as? String
1236+
let swiftEntry = oneEntry._swiftObject
1237+
let entryName = swiftEntry["name"] as! String
1238+
let entryValue = swiftEntry["value"] as? String
12381239
return URLQueryItem(name: entryName, value: entryValue)
12391240
}
12401241
} else {

TestFoundation/TestNSURL.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ class TestNSURLComponents : XCTestCase {
439439
("test_string", test_string),
440440
("test_port", test_portSetter),
441441
("test_url", test_url),
442-
("test_copy", test_copy)
442+
("test_copy", test_copy),
443+
("test_createURLWithComponents", test_createURLWithComponents)
443444
]
444445
}
445446

@@ -517,4 +518,20 @@ class TestNSURLComponents : XCTestCase {
517518
/* Assert that NSURLComponents.copy is actually a copy of NSURLComponents */
518519
XCTAssertTrue(copy.isEqual(urlComponent))
519520
}
521+
522+
func test_createURLWithComponents() {
523+
let urlComponents = NSURLComponents()
524+
urlComponents.scheme = "https";
525+
urlComponents.host = "com.test.swift";
526+
urlComponents.path = "/test/path";
527+
let date = Date()
528+
let query1 = URLQueryItem(name: "date", value: date.description)
529+
let query2 = URLQueryItem(name: "simpleDict", value: "false")
530+
let query3 = URLQueryItem(name: "checkTest", value: "false")
531+
let query4 = URLQueryItem(name: "someKey", value: "afsdjhfgsdkf^fhdjgf")
532+
urlComponents.queryItems = [query1, query2, query3, query4]
533+
XCTAssertNotNil(urlComponents.url?.query)
534+
XCTAssertEqual(urlComponents.queryItems?.count, 4)
535+
}
536+
520537
}

0 commit comments

Comments
 (0)