mirror of
https://github.com/swisspol/GCDWebServer.git
synced 2026-04-24 00:00:04 +08:00
Make contentType and contentLength properties
This commit is contained in:
@@ -324,19 +324,20 @@ static dispatch_queue_t _formatterQueue = NULL;
|
|||||||
|
|
||||||
if (_response) {
|
if (_response) {
|
||||||
[self _initializeResponseHeadersWithStatusCode:_response.statusCode];
|
[self _initializeResponseHeadersWithStatusCode:_response.statusCode];
|
||||||
NSUInteger maxAge = _response.cacheControlMaxAge;
|
if (_response.cacheControlMaxAge > 0) {
|
||||||
if (maxAge > 0) {
|
CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Cache-Control"), (ARC_BRIDGE CFStringRef)[NSString stringWithFormat:@"max-age=%i, public", (int)_response.cacheControlMaxAge]);
|
||||||
CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Cache-Control"), (ARC_BRIDGE CFStringRef)[NSString stringWithFormat:@"max-age=%i, public", (int)maxAge]);
|
|
||||||
} else {
|
} else {
|
||||||
CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Cache-Control"), CFSTR("no-cache"));
|
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) {
|
[_response.additionalHeaders enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL* stop) {
|
||||||
CFHTTPMessageSetHeaderFieldValue(_responseMessage, (ARC_BRIDGE CFStringRef)key, (ARC_BRIDGE CFStringRef)obj);
|
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) {
|
[self _writeHeadersWithCompletionBlock:^(BOOL success) {
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
|
|||||||
@@ -28,14 +28,13 @@
|
|||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
@interface GCDWebServerResponse : NSObject
|
@interface GCDWebServerResponse : NSObject
|
||||||
@property(nonatomic, readonly) NSString* contentType;
|
@property(nonatomic, copy) NSString* contentType; // Default is nil i.e. no body
|
||||||
@property(nonatomic, readonly) NSUInteger contentLength;
|
@property(nonatomic) NSUInteger contentLength; // Default is NSNotFound i.e. undefined
|
||||||
@property(nonatomic) NSInteger statusCode; // Default is 200
|
@property(nonatomic) NSInteger statusCode; // Default is 200
|
||||||
@property(nonatomic) NSUInteger cacheControlMaxAge; // Default is 0 seconds i.e. "no-cache"
|
@property(nonatomic) NSUInteger cacheControlMaxAge; // Default is 0 seconds i.e. "no-cache"
|
||||||
@property(nonatomic, readonly) NSDictionary* additionalHeaders;
|
@property(nonatomic, readonly) NSDictionary* additionalHeaders;
|
||||||
+ (GCDWebServerResponse*) response;
|
+ (GCDWebServerResponse*) response;
|
||||||
- (id)init;
|
- (id)init;
|
||||||
- (id)initWithContentType:(NSString*)type contentLength:(NSUInteger)length; // Pass nil contentType to indicate empty body
|
|
||||||
- (void)setValue:(NSString*)value forAdditionalHeader:(NSString*)header;
|
- (void)setValue:(NSString*)value forAdditionalHeader:(NSString*)header;
|
||||||
- (BOOL)hasBody; // Convenience method
|
- (BOOL)hasBody; // Convenience method
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -64,20 +64,12 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (id)init {
|
- (id)init {
|
||||||
return [self initWithContentType:nil contentLength:0];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (id)initWithContentType:(NSString*)type contentLength:(NSUInteger)length {
|
|
||||||
if ((self = [super init])) {
|
if ((self = [super init])) {
|
||||||
_type = [type copy];
|
_type = nil;
|
||||||
_length = length;
|
_length = NSNotFound;
|
||||||
_status = 200;
|
_status = 200;
|
||||||
_maxAge = 0;
|
_maxAge = 0;
|
||||||
_headers = [[NSMutableDictionary alloc] init];
|
_headers = [[NSMutableDictionary alloc] init];
|
||||||
|
|
||||||
if ((_length > 0) && (_type == nil)) {
|
|
||||||
_type = [kGCDWebServerDefaultMimeType copy];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
@@ -129,14 +121,14 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (id)initWithStatusCode:(NSInteger)statusCode {
|
- (id)initWithStatusCode:(NSInteger)statusCode {
|
||||||
if ((self = [self initWithContentType:nil contentLength:0])) {
|
if ((self = [self init])) {
|
||||||
self.statusCode = statusCode;
|
self.statusCode = statusCode;
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)initWithRedirect:(NSURL*)location permanent:(BOOL)permanent {
|
- (id)initWithRedirect:(NSURL*)location permanent:(BOOL)permanent {
|
||||||
if ((self = [self initWithContentType:nil contentLength:0])) {
|
if ((self = [self init])) {
|
||||||
self.statusCode = permanent ? 301 : 307;
|
self.statusCode = permanent ? 301 : 307;
|
||||||
[self setValue:[location absoluteString] forAdditionalHeader:@"Location"];
|
[self setValue:[location absoluteString] forAdditionalHeader:@"Location"];
|
||||||
}
|
}
|
||||||
@@ -158,9 +150,12 @@
|
|||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((self = [super initWithContentType:type contentLength:data.length])) {
|
if ((self = [super init])) {
|
||||||
_data = ARC_RETAIN(data);
|
_data = ARC_RETAIN(data);
|
||||||
_offset = -1;
|
_offset = -1;
|
||||||
|
|
||||||
|
self.contentType = type;
|
||||||
|
self.contentLength = data.length;
|
||||||
}
|
}
|
||||||
return self;
|
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];
|
_path = [path copy];
|
||||||
if (range.location != NSNotFound) {
|
if (range.location != NSNotFound) {
|
||||||
_offset = range.location;
|
_offset = range.location;
|
||||||
@@ -327,6 +322,7 @@
|
|||||||
_offset = 0;
|
_offset = 0;
|
||||||
_size = (NSUInteger)info.st_size;
|
_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
|
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];
|
NSData* data = [[path lastPathComponent] dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:YES];
|
||||||
NSString* fileName = data ? [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding] : nil;
|
NSString* fileName = data ? [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding] : nil;
|
||||||
@@ -337,6 +333,9 @@
|
|||||||
DNOT_REACHED();
|
DNOT_REACHED();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.contentType = GCDWebServerGetMimeTypeForExtension([path pathExtension]);
|
||||||
|
self.contentLength = (range.location != NSNotFound ? range.length : (NSUInteger)info.st_size);
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user