-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebDAV.m
More file actions
345 lines (270 loc) · 9.46 KB
/
WebDAV.m
File metadata and controls
345 lines (270 loc) · 9.46 KB
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
//
// WebDAV.m
// MobileMe
//
// Created by Ryan Detzel on 1/12/09.
// Copyright 2009 Fifth Floor Media. All rights reserved.
//
#import "WebDAV.h"
@interface WebDAV (Private)
-(void)listDirSuccess:(NSMutableArray *)dirList;
-(void)listDirFailed:(NSString *)error;
-(void)makeDirFailed:(NSString *)error;
-(void)makeDirSuccess;
-(void)uploadFileFailed:(NSString *)error;
-(void)uploadFileSuccess;
-(void)uploadDataFailed:(NSString *)error;
-(void)uploadDataSuccess;
@end
@implementation WebDAV
@synthesize username,password,globalTimeout,connection,pendingChallenge;
@synthesize incomingData, documentURL;
static NSString *kURLTemplate = @"https://idisk.me.com/%@";
-(id)init{
self = [super init];
self.connection = nil;
self.pendingChallenge = nil;
self.incomingData = [[NSMutableData alloc] init];
self.globalTimeout = 15;
return self;
}
-(id)initWithUsername:(NSString *)u password:(NSString *)p{
self = [self init];
self.username = u;
self.password = p;
return self;
}
- (void)setDelegate:(id)val{
delegate = val;
}
- (id)delegate{
return delegate;
}
-(NSString *)buildURL{
return [NSString stringWithFormat:kURLTemplate,self.username];
}
-(void)setup:(NSString *)u password:(NSString *)p{
if (self.pendingChallenge != nil) {
if (p != nil) {
NSURLCredential *cred;
NSURLCredentialPersistence credPersist;
credPersist = NSURLCredentialPersistencePermanent;
#if TARGET_IPHONE_SIMULATOR
credPersist = NSURLCredentialPersistenceForSession;
#endif
cred = [NSURLCredential credentialWithUser:username password:p persistence:credPersist];
[self.pendingChallenge.sender useCredential:cred forAuthenticationChallenge:self.pendingChallenge];
} else {
[self.pendingChallenge.sender cancelAuthenticationChallenge:self.pendingChallenge];
}
self.pendingChallenge = nil;
}
}
/* ********* List Directory *****************/
/* Path must start and end with a slash (/) */
-(void)listDir:(NSString *)path{
connectionState = kConnectionState_listDir;
if ([path length] <= 0){
return [self throwError:@"Path length is zero"];
}
NSRange range;
range.location = [path length]-1;
range.length = 1;
NSString *lastChar = [path substringWithRange:range];
if (![lastChar isEqualToString:@"/"]){
return [self throwError:@"Path must end with a /"];
}
if (self.connection != nil){
return [self throwError:@"A connection is already open; close it"];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
directoryList = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[self buildURL],path]];
_uriLength = [[url path] length] + 1;
NSString *xml = @"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\"><D:allprop/></D:propfind>";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setTimeoutInterval:self.globalTimeout];
[request setHTTPMethod:@"PROPFIND"];
[request setValue:@"1" forHTTPHeaderField:@"Depth"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type:"];
[request setHTTPBody:[xml dataUsingEncoding:NSUTF8StringEncoding]];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
/************** Make Directory ******************/
/* Pass in the full path to create */
-(void)makeDir:(NSString *)path{
connectionState = kConnectionState_makeDir;
if (self.connection != nil){
return [self throwError:@"A connection is already open; close it"];
}
NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"%@%@",[self buildURL],path] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
if (url == nil){
return [self throwError:@"Failed to create URL, maybe the filename is invalid?"];
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"MKCOL"];
[request setTimeoutInterval:self.globalTimeout];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
/************ Upload File *****************/
/** Pass a data object if you already have it read in */
-(void)uploadData:(NSData *)data destination:(NSString *)path{
if (connectionState != kConnectionState_uploadFile)
connectionState = kConnectionState_uploadData;
if (path == nil || [path isEqualToString:@""]){
return [self throwError:@"The destination path is missing"];
}
if (data == nil){
return [self throwError:@"Data is missing"];
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[self buildURL],path]];
NSLog(@"updateData URL: %@",url);
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"PUT"];
[request setHTTPBody:data];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
/** Pass a filename if it's on disk */
-(void)uploadFile:(NSString *)local destination:(NSString *)path{
connectionState = kConnectionState_uploadFile;
if (path == nil || [path isEqualToString:@""]){
return [self throwError:@"The destination path is missing"];
}
if ([[NSFileManager defaultManager] fileExistsAtPath:local]){
NSData *outgoingData = [NSData dataWithContentsOfFile:local];
if (outgoingData){
[self uploadData:outgoingData destination:path];
}
else{
return [self throwError:@"Failed reading in file"];
}
}
else{
return [self throwError:@"That file does not exist"];
}
}
- (void)connection:(NSURLConnection *)conn didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
self.pendingChallenge = challenge;
[self setup:self.username password:self.password];
}
- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response{
NSInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;
switch (connectionState) {
case kConnectionState_makeDir:
if (statusCode == 201){
if ( [delegate respondsToSelector:@selector(makeDirSuccess)] ) {
[delegate makeDirSuccess];
}
}
else{
[self throwError:@"Failed to create directory"];
}
break;
case kConnectionState_uploadData:
if (statusCode == 201){
if ( [delegate respondsToSelector:@selector(uploadDataSuccess)] ) {
[delegate uploadDataSuccess];
}
}
else{
[self throwError:@"Failed to upload data"];
}
break;
case kConnectionState_uploadFile:
if (statusCode == 201){
if ( [delegate respondsToSelector:@selector(uploadFileSuccess)] ) {
[delegate uploadFileSuccess];
}
}
else{
[self throwError:@"Failed to upload file"];
}
break;
default:
NSLog(@"Status Code: %d",statusCode);
break;
}
}
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data{
if (self.incomingData == nil) {
[self setIncomingData:[NSMutableData data]];
} else {
[self.incomingData appendData:data];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)conn{
NSData *finalData = [self.incomingData retain];
switch (connectionState) {
case kConnectionState_listDir:
if (finalData != nil) {
NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:finalData] autorelease];
[parser setDelegate:self];
if ([parser parse]){
if ( [delegate respondsToSelector:@selector(listDirSuccess:)] ) {
[delegate listDirSuccess:[directoryList autorelease]];
}
}
else{
return [self throwError:@"Failed to get data from listDir"];
}
}
else{
return [self throwError:@"Failed to get data from listDir"];
}
break;
}
[finalData release];
self.connection = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
-(void)throwError:(NSString *)error{
switch (connectionState) {
case kConnectionState_listDir:
if ( [delegate respondsToSelector:@selector(listDirFailed:)] ) {
[delegate listDirFailed:error];
}
break;
case kConnectionState_makeDir:
if ( [delegate respondsToSelector:@selector(makeDirFailed:)] ) {
[delegate makeDirFailed:error];
}
break;
case kConnectionState_uploadFile:
if ( [delegate respondsToSelector:@selector(uploadFileFailed:)] ) {
[delegate uploadFileFailed:error];
}
break;
case kConnectionState_uploadData:
if ( [delegate respondsToSelector:@selector(uploadDataFailed:)] ) {
[delegate uploadDataFailed:error];
}
break;
}
}
- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error{
self.connection = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
return [self throwError:[error description]];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if (!_xmlChars) {
_xmlChars = [[NSMutableString string] retain];
}
[_xmlChars setString:@""];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
switch (connectionState) {
case kConnectionState_listDir:
if ( [elementName isEqualToString:@"D:href"]) {
NSString *lastBit = [_xmlChars substringFromIndex:_uriLength];
if ([lastBit length]) {
[directoryList addObject:lastBit];
}
}
break;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[_xmlChars appendString:string];
}
@end