Skip to content

Commit

Permalink
Merge pull request #16 from d1r7y/lock_refresh
Browse files Browse the repository at this point in the history
Fake out lock refreshes.
  • Loading branch information
robbiehanson committed Mar 24, 2013
2 parents 0e63da2 + 2eaaaea commit 519fad7
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions Extensions/WebDAV/DAVResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ - (id) initWithMethod:(NSString*)method headers:(NSDictionary*)headers bodyData:
NSString* scope = nil;
NSString* type = nil;
NSString* owner = nil;
NSString* token = nil;
xmlDocPtr document = xmlReadMemory(body.bytes, (int)body.length, NULL, NULL, kXMLParseOptions);
if (document) {
xmlNodePtr node = _XMLChildWithName(document->children, (const xmlChar*)"lockinfo");
Expand All @@ -267,15 +268,26 @@ - (id) initWithMethod:(NSString*)method headers:(NSDictionary*)headers bodyData:
HTTPLogWarn(@"HTTP Server: Invalid DAV properties\n%@", [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]);
}
xmlFreeDoc(document);
}
} else {
// No body, see if they're trying to refresh an existing lock. If so, then just fake up the scope, type and depth so we fall
// into the lock create case.
NSString* lockToken;
if ((lockToken = [headers objectForKey:@"If"]) != nil) {
scope = @"exclusive";
type = @"write";
depth = @"0";
token = [lockToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"(<>)"]];
}
}
if ([scope isEqualToString:@"exclusive"] && [type isEqualToString:@"write"] && [depth isEqualToString:@"0"] &&
([[NSFileManager defaultManager] fileExistsAtPath:path] || [[NSData data] writeToFile:path atomically:YES])) {
NSString* timeout = [headers objectForKey:@"Timeout"];

CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
NSString* token = [NSString stringWithFormat:@"urn:uuid:%@", uuidStr];
CFRelease(uuid);
if (!token) {
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
token = [NSString stringWithFormat:@"urn:uuid:%@", uuidStr];
CFRelease(uuid);
}

NSMutableString* xmlString = [NSMutableString stringWithString:@"<?xml version=\"1.0\" encoding=\"utf-8\" ?>"];
[xmlString appendString:@"<D:prop xmlns:D=\"DAV:\">\n"];
Expand All @@ -290,7 +302,8 @@ - (id) initWithMethod:(NSString*)method headers:(NSDictionary*)headers bodyData:
[xmlString appendFormat:@"<D:timeout>%@</D:timeout>\n", timeout];
}
[xmlString appendFormat:@"<D:locktoken><D:href>%@</D:href></D:locktoken>\n", token];
// [xmlString appendFormat:@"<D:lockroot><D:href>%@</D:href></D:lockroot>\n", root];
NSString* lockroot = [@"http://" stringByAppendingString:[[headers objectForKey:@"Host"] stringByAppendingString:[@"/" stringByAppendingString:resourcePath]]];
[xmlString appendFormat:@"<D:lockroot><D:href>%@</D:href></D:lockroot>\n", lockroot];
[xmlString appendString:@"</D:activelock>\n</D:lockdiscovery>\n"];
[xmlString appendString:@"</D:prop>"];

Expand Down

0 comments on commit 519fad7

Please sign in to comment.