Эх сурвалжийг харах

Make contentType and contentLength properties

Pierre-Olivier Latour 11 жил өмнө
parent
commit
8116d88ec4

+ 8 - 7
CGDWebServer/GCDWebServerConnection.m

@@ -324,19 +324,20 @@ static dispatch_queue_t _formatterQueue = NULL;
   
   if (_response) {
     [self _initializeResponseHeadersWithStatusCode:_response.statusCode];
-    NSUInteger maxAge = _response.cacheControlMaxAge;
-    if (maxAge > 0) {
-      CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Cache-Control"), (ARC_BRIDGE CFStringRef)[NSString stringWithFormat:@"max-age=%i, public", (int)maxAge]);
+    if (_response.cacheControlMaxAge > 0) {
+      CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Cache-Control"), (ARC_BRIDGE CFStringRef)[NSString stringWithFormat:@"max-age=%i, public", (int)_response.cacheControlMaxAge]);
     } else {
       CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Cache-Control"), CFSTR("no-cache"));
     }
+    if (_response.contentType != nil) {
+      CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Content-Type"), (ARC_BRIDGE CFStringRef)_response.contentType);
+    }
+    if (_response.contentLength != NSNotFound) {
+      CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Content-Length"), (ARC_BRIDGE CFStringRef)[NSString stringWithFormat:@"%lu", (unsigned long)_response.contentLength]);
+    }
     [_response.additionalHeaders enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL* stop) {
       CFHTTPMessageSetHeaderFieldValue(_responseMessage, (ARC_BRIDGE CFStringRef)key, (ARC_BRIDGE CFStringRef)obj);
     }];
-    if ([_response hasBody]) {
-      CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Content-Type"), (ARC_BRIDGE CFStringRef)_response.contentType);
-      CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Content-Length"), (ARC_BRIDGE CFStringRef)[NSString stringWithFormat:@"%i", (int)_response.contentLength]);
-    }
     [self _writeHeadersWithCompletionBlock:^(BOOL success) {
       
       if (success) {

+ 2 - 3
CGDWebServer/GCDWebServerResponse.h

@@ -28,14 +28,13 @@
 #import <Foundation/Foundation.h>
 
 @interface GCDWebServerResponse : NSObject
-@property(nonatomic, readonly) NSString* contentType;
-@property(nonatomic, readonly) NSUInteger contentLength;
+@property(nonatomic, copy) NSString* contentType;  // Default is nil i.e. no body
+@property(nonatomic) NSUInteger contentLength;  // Default is NSNotFound i.e. undefined
 @property(nonatomic) NSInteger statusCode;  // Default is 200
 @property(nonatomic) NSUInteger cacheControlMaxAge;  // Default is 0 seconds i.e. "no-cache"
 @property(nonatomic, readonly) NSDictionary* additionalHeaders;
 + (GCDWebServerResponse*) response;
 - (id)init;
-- (id)initWithContentType:(NSString*)type contentLength:(NSUInteger)length;  // Pass nil contentType to indicate empty body
 - (void)setValue:(NSString*)value forAdditionalHeader:(NSString*)header;
 - (BOOL)hasBody;  // Convenience method
 @end

+ 13 - 14
CGDWebServer/GCDWebServerResponse.m

@@ -64,20 +64,12 @@
 }
 
 - (id)init {
-  return [self initWithContentType:nil contentLength:0];
-}
-
-- (id)initWithContentType:(NSString*)type contentLength:(NSUInteger)length {
   if ((self = [super init])) {
-    _type = [type copy];
-    _length = length;
+    _type = nil;
+    _length = NSNotFound;
     _status = 200;
     _maxAge = 0;
     _headers = [[NSMutableDictionary alloc] init];
-    
-    if ((_length > 0) && (_type == nil)) {
-      _type = [kGCDWebServerDefaultMimeType copy];
-    }
   }
   return self;
 }
@@ -129,14 +121,14 @@
 }
 
 - (id)initWithStatusCode:(NSInteger)statusCode {
-  if ((self = [self initWithContentType:nil contentLength:0])) {
+  if ((self = [self init])) {
     self.statusCode = statusCode;
   }
   return self;
 }
 
 - (id)initWithRedirect:(NSURL*)location permanent:(BOOL)permanent {
-  if ((self = [self initWithContentType:nil contentLength:0])) {
+  if ((self = [self init])) {
     self.statusCode = permanent ? 301 : 307;
     [self setValue:[location absoluteString] forAdditionalHeader:@"Location"];
   }
@@ -158,9 +150,12 @@
     return nil;
   }
   
-  if ((self = [super initWithContentType:type contentLength:data.length])) {
+  if ((self = [super init])) {
     _data = ARC_RETAIN(data);
     _offset = -1;
+    
+    self.contentType = type;
+    self.contentLength = data.length;
   }
   return self;
 }
@@ -315,7 +310,7 @@
     }
   }
   
-  if ((self = [super initWithContentType:GCDWebServerGetMimeTypeForExtension([path pathExtension]) contentLength:(range.location != NSNotFound ? range.length : (NSUInteger)info.st_size)])) {
+  if ((self = [super init])) {
     _path = [path copy];
     if (range.location != NSNotFound) {
       _offset = range.location;
@@ -327,6 +322,7 @@
       _offset = 0;
       _size = (NSUInteger)info.st_size;
     }
+    
     if (attachment) {  // TODO: Use http://tools.ietf.org/html/rfc5987 to encode file names with special characters instead of using lossy conversion to ISO 8859-1
       NSData* data = [[path lastPathComponent] dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:YES];
       NSString* fileName = data ? [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding] : nil;
@@ -337,6 +333,9 @@
         DNOT_REACHED();
       }
     }
+    
+    self.contentType = GCDWebServerGetMimeTypeForExtension([path pathExtension]);
+    self.contentLength = (range.location != NSNotFound ? range.length : (NSUInteger)info.st_size);
   }
   return self;
 }