Skip to content

Commit 2dda0c9

Browse files
committed
swisspol#50 Use NSUIntegerMax instead of NSNotFound to indicate undefined length
1 parent 70a38c8 commit 2dda0c9

10 files changed

Lines changed: 42 additions & 39 deletions

GCDWebServer/Core/GCDWebServer.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ static CFHTTPMessageRef _CreateHTTPMessageFromPerformingRequest(NSData* inData,
937937
while (1) {
938938
ssize_t result = read(httpSocket, (char*)outData.mutableBytes + length, outData.length - length);
939939
if (result < 0) {
940-
length = NSNotFound;
940+
length = NSUIntegerMax;
941941
break;
942942
} else if (result == 0) {
943943
break;
@@ -947,7 +947,7 @@ static CFHTTPMessageRef _CreateHTTPMessageFromPerformingRequest(NSData* inData,
947947
outData.length = 2 * outData.length;
948948
}
949949
}
950-
if (length != NSNotFound) {
950+
if (length != NSUIntegerMax) {
951951
outData.length = length;
952952
response = _CreateHTTPMessageFromData(outData, NO);
953953
} else {

GCDWebServer/Core/GCDWebServerConnection.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ - (void)_processRequest {
416416
if (_response.contentType != nil) {
417417
CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Content-Type"), (ARC_BRIDGE CFStringRef)GCDWebServerNormalizeHeaderValue(_response.contentType));
418418
}
419-
if (_response.contentLength != NSNotFound) {
419+
if (_response.contentLength != NSUIntegerMax) {
420420
CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Content-Length"), (ARC_BRIDGE CFStringRef)[NSString stringWithFormat:@"%lu", (unsigned long)_response.contentLength]);
421421
}
422422
if (_response.usesChunkedTransferEncoding) {

GCDWebServer/Core/GCDWebServerPrivate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ extern void GCDLogMessage(GCDWebServerLogLevel level, NSString* format, ...) NS_
111111
#define kGCDWebServerErrorDomain @"GCDWebServerErrorDomain"
112112

113113
static inline BOOL GCDWebServerIsValidByteRange(NSRange range) {
114-
return ((range.location != NSNotFound) || (range.length > 0));
114+
return ((range.location != NSUIntegerMax) || (range.length > 0));
115115
}
116116

117117
static inline NSError* GCDWebServerMakePosixError(int code) {

GCDWebServer/Core/GCDWebServerRequest.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
* Returns the content length for the body of the request parsed from the
120120
* "Content-Length" header.
121121
*
122-
* This property will be set to "NSNotFound" if the request has no body or
122+
* This property will be set to "NSUIntegerMax" if the request has no body or
123123
* if there is a body but no "Content-Length" header, typically because
124124
* chunked transfer encoding is used.
125125
*/
@@ -136,9 +136,9 @@
136136
@property(nonatomic, readonly) NSString* ifNoneMatch;
137137

138138
/**
139-
* Returns the parsed "Range" header or (NSNotFound, 0) if absent or malformed.
139+
* Returns the parsed "Range" header or (NSUIntegerMax, 0) if absent or malformed.
140140
* The range will be set to (offset, length) if expressed from the beginning
141-
* of the entity body, or (NSNotFound, -length) if expressed from its end.
141+
* of the entity body, or (NSUIntegerMax, length) if expressed from its end.
142142
*/
143143
@property(nonatomic, readonly) NSRange byteRange;
144144

GCDWebServer/Core/GCDWebServerRequest.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ - (instancetype)initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDict
187187
if (_type == nil) {
188188
_type = kGCDWebServerDefaultMimeType;
189189
}
190-
_length = NSNotFound;
190+
_length = NSUIntegerMax;
191191
} else {
192192
if (_type) {
193193
DNOT_REACHED();
194194
ARC_RELEASE(self);
195195
return nil;
196196
}
197-
_length = NSNotFound;
197+
_length = NSUIntegerMax;
198198
}
199199

200200
NSString* modifiedHeader = [_headers objectForKey:@"If-Modified-Since"];
@@ -203,7 +203,7 @@ - (instancetype)initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDict
203203
}
204204
_noneMatch = ARC_RETAIN([_headers objectForKey:@"If-None-Match"]);
205205

206-
_range = NSMakeRange(NSNotFound, 0);
206+
_range = NSMakeRange(NSUIntegerMax, 0);
207207
NSString* rangeHeader = GCDWebServerNormalizeHeaderValue([_headers objectForKey:@"Range"]);
208208
if (rangeHeader) {
209209
if ([rangeHeader hasPrefix:@"bytes="]) {
@@ -222,13 +222,13 @@ - (instancetype)initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDict
222222
_range.location = startValue;
223223
_range.length = NSUIntegerMax;
224224
} else if (endString.length && (endValue > 0)) { // The final 500 bytes: "-500"
225-
_range.location = NSNotFound;
225+
_range.location = NSUIntegerMax;
226226
_range.length = endValue;
227227
}
228228
}
229229
}
230230
}
231-
if ((_range.location == NSNotFound) && (_range.length == 0)) { // Ignore "Range" header if syntactically invalid
231+
if ((_range.location == NSUIntegerMax) && (_range.length == 0)) { // Ignore "Range" header if syntactically invalid
232232
LOG_WARNING(@"Failed to parse 'Range' header \"%@\" for url: %@", rangeHeader, url);
233233
}
234234
}

GCDWebServer/Core/GCDWebServerResponse.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@
8787

8888
/**
8989
* Sets the content length for the body of the response. If a body is present
90-
* but this property is set to "NSNotFound", this means the length of the body
90+
* but this property is set to "NSUIntegerMax", this means the length of the body
9191
* cannot be known ahead of time. Chunked transfer encoding will be
9292
* automatically enabled by the GCDWebServerConnection to comply with HTTP/1.1
9393
* specifications.
9494
*
95-
* The default value is "NSNotFound" i.e. the response has no body or its length
95+
* The default value is "NSUIntegerMax" i.e. the response has no body or its length
9696
* is undefined.
9797
*/
9898
@property(nonatomic) NSUInteger contentLength;

GCDWebServer/Core/GCDWebServerResponse.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ @implementation GCDWebServerGZipEncoder
8181

8282
- (id)initWithResponse:(GCDWebServerResponse*)response reader:(id<GCDWebServerBodyReader>)reader {
8383
if ((self = [super initWithResponse:response reader:reader])) {
84-
response.contentLength = NSNotFound; // Make sure "Content-Length" header is not set since we don't know it
84+
response.contentLength = NSUIntegerMax; // Make sure "Content-Length" header is not set since we don't know it
8585
[response setValue:@"gzip" forAdditionalHeader:@"Content-Encoding"];
8686
}
8787
return self;
@@ -180,7 +180,7 @@ + (instancetype)response {
180180
- (instancetype)init {
181181
if ((self = [super init])) {
182182
_type = nil;
183-
_length = NSNotFound;
183+
_length = NSUIntegerMax;
184184
_status = kGCDWebServerHTTPStatusCode_OK;
185185
_maxAge = 0;
186186
_headers = [[NSMutableDictionary alloc] init];
@@ -208,7 +208,7 @@ - (BOOL)hasBody {
208208
}
209209

210210
- (BOOL)usesChunkedTransferEncoding {
211-
return (_type != nil) && (_length == NSNotFound);
211+
return (_type != nil) && (_length == NSUIntegerMax);
212212
}
213213

214214
- (BOOL)open:(NSError**)error {
@@ -259,7 +259,7 @@ - (NSString*)description {
259259
if (_type) {
260260
[description appendFormat:@"\nContent Type = %@", _type];
261261
}
262-
if (_length != NSNotFound) {
262+
if (_length != NSUIntegerMax) {
263263
[description appendFormat:@"\nContent Length = %lu", (unsigned long)_length];
264264
}
265265
[description appendFormat:@"\nCache Control Max Age = %lu", (unsigned long)_maxAge];

GCDWebServer/Requests/GCDWebServerDataRequest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ - (void)dealloc {
4949
}
5050

5151
- (BOOL)open:(NSError**)error {
52-
if (self.contentLength != NSNotFound) {
52+
if (self.contentLength != NSUIntegerMax) {
5353
_data = [[NSMutableData alloc] initWithCapacity:self.contentLength];
5454
} else {
5555
_data = [[NSMutableData alloc] init];

GCDWebServer/Responses/GCDWebServerFileResponse.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@
7777

7878
/**
7979
* Initializes a response like -initWithFile: but restricts the file contents
80-
* to a specific byte range. This range should be set to (NSNotFound, 0) for
80+
* to a specific byte range. This range should be set to (NSUIntegerMax, 0) for
8181
* the full file, (offset, length) if expressed from the beginning of the file,
82-
* or (NSNotFound, -length) if expressed from the end of the file. The "offset"
82+
* or (NSUIntegerMax, length) if expressed from the end of the file. The "offset"
8383
* and "length" values will be automatically adjusted to be compatible with the
8484
* actual size of the file.
8585
*

GCDWebServer/Responses/GCDWebServerFileResponse.m

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)