mirror of
https://github.com/swisspol/GCDWebServer.git
synced 2026-05-31 00:00:10 +08:00
Added JSON and text extensions to GCDWebServerDataRequest
This commit is contained in:
@@ -30,3 +30,8 @@
|
|||||||
@interface GCDWebServerDataRequest : GCDWebServerRequest
|
@interface GCDWebServerDataRequest : GCDWebServerRequest
|
||||||
@property(nonatomic, readonly) NSData* data;
|
@property(nonatomic, readonly) NSData* data;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@interface GCDWebServerDataRequest (Extensions)
|
||||||
|
@property(nonatomic, readonly) NSString* text; // Text encoding is extracted from Content-Type or defaults to UTF-8 - Returns nil on error
|
||||||
|
@property(nonatomic, readonly) id jsonObject; // Returns nil on error
|
||||||
|
@end
|
||||||
|
|||||||
@@ -30,6 +30,9 @@
|
|||||||
@interface GCDWebServerDataRequest () {
|
@interface GCDWebServerDataRequest () {
|
||||||
@private
|
@private
|
||||||
NSMutableData* _data;
|
NSMutableData* _data;
|
||||||
|
|
||||||
|
NSString* _text;
|
||||||
|
id _jsonObject;
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@@ -39,6 +42,8 @@
|
|||||||
|
|
||||||
- (void)dealloc {
|
- (void)dealloc {
|
||||||
ARC_RELEASE(_data);
|
ARC_RELEASE(_data);
|
||||||
|
ARC_RELEASE(_text);
|
||||||
|
ARC_RELEASE(_jsonObject);
|
||||||
|
|
||||||
ARC_DEALLOC(super);
|
ARC_DEALLOC(super);
|
||||||
}
|
}
|
||||||
@@ -69,3 +74,30 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@implementation GCDWebServerDataRequest (Extensions)
|
||||||
|
|
||||||
|
- (NSString*)text {
|
||||||
|
if (_text == nil) {
|
||||||
|
if ([self.contentType hasPrefix:@"text/"]) {
|
||||||
|
NSString* charset = GCDWebServerExtractHeaderParameter(self.contentType, @"charset");
|
||||||
|
_text = [[NSString alloc] initWithData:self.data encoding:GCDWebServerStringEncodingFromCharset(charset)];
|
||||||
|
} else {
|
||||||
|
DNOT_REACHED();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _text;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (id)jsonObject {
|
||||||
|
if (_jsonObject == nil) {
|
||||||
|
if ([self.contentType isEqualToString:@"application/json"] || [self.contentType isEqualToString:@"text/json"] || [self.contentType isEqualToString:@"text/javascript"]) {
|
||||||
|
_jsonObject = ARC_RETAIN([NSJSONSerialization JSONObjectWithData:_data options:0 error:NULL]);
|
||||||
|
} else {
|
||||||
|
DNOT_REACHED();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|||||||
@@ -28,6 +28,6 @@
|
|||||||
#import "GCDWebServerDataRequest.h"
|
#import "GCDWebServerDataRequest.h"
|
||||||
|
|
||||||
@interface GCDWebServerURLEncodedFormRequest : GCDWebServerDataRequest
|
@interface GCDWebServerURLEncodedFormRequest : GCDWebServerDataRequest
|
||||||
@property(nonatomic, readonly) NSDictionary* arguments;
|
@property(nonatomic, readonly) NSDictionary* arguments; // Text encoding is extracted from Content-Type or defaults to UTF-8
|
||||||
+ (NSString*)mimeType;
|
+ (NSString*)mimeType;
|
||||||
@end
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user