-rw-r--r-- | src/fastcgi.cc | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/fastcgi.cc b/src/fastcgi.cc new file mode 100644 index 0000000..7484449 --- a/dev/null +++ b/src/fastcgi.cc | |||
@@ -0,0 +1,67 @@ | |||
1 | #include <unistd.h> | ||
2 | #include <sys/types.h> | ||
3 | #include <sys/stat.h> | ||
4 | #include "kingate/fastcgi.h" | ||
5 | #include "kingate/exception.h" | ||
6 | |||
7 | namespace kingate { | ||
8 | |||
9 | bool fcgi_socket::_initialized = false; | ||
10 | |||
11 | fcgi_socket::fcgi_socket(const char *s,int bl) | ||
12 | : sock(-1) { | ||
13 | if(!_initialized) { | ||
14 | if( FCGX_Init() ) | ||
15 | throw exception(CODEPOINT,"failed to FCGX_Init()"); | ||
16 | _initialized = true; | ||
17 | } | ||
18 | sock = FCGX_OpenSocket(s,bl); | ||
19 | if(sock<0) | ||
20 | throw exception(CODEPOINT,"failed to FCGX_OpenSocket("); | ||
21 | // TODO: check if there is a ':', not if it starts with ':' | ||
22 | if(*s != ':') | ||
23 | if(chmod(s,0777)) // XXX: configurable. | ||
24 | throw exception(CODEPOINT,"failed to chmod()"); | ||
25 | } | ||
26 | fcgi_socket::fcgi_socket(int s) | ||
27 | : sock(0) { | ||
28 | if(!_initialized) { | ||
29 | if( FCGX_Init() ) | ||
30 | throw exception(CODEPOINT,"failed to FCGX_Init()"); | ||
31 | _initialized = true; | ||
32 | } | ||
33 | } | ||
34 | fcgi_socket::~fcgi_socket() { | ||
35 | if(sock>=0) | ||
36 | close(sock); | ||
37 | } | ||
38 | |||
39 | fcgi_interface::fcgi_interface(fcgi_socket& s,int f) | ||
40 | : sin(&sbin), sout(&sbout), serr(&sberr) { | ||
41 | if( FCGX_InitRequest(&request,s.sock,f) ) | ||
42 | throw exception(CODEPOINT,"failed to FCGX_InitRequest()"); | ||
43 | if( FCGX_Accept_r(&request) ) | ||
44 | throw exception(CODEPOINT,"failed to FCGX_Accept_r()"); | ||
45 | sbin.attach(request.in); | ||
46 | sbout.attach(request.out); | ||
47 | sberr.attach(request.err); | ||
48 | metavars.clear(); // XXX: redundant. | ||
49 | for(char **p = request.envp; *p; p++) { | ||
50 | const char *e = strchr(*p,'='); | ||
51 | if(!e){ | ||
52 | // XXX: check if we have it already? | ||
53 | metavars[*p] = string(0); | ||
54 | }else{ | ||
55 | int l = e-*p; e++; | ||
56 | // XXX: check if we have it already? | ||
57 | metavars[string(*p,l)]=e; | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | fcgi_interface::~fcgi_interface() { | ||
62 | sout.flush(); | ||
63 | serr.flush(); | ||
64 | FCGX_Finish_r(&request); | ||
65 | } | ||
66 | |||
67 | } | ||