Skip to content

Commit

Permalink
Fix a path-escaping bug with replication
Browse files Browse the repository at this point in the history
Regression from the initial BrowserID patch (649d14f)
  • Loading branch information
snej committed Jan 17, 2013
1 parent 6375d6c commit 64fbf8e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
6 changes: 1 addition & 5 deletions Source/ChangeTracker/TDChangeTracker.m
Expand Up @@ -100,11 +100,7 @@ - (NSString*) changesFeedPath {
}

- (NSURL*) changesFeedURL {
NSMutableString* urlStr = [_databaseURL.absoluteString mutableCopy];
if (![urlStr hasSuffix: @"/"])
[urlStr appendString: @"/"];
[urlStr appendString: self.changesFeedPath];
return [NSURL URLWithString: urlStr];
return TDAppendToURL(_databaseURL, self.changesFeedPath);
}

- (NSString*) description {
Expand Down
3 changes: 3 additions & 0 deletions Source/TDMisc.h
Expand Up @@ -54,3 +54,6 @@ BOOL TDIsFileExistsError( NSError* error );

/** Returns the input URL without the query string or fragment identifier, just ending with the path. */
NSURL* TDURLWithoutQuery( NSURL* url );

/** Appends path components to a URL. These will NOT be URL-escaped, so you can include queries. */
NSURL* TDAppendToURL(NSURL* baseURL, NSString* toAppend);
12 changes: 12 additions & 0 deletions Source/TDMisc.m
Expand Up @@ -257,6 +257,18 @@ BOOL TDMayBeTransientError( NSError* error ) {
#endif
}


NSURL* TDAppendToURL(NSURL* baseURL, NSString* toAppend) {
if (toAppend.length == 0 || $equal(toAppend, @"."))
return baseURL;
NSMutableString* urlStr = baseURL.absoluteString.mutableCopy;
if (![urlStr hasSuffix: @"/"])
[urlStr appendString: @"/"];
[urlStr appendString: toAppend];
return [NSURL URLWithString: urlStr];
}


TestCase(TDQuoteString) {
CAssertEqual(TDQuoteString(@""), @"\"\"");
CAssertEqual(TDQuoteString(@"foo"), @"\"foo\"");
Expand Down
8 changes: 3 additions & 5 deletions Source/TDPuller.m
Expand Up @@ -347,20 +347,18 @@ - (void) pullRemoteRevision: (TD_Revision*)rev
// been added since the latest revisions we have locally.
// See: http://wiki.apache.org/couchdb/HTTP_Document_API#GET
// See: http://wiki.apache.org/couchdb/HTTP_Document_API#Getting_Attachments_With_a_Document
NSString* path = $sprintf(@"/%@?rev=%@&revs=true&attachments=true",
NSString* path = $sprintf(@"%@?rev=%@&revs=true&attachments=true",
TDEscapeID(rev.docID), TDEscapeID(rev.revID));
NSArray* knownRevs = [_db getPossibleAncestorRevisionIDs: rev limit: kMaxNumberOfAttsSince];
if (knownRevs.count > 0)
path = [path stringByAppendingFormat: @"&atts_since=%@", joinQuotedEscaped(knownRevs)];

LogTo(SyncVerbose, @"%@: GET .%@", self, path);
NSString* urlStr = [_remote.absoluteString stringByAppendingString: path];
LogTo(SyncVerbose, @"%@: GET %@", self, path);

// Under ARC, using variable dl directly in the block given as an argument to initWithURL:...
// results in compiler error (could be undefined variable)
__weak TDPuller *weakSelf = self;
__block TDMultipartDownloader *dl = nil;
dl = [[TDMultipartDownloader alloc] initWithURL: [NSURL URLWithString: urlStr]
dl = [[TDMultipartDownloader alloc] initWithURL: TDAppendToURL(_remote, path)
database: _db
requestHeaders: self.requestHeaders
onCompletion:
Expand Down
5 changes: 2 additions & 3 deletions Source/TDPusher.m
Expand Up @@ -332,10 +332,9 @@ - (BOOL) uploadMultipartRevision: (TD_Revision*)rev {
self.changesTotal++;
[self asyncTaskStarted];

NSString* path = $sprintf(@"/%@?new_edits=false", TDEscapeID(rev.docID));
NSString* urlStr = [_remote.absoluteString stringByAppendingString: path];
NSString* path = $sprintf(@"%@?new_edits=false", TDEscapeID(rev.docID));
__block TDMultipartUploader* uploader = [[TDMultipartUploader alloc]
initWithURL: [NSURL URLWithString: urlStr]
initWithURL: TDAppendToURL(_remote, path)
streamer: bodyStream
requestHeaders: self.requestHeaders
onCompletion: ^(id response, NSError *error) {
Expand Down
10 changes: 5 additions & 5 deletions Source/TDReplicator.m
Expand Up @@ -493,16 +493,16 @@ - (void) login {


- (TDRemoteJSONRequest*) sendAsyncRequest: (NSString*)method
path: (NSString*)relativePath
path: (NSString*)path
body: (id)body
onCompletion: (TDRemoteRequestCompletionBlock)onCompletion
{
LogTo(SyncVerbose, @"%@: %@ .%@", self, method, relativePath);
LogTo(SyncVerbose, @"%@: %@ %@", self, method, path);
NSURL* url;
if ([relativePath hasPrefix: @"/"]) {
url = [[NSURL URLWithString: relativePath relativeToURL: _remote] absoluteURL];
if ([path hasPrefix: @"/"]) {
url = [[NSURL URLWithString: path relativeToURL: _remote] absoluteURL];
} else {
url = [_remote URLByAppendingPathComponent: relativePath];
url = TDAppendToURL(_remote, path);
}
onCompletion = [onCompletion copy];

Expand Down

0 comments on commit 64fbf8e

Please sign in to comment.