Skip to content
Browse files

improved checking of the status code returned by the server

  • Loading branch information...
1 parent dc485b0 commit b6bbb09b395e96559a10e878ecd295bb28b0877d @samdeane samdeane committed Dec 21, 2012
Showing with 39 additions and 8 deletions.
  1. +2 −0 Sources/Core/DAVRequest.h
  2. +11 −8 Sources/Core/DAVRequest.m
  3. +26 −0 Sources/Core/DAVRequests.m
View
2 Sources/Core/DAVRequest.h
@@ -19,12 +19,14 @@ extern NSString *const DAVClientErrorDomain;
NSMutableData *_data;
BOOL _done;
BOOL _executing;
+ NSIndexSet* _expectedStatuses;
id <DAVRequestDelegate> _delegate;
}
- (id)initWithPath:(NSString *)aPath session:(DAVSession *)session delegate:(id <DAVRequestDelegate>)delegate;
@property(readonly) NSString *path;
+@property(nonatomic, strong) NSIndexSet* expectedStatuses;
@property(nonatomic, assign, readonly) id <DAVRequestDelegate> delegate;
- (NSURL *)concatenatedURLWithPath:(NSString *)aPath;
View
19 Sources/Core/DAVRequest.m
@@ -19,6 +19,7 @@ - (void)_didFinish;
@implementation DAVRequest
+
NSString *const DAVClientErrorDomain = @"com.MattRajca.DAVKit.error";
#define DEFAULT_TIMEOUT 60
@@ -35,6 +36,7 @@ - (id)initWithPath:(NSString *)aPath session:(DAVSession *)session delegate:(id
return self;
}
+@synthesize expectedStatuses = _expectedStatuses;
@synthesize path = _path;
@synthesize delegate = _delegate;
@@ -135,7 +137,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
// Report to transcript
[[self session] appendFormatToReceivedTranscript:@"%i %@", code, [[resp class] localizedStringForStatusCode:code]];
- if (code >= 400) {
+ if ((code >= 400) || (self.expectedStatuses && ![self.expectedStatuses containsIndex:code])) {
[_connection cancel];
// TODO: Formalize inclusion of response
@@ -219,19 +221,20 @@ - (void)_didFinish {
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
- if ([_delegate respondsToSelector:@selector(request:didSucceedWithResult:)]) {
- id result = [self resultForData:_data];
-
- [_delegate request:self didSucceedWithResult:[[result retain] autorelease]];
- }
-
- [self _didFinish];
+ if ([_delegate respondsToSelector:@selector(request:didSucceedWithResult:)]) {
+ id result = [self resultForData:_data];
+
+ [_delegate request:self didSucceedWithResult:[[result retain] autorelease]];
+ }
+
+ [self _didFinish];
}
- (void)dealloc {
[_path release];
[_connection release];
[_data release];
+ [_expectedStatuses release];
[super dealloc];
}
View
26 Sources/Core/DAVRequests.m
@@ -47,6 +47,16 @@ - (void)dealloc {
@implementation DAVDeleteRequest
+- (id)initWithPath:(NSString *)aPath session:(DAVSession *)session delegate:(id<DAVRequestDelegate>)delegate
+{
+ if ((self = [super initWithPath:aPath session:session delegate:delegate]) != nil)
+ {
+ self.expectedStatuses = [NSIndexSet indexSetWithIndex:204];
+ }
+
+ return self;
+}
+
- (NSURLRequest *)request {
return [[self newRequestWithPath:self.path method:@"DELETE"] autorelease];
}
@@ -122,6 +132,16 @@ - (id)resultForData:(NSData *)data {
@implementation DAVMakeCollectionRequest
+- (id)initWithPath:(NSString *)aPath session:(DAVSession *)session delegate:(id<DAVRequestDelegate>)delegate
+{
+ if ((self = [super initWithPath:aPath session:session delegate:delegate]) != nil)
+ {
+ self.expectedStatuses = [NSIndexSet indexSetWithIndex:204];
+ }
+
+ return self;
+}
+
- (NSURLRequest *)request {
return [[self newRequestWithPath:self.path method:@"MKCOL"] autorelease];
}
@@ -145,6 +165,12 @@ - (id)initWithSession:(DAVSession *)session;
if (self = [super initWithSession:session])
{
_MIMEType = @"application/octet-stream";
+ NSMutableIndexSet* indexes = [[NSMutableIndexSet alloc] init];
+ [indexes addIndex:201]; // The resource was created successfully
+ [indexes addIndex:202]; // The resource will be created or deleted, but this has not happened yet
+ [indexes addIndex:204]; // The server has fulfilled the request but does not need to return an entity body, and might return updated metadata.
+ self.expectedStatuses = indexes;
+ [indexes release];
}
return self;
}

0 comments on commit b6bbb09

Please sign in to comment.
Something went wrong with that request. Please try again.