You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by sh...@apache.org on 2012/07/11 23:29:01 UTC

ios commit: Properly escape URLs within FileTransfer that end with slash.

Updated Branches:
  refs/heads/master dea6eb24d -> 3d2487443


Properly escape URLs within FileTransfer that end with slash.

This rewrites the escaping function to use a regular expression instead.
I also added a test for the function.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/3d248744
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/3d248744
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/3d248744

Branch: refs/heads/master
Commit: 3d2487443a918b7f1d9b9e415e2a140d48185c83
Parents: dea6eb2
Author: Andrew Grieve <ag...@chromium.org>
Authored: Wed Jul 11 15:32:42 2012 -0400
Committer: Shazron Abdullah <sh...@apache.org>
Committed: Wed Jul 11 14:28:48 2012 -0700

----------------------------------------------------------------------
 CordovaLib/Classes/CDVFileTransfer.m              |   25 ++++++----------
 CordovaLib/CordovaLibTests/CDVFileTransferTests.m |   13 ++++++++
 2 files changed, 22 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/3d248744/CordovaLib/Classes/CDVFileTransfer.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVFileTransfer.m b/CordovaLib/Classes/CDVFileTransfer.m
index b7c16d0..fcb200a 100644
--- a/CordovaLib/Classes/CDVFileTransfer.m
+++ b/CordovaLib/Classes/CDVFileTransfer.m
@@ -61,25 +61,17 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) {
 
 - (NSString*) escapePathComponentForUrlString:(NSString*)urlString
 {
-    // separate the scheme and location components
-    NSArray* schemeAndLocationComponents = [urlString componentsSeparatedByString:@"://"];
-    if ([schemeAndLocationComponents count] < 2) {
+    NSRange schemeAndHostRange = [urlString rangeOfString:@"://.*?/" options:NSRegularExpressionSearch];
+    if (schemeAndHostRange.length == 0) {
         return urlString;
     }
-    
-    // separate the domain and path components
-    NSArray* pathComponents = [[schemeAndLocationComponents lastObject] componentsSeparatedByString:@"/"];
-    if ([pathComponents count] < 2) {
-        return urlString;
-    }
-    
-    NSString* pathComponent = [pathComponents lastObject];
-    NSRange rangeOfSubstring = [urlString rangeOfString:pathComponent];
-    urlString = [urlString substringToIndex:rangeOfSubstring.location];
-    
+
+    NSInteger schemeAndHostEndIndex = NSMaxRange(schemeAndHostRange);
+    NSString* schemeAndHost = [urlString substringToIndex:schemeAndHostEndIndex];
+    NSString* pathComponent = [urlString substringFromIndex:schemeAndHostEndIndex];
     pathComponent = [pathComponent stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     
-    return [urlString stringByAppendingString:pathComponent];
+    return [schemeAndHost stringByAppendingString:pathComponent];
 }
 
 - (NSURLRequest*) requestForUpload:(NSArray*)arguments withDict:(NSDictionary*)options fileData:(NSData*)fileData {
@@ -107,7 +99,8 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) {
     CDVPluginResult* result = nil;
     CDVFileTransferError errorCode = 0;
 
-    
+    // NSURL does not accepts URLs with spaces in the path. We escape the path in order
+    // to be more lenient.
     NSURL *url = [NSURL URLWithString:[self escapePathComponentForUrlString:server]];
     
     if (!url) {

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/3d248744/CordovaLib/CordovaLibTests/CDVFileTransferTests.m
----------------------------------------------------------------------
diff --git a/CordovaLib/CordovaLibTests/CDVFileTransferTests.m b/CordovaLib/CordovaLibTests/CDVFileTransferTests.m
index cfdfa48..c0817f5 100644
--- a/CordovaLib/CordovaLibTests/CDVFileTransferTests.m
+++ b/CordovaLib/CordovaLibTests/CDVFileTransferTests.m
@@ -110,6 +110,19 @@ static NSData* readStream(NSInputStream* stream) {
     STAssertEquals([payloadData length], contentLength, nil);
 }
 
+- (void)testEscapePathComponentForUrlString {
+    STAssertTrue([@"" isEqualToString:
+        [_fileTransfer escapePathComponentForUrlString:@""]], nil);
+    STAssertTrue([@"foo" isEqualToString:
+        [_fileTransfer escapePathComponentForUrlString:@"foo"]], nil);
+    STAssertTrue([@"http://a.org/spa%20ce%25" isEqualToString:
+        [_fileTransfer escapePathComponentForUrlString:@"http://a.org/spa ce%"]], nil);
+    STAssertTrue([@"http://a.org/spa%20ce%25/" isEqualToString:
+        [_fileTransfer escapePathComponentForUrlString:@"http://a.org/spa ce%/"]], nil);
+    STAssertTrue([@"http://a.org/%25/%25/" isEqualToString:
+        [_fileTransfer escapePathComponentForUrlString:@"http://a.org/%/%/"]], nil);
+}
+
 - (void)testUpload_invalidServerUrl
 {
     [self setServerUrlArg:@"invalid url"];