From 28593ac70ea473e87f86d7bfd3488f17b6df276b Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 8 May 2020 10:18:33 +0200 Subject: Added FCGI unix domain sockets --- TODO | 2 +- debian/changelog | 1 + plugins/fcgi/fcgi.cpp | 2 +- plugins/fcgi/socket.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++-- plugins/fcgi/socket.h | 13 +++++++++-- webserver.conf | 4 ++++ 6 files changed, 78 insertions(+), 6 deletions(-) diff --git a/TODO b/TODO index 8a9d15b..d10fd0f 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,4 @@ -fcgi: via unix domain socket +Match prefix Speed up config.GetPath weblog: link consistency check (cron?) Debian: keyring package diff --git a/debian/changelog b/debian/changelog index 780c4c5..00b4096 100644 --- a/debian/changelog +++ b/debian/changelog @@ -3,6 +3,7 @@ webserver (1.4) UNRELEASED; urgency=medium * Added redirect plugin * Plugin Documentation * Minor corrections + * Added Unix domain sockets for FCGI -- Roland Reichwein Sun, 03 May 2020 09:49:32 +0200 diff --git a/plugins/fcgi/fcgi.cpp b/plugins/fcgi/fcgi.cpp index 4ff8253..c990a52 100644 --- a/plugins/fcgi/fcgi.cpp +++ b/plugins/fcgi/fcgi.cpp @@ -366,7 +366,7 @@ std::string fcgi_plugin::fcgiQuery(FCGIContext& context) std::lock_guard socket_lock{socket->getMutex()}; - socket->close(); // TODO: Bug workaround: Keeping TCP socket open doesn't work for now + socket->close(); // TODO: Bug workaround: Keeping socket open doesn't work for now if (!socket->is_open()) { std::cout << "FCGI: Opening new socket" << std::endl; diff --git a/plugins/fcgi/socket.cpp b/plugins/fcgi/socket.cpp index 0a2a381..49a8651 100644 --- a/plugins/fcgi/socket.cpp +++ b/plugins/fcgi/socket.cpp @@ -29,8 +29,7 @@ std::shared_ptr SocketFactory::create(const std::string& app_addr) return std::make_shared(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 - // TODO - std::cerr << "FCGI Error: Unix domain sockets not yet implemented." << std::endl; + return std::make_shared(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; @@ -119,3 +118,62 @@ size_t TCPSocket::read(std::vector& data) } } +FileSocket::FileSocket(const std::string& app_addr, boost::asio::io_context& io_context) + : m_app_addr(app_addr) + , m_socket(io_context) +{ +} + +FileSocket::~FileSocket() +{ +} + +void FileSocket::open() +{ + try { + boost::asio::local::stream_protocol::endpoint ep(m_app_addr); + m_socket.connect(ep); + } catch(const std::exception& ex) { + std::cerr << "FCGI Error: Error on connecting to " << m_app_addr << ": " << ex.what() << std::endl; + return; + } +} + +bool FileSocket::is_open() +{ + return m_socket.is_open(); +} + +void FileSocket::close() +{ + m_socket.close(); +} + +size_t FileSocket::write(const std::vector& data) +{ + try { + return m_socket.write_some(boost::asio::buffer(data)); + } catch (const boost::system::system_error& ex) { + if (ex.code() == boost::asio::error::eof) { + throw fcgi_eof_error("EOF on write"); + } else + throw std::runtime_error("FCGI Error: Unknown boost asio exception on write: "s + ex.what()); + } catch (const std::exception& ex) { + throw std::runtime_error("FCGI Error: Unknown exception on write: "s + ex.what()); + } +} + +size_t FileSocket::read(std::vector& data) +{ + try { + return m_socket.read_some(boost::asio::buffer(data)); + } catch (const boost::system::system_error& ex) { + if (ex.code() == boost::asio::error::eof) { + throw fcgi_eof_error("EOF on read"); + } else + throw std::runtime_error("FCGI Error: Unknown boost asio exception on read: "s + ex.what()); + } catch (const std::exception& ex) { + throw std::runtime_error("FCGI Error: Unknown exception on read: "s + ex.what()); + } +} + diff --git a/plugins/fcgi/socket.h b/plugins/fcgi/socket.h index b4ec54b..8fb5610 100644 --- a/plugins/fcgi/socket.h +++ b/plugins/fcgi/socket.h @@ -61,10 +61,19 @@ public: size_t read(std::vector& data) override; }; -#if 0 class FileSocket: public Socket { + std::string m_app_addr; + boost::asio::local::stream_protocol::socket m_socket; + +public: + FileSocket(const std::string& app_addr, boost::asio::io_context& io_context); ~FileSocket() override; + + void open() override; + void close() override; + bool is_open() override; + size_t write(const std::vector& data) override; + size_t read(std::vector& data) override; }; -#endif diff --git a/webserver.conf b/webserver.conf index 66d4f1f..5088ef8 100644 --- a/webserver.conf +++ b/webserver.conf @@ -50,6 +50,10 @@ fcgi 127.0.0.1:9000 + + fcgi + /home/ernie/code/webserver/fastcgi/socket + redirect https://www.antcom.de/ -- cgit v1.2.3