some experiments
This commit is contained in:
parent
ceae213b03
commit
6367dbffe8
@ -1,4 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
cmake_policy(SET CMP0076 NEW)
|
||||
|
||||
project(birdbadge
|
||||
VERSION 0.0.1
|
||||
@ -12,7 +13,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(FCGI fcgi)
|
||||
|
||||
add_executable(birdbadge main.cpp)
|
||||
add_executable(birdbadge )
|
||||
|
||||
add_subdirectory(src)
|
||||
|
||||
target_link_libraries(birdbadge
|
||||
fcgi
|
||||
|
10
src/CMakeLists.txt
Normal file
10
src/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
set(SOURCE_FILES
|
||||
main.cpp
|
||||
server.cpp
|
||||
)
|
||||
|
||||
set(HEADER_FILES
|
||||
server.h
|
||||
)
|
||||
|
||||
target_sources(birdbadge PRIVATE ${SOURCE_FILES})
|
32
src/main.cpp
Normal file
32
src/main.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include <fcgiapp.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "server.h"
|
||||
|
||||
int main() {
|
||||
const char* socketPath = "/run/birdbadge/birdbadge.sock";
|
||||
int sockfd = FCGX_OpenSocket(socketPath, 1024);
|
||||
if (sockfd < 0) {
|
||||
std::cerr << "Error opening socket" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (chmod(socketPath, 0770) != 0) {
|
||||
std::cerr << "Couldn't set socket permissions" << std::endl;
|
||||
return 2;
|
||||
}
|
||||
|
||||
Server server;
|
||||
FCGX_Request request;
|
||||
|
||||
FCGX_Init();
|
||||
FCGX_InitRequest(&request, sockfd, 0);
|
||||
|
||||
while (FCGX_Accept_r(&request) == 0) {
|
||||
server.handleRequest(request);
|
||||
|
||||
FCGX_Finish_r(&request);
|
||||
}
|
||||
}
|
@ -1,21 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fcgio.h>
|
||||
#include <fcgiapp.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include "server.h"
|
||||
|
||||
constexpr static const char* GET("GET");
|
||||
|
||||
constexpr static const char* REQUEST_METHOD("REQUEST_METHOD");
|
||||
constexpr static const char* REQUEST_URI("REQUEST_URI");
|
||||
|
||||
constexpr static const char* DOCUMENT_URI("DOCUMENT_URI");
|
||||
constexpr static const char* DOCUMENT_ROOT("DOCUMENT_ROOT");
|
||||
|
||||
constexpr static const char* SCRIPT_NAME("SCRIPT_NAME");
|
||||
constexpr static const char* SCRIPT_FILENAME("SCRIPT_FILENAME");
|
||||
|
||||
constexpr static const char* status405("Status: 405 Method Not Allowed");
|
||||
constexpr static const char* contentTypeHtml("Content-type: text/html");
|
||||
|
||||
constexpr static const char* headerEnd("\n\n");
|
||||
|
||||
void handleRequest(FCGX_Request& request) {
|
||||
Server::Server():
|
||||
requestCount(0)
|
||||
{}
|
||||
|
||||
Server::~Server() {
|
||||
}
|
||||
|
||||
void Server::handleRequest(FCGX_Request& request) {
|
||||
++requestCount;
|
||||
|
||||
fcgi_streambuf cin_fcgi_streambuf(request.in);
|
||||
fcgi_streambuf cout_fcgi_streambuf(request.out);
|
||||
fcgi_streambuf cerr_fcgi_streambuf(request.err);
|
||||
@ -29,19 +39,21 @@ void handleRequest(FCGX_Request& request) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string scriptName{FCGX_GetParam(SCRIPT_NAME, request.envp)};
|
||||
os << contentTypeHtml;
|
||||
os << headerEnd;
|
||||
os << scriptName << "\n";
|
||||
}
|
||||
try {
|
||||
printEnv(os, request);
|
||||
|
||||
int main() {
|
||||
FCGX_Request request;
|
||||
|
||||
FCGX_Init();
|
||||
FCGX_InitRequest(&request, 0, 0);
|
||||
|
||||
while (FCGX_Accept_r(&request) == 0) {
|
||||
handleRequest(request);
|
||||
os << requestCount;
|
||||
} catch (const std::exception e) {
|
||||
os << contentTypeHtml;
|
||||
os << headerEnd;
|
||||
os << e.what();
|
||||
}
|
||||
}
|
||||
|
||||
void Server::printEnv(std::ostream& out, FCGX_Request& request) {
|
||||
char **envp = request.envp;
|
||||
for (int i = 0; envp[i] != nullptr; ++i) {
|
||||
out << envp[i] << "</br>";
|
||||
}
|
||||
}
|
||||
|
21
src/server.h
Normal file
21
src/server.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <fcgiapp.h>
|
||||
#include <fcgio.h>
|
||||
|
||||
class Server {
|
||||
public:
|
||||
Server();
|
||||
~Server();
|
||||
|
||||
void handleRequest(FCGX_Request& request);
|
||||
|
||||
private:
|
||||
void printEnv(std::ostream& out, FCGX_Request& request);
|
||||
|
||||
private:
|
||||
uint64_t requestCount;
|
||||
};
|
Loading…
Reference in New Issue
Block a user