My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//
// FMWebDAVRequest.m
// davtest
//
// Created by August Mueller on 8/7/08.
// Copyright 2008 Flying Meat Inc. All rights reserved.
//

#import "FMWebDAVRequest.h"
#import "FMFileDAVRequest.h"
#import "ISO8601DateFormatter.h"

@implementation FMWebDAVRequest

@synthesize connection=_connection;
@synthesize responseData=_responseData;
@synthesize url=_url;
@synthesize delegate=_delegate;
@synthesize contextInfo=_contextInfo;
@synthesize endSelector=_endSelector;
@synthesize responseStatusCode=_responseStatusCode;
@synthesize error=_error;

+ (id) requestToURL:(NSURL*)url {

FMWebDAVRequest *request = [url isFileURL] ? [[FMFileDAVRequest alloc] init] : [[FMWebDAVRequest alloc] init];;

[request setUrl:url];

return request;
}

+ (id) requestToURL:(NSURL*)url delegate:(id)del endSelector:(SEL)anEndSelector contextInfo:(id)context {

FMWebDAVRequest *request = [url isFileURL] ? [[FMFileDAVRequest alloc] init] : [[FMWebDAVRequest alloc] init];;

[request setUrl:url];
[request setDelegate:del];
[request setContextInfo:context];
[request setEndSelector:anEndSelector];

return request;
}

- (void)dealloc {
// _delegate isn't retained.

[_connection release];
[_responseData release];
[_url release];
[_contextInfo release];
[_xmlChars release];
[_directoryBucket release];

[super dealloc];
}

- (FMWebDAVRequest*) synchronous {
_synchronous = YES;
return [self autorelease];
}

- (void) sendRequest:(NSMutableURLRequest *)req {


// defaults write com.flyingmeat.VoodooPad_Pro FMWebDAVRequestDebug 1
// defaults delete com.flyingmeat.VoodooPad_Pro FMWebDAVRequestDebug

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FMWebDAVRequestDebug"]) {
NSData *d = [req HTTPBody];

if (d) {
NSString *junk = [[[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"%@", junk);
}
}



if (_synchronous) {
NSURLResponse *response = 0x00;

self.responseData = (NSMutableData*)[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&_error];

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;

if (![httpResponse isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(@"%s:%d", __FUNCTION__, __LINE__);
NSLog(@"FMWebDAVRequest Unknown response type: %@", httpResponse);
}
else {
_responseStatusCode = [httpResponse statusCode];
}
}
else {
[NSURLConnection connectionWithRequest:req delegate:self];
}

}

- (FMWebDAVRequest*) createDirectory {
if (!_endSelector) {
_endSelector = @selector(requestDidCreateDirectory:);
}

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:_url];

[req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[req setTimeoutInterval:60 * 5];

[req setHTTPMethod:@"MKCOL"];

// defaults write com.flyingmeat.VoodooPad_Pro skipMKCOLContentType 1
// defaults delete com.flyingmeat.VoodooPad_Pro skipMKCOLContentType
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"skipMKCOLContentType"]) {
[req setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
}

[self sendRequest:req];

return self;
}

- (FMWebDAVRequest*) delete {
if (!_endSelector) {
_endSelector = @selector(requestDidDelete:);
}

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:_url];

[req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[req setTimeoutInterval:60 * 5];

[req setHTTPMethod:@"DELETE"];

[req setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];

[self sendRequest:req];

return self;
}

- (FMWebDAVRequest*) putData:(NSData*)data {

if (!_endSelector) {
_endSelector = @selector(requestDidPutData:);
}

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:_url];

[req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[req setTimeoutInterval:60 * 5];

[req setHTTPMethod:@"PUT"];

[req setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];

// this actually speeds things up for some reason.
[req setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];

[req setHTTPBody:data];

[self sendRequest:req];

return self;
}

- (FMWebDAVRequest*) get {

if (!_endSelector) {
_endSelector = @selector(requestDidGet:);
}

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:_url];

[req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[req setTimeoutInterval:60 * 5];

[self sendRequest:req];

return self;
}

- (FMWebDAVRequest*) copyToDestinationURL:(NSURL*)dest {

if (!_endSelector) {
_endSelector = @selector(requestDidCopy:);
}

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:_url];

[req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[req setTimeoutInterval:60 * 5];

[req setHTTPMethod:@"COPY"];

[req setValue:[dest absoluteString] forHTTPHeaderField:@"Destination"];

[self sendRequest:req];

return self;
}

- (FMWebDAVRequest*) moveToDestinationURL:(NSURL*)dest {

if (!_endSelector) {
_endSelector = @selector(requestDidCopy:);
}

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:_url];

[req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[req setTimeoutInterval:60 * 5];

[req setHTTPMethod:@"MOVE"];

[req setValue:[dest absoluteString] forHTTPHeaderField:@"Destination"];

[self sendRequest:req];

return self;
}

- (FMWebDAVRequest*) head {

if (!_endSelector) {
_endSelector = @selector(requestDidHead:);
}

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:_url];

[req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[req setTimeoutInterval:60 * 5];

[req setHTTPMethod:@"HEAD"];

[self sendRequest:req];

return self;
}

- (FMWebDAVRequest*) propfind {

if (!_endSelector) {
_endSelector = @selector(requestDidPropfind:);
}

return [self fetchDirectoryListingWithDepth:0];
}


- (FMWebDAVRequest*) fetchDirectoryListing {
return [self fetchDirectoryListingWithDepth:1];
}

- (FMWebDAVRequest*) fetchDirectoryListingWithDepth:(NSUInteger)depth {
return [self fetchDirectoryListingWithDepth:depth extraToPropfind:@""];
}

// <D:prop><D:creationdate/></D:prop>
- (FMWebDAVRequest*) fetchDirectoryListingWithDepth:(NSUInteger)depth extraToPropfind:(NSString*)extra {

if (!_endSelector) {
_endSelector = @selector(requestDidFetchDirectoryListing:);
}

if (!extra) {
extra = @"";
}

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:_url];

[req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[req setTimeoutInterval:60 * 5];

// the trailing / always gets stripped off for some reason...
_uriLength = [[_url path] length] + 1;

[req setHTTPMethod:@"PROPFIND"];

NSString *xml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\"><D:allprop/>%@</D:propfind>", extra];

if (depth > 1) {
// http://tools.ietf.org/html/rfc2518#section-9.2
[req setValue:@"infinity" forHTTPHeaderField:@"Depth"];
}
else {
[req setValue:[NSString stringWithFormat:@"%d", depth] forHTTPHeaderField:@"Depth"];
}

[req setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];

[req setHTTPBody:[xml dataUsingEncoding:NSUTF8StringEncoding]];

[self sendRequest:req];

return self;
}

