diff --git a/configure.ac b/configure.ac index abaf7cc7..1f7d0982 100644 --- a/configure.ac +++ b/configure.ac @@ -26,6 +26,10 @@ PKG_CHECK_MODULES(STUFF, sigc++-2.0 libcurl >= 7.12.0 libtorrent >= 0.11.3, CXXFLAGS="$CXXFLAGS $STUFF_CFLAGS"; LIBS="$LIBS $STUFF_LIBS") +AC_LANG_PUSH(C++) +TORRENT_WITH_FASTCGI +AC_LANG_POP(C++) + AC_DEFINE(HAVE_CONFIG_H, 1, true if config.h was included) AC_DEFINE(USER_AGENT, [std::string(PACKAGE "/" VERSION "/") + torrent::version()], Http user agent) @@ -41,6 +45,7 @@ AC_OUTPUT([ src/core/Makefile src/display/Makefile src/input/Makefile + src/rpc/Makefile src/ui/Makefile src/utils/Makefile ]) diff --git a/scripts/checks.m4 b/scripts/checks.m4 index 3f54e49c..77194aa8 100644 --- a/scripts/checks.m4 +++ b/scripts/checks.m4 @@ -288,3 +288,54 @@ AC_DEFUN([TORRENT_WITH_ADDRESS_SPACE], [ fi ]) ]) + + +AC_DEFUN([TORRENT_WITH_FASTCGI], [ + AC_ARG_WITH(fastcgi, + [ --with-fastcgi=PATH Enable FastCGI RPC support.], + [ + AC_MSG_CHECKING(for FastCGI) + + if test "$withval" = "no"; then + AC_MSG_RESULT(no) + + elif test "$withval" = "yes"; then + CXXFLAGS="$CXXFLAGS" + LIBS="$LIBS -lfcgi" + + AC_TRY_LINK( + [ #include + ],[ FCGX_Init(); ], + [ + AC_MSG_RESULT(ok) + ], + [ + AC_MSG_RESULT(not found) + AC_MSG_ERROR(Could not compile FastCGI test.) + ]) + + AC_DEFINE(HAVE_FASTCGI, 1, Support for FastCGI.) + + else + CXXFLAGS="$CXXFLAGS -I$withval/include" + LIBS="$LIBS -lfcgi -L$withval/lib" + + AC_TRY_LINK( + [ #include + ],[ FCGX_Init(); ], + [ + AC_MSG_RESULT(ok) + ], + [ + AC_MSG_RESULT(not found) + AC_MSG_ERROR(Could not compile FastCGI test.) + ]) + + AC_DEFINE(HAVE_FASTCGI, 1, Support for FastCGI.) + fi + ],[ + AC_MSG_CHECKING(for FastCGI) + AC_MSG_RESULT(no) + ]) +]) + diff --git a/src/Makefile.am b/src/Makefile.am index 7c60c6bb..a6607f15 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,6 +2,7 @@ SUBDIRS = \ core \ display \ input \ + rpc \ ui \ utils @@ -12,6 +13,7 @@ rtorrent_LDADD = \ $(top_srcdir)/src/core/libsub_core.a \ $(top_srcdir)/src/display/libsub_display.a \ $(top_srcdir)/src/input/libsub_input.a \ + $(top_srcdir)/src/rpc/libsub_rpc.a \ $(top_srcdir)/src/utils/libsub_utils.a rtorrent_SOURCES = \ diff --git a/src/control.cc b/src/control.cc index e5b451cb..35ceef4a 100644 --- a/src/control.cc +++ b/src/control.cc @@ -50,6 +50,7 @@ #include "display/manager.h" #include "input/manager.h" #include "input/input_event.h" +#include "rpc/fast_cgi.h" #include "ui/root.h" #include "utils/variable_map.h" @@ -70,6 +71,8 @@ Control::Control() : m_variables(new utils::VariableMap()), m_downloadVariables(new utils::VariableMap()), + m_fastCgi(NULL), + m_tick(0) { m_core = new core::Manager(); @@ -123,6 +126,8 @@ Control::initialize() { void Control::cleanup() { + delete m_fastCgi; m_fastCgi = NULL; + priority_queue_erase(&taskScheduler, &m_taskShutdown); m_inputStdin->remove(m_core->get_poll_manager()->get_torrent_poll()); diff --git a/src/control.h b/src/control.h index feddc9bf..965e1728 100644 --- a/src/control.h +++ b/src/control.h @@ -62,6 +62,10 @@ namespace input { class Manager; } +namespace rpc { + class FastCgi; +} + namespace utils { class VariableMap; } @@ -99,6 +103,9 @@ public: utils::VariableMap* variable() { return m_variables; } utils::VariableMap* download_variables() { return m_downloadVariables; } + rpc::FastCgi* fast_cgi() { return m_fastCgi; } + void set_fast_cgi(rpc::FastCgi* f) { m_fastCgi = f; } + uint64_t tick() const { return m_tick; } void inc_tick() { m_tick++; } @@ -128,6 +135,8 @@ private: utils::VariableMap* m_variables; utils::VariableMap* m_downloadVariables; + rpc::FastCgi* m_fastCgi; + uint64_t m_tick; mode_t m_umask; diff --git a/src/option_handler_rules.cc b/src/option_handler_rules.cc index 2e4d0635..e605ddc5 100644 --- a/src/option_handler_rules.cc +++ b/src/option_handler_rules.cc @@ -64,6 +64,7 @@ #include "core/manager.h" #include "core/scheduler.h" #include "core/view_manager.h" +#include "rpc/fast_cgi.h" #include "ui/root.h" #include "utils/directory.h" #include "utils/parse.h" @@ -303,6 +304,14 @@ apply_tos(const std::string& arg) { cm->set_priority(value); } +void +apply_fast_cgi(const std::string& arg) { + if (control->fast_cgi() != NULL) + throw torrent::input_error("FastCGI already enabled."); + + control->set_fast_cgi(new rpc::FastCgi(arg)); +} + void apply_view_filter(const torrent::Object::list_type& args) { if (args.size() < 1) @@ -452,6 +461,7 @@ initialize_variables() { variables->insert("http_proxy", new utils::VariableStringSlot(rak::mem_fn(control->core()->get_poll_manager()->get_http_stack(), &core::CurlStack::http_proxy), rak::mem_fn(control->core()->get_poll_manager()->get_http_stack(), &core::CurlStack::set_http_proxy))); + variables->insert("fast_cgi", new utils::VariableStringSlot(NULL, rak::ptr_fn(&apply_fast_cgi))); variables->insert("max_chunks_queued", new utils::VariableValue(0)); variables->insert("min_peers", new utils::VariableValue(40)); diff --git a/src/rpc/Makefile.am b/src/rpc/Makefile.am new file mode 100644 index 00000000..78d299a1 --- /dev/null +++ b/src/rpc/Makefile.am @@ -0,0 +1,7 @@ +noinst_LIBRARIES = libsub_rpc.a + +libsub_rpc_a_SOURCES = \ + fast_cgi.cc \ + fast_cgi.h + +INCLUDES = -I$(srcdir) -I$(srcdir)/.. -I$(top_srcdir) diff --git a/src/rpc/fast_cgi.cc b/src/rpc/fast_cgi.cc new file mode 100644 index 00000000..f22651b6 --- /dev/null +++ b/src/rpc/fast_cgi.cc @@ -0,0 +1,167 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005-2006, Jari Sundell +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// In addition, as a special exception, the copyright holders give +// permission to link the code of portions of this program with the +// OpenSSL library under certain conditions as described in each +// individual source file, and distribute linked combinations +// including the two. +// +// You must obey the GNU General Public License in all respects for +// all of the code used other than OpenSSL. If you modify file(s) +// with this exception, you may extend this exception to your version +// of the file(s), but you are not obligated to do so. If you do not +// wish to do so, delete this exception statement from your version. +// If you delete this exception statement from all source files in the +// program, then also delete it here. +// +// Contact: Jari Sundell +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#include "config.h" + +#ifdef HAVE_FASTCGI +#include +#include + +#include +#include +#include +#include + +#include "globals.h" +#include "control.h" +#include "core/manager.h" + +#endif + +#include "fast_cgi.h" + +namespace rpc { + +bool FastCgi::m_initialized = false; + +#ifdef HAVE_FASTCGI + +FastCgi::FastCgi(const std::string& path) : m_path(path) { + if (!m_initialized) + if (FCGX_Init() != 0) + throw torrent::resource_error("Could not initialize FastCGI library."); + + // Register the fd with torrent::ConnectionManager. + + // Set non-blocking as per + // http://www.fastcgi.com/archives/fastcgi-developers/2004-January/003136.html? + if ((m_fileDesc = FCGX_OpenSocket(m_path.c_str(), 5)) == -1) + throw torrent::resource_error("FastCGI could not open socket."); + + m_request = new FCGX_Request; + + // Need FCGI_FAIL_ON_INTR flag? + if (fcntl(m_fileDesc, F_SETFL, O_NONBLOCK) != 0 || + FCGX_InitRequest(m_request, m_fileDesc, 0) != 0) { + ::close(m_fileDesc); + delete m_request; + + throw torrent::resource_error("FastCGI could not initialize request."); + } + + // Meh. + control->core()->get_poll_manager()->get_torrent_poll()->open(this); + control->core()->get_poll_manager()->get_torrent_poll()->insert_read(this); +} + +FastCgi::~FastCgi() { + if (m_request == NULL) + return; + + control->core()->get_poll_manager()->get_torrent_poll()->remove_read(this); + control->core()->get_poll_manager()->get_torrent_poll()->close(this); + + FCGX_Free(m_request, true); + + m_fileDesc = 0; + m_request = NULL; + + // Also unlink the socket file. +} + +// This is fundementally wrong as it blocks. We can live with it as +// the http server will usually buffer the whole requests before +// opening the connection. + +void +FastCgi::event_read() { + if (FCGX_Accept_r(m_request) != 0) { + control->core()->push_log("FastCGI accept failed."); + return; + } + + int length; + char* endPtr; + char* buffer = NULL; + + const char* contentLength = FCGX_GetParam("CONTENT_LENGTH", m_request->envp); + + if (contentLength == NULL || (length = strtol(contentLength, &endPtr, 10)) < 0 || *endPtr != '\0') { + control->core()->push_log("FastCGI invalid content length."); + goto event_read_exit; + } + + buffer = new char[length]; + + if (FCGX_GetStr(buffer, length, m_request->in) != length) { + control->core()->push_log("FastCGI could not read sufficient data."); + goto event_read_exit; + } + + if (FCGX_PutS("This worked!!!", m_request->out) == -1) { + control->core()->push_log("FastCGI could not write data."); + goto event_read_exit; + } + + control->core()->push_log("Got a message: " + std::string(buffer, buffer + 30)); + + FCGX_Finish_r(m_request); + +event_read_exit: + FCGX_Finish_r(m_request); + delete buffer; +} + +void +FastCgi::event_write() { +} + +void +FastCgi::event_error() { + throw torrent::internal_error("FastCgi::event_error()."); +} + +#else + +FastCgi::FastCgi(const std::string& path) { throw torrent::resource_error("FastCGI not supported."); } +FastCgi::~FastCgi() {} +void FastCgi::event_read() {} +void FastCgi::event_write() {} +void FastCgi::event_error() {} + +#endif + +} diff --git a/src/rpc/fast_cgi.h b/src/rpc/fast_cgi.h new file mode 100644 index 00000000..a781b600 --- /dev/null +++ b/src/rpc/fast_cgi.h @@ -0,0 +1,67 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005-2006, Jari Sundell +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// In addition, as a special exception, the copyright holders give +// permission to link the code of portions of this program with the +// OpenSSL library under certain conditions as described in each +// individual source file, and distribute linked combinations +// including the two. +// +// You must obey the GNU General Public License in all respects for +// all of the code used other than OpenSSL. If you modify file(s) +// with this exception, you may extend this exception to your version +// of the file(s), but you are not obligated to do so. If you do not +// wish to do so, delete this exception statement from your version. +// If you delete this exception statement from all source files in the +// program, then also delete it here. +// +// Contact: Jari Sundell +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#ifndef RTORRENT_RPC_FAST_CGI_H +#define RTORRENT_RPC_FAST_CGI_H + +#include +#include + +struct FCGX_Request; + +namespace rpc { + +class FastCgi : public torrent::Event { +public: + FastCgi(const std::string& path); + virtual ~FastCgi(); + + const std::string path() const { return m_path; } + + virtual void event_read(); + virtual void event_write(); + virtual void event_error(); + +private: + static bool m_initialized; + + FCGX_Request* m_request; + std::string m_path; +}; + +} + +#endif