mirror of
https://github.com/swisspol/GCDWebServer.git
synced 2026-05-13 00:02:02 +08:00
Added JSON and text extensions to GCDWebServerDataRequest
This commit is contained in:
@@ -30,3 +30,8 @@
|
||||
@interface GCDWebServerDataRequest : GCDWebServerRequest
|
||||
@property(nonatomic, readonly) NSData* data;
|
||||
@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 () {
|
||||
@private
|
||||
NSMutableData* _data;
|
||||
|
||||
NSString* _text;
|
||||
id _jsonObject;
|
||||
}
|
||||
@end
|
||||
|
||||
@@ -39,6 +42,8 @@
|
||||
|
||||
- (void)dealloc {
|
||||
ARC_RELEASE(_data);
|
||||
ARC_RELEASE(_text);
|
||||
ARC_RELEASE(_jsonObject);
|
||||
|
||||
ARC_DEALLOC(super);
|
||||
}
|
||||
@@ -69,3 +74,30 @@
|
||||
}
|
||||
|
||||
@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"
|
||||
|
||||
@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;
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user