-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.C
More file actions
470 lines (459 loc) · 11.7 KB
/
server.C
File metadata and controls
470 lines (459 loc) · 11.7 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
* */
#include <cpoll/cpoll.H>
#include <unistd.h>
#include <iostream>
#include <signal.h>
#include <cppsp/page.H>
#include <cppsp/cppsp_cpoll.H>
#include <cppsp/common.H>
#include <sys/stat.h>
using namespace std;
using namespace CP;
using namespace cppsp;
using namespace RGC;
#define rmb() /**/
#define wmb() /**/
#define CPPSP_SENDFILE_MIN_SIZE (1024*1024)
#define CPPSP_SENDFILE_BUFSIZE (1024*16)
namespace cppspServer
{
#define CACHELINE_SIZE 64
//currently not used
template<class T> class RingBuffer
{
public:
union {
struct {
T* items;
int length;
};
char padding1[CACHELINE_SIZE];
};
union {
int rpos;
char padding2[CACHELINE_SIZE];
};
union {
int wpos;
char padding3[CACHELINE_SIZE];
};
RingBuffer(int length): length(length),rpos(0), wpos(0) {
items=new T[length];
}
inline int __getlength(int i1, int i2, int wrap)
{
return (i2 < i1 ? i2 + wrap : i2) - i1;
}
inline bool canEnqueue()
{
return __getlength(rpos, wpos, (length*2)) < length;
}
inline bool canDequeue()
{
return __getlength(rpos, wpos, (length*2)) > 0;
}
T* beginEnqueue() {
if(canEnqueue()) return items+(wpos%length);
else return NULL;
}
void endEnqueue() {
wmb();
wpos=(wpos+1)%(length*2);
}
T* beginDequeue() {
if(!canDequeue()) return NULL;
rmb();
return items+(rpos%length);
}
void endDequeue() {
rpos=(rpos+1)%(length*2);
}
};
template<class T>
class ObjectPool: public RGC::Object
{
public:
T** items;
int size;
int length;
ObjectPool(int size):size(size),length(0) {
items=new T*[size];
}
~ObjectPool() {
for(int i=0;i<length;i++) delete items[i];
delete[] items;
}
T* tryGet() {
if(length<=0) return NULL;
return items[--length];
}
void put(T* obj) {
if(length>=size) delete obj;
else {
items[length++]=obj;
}
}
};
class Host: public cppsp::DefaultHost {
public:
cppsp::Server server;
Timer t;
ObjectPool<Response> _responsePool;
int _lastRequests=0;
int timerState=0;
void timerCB(int i) {
if(!updateTime() && timerState==1) {
disableTimer();
return;
}
if(timerState==2 && performanceCounters.totalRequestsReceived<=_lastRequests) {
slowTimer();
}
_lastRequests=performanceCounters.totalRequestsReceived;
}
Host(Poll* p, string root): _responsePool(128) {
this->poll=p;
defaultServer=&server;
addServer(&server);
server.root=root;
updateTime();
t.setCallback({&Host::timerCB,this});
p->add(t);
}
void loadDefaultMimeDB() {
File f("/usr/share/mime/globs",O_RDONLY);
f.setBlocking(true);
StreamReader sr(f);
mgr->loadMimeDB(sr);
}
void enableTimer() {
if(timerState==0) printf("enabling timer\n");
t.setInterval(timerShortInterval*1000);
timerState=2;
updateTime(true); //true indicates to inhibit cache cleaning
}
void slowTimer() {
t.setInterval(timerLongInterval*1000);
timerState=1;
}
void disableTimer() {
printf("disabling timer\n");
t.setInterval(0);
timerState=0;
}
inline void _requestReceived() {
if(unlikely(timerState<2)) enableTimer();
performanceCounters.totalRequestsReceived++;
}
AsyncValue<Handler> routeStaticRequest(String path) override;
AsyncValue<Handler> routeDynamicRequest(String path) override;
};
typedef Host Server;
class Request:public cppsp::CPollRequest
{
public:
Request(CP::Socket& s, CP::StringPool* sp) :
CPollRequest(s, sp) {
}
void* _handler;
};
//handles a single connection
//just instantiate-and-forget; it will self-destruct when connection is closed
struct handler:public RGC::Object {
Host& thr;
CP::Poll& p;
Socket& s;
StringPool sp;
Request req;
cppsp::Server* server;
cppsp::Response* resp;
//Page* p;
//MemoryStream ms;
uint8_t* buf;
union {
iovec iov[2];
int64_t _sendFileOffset;
};
staticPage* _staticPage;
bool readLoopRunning;
bool shouldContinueReading;
bool keepAlive;
handler(Host& thr,CP::Poll& poll,Socket& s):thr(thr),
p(poll),s(s),sp(2048),req(this->s,&sp) {
//printf("handler()\n");
req._handler=this;
poll.add(this->s);
s.retain();
readLoop();
}
void readLoop() {
readLoopRunning=true;
shouldContinueReading=true;
while(shouldContinueReading && req.readRequest({&handler::readCB, this})) readCB(true);
readLoopRunning=false;
}
void readCB(bool success) {
shouldContinueReading=false;
if(unlikely(!success)) {
destruct();
return;
}
//if((sp=thr._stringPoolPool.tryGet())==nullptr) sp=new StringPool();
if((resp=thr._responsePool.tryGet())) resp->init(this->s,&sp);
else resp=new Response(this->s,&sp);
thr._requestReceived();
//keepAlive=true;
auto it=req.headers.find("connection");
if(it!=req.headers.end() && (*it).value=="close")keepAlive=false;
else keepAlive=true;
resp->headers.insert({"Connection", keepAlive?"keep-alive":"close"});
/*char* date=sp.beginAdd(32);
tm time;
gmtime_r(&thr.curClockTime.tv_sec,&time);
int l=rfctime(time,date);
if(l>32)l=32;
sp.endAdd(l);
*/
resp->headers.insert({"Date", sp.addString(thr.curRFCTime)});
//perform vhost routing
server=thr.preRouteRequest(req);
server->performanceCounters.totalRequestsReceived++;
try {
server->handleRequest(req,*resp,{&handler::finalize,this});
} catch(exception& ex) {
server->handleError(req,*resp,ex,{&handler::finalize,this});
}
}
static inline int itoa64(int64_t i, char* b) {
static char const digit[] = "0123456789";
char* p = b;
int l;
p += (l=((i==0?0:int(log10(i))) + 1));
*p = '\0';
do { //Move back, inserting digits as u go
*--p = digit[i % 10];
i = i / 10;
} while (i);
return l;
}
void handleStatic(staticPage* Sp) {
Response& resp(*this->resp);
(_staticPage=Sp)->retain();
try {
int bufferL = resp.buffer.length();
if(Sp->mime.length()>0)resp.headers["Content-Type"]=Sp->mime;
{
char* tmps = sp.beginAdd(22);
int l = itoa64(Sp->fileLen, tmps);
sp.endAdd(l);
resp.headers.insert({"Content-Length", { tmps, l }});
StreamWriter sw(resp.buffer);
resp.serializeHeaders(sw);
}
if(Sp->fileLen>=CPPSP_SENDFILE_MIN_SIZE) {
_sendFileOffset=0;
s.sendAll(resp.buffer.data()+bufferL,resp.buffer.length()-bufferL,
MSG_MORE, { &handler::sendHeadersCB, this });
} else {
String data=Sp->data;
iov[0]= {resp.buffer.data()+bufferL, (size_t)(resp.buffer.length()-bufferL)};
iov[1]= {data.data(), (size_t)data.length()};
resp.outputStream->writevAll(iov, data.length()<=0?1:2, { &handler::writevCB, this });
}
} catch(exception& ex) {
Sp->release();
server->handleError(req,resp,ex,{&handler::finalize,this});
}
}
void sendHeadersCB(int r) {
if(r<0) {
_staticPage->release();
end();
return;
}
_beginSendFile();
}
void _beginSendFile() {
s.sendFileFrom(_staticPage->fd,_sendFileOffset,CPPSP_SENDFILE_BUFSIZE,{&handler::sendFileCB,this});
}
void sendFileCB(int r) {
if(r<0) {
_staticPage->release();
end();
} else if(r==0) {
_staticPage->release();
finalize();
} else {
_sendFileOffset+=(int64_t)r;
_beginSendFile();
}
}
void handleDynamic(loadedPage* lp) {
Response& resp(*this->resp);
Page* p=lp->doCreate(&sp);
//hold a strong reference to lp so that if cleanCache() etc is called by application code,
//the application does not unload itself, causing a segfault
p->lp=lp;
p->sp=&sp;
p->request=&req;
p->response=&resp;
p->poll=&this->p;
p->server=server;
p->handleRequest({&handler::handleRequestCB,this});
}
void sockReadCB(int r) {
if(r<=0) {
free(buf);
destruct();
}
}
void flushCB(Response& resp) {
//s->shutdown(SHUT_WR);
//release();
finalize();
}
void writevCB(int i) {
_staticPage->release();
if(likely(i>=0)) finalize();
else end();
}
void handleRequestCB() {
//s->shutdown(SHUT_WR);
//release();
//s->repeatRead(buf,sizeof(buf),{&handler::sockReadCB,this});
finalize();
}
void finalize() {
if(resp->closed) {
end(); return;
}
cleanup();
if(keepAlive) {
req.init(s,&sp);
if(readLoopRunning) shouldContinueReading=true;
else readLoop();
} else {
s.shutdown(SHUT_WR);
buf=(uint8_t*)malloc(4096);
s.repeatRead(buf,4096,{&handler::sockReadCB,this});
}
}
//cleanup and terminate the connection
void end() {
cleanup();
destruct();
}
//deallocate resources after a request has been completed
void cleanup() {
server->performanceCounters.totalRequestsFinished++;
thr.performanceCounters.totalRequestsFinished++;
req.reset();
resp->reset();
thr._responsePool.put(resp);
resp=nullptr;
sp.clear();
}
~handler() {
//printf("~handler()\n");
s.release();
}
};
void staticHandler(staticPage* v,cppsp::Request& req, Response& resp, Delegate<void()> cb) {
cppspServer::Request& r=static_cast<cppspServer::Request&>(req);
(*(handler*)r._handler).handleStatic(v);
}
void dynamicHandler(loadedPage* v,cppsp::Request& req, Response& resp, Delegate<void()> cb) {
cppspServer::Request& r=static_cast<cppspServer::Request&>(req);
(*(handler*)r._handler).handleDynamic(v);
}
AsyncValue<Handler> Host::routeStaticRequest(String path) {
struct stat st;
string tmps=path.toSTDString();
if(::stat(tmps.c_str(), &st)!=0) throwUNIXException(tmps);
staticPage* sp;
if(st.st_size>=CPPSP_SENDFILE_MIN_SIZE)
sp=loadStaticPage(path,true,false);
else
sp=loadStaticPage(path);
return Handler(&staticHandler,sp);
}
struct requestRouterState
{
Delegate<void(Handler,exception*)> cb;
void operator()(loadedPage* lp, exception* ex) {
if(lp==NULL)cb(nullptr,ex);
else cb(Handler(&dynamicHandler,lp),nullptr);
delete this;
}
};
AsyncValue<Handler> Host::routeDynamicRequest(String path) {
auto lp=loadPage(path);
if(lp) {
return Handler(&dynamicHandler,lp());
}
requestRouterState* st=new requestRouterState();
lp.wait(st);
return Future<Handler>(&st->cb);
}
}
namespace cppsp {
struct HandlerBase
{
Request* request; Response* response; Delegate<void()> cb;
};
template<class T>
Handler makeHandler() {
struct H: public RGC::Object
{
T* construct(RGC::Allocator& alloc) {
return alloc.New<T>();
}
void operator()(Request& req, Response& resp, Delegate<void()> cb) {
T* tmp=construct(*req.sp);
tmp->request=&req;
tmp->response=&resp;
tmp->cb=cb;
tmp->process();
}
};
return newObj<H>();
}
};
namespace cppspEmbedded
{
class Server
{
public:
cppspServer::Host host;
Server(Poll* p, string root):host(p,root) {
}
void listen(Socket& s) {
struct CB: public RGC::Object {
Server* s;
CB(Server* s):s(s){}
void operator()(Socket* clientSock) {
new cppspServer::handler(s->host,*s->host.poll,*clientSock);
clientSock->release();
}
};
s.repeatAccept(newObj<CB>(this));
}
HandleRequestChain::item* attachHandler(const Handler& h) {
return host.defaultServer->handleRequest.attach(h);
}
void detachHandler(HandleRequestChain::item* it) {
return host.defaultServer->handleRequest.detach(it);
}
};
};