forked from PerfectlySoft/Perfect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_perfect.cpp
674 lines (611 loc) · 16.1 KB
/
mod_perfect.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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//
// Copyright (C) 2015 PerfectlySoft, Inc.
//
// 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, as supplemented by the
// Perfect Additional Terms.
//
// 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, as supplemented by the
// Perfect Additional Terms, for more details.
//
// You should have received a copy of the GNU Affero General Public License
// and the Perfect Additional Terms that immediately follow the terms and
// conditions of the GNU Affero General Public License along with this
// program. If not, see <http://www.perfect.org/AGPL_3_0_With_Perfect_Additional_Terms.txt>.
//
#include "mod_perfect.h"
#include <string>
#ifndef WIN32
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <syslog.h>
#include <ctype.h>
#include <sys/un.h>
#define LP_OPEN_FLAGS (O_RDONLY)
#define CACHE_HOSTS 1
#define SOCKET int
#define CLOSE_SOCKET(S) close(S)
#else
#include <winsock2.h>
#include <stdint.h>
#define LP_OPEN_FLAGS (O_RDONLY | O_BINARY)
#define CACHE_HOSTS 0
#define usleep(N) Sleep((N)/1000)
#define CLOSE_SOCKET(S) CloseHandle((HANDLE)S)
#endif
#include <sys/stat.h>
#include <http_request.h>
#include <http_log.h>
#include <apr_strings.h>
#include <util_script.h>
#include "fastcgi.h"
#include <sqlite3.h>
#ifndef INVALID_SOCKET
#define INVALID_SOCKET (-1)
#endif
#ifdef WIN32
static void child_init_handler(apr_pool_t *, server_rec *)
{
}
inline std::string & fixNamedPipe(std::string & inout)
{
std::string front("\\\\.\\pipe\\"), nout;
const size_t maxLen = 256-front.size();
bool lastAt = false;
for (std::string::iterator it = inout.begin(), end = inout.end(); it != end; ++it)
{
if (*it == '\\' || *it == '/')
{
if (!lastAt)
{
nout.append(1, '@');
lastAt = true;
}
}
else
{
nout.append(1, *it);
lastAt = false;
}
}
if (nout.size() > maxLen)
inout = front+nout.substr(nout.size()-maxLen, maxLen);
else
inout = front+nout;
return inout;
}
inline const char * _formatMessage(DWORD code, std::string & outStr)
{
char lpMsgBuf[256];
if (int num = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), lpMsgBuf, 256, NULL))
{
lpMsgBuf[num] = '\0';
outStr = lpMsgBuf;
return outStr.c_str();
}
return "Unknown Error";
}
#else
#endif
#if (AP_SERVER_MAJORVERSION_NUMBER == 2) && (AP_SERVER_MINORVERSION_NUMBER < 4)
#define AP_LOG_FAIL(F,S) \
ap_log_cerror(__FILE__, __LINE__, APLOG_CRIT, 0, r->connection, F,S);
#define AP_LOG_FAIL_2(F,S,S2) \
ap_log_cerror(__FILE__, __LINE__, APLOG_CRIT, 0, r->connection, F,S,S2);
#define AP_LOG_FAIL_3(F,S,S2,S3) \
ap_log_cerror(__FILE__, __LINE__, APLOG_CRIT, 0, r->connection, F,S,S2,S3);
#define AP_LOG_FAIL_4(F,S,S2,S3,S4) \
ap_log_cerror(__FILE__, __LINE__, APLOG_CRIT, 0, r->connection, F,S,S2,S3,S4);
#else
#define AP_LOG_FAIL(F,S) \
ap_log_cerror(__FILE__, __LINE__, APLOG_CRIT, 0, 500, r->connection, F,S);
#define AP_LOG_FAIL_2(F,S,S2) \
ap_log_cerror(__FILE__, __LINE__, APLOG_CRIT, 0, 500, r->connection, F,S,S2);
#define AP_LOG_FAIL_3(F,S,S2,S3) \
ap_log_cerror(__FILE__, __LINE__, APLOG_CRIT, 0, 500, r->connection, F,S,S2,S3);
#define AP_LOG_FAIL_4(F,S,S2,S3,S4) \
ap_log_cerror(__FILE__, __LINE__, APLOG_CRIT, 0, 500, r->connection, F,S,S2,S3,S4);
#endif
#define X_BUFFER_SIZE (1024*128) // when reading writing X_STDIN
enum { FCGI_X_STDIN = 50 };
static int perfect_handler (request_rec *r);
#if CACHE_HOSTS
static char * cached_host = (char*)malloc(1024);
static char * cached_home = (char*)malloc(2048);
inline const char * is_cached_host(const char * host, server_rec *s)
{
if (cached_host && strcasecmp(cached_host, host) == 0)
{
// ap_log_error(__FILE__, __LINE__, APLOG_NOTICE, 0, s, "%s %s", "cached host", host);
return cached_home;
}
// ap_log_error(__FILE__, __LINE__, APLOG_NOTICE, 0, s, "%s %s", "uncached host", host);
return NULL;
}
inline void cache_host(const char * host, const char * home, server_rec *s)
{
size_t len = strlen(host),
len2 = strlen(home);
strncpy(cached_host, host, len<1023?len:1023);
strncpy(cached_home, home, len2<2047?len2:2047);
cached_host[len<1023?len:1023] = cached_home[len2<2047?len2:2047] = 0;
// ap_log_error(__FILE__, __LINE__, APLOG_NOTICE, 0, s, "%s %s %s", "caching host", host, home);
}
#endif
struct header_gather
{
std::string buffer;
request_rec * r;
};
#ifdef _WINDOWS
_declspec(dllexport)
#else
static
#endif
void register_9hooks(apr_pool_t *p);
void register_9hooks(apr_pool_t *p)
{
ap_hook_handler(perfect_handler, NULL, NULL, APR_HOOK_MIDDLE);
#ifdef WIN32
ap_hook_child_init(child_init_handler,NULL,NULL,APR_HOOK_MIDDLE);
#endif
// ADD A WEBSOCKET HANDLER
}
static void writeRaw(std::string & buffer, const void * data, size_t size)
{
buffer.append((const char*)data, size);
}
static void writeMsg(request_rec *r, std::string & buffer, uint8_t type, const void * data, uint16_t size)
{
// AP_LOG_FAIL_3("%s %d %d", "writeMsg", (int)type, (int)size);
FCGI_Header fcgiHead = {
FCGI_VERSION_1,
type,
0,
htons(size),
static_cast<uint8_t>(size%8),
0};
writeRaw(buffer, &fcgiHead, sizeof(fcgiHead));
writeRaw(buffer, data, size);
if (size%8)
{
uint8_t z[7] = {0};
writeRaw(buffer, z, size%8);
}
}
inline void addNameValue(request_rec *r, std::string & buffer, const char * name, const char * value)
{
if (!name)
return;
if (!value)
value = "";
// AP_LOG_FAIL_2("(%s = %s)", name, value);
size_t nLen = strlen(name),
vLen = strlen(value);
if (nLen > 0x7fffffff)
nLen = 0x7fffffff;
if (vLen > 0x7fffffff)
vLen = 0x7fffffff;
if (nLen > 127)
{
buffer.append(1, ((nLen >> 24) & 0xff) | 0x80);
buffer.append(1, (nLen >> 16) & 0xff);
buffer.append(1, (nLen >> 8) & 0xff);
buffer.append(1, (nLen >> 0) & 0xff);
}
else
{
buffer.append(1, (nLen >> 0) & 0xff);
}
if (vLen > 127)
{
buffer.append(1, ((vLen >> 24) & 0xff) | 0x80);
buffer.append(1, (vLen >> 16) & 0xff);
buffer.append(1, (vLen >> 8) & 0xff);
buffer.append(1, (vLen >> 0) & 0xff);
}
else
{
buffer.append(1, (vLen >> 0) & 0xff);
}
buffer.append(name);
buffer.append(value);
}
inline void addNameValue(request_rec *r, std::string & buffer, const char * name, int value)
{
char tmp[1024];
sprintf(tmp, "%d", value);
addNameValue(r, buffer, name, tmp);
}
static int find_initial_headers(void * data, const char * key, const char * val)
{
header_gather * fnd = (header_gather*)data;
addNameValue(fnd->r, fnd->buffer, key, val);
return TRUE;
}
static int socket_read_content(request_rec *r, SOCKET sock, char * data, size_t len, size_t padding)
{
while(len > 0)
{
#ifdef WIN32
DWORD read = 0;
BOOL res = ReadFile((HANDLE)sock, data, (DWORD)len, &read, NULL);
if (!res)
return -1;
len -= read;
data += read;
#else
size_t res = recv(sock, data, (int)len, 0);
if (res < 1)
{
AP_LOG_FAIL_4("%s %d %d %s", "Error from recv:", (int)res, (int)errno, strerror(errno));
return -1;
}
len -= res;
data += res;
#endif
}
if (padding > 0)
{
char pd[7] = {0};
return socket_read_content(r, sock, pd, padding, 0);
}
return 0;
}
static int socket_read_header(request_rec *r, SOCKET sock, FCGI_Header * header)
{
size_t len = FCGI_HEADER_LEN;
char * data = (char*)header;
while(len > 0)
{
#ifdef WIN32
DWORD read = 0;
BOOL res = ReadFile((HANDLE)sock, data, (DWORD)len, &read, NULL);
if (!res)
return -1;
len -= read;
data += read;
#else
size_t res = recv(sock, data, (int)len, 0);
if (res < 1)
return -1;
len -= res;
data += res;
#endif
}
return 0;
}
static int socket_write(request_rec *r, SOCKET sock, const char * data, size_t len)
{
while(len > 0)
{
#ifdef WIN32
DWORD wrote = 0;
BOOL res = WriteFile((HANDLE)sock, data, (DWORD)len, &wrote, NULL);
if (!res)
return -1;
len -= wrote;
data += wrote;
#else
ssize_t res = send(sock, data, (int)len, 0);
if (res == -1)
return -1;
len -= res;
data += res;
#endif
}
return 0;
}
static int pull_header_line(const char *& data, uint16_t & len,
std::string & name, std::string & value)
{
if (data[0] == '\r')
{
++data;
--len;
if (data[0] == '\n')
{
++data;
--len;
}
return 0;
}
if (data[0] == '\n')
{
++data;
--len;
return 0;
}
while(len)
{
if (*data == ':')
{
++data;
--len;
while(len && isspace(*data))
{
++data;
--len;
}
break;
}
name.append(1, *data);
++data;
--len;
}
while(len)
{
if (*data == '\r')
{
++data;
--len;
if (*data == '\n')
{
++data;
--len;
}
if (*data == ' ' || *data == '\t') // wrapped
{
while(len && (*data == ' ' || *data == '\t'))
{
++data;
--len;
}
}
else
{
break;
}
}
value.append(1, *data);
++data;
--len;
}
return 1;
}
int perfect_handler(request_rec *r)
{
// AP_LOG_FAIL_2("%s %s", "got request", r->handler);
if (strncmp(r->handler,"perfect", 7) != 0 && strcmp(r->handler, "application/x-httpd-perfect") != 0)
return DECLINED;
#ifdef WIN32
std::string matchHome,
matchSocket;
#else
const char * matchHome = NULL,
* matchSocket = NULL;
#endif
int res = 0;
FCGI_BeginRequestBody brb = {FCGI_RESPONDER, 0, {0,0,0,0,0}};
std::string body;
header_gather fnd;
matchHome = ap_context_document_root(r);
matchSocket = "/../perfect.fastcgi.sock";
SOCKET sock = INVALID_SOCKET;
#ifdef WIN32
DWORD error;
fixNamedPipe(matchSocket);
do {
sock = (SOCKET)CreateFileA(matchSocket.c_str(), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
error = GetLastError();
} while(sock == INVALID_SOCKET && error == ERROR_PIPE_BUSY);
if (prep)
sqlite3_finalize(prep);
if (sql)
sqlite3_close(sql);
if (sock == INVALID_SOCKET)
{
std::string errorS;
AP_LOG_FAIL_4("%s%s %d %s", "Unable to connect on: ", matchSocket.c_str(), error, _formatMessage(error, errorS));
goto close_denied;
}
#else
sock = socket(AF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, matchHome);
strcat(addr.sun_path, matchSocket);
res = connect(sock, (struct sockaddr*)&addr, sizeof(addr));
if (res != 0)
{
close(sock);
AP_LOG_FAIL_4("%s%s %d %s", "Unable to connect on: ", addr.sun_path, errno, strerror(errno));
return HTTP_INTERNAL_SERVER_ERROR;
}
int sockBufferSize = X_BUFFER_SIZE;
res = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sockBufferSize, sizeof(sockBufferSize));
if (res != 0)
{
AP_LOG_FAIL("%s", "Couldn't set SO_SNDBUF");
}
res = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &sockBufferSize, sizeof(sockBufferSize));
if (res != 0)
{
AP_LOG_FAIL("%s", "Couldn't set SO_RCVBUF");
}
#endif
fnd.r = r;
writeMsg(r, body, FCGI_BEGIN_REQUEST, &brb, sizeof(brb));
ap_add_common_vars(r);
ap_add_cgi_vars(r);
apr_table_do(find_initial_headers, &fnd, r->subprocess_env, NULL);
const char * authStr = apr_table_get(r->headers_in, "Authorization");
if (authStr)
addNameValue(r, fnd.buffer, "HTTP_AUTHORIZATION", authStr);
addNameValue(r, fnd.buffer, "L_APACHE_HANDLER", r->handler);
addNameValue(r, fnd.buffer, "", "");
writeMsg(r, body, FCGI_PARAMS, fnd.buffer.c_str(), (uint16_t)fnd.buffer.size());
res = socket_write(r, sock, body.c_str(), body.size());
if (res != 0)
{
CLOSE_SOCKET(sock);
AP_LOG_FAIL("%s", "Unable to initiate request");
return HTTP_INTERNAL_SERVER_ERROR;
}
body.clear();
// POST ARGS
int toRead = 0;
if ( ap_setup_client_block(r, REQUEST_CHUNKED_ERROR) == OK )
{
if ( ap_should_client_block(r) == 1)
{
std::string buffers;
char buffer[X_BUFFER_SIZE];
long readLen = 0;
toRead = (int)r->remaining;
uint32_t readSize = X_BUFFER_SIZE;
// bool isX = toRead > X_BUFFER_SIZE; // extension for greater throughput
uint32_t swappedRead = htonl(toRead);
writeMsg(r, body, FCGI_X_STDIN, (void*)&swappedRead, sizeof(uint32_t));
while ( (readLen = ap_get_client_block(r, buffer, readSize)) > 0 )
{
// AP_LOG_FAIL_3("%s %d %d", "Read post ", readLen, toRead);
if (buffers.size() + readLen > X_BUFFER_SIZE)
{
body.append(buffers);
res = socket_write(r, sock, body.c_str(), body.size());
// AP_LOG_FAIL_3("%s %ld %d", "wrote 1", body.size(), toRead);
if (res != 0)
{
CLOSE_SOCKET(sock);
AP_LOG_FAIL("%s", "Unable to send post");
return HTTP_INTERNAL_SERVER_ERROR;
}
body.clear();
buffers.clear();
}
buffers.append(buffer, readLen);
toRead -= readLen;
readSize = toRead < X_BUFFER_SIZE ? toRead : X_BUFFER_SIZE;
}
if (readLen == -1)
{
CLOSE_SOCKET(sock);// client terminated
return HTTP_INTERNAL_SERVER_ERROR;
}
if (buffers.size() > 0)
{
body.append(buffers);
res = socket_write(r, sock, body.c_str(), body.size());
// AP_LOG_FAIL_3("%s %ld %d", "wrote 2", body.size(), toRead);
if (res != 0)
{
CLOSE_SOCKET(sock);
AP_LOG_FAIL("%s", "Unable to send post");
return HTTP_INTERNAL_SERVER_ERROR;
}
body.clear();
}
}
}
writeMsg(r, body, FCGI_STDIN, NULL, 0);
res = socket_write(r, sock, body.c_str(), body.size());
if (res != 0)
{
CLOSE_SOCKET(sock);
AP_LOG_FAIL("%s", "Unable to initiate request");
return HTTP_INTERNAL_SERVER_ERROR;
}
bool inBody = false;
FCGI_Header readHead;
memset(&readHead, 0, sizeof(readHead));
res = socket_read_header(r, sock, &readHead);
while(res == 0)
{
const char * contentData = NULL;
uint16_t contentSize = ntohs(readHead.contentLength);
// AP_LOG_FAIL_3("%s %d %d", "Got header ", contentSize, readHead.type);
body.clear();
if (contentSize)
{
body.append(contentSize, 0);
res = socket_read_content(r, sock, (char*)body.data(), contentSize, readHead.paddingLength);
if (res != 0)
{
CLOSE_SOCKET(sock);
AP_LOG_FAIL_3("%s %d %s", "Unable to read content", contentSize, r->uri);
return HTTP_INTERNAL_SERVER_ERROR;
}
contentData = body.c_str();
// AP_LOG_FAIL_2("%s%s", "content ", contentData);
}
switch(readHead.type)
{
case FCGI_STDOUT:
if (contentSize)
{
while (!inBody && contentSize)
{
std::string name, value;
res = pull_header_line(contentData, contentSize, name, value);
// AP_LOG_FAIL_3("%s %s %s", "header", name.c_str(), value.c_str());
if (res == 0)
{
inBody = true;
// AP_LOG_FAIL_2("%s %d", "moving to body", contentSize);
ap_send_http_header(r);
}
else
{
if (strcasecmp(name.c_str(), "status") == 0)
{
r->status = atoi(value.c_str());
}
else if ( strcasecmp(name.c_str(), "content-type") == 0 )
{
ap_set_content_type(r, apr_pstrdup(r->pool, value.c_str()));
}
else
{
apr_table_add(r->headers_out, name.c_str(), value.c_str());
}
}
}
if (inBody && contentSize)
{
apr_bucket_brigade * bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
apr_bucket * b = apr_bucket_heap_create(contentData, contentSize, NULL, r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_flush_create(r->connection->bucket_alloc));
ap_pass_brigade(r->output_filters, bb);
}
}
break;
case FCGI_END_REQUEST:
goto close_ok;
case FCGI_ABORT_REQUEST:
goto close_denied;
}
memset(&readHead, 0, sizeof(readHead));
res = socket_read_header(r, sock, &readHead);
}
close_ok:
CLOSE_SOCKET(sock);
ap_finalize_request_protocol(r);
return OK;
close_denied:
CLOSE_SOCKET(sock);
return HTTP_INTERNAL_SERVER_ERROR;
}
extern "C"
{
module AP_MODULE_DECLARE_DATA perfect_module =
{
STANDARD20_MODULE_STUFF,
NULL, /* dir config creator */
NULL, /* dir merger ensure strictness */
NULL,//create_mod_lasso9_config, /* server config */
NULL, /* merge server config */
NULL, /* command table */
register_9hooks, /* register hooks */
};
}