This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
TDHTTPResponse.m
256 lines (213 loc) · 7.41 KB
/
TDHTTPResponse.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
// TDHTTPResponse.m
// TouchDB
//
// Created by Jens Alfke on 12/29/11.
// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
#import "TDHTTPResponse.h"
#import "TDHTTPConnection.h"
#import "TDListener.h"
#import "TDRouter.h"
#import "TDBody.h"
#import "Logging.h"
@interface TDHTTPResponse ()
- (void) onResponseReady: (TDResponse*)response;
- (void) onDataAvailable: (NSData*)data finished: (BOOL)finished;
- (void) onFinished;
@end
@implementation TDHTTPResponse
- (id) initWithRouter: (TDRouter*)router forConnection:(TDHTTPConnection*)connection {
self = [super init];
if (self) {
//EnableLog(YES);
//EnableLogTo(TDListenerVerbose, YES);
_router = [router retain];
_connection = connection;
router.onResponseReady = ^(TDResponse* r) {
[self onResponseReady: r];
};
router.onDataAvailable = ^(NSData* data, BOOL finished) {
[self onDataAvailable: data finished: finished];
};
router.onFinished = ^{
[self onFinished];
};
if (connection.listener.readOnly) {
router.onAccessCheck = ^TDStatus(TDDatabase* db, NSString* docID, SEL action) {
NSString* method = router.request.HTTPMethod;
if ([method isEqualToString: @"GET"] || [method isEqualToString: @"HEAD"])
return kTDStatusOK;
if ([method isEqualToString: @"POST"]) {
NSString* actionStr = NSStringFromSelector(action);
if ([actionStr isEqualToString: @"do_POST_all_docs:"]
|| [actionStr isEqualToString: @"do_POST_revs_diff:"])
return kTDStatusOK;
}
return kTDStatusForbidden;
};
}
// Run the router, asynchronously:
LogTo(TDListenerVerbose, @"%@: Starting...", self);
[router start];
[self retain]; // will be released in -cleanUp
LogTo(TDListenerVerbose, @"%@: Returning from -init", self);
}
return self;
}
- (void)dealloc {
LogTo(TDListenerVerbose, @"DEALLOC %@", self);
[_router release];
[_response release];
[_data release];
[super dealloc];
}
- (NSString*) description {
return [NSString stringWithFormat: @"Response[%@ %@]",
_router.request.HTTPMethod, _router.request.URL.path];
}
/**
* If you don't know the content-length in advance,
* implement this method in your custom response class and return YES.
**/
- (BOOL) isChunked {
@synchronized(self) {
if (!_askedIfChunked) {
_chunked = !_finished;
}
LogTo(TDListenerVerbose, @"%@ answers isChunked=%d", self, _chunked);
return _chunked;
}
}
- (BOOL) delayResponseHeaders {
@synchronized(self) {
LogTo(TDListenerVerbose, @"%@ answers delayResponseHeaders=%d", self, !_response);
if (!_response)
_delayedHeaders = YES;
return !_response;
}
}
- (void) onResponseReady: (TDResponse*)response {
@synchronized(self) {
_response = [response retain];
LogTo(TDListener, @" %@ --> %i", self, _response.status);
if (_delayedHeaders)
[_connection responseHasAvailableData: self];
}
}
- (NSInteger) status {
LogTo(TDListenerVerbose, @"%@ answers status=%d", self, _response.status);
return _response.status;
}
- (NSDictionary *) httpHeaders {
LogTo(TDListenerVerbose, @"%@ answers httpHeaders={%u headers}", self, (unsigned)_response.headers.count);
return _response.headers;
}
- (void) onDataAvailable: (NSData*)data finished: (BOOL)finished {
@synchronized(self) {
LogTo(TDListenerVerbose, @"%@ adding %u bytes", self, (unsigned)data.length);
if (!_data) {
_data = [data copy];
_dataMutable = NO;
} else {
if (!_dataMutable) {
[_data autorelease];
_data = [_data mutableCopy];
_dataMutable = YES;
}
[(NSMutableData*)_data appendData: data];
}
if (finished)
[self onFinished];
else if (_chunked)
[_connection responseHasAvailableData: self];
}
}
@synthesize offset=_offset;
- (UInt64) contentLength {
@synchronized(self) {
if (!_finished)
return 0;
return _dataOffset + _data.length;
}
}
- (NSData*) readDataOfLength: (NSUInteger)length {
@synchronized(self) {
NSAssert(_offset >= _dataOffset, @"Invalid offset %llu, min is %llu", _offset, _dataOffset);
NSRange range;
range.location = (NSUInteger)(_offset - _dataOffset);
if (range.location >= _data.length) {
LogTo(TDListenerVerbose, @"%@ sending nil bytes", self);
return nil;
}
NSUInteger bytesAvailable = _data.length - range.location;
range.length = MIN(length, bytesAvailable);
NSData* result = [_data subdataWithRange: range];
_offset += range.length;
if (range.length == bytesAvailable) {
// Client has read all of the available data, so we can discard it
_dataOffset += _data.length;
[_data autorelease];
_data = nil;
}
LogTo(TDListenerVerbose, @"%@ sending %u bytes", self, (unsigned)result.length);
return result;
}
}
- (BOOL) isDone {
LogTo(TDListenerVerbose, @"%@ answers isDone=%d", self, _finished);
return _finished;
}
- (void) cleanUp {
// Break cycles:
_router.onResponseReady = nil;
_router.onDataAvailable = nil;
_router.onFinished = nil;
if (!_finished) {
_finished = true;
[self autorelease];
}
}
- (void) onFinished {
@synchronized(self) {
if (_finished)
return;
_askedIfChunked = true;
[self cleanUp];
LogTo(TDListenerVerbose, @"%@ Finished!", self);
if ((!_chunked || _offset == 0) && ![_router.request.HTTPMethod isEqualToString: @"HEAD"]) {
// Response finished immediately, before the connection asked for any data, so we're free
// to massage the response:
LogTo(TDListenerVerbose, @"%@ prettifying response body", self);
#if DEBUG
BOOL pretty = YES;
#else
BOOL pretty = [_router boolQuery: @"pretty"];
#endif
if (pretty) {
NSString* contentType = (_response.headers)[@"Content-Type"];
if ([contentType hasPrefix: @"application/json"] && _data.length < 100000) {
[_data release];
_data = [_response.body.asPrettyJSON mutableCopy];
}
}
}
[_connection responseHasAvailableData: self];
}
}
- (void)connectionDidClose {
@synchronized(self) {
_connection = nil;
[_data release];
_data = nil;
[self cleanUp];
}
}
@end