- (NSArray*) directoryListing {

NSMutableArray *ret = [NSMutableArray array];

for (NSDictionary *dict in [self directoryListingWithAttributes]) {
if ([dict objectForKey:@"href"]) {
[ret addObject:[dict objectForKey:@"href"]];
}
}

return ret;

}

- (NSArray*) directoryListingWithAttributes {

if (!_responseData) {
return nil;
}

_parseState = FMWebDAVDirectoryListing;
_directoryBucket = [[NSMutableArray array] retain];

NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:_responseData] autorelease];
[parser setDelegate:self];
[parser parse];

return _directoryBucket;
}

- (NSString*) responseString {
return [[[NSString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding] autorelease];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

if (!_responseData) {
[self setResponseData:[NSMutableData data]];
}

[_responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if (self.delegate && [self.delegate respondsToSelector:@selector(request:didReceiveAuthenticationChallenge:)]) {
[self.delegate request:self didReceiveAuthenticationChallenge:challenge];
}
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)URLresponse {

[_responseData setLength:0];

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)URLresponse;

if (![httpResponse isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(@"%s:%d", __FUNCTION__, __LINE__);
NSLog(@"Unknown response type: %@", URLresponse);
return;
}

_responseStatusCode = [httpResponse statusCode];

if (_responseStatusCode >= 400) {

if (self.delegate && [self.delegate respondsToSelector:@selector(request:hadStatusCodeErrorWithResponse:)]) {
[self.delegate request:self hadStatusCodeErrorWithResponse:httpResponse];
}
}
}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

// hrm... do we really want to do this?
if (self.delegate && [self.delegate respondsToSelector:@selector(connection:didFailWithError:)]) {
[self.delegate connection:connection didFailWithError:error];
}

// what about this?
if (self.delegate && [self.delegate respondsToSelector:_endSelector]) {
[self.delegate performSelector:_endSelector withObject:self];
}

[self autorelease];
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

if (self.delegate && [self.delegate respondsToSelector:_endSelector]) {
[self.delegate performSelector:_endSelector withObject:self];
}

[self autorelease];
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
//debug(@"start: %@", elementName);

if (!_xmlChars) {
_xmlChars = [[NSMutableString string] retain];
}

[_xmlChars setString:@""];


if (_parseState == FMWebDAVDirectoryListing) {

if ([elementName isEqualToString:@"D:response"]) {
_xmlBucket = [[NSMutableDictionary dictionary] retain];
}
// ...
}
}

+ (NSDate*) parseDateString:(NSString*)dateString {


ISO8601DateFormatter *formatter = [[[ISO8601DateFormatter alloc] init] autorelease];

NSDate *date = [formatter dateFromString:dateString];

if (!date) {
NSLog(@"Could not parse %@", dateString);
}

return date;

/*

NSLog(@"dateString: %@", dateString);

static NSDateFormatter* formatterA = nil;
if (!formatterA) {
formatterA = [[NSDateFormatter alloc] init];
[formatterA setTimeStyle:NSDateFormatterFullStyle];
[formatterA setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"]; // NOTE: problem! (but I'm not sure what that problem would be)
}
static NSDateFormatter* formatterB = nil;
if (!formatterB) {
formatterB = [[NSDateFormatter alloc] init];
[formatterB setTimeStyle:NSDateFormatterFullStyle];
[formatterB setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; // NOTE: problem! (but I'm not sure what that problem would be)
}


NSArray *formatters = [NSArray arrayWithObjects:formatterA, formatterB, nil];

for (NSDateFormatter *formatter in formatters) {

NSString *stringToWorkOn = dateString;

if ([stringToWorkOn hasSuffix:@"Z"]) {
NSLog(@"stripping the z");
stringToWorkOn = [[stringToWorkOn substringToIndex:(dateString.length-1)] stringByAppendingString:@"GMT"];
}

NSDate *d = [formatter dateFromString:stringToWorkOn];

if (!d) {
// 2009-06-30T02:46:53GM
NSLog(@"Initial parse failed");
}

if (!d && ![stringToWorkOn hasSuffix:@"GMT"]) {
stringToWorkOn = [stringToWorkOn stringByAppendingString:@"GMT"];
d = [formatter dateFromString:stringToWorkOn];
}

if (d) {
return d;
}
}


NSLog(@"Could not parse %@", dateString);

return nil;
*/
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
//debug(@"end: %@, '%@'", elementName, _xmlChars);

if (_parseState == FMWebDAVDirectoryListing) {
if ([elementName isEqualToString:@"D:href"]) {

if ([_xmlChars length] < _uriLength) {
// whoa, problemo.
return;
}

if ([_xmlChars hasPrefix:@"http"]) {
// aakkkk!
NSURL *junk = [NSURL URLWithString:_xmlChars];
[_xmlChars setString:[[junk path] stringByAppendingString:@"/"]];
}

NSString *lastBit = [_xmlChars substringFromIndex:_uriLength];
if ([lastBit length]) {
[_xmlBucket setObject:lastBit forKey:@"href"];
}
}
else if ([elementName hasSuffix:@":creationdate"] || [elementName hasSuffix:@":modificationdate"]) {

// 2009-06-30T02:46:53GMT
// '2008-10-30T02:52:47Z'
// 1997-12-01T17:42:21-08:00
// date-time = full-date "T" full-time, aka ISO-8601

// stolen from http://www.cocoabuilder.com/archive/message/cocoa/2008/3/18/201578

NSDate *d = [[self class] parseDateString:_xmlChars];

if (d) {

int colIdx = [elementName rangeOfString:@":"].location;

[_xmlBucket setObject:d forKey:[elementName substringFromIndex:colIdx + 1]];
}
else {
NSLog(@"Could not parse date string '%@' for '%@'", _xmlChars, elementName);
}

}

else if ([elementName hasSuffix:@":getlastmodified"]) {
// 'Thu, 30 Oct 2008 02:52:47 GMT'
// Monday, 12-Jan-98 09:25:56 GMT
// Value: HTTP-date ; defined in section 3.3.1 of RFC2068
// of course it's fucking different than creationdate.
//
// That makes complete sense.
//
// I thought for a while, that WebDAV was pretty sane. then I saw this.
// ok ok ok, it's not _that_ bad... but, really?
//
// obviously there's no code here to deal with it.
//
// I'll take a patch. kthx, bai now.
}
else if ([elementName isEqualToString:@"D:response"] && [_xmlBucket objectForKey:@"href"]) {
[_directoryBucket addObject:_xmlBucket];
[_xmlBucket release];
_xmlBucket = nil;
}


}

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[_xmlChars appendString:string];
}


@end

Change log

r154 by gusmueller on Dec 28, 2009   Diff
first stab at a file version of these
classes
Go to: 
Project members, sign in to write a code review

Older revisions

r153 by gusmueller on Oct 19, 2009   Diff
new method for propfind
r144 by gusmueller on Jun 30, 2009   Diff
auto
r105 by gusmueller on Feb 20, 2009   Diff
auto
All revisions of this file

File info

Size: 16495 bytes, 559 lines
Powered by Google Project Hosting