@@ -59,11 +59,11 @@ + (instancetype)responseWithFile:(NSString*)path byteRange:(NSRange)range isAtta
5959}
6060
6161- (instancetype )initWithFile : (NSString *)path {
62- return [self initWithFile: path byteRange: NSMakeRange (NSNotFound , 0 ) isAttachment: NO ];
62+ return [self initWithFile: path byteRange: NSMakeRange (NSUIntegerMax , 0 ) isAttachment: NO ];
6363}
6464
6565- (instancetype )initWithFile : (NSString *)path isAttachment : (BOOL )attachment {
66- return [self initWithFile: path byteRange: NSMakeRange (NSNotFound , 0 ) isAttachment: attachment];
66+ return [self initWithFile: path byteRange: NSMakeRange (NSUIntegerMax , 0 ) isAttachment: attachment];
6767}
6868
6969- (instancetype )initWithFile : (NSString *)path byteRange : (NSRange )range {
@@ -81,31 +81,34 @@ - (instancetype)initWithFile:(NSString*)path byteRange:(NSRange)range isAttachme
8181 ARC_RELEASE (self);
8282 return nil ;
8383 }
84- if (GCDWebServerIsValidByteRange (range)) {
85- if (range.location != NSNotFound ) {
86- range.location = MIN (range.location , (NSUInteger )info.st_size );
87- range.length = MIN (range.length , (NSUInteger )info.st_size - range.location );
84+ NSUInteger fileSize = (NSUInteger )info.st_size ;
85+
86+ BOOL hasByteRange = GCDWebServerIsValidByteRange (range);
87+ if (hasByteRange) {
88+ if (range.location != NSUIntegerMax) {
89+ range.location = MIN (range.location , fileSize);
90+ range.length = MIN (range.length , fileSize - range.location );
8891 } else {
89- range.length = MIN (range.length , ( NSUInteger )info. st_size );
90- range.location = ( NSUInteger )info. st_size - range.length ;
92+ range.length = MIN (range.length , fileSize );
93+ range.location = fileSize - range.length ;
9194 }
9295 if (range.length == 0 ) {
9396 ARC_RELEASE (self);
9497 return nil ; // TODO: Return 416 status code and "Content-Range: bytes */{file length}" header
9598 }
99+ } else {
100+ range.location = 0 ;
101+ range.length = fileSize;
96102 }
97103
98104 if ((self = [super init ])) {
99105 _path = [path copy ];
100- if ( range.location != NSNotFound ) {
101- _offset = range.location ;
102- _size = range. length ;
106+ _offset = range.location ;
107+ _size = range.length ;
108+ if (hasByteRange) {
103109 [self setStatusCode: kGCDWebServerHTTPStatusCode_PartialContent ];
104- [self setValue: [NSString stringWithFormat: @" bytes %i -%i /%i " , (int )range.location, (int )(range.location + range.length - 1 ), (int )info.st_size] forAdditionalHeader: @" Content-Range" ];
105- LOG_DEBUG (@" Using content bytes range [%i -%i ] for file \" %@ \" " , (int )range.location , (int )(range.location + range.length - 1 ), path);
106- } else {
107- _offset = 0 ;
108- _size = (NSUInteger )info.st_size ;
110+ [self setValue: [NSString stringWithFormat: @" bytes %lu -%lu /%lu " , (unsigned long )_offset, (unsigned long )(_offset + _size - 1 ), (unsigned long )fileSize] forAdditionalHeader: @" Content-Range" ];
111+ LOG_DEBUG (@" Using content bytes range [%lu -%lu ] for file \" %@ \" " , (unsigned long )_offset, (unsigned long )(_offset + _size - 1 ), path);
109112 }
110113
111114 if (attachment) {
@@ -121,8 +124,8 @@ - (instancetype)initWithFile:(NSString*)path byteRange:(NSRange)range isAttachme
121124 }
122125 }
123126
124- self.contentType = GCDWebServerGetMimeTypeForExtension ([path pathExtension ]);
125- self.contentLength = (range. location != NSNotFound ? range. length : ( NSUInteger )info. st_size ) ;
127+ self.contentType = GCDWebServerGetMimeTypeForExtension ([_path pathExtension ]);
128+ self.contentLength = _size ;
126129 self.lastModifiedDate = _NSDateFromTimeSpec (&info.st_mtimespec );
127130 self.eTag = [NSString stringWithFormat: @" %llu /%li /%li " , info.st_ino, info.st_mtimespec.tv_sec, info.st_mtimespec.tv_nsec];
128131 }
0 commit comments