Permalink
Browse files

Change tracker needs to send cookies in its request

TDSocketChangeTracker uses CFHTTPMessageCreateRequest, which is too low-level to use the shared
cookie store, so it doesn't send any cookies. This breaks BrowserID auth, which is session-based.
Fixed this by explicitly getting the cookies from the NSHTTPCookieStore and adding their headers.
  • Loading branch information...
snej committed Jan 13, 2013
1 parent 5d27889 commit 9917fe728730fa815fd6eb766d2497ae70b16221
Showing with 19 additions and 7 deletions.
  1. +16 −4 Source/ChangeTracker/TDSocketChangeTracker.m
  2. +3 −3 Source/TDReplicator.m
@@ -39,17 +39,28 @@ - (BOOL) start {
LogTo(ChangeTracker, @"%@: Starting...", self);
[super start];
-
+
+ NSURL* url = self.changesFeedURL;
CFHTTPMessageRef request = CFHTTPMessageCreateRequest(NULL, CFSTR("GET"),
- (__bridge CFURLRef)self.changesFeedURL,
+ (__bridge CFURLRef)url,
kCFHTTPVersion1_1);
Assert(request);
- // Add headers.
+ // Add headers from my .requestHeaders property:
[self.requestHeaders enumerateKeysAndObjectsUsingBlock: ^(id key, id value, BOOL *stop) {
CFHTTPMessageSetHeaderFieldValue(request, (__bridge CFStringRef)key, (__bridge CFStringRef)value);
}];
-
+
+ // Add cookie headers from the NSHTTPCookieStorage:
+ NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL: url];
+ NSDictionary* cookieHeaders = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];
+ for (NSString* headerName in cookieHeaders) {
+ CFHTTPMessageSetHeaderFieldValue(request,
+ (__bridge CFStringRef)headerName,
+ (__bridge CFStringRef)cookieHeaders[headerName]);
+ }
+
+ // If this is a retry, set auth headers from the credential we got:
if (_unauthResponse && _credential) {
NSString* password = _credential.password;
if (!password) {
@@ -80,6 +91,7 @@ - (BOOL) start {
(__bridge CFStringRef)(authHeader));
}
+ // Now open the connection:
CFReadStreamRef cfInputStream = CFReadStreamCreateForHTTPRequest(NULL, request);
CFRelease(request);
if (!cfInputStream)
View
@@ -434,17 +434,17 @@ - (void) login {
if ([_authorizer respondsToSelector: @selector(loginParameters)]) {
NSDictionary* loginParameters = _authorizer.loginParameters;
if (loginParameters != nil) {
- LogTo(Sync, @"Logging in with %@ at %@ ...", _authorizer.class, _authorizer.loginPath);
+ LogTo(Sync, @"%@: Logging in with %@ at %@ ...", self, _authorizer.class, _authorizer.loginPath);
[self asyncTaskStarted];
[self sendAsyncRequest: @"POST"
path: _authorizer.loginPath
body: _authorizer.loginParameters
onCompletion: ^(id result, NSError *error) {
if (error) {
- LogTo(Sync, @"Login failed!");
+ LogTo(Sync, @"%@: Login failed!", self);
self.error = error;
} else {
- LogTo(Sync, @"Successfully logged in!");
+ LogTo(Sync, @"%@: Successfully logged in!", self);
[self fetchRemoteCheckpointDoc];
}
[self asyncTasksFinished: 1];

0 comments on commit 9917fe7

Please sign in to comment.