Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Escape troublesome characters in relativePath when returning Resource URL. #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions Couch/CouchDesignDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,29 @@ - (RESTOperation*) saveChanges {
}


#pragma mark - Overrides

- (NSURL *)URL {
NSArray *relativePathComponents = [self.relativePath pathComponents];
NSString *prefix = relativePathComponents[0];

// Init buffer with prefix and unescaped '/'
NSMutableString *escapedCompsBuffer;
escapedCompsBuffer = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"%@/", prefix]];

// Go over rest of components, accumulating after escaping them
for (int i = 1; i < [relativePathComponents count]; i++) {
NSString *comp = relativePathComponents[i];
comp = EscapeRelativePath(comp);
[escapedCompsBuffer appendString:comp];
}

// Add a forward slash, if it's missing, to the parent URL
NSURL *parentURL = [self.parent.URL URLByAppendingPathComponent:@""];
NSURL *URL = [NSURL URLWithString:escapedCompsBuffer relativeToURL:parentURL];

return URL;
}


@end
25 changes: 25 additions & 0 deletions Couch/CouchQuery.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,31 @@ - (CouchLiveQuery*) asLiveQuery {
}


#pragma mark - Overrides

- (NSURL *)URL {
NSArray *relativePathComponents = [self.relativePath pathComponents];
NSString *prefix = relativePathComponents[0];

// Init buffer with prefix and unescaped '/'
NSMutableString *escapedCompsBuffer;
escapedCompsBuffer = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"%@/", prefix]];

// Go over rest of components, accumulating after escaping them
for (int i = 1; i<[relativePathComponents count]; i++) {
NSString *comp = relativePathComponents[i];
comp = EscapeRelativePath(comp);
[escapedCompsBuffer appendString:comp];
}

// Add a forward slash, if it's missing, to the parent URL
NSURL *parentURL = [self.parent.URL URLByAppendingPathComponent:@""];
NSURL *URL = [NSURL URLWithString:escapedCompsBuffer relativeToURL:parentURL];

return URL;
}


@end


Expand Down
2 changes: 1 addition & 1 deletion REST/RESTInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void RESTWarn(NSString* format, ...) __attribute__((format(__NSString__, 1, 2)))
#define Warn RESTWarn

extern BOOL gRESTWarnRaisesException;

NSString *EscapeRelativePath(NSString *path);

// Safe dynamic cast that returns nil if the object is not the expected class:
#define $castIf(CLASSNAME,OBJ) ((CLASSNAME*)(RESTCastIf([CLASSNAME class],(OBJ))))
Expand Down
12 changes: 12 additions & 0 deletions REST/RESTInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ id RESTCastIf( Class requiredClass, id object )
return array;
}

NSString *EscapeRelativePath(NSString *path) {
/*
Escapes reserved URI characters.
RFC 3986 section 2.2 http://www.ietf.org/rfc/rfc3986.txt
*/
CFStringRef escapedPath = CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)path,
NULL,
(CFStringRef)@":/?#[]@!$&'()*+,;=",
kCFStringEncodingUTF8);
return [(NSString *)escapedPath autorelease];
}

@implementation NSArray (RESTExtensions)

Expand Down
13 changes: 8 additions & 5 deletions REST/RESTResource.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ - (NSString*) description {
}


- (NSURL*) URL {
if (_url)
- (NSURL *)URL {
if (_url) {
return _url;
else if (_relativePath)
return [_parent.URL URLByAppendingPathComponent: _relativePath];
else
} else if (_relativePath) {
// add a forward slash if it's missing
NSURL *parentURL = [_parent.URL URLByAppendingPathComponent:@""];
return [NSURL URLWithString:EscapeRelativePath(_relativePath) relativeToURL:parentURL];
} else {
return nil;
}
}


Expand Down