-
Notifications
You must be signed in to change notification settings - Fork 6
/
WebDAVResource.m
287 lines (261 loc) · 7.71 KB
/
WebDAVResource.m
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
#import <GNUstepBase/GSXML.h>
#import <GNUstepBase/GSMime.h>
#import "NSString+SimpleAgenda.h"
#import "WebDAVResource.h"
@implementation WebDAVResource
- (void)dealloc
{
[_user release];
[_password release];
[_url release];
[_lastModified release];
[_data release];
[_reason release];
[_etag release];
[super dealloc];
}
- (void)setURL:(NSURL *)anURL
{
if ([[anURL scheme] hasPrefix:@"webcal"])
ASSIGN(_url, [NSURL URLWithString:[[anURL absoluteString] stringByReplacingString:@"webcal" withString:@"http"]]);
else
ASSIGN(_url, anURL);
_handleClass = [NSURLHandle URLHandleClassForURL:_url];
if ([_url user])
ASSIGN(_user, [_url user]);
if ([_url password])
ASSIGN(_password, [_url password]);
}
- (id)initWithURL:(NSURL *)anUrl
{
if ((self = [super init]))
[self setURL:anUrl];
return self;
}
- (id)initWithURL:(NSURL *)url
username:(NSString *)username
password:(NSString *)password
{
self = [self initWithURL:url];
if (self) {
ASSIGN(_user, username);
ASSIGN(_password, password);
}
return self;
}
/* FIXME : ugly hack to work around NSURLHandle shortcomings */
- (NSString *)basicAuth
{
NSMutableString *authorisation;
NSString *toEncode;
authorisation = [NSMutableString stringWithCapacity: 64];
if ([_password length] > 0)
toEncode = [NSString stringWithFormat: @"%@:%@", _user, _password];
else
toEncode = [NSString stringWithFormat: @"%@", _user];
[authorisation appendFormat: @"Basic %@", [GSMimeDocument encodeBase64String: toEncode]];
return authorisation;
}
- (BOOL)requestWithMethod:(NSString *)method body:(NSData *)body attributes:(NSDictionary *)attributes
{
NSEnumerator *keys;
NSString *key;
NSData *data;
NSString *property;
NSURLHandle *handle;
restart:
handle = [[_handleClass alloc] initWithURL:_url cached:NO];
[handle writeProperty:method forKey:GSHTTPPropertyMethodKey];
if (attributes) {
keys = [attributes keyEnumerator];
while ((key = [keys nextObject]))
[handle writeProperty:[attributes objectForKey:key] forKey:key];
}
if (_user && ![_url user])
[handle writeProperty:[self basicAuth] forKey:@"Authorization"];
if (_etag && ([method isEqual:@"PUT"] || [method isEqual:@"DELETE"]))
[handle writeProperty:[NSString stringWithFormat:@"([%@])", _etag] forKey:@"If"];
if (body)
[handle writeData:body];
if (attributes)
NSDebugMLLog(@"WebDAVResource", @"%@ %@ (%@)", method, [_url anonymousAbsoluteString], [attributes description]);
else
NSDebugMLLog(@"WebDAVResource", @"%@ %@", method, [_url anonymousAbsoluteString]);
DESTROY(_data);
data = [handle resourceData];
/* FIXME : this is more than ugly */
if ([_url isFileURL])
_httpStatus = data ? 200 : 199;
else
_httpStatus = [[handle propertyForKeyIfAvailable:NSHTTPPropertyStatusCodeKey] intValue];
/* FIXME : why do we have to check for httpStatus == 0 */
if ((_httpStatus == 0 ||_httpStatus == 301 || _httpStatus == 302) && [handle propertyForKey:@"Location"] != nil) {
NSDebugMLLog(@"WebDAVResource", @"Redirection to %@", [handle propertyForKey:@"Location"]);
[self setURL:[NSURL URLWithString:[handle propertyForKey:@"Location"]]];
goto restart;
}
NSDebugMLLog(@"WebDAVResource", @"%@ status %d", method, _httpStatus);
property = [handle propertyForKeyIfAvailable:NSHTTPPropertyStatusReasonKey];
if (property)
ASSIGN(_reason, property);
else
DESTROY(_reason);
if (_httpStatus < 200 || _httpStatus > 299) {
if (_reason)
NSLog(@"%s %@ : %d %@", __PRETTY_FUNCTION__, method, _httpStatus, _reason);
else
NSLog(@"%s %@ : %d", __PRETTY_FUNCTION__, method, _httpStatus);
[handle release];
return NO;
}
if (data)
ASSIGN(_data, data);
if ([method isEqual:@"GET"]) {
property = [handle propertyForKeyIfAvailable:@"Last-Modified"];
if (!_lastModified || (property && ![property isEqual:_lastModified])) {
_dataChanged = YES;
ASSIGN(_lastModified, property);
}
property = [handle propertyForKeyIfAvailable:@"ETag"];
if (!_etag || (property && ![property isEqual:_etag])) {
_dataChanged = YES;
ASSIGN(_etag, property);
}
}
[handle release];
return YES;
}
/*
* Status | Meaning
* 200 | OK
* 207 | MULTI STATUS
* 304 | NOT MODIFIED
* 401 | NO AUTH
* 403 | WRONG PERM
* 404 | NO FILE
* ...
*/
- (BOOL)readable
{
[self get];
if ((_httpStatus > 199 && _httpStatus < 300) || _httpStatus == 404)
return YES;
return NO;
}
/*
* Status | Meaning
* 201 | OK OVERWRITE
* 204 | OK CREATE
* 401 | NO AUTH
* 403 | WRONG PERM
* ...
*/
- (BOOL)writableWithData:(NSData *)data
{
return [self put:data attributes:nil];
}
- (int)httpStatus
{
return _httpStatus;
}
- (NSData *)data
{
return _data;
}
- (NSString *)reason
{
return _reason;
}
- (NSURL *)url
{
return _url;
}
- (BOOL)get
{
return [self requestWithMethod:@"GET" body:nil attributes:nil];
}
- (BOOL)put:(NSData *)data attributes:(NSDictionary *)attributes
{
return [self requestWithMethod:@"PUT" body:data attributes:attributes];
}
- (BOOL)delete
{
return [self requestWithMethod:@"DELETE" body:nil attributes:nil];
}
- (BOOL)propfind:(NSData *)data attributes:(NSDictionary *)attributes
{
return [self requestWithMethod:@"PROPFIND" body:data attributes:attributes];
}
static NSString * const GETETAG = @"string(/multistatus/response/propstat/prop/getetag/text())";
static NSString * const GETLASTMODIFIED = @"string(/multistatus/response/propstat/prop/getlastmodified/text())";
- (void)updateAttributes;
{
GSXMLParser *parser;
GSXPathContext *xpc;
GSXPathString *result;
if ([self propfind:nil attributes:nil]) {
parser = [GSXMLParser parserWithData:[self data]];
if ([parser parse]) {
xpc = [[GSXPathContext alloc] initWithDocument:[[parser document] strippedDocument]];
result = (GSXPathString *)[xpc evaluateExpression:GETETAG];
if (result)
ASSIGN(_etag, [result stringValue]);
result = (GSXPathString *)[xpc evaluateExpression:GETLASTMODIFIED];
if (result)
ASSIGN(_lastModified, [result stringValue]);
[xpc release];
}
}
}
@end
@implementation NSURL(SimpleAgenda)
+ (NSURL *)URLWithString:(NSString *)string possiblyRelativeToURL:(NSURL *)base
{
if ([string isValidURL])
return [NSURL URLWithString:string];
return [NSURL URLWithString:string relativeToURL:base];
}
- (NSString *)anonymousAbsoluteString
{
NSString *as = [self absoluteString];
if (![self user] && ![self password])
return as;
return [as stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@:%@@", [self user], [self password]]
withString:@""];
}
@end
@implementation GSXMLDocument(SimpleAgenda)
static GSXMLDocument *removeXSLT;
static const NSString *removeString = @"<?xml version='1.0' encoding='UTF-8'?> \
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> \
<xsl:output method='xml' encoding='UTF-8' /> \
<xsl:template match='/'> \
<xsl:copy> \
<xsl:apply-templates /> \
</xsl:copy> \
</xsl:template> \
<xsl:template match='*'> \
<xsl:element name='{local-name()}'> \
<xsl:apply-templates select='@* | node()' /> \
</xsl:element> \
</xsl:template> \
<xsl:template match='@*'> \
<xsl:attribute name='{local-name()}'><xsl:value-of select='.' /></xsl:attribute> \
</xsl:template> \
<xsl:template match='text() | processing-instruction() | comment()'> \
<xsl:copy /> \
</xsl:template> \
</xsl:stylesheet>";
- (GSXMLDocument *)strippedDocument
{
if (removeXSLT == nil) {
GSXMLParser *parser = [GSXMLParser parserWithData:[removeString dataUsingEncoding:NSUTF8StringEncoding]];
if (![parser parse]) {
NSLog(@"Error parsing xslt document");
return nil;
}
removeXSLT = [[parser document] retain];
}
return [self xsltTransform:removeXSLT];
}
@end