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 --- plugins/fcgi/socket.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) (limited to 'plugins/fcgi/socket.cpp') 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()); + } +} + -- cgit v1.2.3