From 7b684b80a2d366211e53529f8ed6402bb8e2446e Mon Sep 17 00:00:00 2001 From: robbin Date: Mon, 2 Jul 2018 10:36:09 +0800 Subject: [PATCH] [BUGFIX] CRASH: index 0 beyond bounds for empty array solution from robbiehanson/CocoaHTTPServer issues 87 --- KTVCocoaHTTPServer/Classes/HTTPConnection.m | 42 ++++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/KTVCocoaHTTPServer/Classes/HTTPConnection.m b/KTVCocoaHTTPServer/Classes/HTTPConnection.m index d4ac82f..d46e692 100755 --- a/KTVCocoaHTTPServer/Classes/HTTPConnection.m +++ b/KTVCocoaHTTPServer/Classes/HTTPConnection.m @@ -842,9 +842,17 @@ - (BOOL)parseRangeRequest:(NSString *)rangeHeader withContentLength:(UInt64)cont // Note: The range is inclusive. So 0-1 has a length of 2 bytes. if(r1 > r2) return NO; - if(r2 >= contentLength) return NO; - - [ranges addObject:[NSValue valueWithDDRange:DDMakeRange(r1, r2 - r1 + 1)]]; + /* + modify by robbin(irobbin1024@gmail.com) + old code : if(r2 >= contentLength) return NO; + new code : below + via : https://github.com/robbiehanson/CocoaHTTPServer/issues/78 + */ + if(r2 >= contentLength) { + r2 = contentLength - 1; + } + + [ranges addObject:[NSValue valueWithDDRange:DDMakeRange(r1, r2 - r1 + 1)]]; } } } @@ -2516,17 +2524,23 @@ - (void)responseHasAvailableData:(NSObject *)sender } else { - if (ranges == nil) - { - [self continueSendingStandardResponseBody]; - } - else - { - if ([ranges count] == 1) - [self continueSendingSingleRangeResponseBody]; - else - [self continueSendingMultiRangeResponseBody]; - } + /* + modify by robbin(irobbin1024@gmail.com) + old code : if ([ranges count] < 1) + new code : below + via : https://github.com/robbiehanson/CocoaHTTPServer/issues/78 + */ + if (!ranges || [ranges count] < 1) + { + [self continueSendingStandardResponseBody]; + } + else + { + if ([ranges count] == 1) + [self continueSendingSingleRangeResponseBody]; + else + [self continueSendingMultiRangeResponseBody]; + } } }}); }