Skip to content

Commit

Permalink
Automatically finish the response if not already finished
Browse files Browse the repository at this point in the history
Also invoke the notFoundBlock if no data has been sent
  • Loading branch information
thecatalinstan committed Oct 24, 2016
1 parent 0e7f27d commit 4f3d0ec
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions Criollo/Source/CRResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ - (void)writeData:(NSData *)data finish:(BOOL)flag {
if ( flag ) {
_finished = YES;
}
_hasWrittenBodyData = YES;
[self.connection sendDataToSocket:data forRequest:self.request];
}

Expand Down
3 changes: 2 additions & 1 deletion Criollo/Source/CRResponse_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) BOOL alreadySentHeaders;
@property (nonatomic, assign) BOOL alreadyBuiltHeaders;
@property (nonatomic, readonly) BOOL finished;
@property (nonatomic, readonly) BOOL hasWrittenBodyData;

- (instancetype)initWithConnection:(CRConnection *)connection HTTPStatusCode:(NSUInteger)HTTPStatusCode;
- (instancetype)initWithConnection:(CRConnection *)connection HTTPStatusCode:(NSUInteger)HTTPStatusCode description:(NSString * _Nullable)description;
Expand All @@ -28,4 +29,4 @@ NS_ASSUME_NONNULL_BEGIN
- (void)buildHeaders;

@end
NS_ASSUME_NONNULL_END
NS_ASSUME_NONNULL_END
2 changes: 1 addition & 1 deletion Criollo/Source/Routing/CRRoute.m
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ - (instancetype)initWithStaticFileAtPath:(NSString *)filePath options:(CRStaticF
return result;
}

@end
@end
11 changes: 11 additions & 0 deletions Criollo/Source/Routing/CRRouter.m
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,17 @@ - (void)executeRoutes:(NSArray<CRRouteMatchingResult *> *)routes forRequest:(CRR
routeBlock (request, response, ^{
shouldStopExecutingBlocks = NO;
currentRouteIndex++;
if ( currentRouteIndex == routes.count && !response.finished ) {
if ( !response.hasWrittenBodyData ) {
notFoundBlock(request, response, ^{
if ( !response.finished) {
[response finish];
}
});
} else {
[response finish];
}
}
});
}
}
Expand Down

0 comments on commit 4f3d0ec

Please sign in to comment.