* Added FastCGI support for RPC. This will be hooked up with XMLRPC-C.

* Experimenting with move semantics for Object.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@886 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2007-04-06 22:12:52 +00:00
parent 93afd05e18
commit d6fd8f08dc
9 changed files with 323 additions and 0 deletions
+5
View File
@@ -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
])
+51
View File
@@ -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 <fcgiapp.h>
],[ 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 <fcgiapp.h>
],[ 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)
])
])
+2
View File
@@ -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 = \
+5
View File
@@ -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());
+9
View File
@@ -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;
+10
View File
@@ -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));
+7
View File
@@ -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)
+167
View File
@@ -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 <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#ifdef HAVE_FASTCGI
#include <memory>
#include <fcgiapp.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <torrent/exceptions.h>
#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
}
+67
View File
@@ -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 <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_RPC_FAST_CGI_H
#define RTORRENT_RPC_FAST_CGI_H
#include <string>
#include <torrent/event.h>
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