Menu

[r699]: / framework / trunk / fcgi.cpp  Maximize  Restore  History

Download this file

94 lines (78 with data), 1.6 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
#include "fcgi.h"
#include "cppcms_error.h"
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
namespace cppcms {
fcgi_stream::fcgi_stream(FCGX_Request &req) :
std::ostream(&fcgi_cout),
request(req),
fcgi_cout(req.out),
fcgi_cerr(req.err),
stream_cerr(&fcgi_cerr)
{
};
std::string fcgi_stream::getenv(const char *variable)
{
char const *p;
if((p=FCGX_GetParam(variable,request.envp))!=NULL)
return p;
return "";
};
size_t fcgi_stream::read(char *d,size_t len)
{
return FCGX_GetStr(d,len,request.in);
};
std::ostream &fcgi_stream::err()
{
return stream_cerr;
};
fcgi_stream::~fcgi_stream()
{
FCGX_Finish_r(&request);
};
pthread_once_t fcgi_api::init_fcgi = PTHREAD_ONCE_INIT;
void fcgi_api::init()
{
FCGX_Init();
}
fcgi_api::fcgi_api(char const *socket,int backlog)
{
pthread_once(&init_fcgi,fcgi_api::init);
if(socket && socket[0]!='\0') {
fd=FCGX_OpenSocket(socket,backlog);
}
else {
fd=0; // STDIN
}
if(fd<0) {
throw cppcms_error(errno,"FCGX_OpenSocket");
}
}
cgi_session *fcgi_api::accept_session()
{
FCGX_Request *request=new FCGX_Request();
FCGX_InitRequest(request,fd,FCGI_FAIL_ACCEPT_ON_INTR);
if(FCGX_Accept_r(request)<0) {
delete request;
return NULL;
}
return new fcgi_session(request,NULL);
}
bool fcgi_session::prepare()
{
connection=new cgicc_connection_fast_cgi(*request);
return true;
}
cgicc_connection &fcgi_session::get_connection()
{
if(!connection) {
throw cppcms_error("Connection was not prepared");
}
return *connection;
}
fcgi_api::~fcgi_api()
{
if(fd!=-1) close(fd);
}
};
MongoDB Logo MongoDB