-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.cpp
140 lines (108 loc) · 5.39 KB
/
Server.cpp
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
/* Cycript - The Truly Universal Scripting Language
* Copyright (C) 2009-2016 Jay Freeman (saurik)
*/
/* GNU Affero General Public License, Version 3 {{{ */
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#include <CoreFoundation/CFLogUtilities.h>
#include <CFNetwork/CFNetwork.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include "Pooling.hpp"
struct Client {
CFHTTPMessageRef message_;
CFSocketRef socket_;
};
static void OnData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) {
switch (type) {
case kCFSocketDataCallBack:
CFDataRef data(reinterpret_cast<CFDataRef>(value));
Client *client(reinterpret_cast<Client *>(info));
if (client->message_ == NULL)
client->message_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, TRUE);
if (!CFHTTPMessageAppendBytes(client->message_, CFDataGetBytePtr(data), CFDataGetLength(data)))
CFLog(kCFLogLevelError, CFSTR("CFHTTPMessageAppendBytes()"));
else if (CFHTTPMessageIsHeaderComplete(client->message_)) {
CFURLRef url(CFHTTPMessageCopyRequestURL(client->message_));
Boolean absolute;
CFStringRef path(CFURLCopyStrictPath(url, &absolute));
CFRelease(client->message_);
CFStringRef code(CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault, path, CFSTR("")));
CFRelease(path);
JSStringRef script(JSStringCreateWithCFString(code));
CFRelease(code);
JSValueRef result(JSEvaluateScript(CYGetJSContext(), script, NULL, NULL, 0, NULL));
JSStringRelease(script);
CFHTTPMessageRef response(CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1));
CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Type"), CFSTR("application/json; charset=utf-8"));
CFStringRef json(CYCopyJSONString(CYGetJSContext(), result, NULL));
CFDataRef body(CFStringCreateExternalRepresentation(kCFAllocatorDefault, json, kCFStringEncodingUTF8, NULL));
CFRelease(json);
CFStringRef length(CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%u"), CFDataGetLength(body)));
CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Length"), length);
CFRelease(length);
CFHTTPMessageSetBody(response, body);
CFRelease(body);
CFDataRef serialized(CFHTTPMessageCopySerializedMessage(response));
CFRelease(response);
CFSocketSendData(socket, NULL, serialized, 0);
CFRelease(serialized);
CFRelease(url);
}
break;
}
}
static void OnAccept(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) {
switch (type) {
case kCFSocketAcceptCallBack:
Client *client(new Client());
client->message_ = NULL;
CFSocketContext context;
context.version = 0;
context.info = client;
context.retain = NULL;
context.release = NULL;
context.copyDescription = NULL;
client->socket_ = CFSocketCreateWithNative(kCFAllocatorDefault, *reinterpret_cast<const CFSocketNativeHandle *>(value), kCFSocketDataCallBack, &OnData, &context);
CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, client->socket_, 0), kCFRunLoopDefaultMode);
break;
}
}
int main(int argc, char *argv[]) {
{
struct sockaddr_in address;
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(787);
CFDataRef data(CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&address), sizeof(address)));
CFSocketSignature signature;
signature.protocolFamily = AF_INET;
signature.socketType = SOCK_STREAM;
signature.protocol = IPPROTO_TCP;
signature.address = data;
CFSocketRef socket(CFSocketCreateWithSocketSignature(kCFAllocatorDefault, &signature, kCFSocketAcceptCallBack, &OnAccept, NULL));
CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0), kCFRunLoopDefaultMode);
}
{
CYServer *server(new CYServer());
server->socket_ = _syscall(socket(PF_UNIX, SOCK_STREAM, 0));
struct sockaddr_un address;
memset(&address, 0, sizeof(address));
address.sun_family = AF_UNIX;
sprintf(address.sun_path, "/tmp/.s.cy");
}
}