summaryrefslogtreecommitdiffabout
path: root/src/fastcgi.cc
blob: 62853706a73d6553eedefbac21f720f6e468f2a0 (plain)
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
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "kingate/fastcgi.h"
#include "kingate/exception.h"

namespace kingate {

	bool fcgi_socket::_initialized = false;

	fcgi_socket::fcgi_socket(const char *s,int bl) 
	    : sock(-1) {
		if(!_initialized) {
		    if( FCGX_Init() )
			throw exception(CODEPOINT,"failed to FCGX_Init()");
		    _initialized = true;
		}
		sock = FCGX_OpenSocket(s,bl);
		if(sock<0)
		    throw exception(CODEPOINT,"failed to FCGX_OpenSocket(");
		// TODO: check if there is a ':', not if it starts with ':'
		if(*s != ':')
		    if(chmod(s,0777)) // XXX: configurable.
			throw exception(CODEPOINT,"failed to chmod()");
	    }
	fcgi_socket::fcgi_socket(int s)
	    : sock(0) {
		if(!_initialized) {
		    if( FCGX_Init() )
			throw exception(CODEPOINT,"failed to FCGX_Init()");
		    _initialized = true;
		}
	    }
	fcgi_socket::~fcgi_socket() {
	    if(sock>=0)
		close(sock);
	}

	fcgi_interface::fcgi_interface(fcgi_socket& s,int f)
	    : sin(&sbin), sout(&sbout), serr(&sberr) {
		if( FCGX_InitRequest(&request,s.sock,f) )
		    throw exception(CODEPOINT,"failed to FCGX_InitRequest()");
		if( FCGX_Accept_r(&request) )
		    throw exception(CODEPOINT,"failed to FCGX_Accept_r()");
		sbin.attach(request.in);
		sbout.attach(request.out);
		sberr.attach(request.err);
		for(char **p = request.envp; *p; p++) {
		    const char *e = strchr(*p,'=');
		    if(!e){
			// XXX: check if we have it already?
			metavars[*p] = string(0);
		    }else{
			int l = e-*p; e++;
			// XXX: check if we have it already?
			metavars[string(*p,l)]=e;
		    }
		}
	    }
	fcgi_interface::~fcgi_interface() {
	    sout.flush();
	    serr.flush();
	    FCGX_Finish_r(&request);
	}

}