uce/src/linux_fastcgi.cpp

132 lines
4.1 KiB
C++

#include "lib/uce_gen.h"
#include "fastcgi/src/fcgicc.cc"
int handle_request(FastCGIRequest& request) {
// This is always the first event to occur. It occurs when the
// server receives all parameters. There may be more data coming on the
// standard input stream.
request.time_init = microtime();
if (request.params.count("REQUEST_URI"))
return 0; // OK, continue processing
else
return 1; // stop processing and return error code
}
int handle_data(FastCGIRequest& request) {
// This event occurs when data is received on the standard input stream.
// A simple string is used to hold the input stream, so it is the
// responsibility of the application to remember which data it has
// processed. The application may modify it; new data will be appended
// to it by the server. The same goes for the output and error streams:
// the application should append data to them; the server will remove
// all sent data from them.
return 0; // still OK
std::transform(request.in.begin(), request.in.end(),
std::back_inserter(request.err),
std::bind1st(std::plus<char>(), 1));
request.in.clear(); // don't process it again
return 0; // still OK
}
int handle_complete(FastCGIRequest& request) {
// The event handler can also be a class member function. This
// event occurs when the parameters and standard input streams are
// both closed, and thus the request is complete.
// printf("(i) request handle\n");
context = &request;
request.server = &server_state;
request.time_start = microtime();
request.invoke = compiler_invoke;
request.header["Content-Type"] = context->server->config.CONTENT_TYPE;
request.get = parse_query(request.params["QUERY_STRING"]);
if(request.params["HTTP_COOKIE"].length() > 0)
request.cookies = parse_cookies(request.params["HTTP_COOKIE"]);
string ct_info = request.params["CONTENT_TYPE"];
string ct_type = nibble(";", ct_info);
if(request.params["REQUEST_METHOD"] == "POST")
{
if(ct_type == "multipart/form-data")
{
nibble("boundary=", ct_info);
request.post = parse_multipart(request.in, string("--")+ct_info, request.uploaded_files);
}
else
{
request.post = parse_query(request.in);
}
}
// printf("(i) request ready\n");
invoke(request.params["SCRIPT_FILENAME"]);
string headers =
var_dump(request.header, "", "\r\n") +
var_dump(request.set_cookies, "", "\r\n") +
"\r\n";
request.out = headers + request.out;
for( auto &f : request.uploaded_files)
{
unlink(f.tmp_name);
}
if(request.session_id.length() > 0)
save_session_data(request.session_id, request.session);
request.time_end = microtime();
printf("(r) %s\t%0.6fs\t%0.1fkB\n",
request.params["REQUEST_URI"].c_str(),
request.time_end - request.time_start,
(f32)(request.out.length()/1024)
);
return 0;
}
FastCGIServer server;
int main(int argc, char** argv)
{
signal(SIGSEGV, on_segfault);
srand(time());
invoke = compiler_invoke;
// Set up our request handlers
server.request_handler(&handle_request);
server.data_handler(&handle_data);
server.complete_handler(&handle_complete);
//if(server_state.config.COMPILER_SYS_PATH == "")
server_state.config.COMPILER_SYS_PATH = get_cwd();
printf("Compiler base path: %s\n", server_state.config.COMPILER_SYS_PATH.c_str());
server_state.config.BIN_DIRECTORY =
server_state.config.COMPILER_SYS_PATH + "/" + server_state.config.BIN_DIRECTORY;
server_state.config.COMPILE_SCRIPT =
server_state.config.COMPILER_SYS_PATH + "/" + server_state.config.COMPILE_SCRIPT;
if(server_state.config.LISTEN_PORT)
server.listen(server_state.config.LISTEN_PORT);
if(server_state.config.SOCKET_PATH != "")
server.listen(server_state.config.SOCKET_PATH);
chmod(server_state.config.SOCKET_PATH.c_str(), S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
dirname(server_state.config.COMPILER_SYS_PATH);
basename(server_state.config.COMPILER_SYS_PATH);
mkdir(server_state.config.TMP_UPLOAD_PATH);
mkdir(server_state.config.SESSION_PATH);
//server.process(100);
//server.process();
server.process_forever();
return 0;
}