summaryrefslogtreecommitdiffhomepage
path: root/plugins/fcgi/socket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/fcgi/socket.cpp')
-rw-r--r--plugins/fcgi/socket.cpp59
1 files changed, 55 insertions, 4 deletions
diff --git a/plugins/fcgi/socket.cpp b/plugins/fcgi/socket.cpp
index 2b34bc3..c899de5 100644
--- a/plugins/fcgi/socket.cpp
+++ b/plugins/fcgi/socket.cpp
@@ -1,5 +1,7 @@
#include "socket.h"
+#include <fmt/core.h>
+
#include <filesystem>
#include <iostream>
@@ -22,16 +24,16 @@ SocketFactory::SocketFactory()
std::shared_ptr<Socket> SocketFactory::create(const std::string& app_addr)
{
+ std::error_code ec;
size_t pos { app_addr.find_last_of(':') };
if (pos != app_addr.npos) { // tcp socket: host:port
return std::make_shared<TCPSocket>(app_addr.substr(0, pos), app_addr.substr(pos + 1), m_io_context);
- } else if (fs::is_socket(fs::path{app_addr})) { // Unix domain socket
+ } else if (fs::is_socket(fs::path{app_addr}, ec)) { // Unix domain socket
return std::make_shared<FileSocket>(app_addr, m_io_context);
- } else if (fs::is_regular_file(fs::path{app_addr})) { // Executable to start
- // TODO
- std::cerr << "FCGI Error: Executable FCGI not yet implemented." << std::endl;
+ } else if (fs::is_regular_file(fs::path{app_addr}, ec)) { // Executable to start
+ return std::make_shared<FileSocketApp>(app_addr, m_io_context);
} else {
std::cerr << "FCGI Error: Invalid app_addr type." << std::endl;
}
@@ -232,3 +234,52 @@ size_t FileSocket::read(std::vector<char>& data)
}
}
+std::string generate_unix_domain_socket(const std::string& directory)
+{
+ for (int i = 0; i < 10000; i++) {
+ std::string path{fmt::format("{}/fcgi-socket{}", directory, i)};
+ if (fs::exists(path))
+ continue;
+ return path;
+ }
+ throw std::runtime_error("dynamic unix domain socket couldn't be generated.");
+}
+
+FileSocketApp::FileSocketApp(const std::string& app_addr, boost::asio::io_context& io_context):
+ m_socket_file{generate_unix_domain_socket("/var/lib/webserver")},
+ m_fcgi_process{app_addr, m_socket_file},
+ m_file_socket{m_socket_file, io_context}
+{
+}
+
+FileSocketApp::~FileSocketApp()
+{
+ std::error_code ec;
+ fs::remove(m_socket_file, ec);
+}
+
+void FileSocketApp::open()
+{
+ m_file_socket.open();
+}
+
+void FileSocketApp::close()
+{
+ m_file_socket.close();
+}
+
+bool FileSocketApp::is_open()
+{
+ return m_file_socket.is_open();
+}
+
+size_t FileSocketApp::write(const std::vector<char>& data)
+{
+ return m_file_socket.write(data);
+}
+
+size_t FileSocketApp::read(std::vector<char>& data)
+{
+ return m_file_socket.read(data);
+}
+