forked from robbiehanson/CocoaHTTPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPResponseTest.m
197 lines (138 loc) · 3.71 KB
/
HTTPResponseTest.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
#import "HTTPResponseTest.h"
#import "HTTPConnection.h"
#import "HTTPLogging.h"
// Does ARC support support GCD objects?
// It does if the minimum deployment target is iOS 6+ or Mac OS X 8+
#if TARGET_OS_IPHONE
// Compiling for iOS
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000 // iOS 6.0 or later
#define NEEDS_DISPATCH_RETAIN_RELEASE 0
#else // iOS 5.X or earlier
#define NEEDS_DISPATCH_RETAIN_RELEASE 1
#endif
#else
// Compiling for Mac OS X
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080 // Mac OS X 10.8 or later
#define NEEDS_DISPATCH_RETAIN_RELEASE 0
#else
#define NEEDS_DISPATCH_RETAIN_RELEASE 1 // Mac OS X 10.7 or earlier
#endif
#endif
// Log levels: off, error, warn, info, verbose
// Other flags: trace
static const int httpLogLevel = HTTP_LOG_LEVEL_OFF; // | HTTP_LOG_FLAG_TRACE;
//
// This class is a UnitTest for the delayResponseHeaders capability of HTTPConnection
//
@interface HTTPResponseTest (PrivateAPI)
- (void)doAsyncStuff;
- (void)asyncStuffFinished;
@end
@implementation HTTPResponseTest
- (id)initWithConnection:(HTTPConnection *)parent
{
if ((self = [super init]))
{
HTTPLogTrace();
connection = parent;
responseQueue = dispatch_queue_create("HTTPResponseTest", NULL);
readyToSendResponseHeaders = NO;
[self doAsyncStuff];
}
return self;
}
- (void)doAsyncStuff
{
HTTPLogTrace();
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_async(concurrentQueue, ^{ @autoreleasepool {
// Simulate a long-running asynchronous task...
[NSThread sleepForTimeInterval:5.0];
// Simulate completion callback
dispatch_async(responseQueue, ^{ @autoreleasepool {
[self asyncStuffFinished];
}});
}});
}
- (void)asyncStuffFinished
{
// This method is executed on the responseQueue
HTTPLogTrace();
// Enable flag that indicates we have enough information to send the response headers.
readyToSendResponseHeaders = YES;
// Then notify the connection that "something has changed".
// The 'responseHasAvailableData:' method is thread-safe.
// It knows what state the connection is in, and will "do the right thing".
// In this case, it will requery us via delayResponseHeaders to see if it can send the headers yet.
[connection responseHasAvailableData:self];
}
- (BOOL)delayResponseHeaders
{
HTTPLogTrace2(@"%@[%p] %@ -> %@", THIS_FILE, self, THIS_METHOD, (readyToSendResponseHeaders ? @"NO" : @"YES"));
__block BOOL delayResponseHeaders = NO;
dispatch_sync(responseQueue, ^{
delayResponseHeaders = !readyToSendResponseHeaders;
});
return delayResponseHeaders;
}
- (void)connectionDidClose
{
HTTPLogTrace();
dispatch_sync(responseQueue, ^{
connection = nil;
});
}
- (UInt64)contentLength
{
HTTPLogTrace();
__block UInt64 contentLength = 0;
dispatch_sync(responseQueue, ^{
// Normal code would go here
contentLength = 0;
});
return contentLength;
}
- (UInt64)offset
{
HTTPLogTrace();
__block UInt64 offset = 0;
dispatch_sync(responseQueue, ^{
// Normal code would go here
offset = 0;
});
return offset;
}
- (void)setOffset:(UInt64)offset
{
HTTPLogTrace();
dispatch_sync(responseQueue, ^{
// Normal code would go here
});
}
- (NSData *)readDataOfLength:(NSUInteger)length
{
HTTPLogTrace();
__block NSData *data = nil;
dispatch_sync(responseQueue, ^{
// Normal code would go here
});
return data;
}
- (BOOL)isDone
{
HTTPLogTrace();
__block BOOL isDone = NO;
dispatch_sync(responseQueue, ^{
// Normal code would go here
isDone = YES;
});
return isDone;
}
- (void)dealloc
{
HTTPLogTrace();
#if NEEDS_DISPATCH_RETAIN_RELEASE
dispatch_release(responseQueue);
#endif
}
@end