110 lines
3.5 KiB
C++
110 lines
3.5 KiB
C++
#include <algorithm>
|
|
#include <sys/stat.h>
|
|
#include <iostream>
|
|
|
|
#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.
|
|
context = &request;
|
|
request.server_state = &server_state;
|
|
request.time_start = microtime();
|
|
|
|
request.header["Content-Type"] = context->server_state->config.CONTENT_TYPE;
|
|
request.get = URI::parse_query(request.params["QUERY_STRING"]);
|
|
|
|
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 = URI::parse_multipart(request.in, string("--")+ct_info, request.uploaded_files);
|
|
}
|
|
else
|
|
{
|
|
request.post = URI::parse_query(request.in);
|
|
}
|
|
}
|
|
|
|
invoke(request.params["SCRIPT_FILENAME"]);
|
|
|
|
string headers = var_dump(request.header, "", "\r\n") + "\r\n";
|
|
request.out = headers + request.out;
|
|
|
|
for( auto &f : request.uploaded_files)
|
|
{
|
|
unlink(f.tmp_name);
|
|
}
|
|
|
|
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)
|
|
{
|
|
// 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();
|
|
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);
|
|
|
|
//server.process(100);
|
|
//server.process();
|
|
server.process_forever();
|
|
|
|
return 0;
|
|
}
|