FCGI Socket open

FossilOrigin-Name: 5eca1feb65a7a48d93bfafbfebd0d1bb620604d923c1aff7788c067e2e4c62f4
This commit is contained in:
nekobit 2022-10-30 22:03:01 +00:00
parent 117b55c51a
commit 5ee629bec4

View file

@ -29,13 +29,104 @@
#include "logger.h"
using namespace std::string_literals;
constexpr int FCGI_LISTENSOCK_FILENO = STDIN_FILENO;
HTTP::Response Route::fcgi_request(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg)
{
using namespace std::string_literals;
return HTTP::Response{ "Hello FCGI"s + req.get_url(), HTTP::MIME::HTML };
}
constexpr int FCGI_LISTENSOCK_FILENO = STDIN_FILENO;
constexpr int FCGI_HEADER_LEN = 8;
constexpr int FCGI_VERSION_1 = 1;
struct FCGI_Header {
unsigned char version;
unsigned char type;
unsigned char requestIdB1;
unsigned char requestIdB0;
unsigned char contentLengthB1;
unsigned char contentLengthB0;
unsigned char paddingLength;
unsigned char reserved;
};
enum FCGI_TYPE {
FCGI_BEGIN_REQUEST = 1,
FCGI_ABORT_REQUEST,
FCGI_END_REQUEST,
FCGI_PARAMS,
FCGI_STDIN,
FCGI_STDOUT,
FCGI_STDERR,
FCGI_DATA,
FCGI_GET_VALUES,
FCGI_GET_VALUES_RESULT,
FCGI_UNKNOWN_TYPE,
};
/*
* Value for requestId component of FCGI_Header
*/
#define FCGI_NULL_REQUEST_ID 0
typedef struct {
unsigned char roleB1;
unsigned char roleB0;
unsigned char flags;
unsigned char reserved[5];
} FCGI_BeginRequestBody;
typedef struct {
FCGI_Header header;
FCGI_BeginRequestBody body;
} FCGI_BeginRequestRecord;
/*
* Mask for flags component of FCGI_BeginRequestBody
*/
#define FCGI_KEEP_CONN 1
/*
* Values for role component of FCGI_BeginRequestBody
*/
#define FCGI_RESPONDER 1
#define FCGI_AUTHORIZER 2
#define FCGI_FILTER 3
struct FCGI_EndRequestBody {
unsigned char appStatusB3;
unsigned char appStatusB2;
unsigned char appStatusB1;
unsigned char appStatusB0;
unsigned char protocolStatus;
unsigned char reserved[3];
};
struct FCGI_EndRequestRecord {
FCGI_Header header;
FCGI_EndRequestBody body;
};
/*
* Values for protocolStatus component of FCGI_EndRequestBody
*/
#define FCGI_REQUEST_COMPLETE 0
#define FCGI_CANT_MPX_CONN 1
#define FCGI_OVERLOADED 2
#define FCGI_UNKNOWN_ROLE 3
/*
* Variable names for FCGI_GET_VALUES / FCGI_GET_VALUES_RESULT records
*/
#define FCGI_MAX_CONNS "FCGI_MAX_CONNS"
#define FCGI_MAX_REQS "FCGI_MAX_REQS"
#define FCGI_MPXS_CONNS "FCGI_MPXS_CONNS"
struct FCGI_UnknownTypeBody {
unsigned char type;
unsigned char reserved[7];
};
struct FCGI_UnknownTypeRecord {
FCGI_Header header;
FCGI_UnknownTypeBody body;
};
namespace {
void setup_routes(HTTP::Server* server)
@ -50,6 +141,12 @@ namespace {
}
}
HTTP::Response Route::fcgi_request(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg)
{
using namespace std::string_literals;
return HTTP::Response{ "Hello FCGI"s + req.get_url(), HTTP::MIME::HTML };
}
void Frontend::init_fcgi(HTTP::Server* server)
{
if (Config::instance().config.frontend.type != Type::FCGI)
@ -60,20 +157,26 @@ void Frontend::init_fcgi(HTTP::Server* server)
void Frontend::poll_fcgi_pipes(std::array<int, 2> data)
{
int in = data[0], out = data[1];
fd_set rfds;
struct timeval tv = {0, 0};
int rc;
int size;
int buf[4096];
do
{
FD_ZERO(&rfds);
FD_SET(data[0], &rfds);
FD_SET(in, &rfds);
rc = select(data[0]+1, &rfds, nullptr, nullptr, &tv);
rc = select(in+1, &rfds, nullptr, nullptr, nullptr);
if (rc == -1)
{
Logger::instance().log("select(): "s + std::strerror(errno), Logger::Level::ERROR);
}
std::cout << "wtf" << std::endl;;
size = read(in, buf, 1);
std::cout << size << std::endl;
}
while (rc);
}
@ -101,6 +204,11 @@ std::array<int, 2> Frontend::fork_fcgi_process()
// FCGI says these aren't open
close(STDOUT_FILENO);
close(STDERR_FILENO);
close(FCGI_LISTENSOCK_FILENO);
if (dup2(fd[1], FCGI_LISTENSOCK_FILENO) == -1)
{
perror("dup2");
}
char* argv[] = {
nullptr
@ -116,7 +224,12 @@ std::array<int, 2> Frontend::fork_fcgi_process()
#endif
(exec.data(), argv, nullptr);
Logger::instance() << "Child process \"" + exec + "\" exited with code " + std::to_string(rc);
if (errno)
{
Logger::instance() << "Process execution failed: "s + strerror(errno);
}
else
Logger::instance() << "Child process \"" + exec + "\" exited with code " + std::to_string(rc);
exit(0);
}