summaryrefslogtreecommitdiffhomepage
path: root/plugins
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-05-08 10:18:33 +0200
committerRoland Reichwein <mail@reichwein.it>2020-05-08 10:18:33 +0200
commit28593ac70ea473e87f86d7bfd3488f17b6df276b (patch)
tree5b408c83501c5c9570ccb32a1db94ec0d939312c /plugins
parent9e635d9b19e72eefef082dd8071d3e4c9d6cfab1 (diff)
Added FCGI unix domain sockets
Diffstat (limited to 'plugins')
-rw-r--r--plugins/fcgi/fcgi.cpp2
-rw-r--r--plugins/fcgi/socket.cpp62
-rw-r--r--plugins/fcgi/socket.h13
3 files changed, 72 insertions, 5 deletions
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<std::mutex> 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<Socket> SocketFactory::create(const std::string& app_addr)
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
- // TODO
- std::cerr << "FCGI Error: Unix domain sockets not yet implemented." << std::endl;
+ 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;
@@ -119,3 +118,62 @@ size_t TCPSocket::read(std::vector<char>& 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<char>& 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<char>& 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<char>& 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<char>& data) override;
+ size_t read(std::vector<char>& data) override;
};
-#endif