/
APSHTTPResponse.m
164 lines (155 loc) · 4.8 KB
/
APSHTTPResponse.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Appcelerator APSHTTPClient Library
* Copyright (c) 2014 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#import "APSHTTPClient.h"
@interface APSHTTPResponse ()
@property (nonatomic, readwrite) NSStringEncoding encoding;
@property (nonatomic, readwrite) BOOL saveToFile;
@property (nonatomic, strong, readonly) NSURL *url;
@end
@implementation APSHTTPResponse {
NSMutableData *_data;
}
- (void)updateResponseParamaters:(NSURLResponse *)response
{
_url = [response URL];
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
_status = [(NSHTTPURLResponse *)response statusCode];
_headers = [(NSHTTPURLResponse *)response allHeaderFields];
NSStringEncoding encoding = [APSHTTPHelper parseStringEncodingFromHeaders:_headers];
encoding = encoding == 0 ? NSUTF8StringEncoding : encoding;
[self setEncoding:encoding];
}
}
- (void)updateRequestParamaters:(NSURLRequest *)request
{
_connectionType = [request HTTPMethod];
_location = [[request URL] absoluteString];
}
- (void)appendData:(NSData *)data
{
if ([self saveToFile]) {
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:[self filePath]];
[fileHandle seekToEndOfFile];
[fileHandle writeData:data];
[fileHandle closeFile];
} else {
if (_data == nil) {
_data = [[NSMutableData alloc] init];
}
[_data appendData:data];
}
}
- (void)setFilePath:(NSString *)filePath
{
_filePath = [filePath copy];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isWritable = NO;
BOOL isDirectory = NO;
BOOL fileExists = [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory];
if (isDirectory) {
DebugLog(@"[ERROR] %@ is directory, ignoring", filePath); // file path
return;
}
if (fileExists) {
isWritable = [fileManager isWritableFileAtPath:filePath];
if (!isWritable) {
DebugLog(@"[ERROR] %@ is not writable, ignoring", filePath);
return;
}
DebugLog(@"[WARN] %@ already exists, replacing", filePath);
NSError *deleteError = nil;
[fileManager removeItemAtPath:filePath error:&deleteError];
if (deleteError != nil) {
DebugLog(@"[WARN] Cannot delete %@, error was %@", filePath, [deleteError localizedDescription]);
return;
}
}
isWritable = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
[self setSaveToFile:isWritable];
}
- (NSData *)responseData
{
if (_data == nil) {
return nil;
}
return [_data copy];
}
- (NSInteger)responseLength
{
if ([self saveToFile]) {
return (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[self filePath] error:nil] fileSize];
}
return [[self responseData] length];
}
- (id)jsonResponse
{
if ([self responseData] == nil)
return nil;
NSError *error = nil;
id json = [NSJSONSerialization JSONObjectWithData:[self responseData]
options:NSJSONReadingAllowFragments
error:&error];
if (error != nil) {
#ifdef DEBUG
NSLog(@"%s - %@", __PRETTY_FUNCTION__, [error localizedDescription]);
#endif
return nil;
}
return json;
}
- (NSString *)responseString
{
if ([self error] != nil) {
#ifdef DEBUG
NSLog(@"%s", __PRETTY_FUNCTION__);
#endif
return [[self error] localizedDescription];
}
if ([self responseData] == nil || [self responseLength] == 0)
return nil;
NSData *data = [self responseData];
NSString *result = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:[self encoding]];
if (result == nil) {
// encoding failed, probably a bad webserver or content we have to deal
// with in a _special_ way
NSStringEncoding encoding = NSUTF8StringEncoding;
BOOL didExtractEncoding = [APSHTTPHelper extractEncodingFromData:data result:&encoding];
if (didExtractEncoding) {
//If I did extract encoding use that
result = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:encoding];
} else {
result = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSISOLatin1StringEncoding];
}
}
return result;
}
- (NSDictionary *)responseDictionary
{
id json = [self jsonResponse];
if ([json isKindOfClass:[NSDictionary class]]) {
#ifdef DEBUG
NSLog(@"%s", __PRETTY_FUNCTION__);
#endif
return (NSDictionary *)json;
}
#ifdef DEBUG
NSLog(@"%s - JSON is %@", __PRETTY_FUNCTION__, [[json superclass] description]);
#endif
return nil;
}
- (NSArray *)responseArray
{
id json = [self jsonResponse];
if ([json isKindOfClass:[NSArray class]]) {
return (NSArray *)json;
}
#ifdef DEBUG
NSLog(@"%s - JSON is %@", __PRETTY_FUNCTION__, [[json superclass] description]);
#endif
return nil;
}
@end