-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathServer.m
More file actions
307 lines (262 loc) · 10.9 KB
/
Server.m
File metadata and controls
307 lines (262 loc) · 10.9 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// Server.m
// Lesson 5
//
// Created by Jiva DeVoe on 9/24/13.
// Copyright (c) 2013 Random Ideas. All rights reserved.
//
#import "Server.h"
#import <sys/socket.h>
#import <netinet/in.h>
#import <CFNetwork/CFNetwork.h>
@interface Server ()
@property (strong, nonatomic) NSFileHandle *listeningSocketHandle;
@property (nonatomic) CFSocketRef socket;
@property (nonatomic) CFMutableDictionaryRef incomingRequests;
@property (strong, nonatomic) NSMutableDictionary *actions;
@property (strong, nonatomic) NSMutableDictionary *actionsWithDocs;
@end
@implementation Server
-(id)init
{
if((self = [super init]))
{
self.port = 8888;
self.serveFilesFromDocumentsDir = NO;
_incomingRequests = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
return self;
}
- (void)start
{
self.socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL);
if (!self.socket)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to create socket." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}
int reuse = true;
int fileDescriptor = CFSocketGetNative(self.socket);
setsockopt(fileDescriptor, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int));
struct sockaddr_in address;
memset(&address, 0, sizeof(address));
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(self.port);
CFDataRef addressData = CFDataCreate(NULL, (const UInt8 *)&address, sizeof(address));
CFAutorelease(addressData);
CFSocketSetAddress(self.socket, addressData);
self.listeningSocketHandle = [[NSFileHandle alloc] initWithFileDescriptor:fileDescriptor closeOnDealloc:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleIncomingConnection:) name:NSFileHandleConnectionAcceptedNotification object:nil];
[self.listeningSocketHandle acceptConnectionInBackgroundAndNotify];
NSLog(@"Server listening.");
}
- (void)stop
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleConnectionAcceptedNotification object:nil];
[self.listeningSocketHandle closeFile];
self.listeningSocketHandle = nil;
NSDictionary *incRequests = [(NSDictionary *)self.incomingRequests copy];
for (NSFileHandle *incomingFileHandle in incRequests)
{
[self closeClientConnectionForFileHandle:incomingFileHandle];
}
if (self.socket)
{
CFSocketInvalidate(self.socket);
CFRelease(self.socket);
self.socket = nil;
}
}
-(void)closeClientConnectionForFileHandle:(NSFileHandle *)fileHandle
{
[fileHandle closeFile];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleDataAvailableNotification object:fileHandle];
CFDictionaryRemoveValue(self.incomingRequests, (__bridge const void *)(fileHandle));
}
-(void)handleIncomingConnection:(NSNotification *)inNot
{
NSFileHandle *incomingFileHandle = [inNot userInfo][NSFileHandleNotificationFileHandleItem];
if(incomingFileHandle)
{
CFHTTPMessageRef msgRef = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, TRUE);
CFDictionaryAddValue(self.incomingRequests, (__bridge const void *)(incomingFileHandle), msgRef);
CFAutorelease(msgRef);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleIncomingData:) name:NSFileHandleDataAvailableNotification object:incomingFileHandle];
[incomingFileHandle waitForDataInBackgroundAndNotify];
}
[self.listeningSocketHandle acceptConnectionInBackgroundAndNotify];
}
-(void)handleIncomingData:(NSNotification *)inNot
{
NSFileHandle *incomingFileHandle = [inNot object];
NSData *data = [incomingFileHandle availableData];
if ([data length] == 0)
{
[self cancelReceiveForFileHandle:incomingFileHandle];
return;
}
CFHTTPMessageRef incomingRequest = (CFHTTPMessageRef)CFDictionaryGetValue(self.incomingRequests, CFBridgingRetain(incomingFileHandle));
if (!incomingRequest)
{
[self closeClientConnectionForFileHandle:incomingFileHandle];
return;
}
if (!CFHTTPMessageAppendBytes(incomingRequest, [data bytes], [data length]))
{
[self closeClientConnectionForFileHandle:incomingFileHandle];
return;
}
if(CFHTTPMessageIsHeaderComplete(incomingRequest))
{
[self respondToRequest:incomingRequest fromSocket:incomingFileHandle];
[self cancelReceiveForFileHandle:incomingFileHandle];
return;
}
[incomingFileHandle waitForDataInBackgroundAndNotify];
}
- (void)cancelReceiveForFileHandle:(NSFileHandle *)fileHandle
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleDataAvailableNotification object:fileHandle];
CFDictionaryRemoveValue(self.incomingRequests, (__bridge const void *)(fileHandle));
}
-(NSDictionary *)dictionaryFromPostParamsString:(NSString *)stringData
{
NSMutableDictionary *ret = [@{} mutableCopy];
NSArray *parts = [stringData componentsSeparatedByString:@"&"];
for(NSString *part in parts)
{
NSArray *components = [part componentsSeparatedByString:@"="];
if([components count] < 2)
continue;
NSString *key = [components[0] stringByRemovingPercentEncoding];
NSString *value = [components[1] stringByRemovingPercentEncoding];
ret[key] = value;
}
return ret;
}
-(void)respondToRequest:(CFHTTPMessageRef)request fromSocket:(NSFileHandle *)fileHandle
{
NSURL *requestURL = (__bridge NSURL *)(CFHTTPMessageCopyRequestURL(request));
NSString *method = CFBridgingRelease(CFHTTPMessageCopyRequestMethod(request));
if([method isEqualToString:@"GET"])
{
NSString *path = [requestURL path];
if(self.actions[path])
{
NSDictionary *params = [self dictionaryFromPostParamsString:[requestURL query]];
NSString *(^action)(NSString *path, NSString *method, NSDictionary *params) = self.actions[path];
[self sendString:action(path, method, params) toSocket:fileHandle];
}
else if([path isEqualToString:@"/"] || [path isEqualToString:@"/index.html"])
{
NSMutableString *ret = [@"<html><body>\n" mutableCopy];
[ret appendString:@"<dl>"];
for(NSString *path in self.actionsWithDocs)
{
NSString *docString = self.actionsWithDocs[path];
docString = [[[docString stringByReplacingOccurrencesOfString: @"\"" withString: @"""]
stringByReplacingOccurrencesOfString: @">" withString: @">"]
stringByReplacingOccurrencesOfString: @"<" withString: @"<"];
[ret appendFormat:@"<dt><a href=\"%@\">%@</a></dt><dd>%@</dd>", path, path, docString];
}
[ret appendString:@"</dl>"];
[ret appendString:@"</body></html>"];
[self sendString:ret toSocket:fileHandle];
}
else if([path isEqualToString:@"/index.json"])
{
NSString *ret = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:self.actionsWithDocs options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
[self sendString:ret toSocket:fileHandle];
}
else
{
if(!self.serveFilesFromDocumentsDir)
[self sendNotFoundErrorToFileHandle:fileHandle];
else
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = paths[0];
NSString *filePath = [docsDir stringByAppendingPathComponent:path];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
[self sendFileAtPath:filePath toFileHandle:fileHandle];
}
else
{
[self sendNotFoundErrorToFileHandle:fileHandle];
}
}
}
}
else if([method isEqualToString:@"POST"])
{
NSData *data = (__bridge NSData *)(CFHTTPMessageCopyBody(request));
NSString *stringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *params = [self dictionaryFromPostParamsString:stringData];
NSString *path = [requestURL path];
if(self.actions[path])
{
NSString *(^action)(NSString *path, NSString *method, NSDictionary *params) = self.actions[path];
[self sendString:action(path, method, params) toSocket:fileHandle];
}
}
}
-(void)sendString:(NSString *)str toSocket:(NSFileHandle *)fileHandle
{
CFHTTPMessageRef response = CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1);
CFHTTPMessageSetHeaderFieldValue(response, (CFStringRef)@"Content-Type", (CFStringRef)@"text/html");
CFHTTPMessageSetHeaderFieldValue(response, (CFStringRef)@"Connection", (CFStringRef)@"close");
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
CFHTTPMessageSetBody(response, (__bridge CFDataRef)data);
CFDataRef headerData = CFHTTPMessageCopySerializedMessage(response);
[fileHandle writeData:(__bridge NSData *)headerData];
CFRelease(headerData);
CFRelease(response);
[self closeClientConnectionForFileHandle:fileHandle];
}
-(void)sendFileAtPath:(NSString *)filePath toFileHandle:(NSFileHandle *)fileHandle
{
NSString *data = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[self sendString:data toSocket:fileHandle];
}
-(void)sendNotFoundErrorToFileHandle:(NSFileHandle *)fileHandle
{
CFHTTPMessageRef response = CFHTTPMessageCreateResponse(kCFAllocatorDefault, 404, NULL, kCFHTTPVersion1_1);
CFHTTPMessageSetHeaderFieldValue(response, (CFStringRef)@"Content-Type", (CFStringRef)@"text/html");
CFHTTPMessageSetHeaderFieldValue(response, (CFStringRef)@"Connection", (CFStringRef)@"close");
NSData *data = [@"404 not found" dataUsingEncoding:NSUTF8StringEncoding];
CFHTTPMessageSetBody(response, (__bridge CFDataRef)data);
CFDataRef headerData = CFHTTPMessageCopySerializedMessage(response);
@try
{
[fileHandle writeData:(__bridge NSData *)headerData];
}
@catch (NSException *exception)
{
// you can ignore exceptions here, it just means the client reset the connection.
}
@finally
{
CFRelease(headerData);
CFRelease(response);
[self closeClientConnectionForFileHandle:fileHandle];
}
}
-(void)addAction:(NSString *(^)(NSString *path, NSString *method, NSDictionary *args))action forPath:(NSString *)path withDocString:(NSString *)docString;
{
if(!self.actions)
{
self.actions = [@{} mutableCopy];
self.actionsWithDocs = [@{} mutableCopy];
}
self.actions[path] = action;
self.actionsWithDocs[path] = docString;
}
-(void)dropActionForPath:(NSString *)path;
{
[self.actions removeObjectForKey:path];
}
@end