* Dynamically sized SCGI read buffer.

* Added 'call_command' that takes a view as the first parameter and
then a list of commands. It will return a list of lists containing the
results from those commands.

* Fixed the xmlrpc-c-config usage.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@954 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2007-08-15 21:38:44 +00:00
parent 6ca373d293
commit 11976871ac
6 changed files with 92 additions and 47 deletions
+1 -1
View File
@@ -204,7 +204,7 @@ parse_command_name(const char* first, const char* last, std::string* dest) {
if (first == last || !std::isalpha(*first))
throw torrent::input_error("Invalid start of name.");
for ( ; first != last && (std::isalnum(*first) || *first == '_'); ++first)
for ( ; first != last && (std::isalnum(*first) || *first == '_' || *first == '.'); ++first)
dest->push_back(*first);
return first;
+1
View File
@@ -110,6 +110,7 @@ inline torrent::Object call_command_t(const char* key, torrent::Tracker* tracker
inline torrent::Object
call_command_d_range(const char* key, core::Download* download, torrent::Object::list_type::const_iterator first, torrent::Object::list_type::const_iterator last) {
// Change to using range ctor.
torrent::Object rawArgs(torrent::Object::TYPE_LIST);
torrent::Object::list_type& args = rawArgs.as_list();
+51 -27
View File
@@ -58,10 +58,11 @@ namespace rpc {
void
SCgiTask::open(SCgi* parent, int fd) {
m_parent = parent;
m_parent = parent;
m_fileDesc = fd;
m_buffer = new char[(m_bufferSize = 2048)];
m_buffer = new char[(m_bufferSize = default_buffer_size) + 1];
m_position = m_buffer;
m_body = NULL;
control->poll()->open(this);
control->poll()->insert_read(this);
@@ -94,7 +95,7 @@ SCgiTask::close() {
void
SCgiTask::event_read() {
int bytes = ::recv(m_fileDesc, m_position, m_bufferSize - 1 - (m_position - m_buffer), 0);
int bytes = ::recv(m_fileDesc, m_position, m_bufferSize - (m_position - m_buffer), 0);
if (bytes == -1) {
if (!rak::error_number::current().is_blocked_momentary())
@@ -103,45 +104,68 @@ SCgiTask::event_read() {
return;
}
// The buffer has space to nul-terminate to ease the parsing below.
m_position += bytes;
*m_position = '\0';
// Don't bother caching the parsed values, as we're likely to
// receive all the data we need the first time.
char* current;
char* contentPos;
if (m_body == NULL) {
// Don't bother caching the parsed values, as we're likely to
// receive all the data we need the first time.
char* current;
int headerSize = strtol(m_buffer, &current, 0);
int contentSize;
int contentSize;
int headerSize = strtol(m_buffer, &current, 0);
if (current == m_buffer || current == m_position)
// Need to validate the header size.
return;
if (current == m_buffer || current == m_position)
return;
if (*current != ':' || headerSize < 17)
goto event_read_failed;
if (*current != ':' || headerSize < 17 || headerSize > max_header_size)
goto event_read_failed;
if (std::distance(++current, m_position) < headerSize + 1)
return;
if (std::distance(++current, m_position) < headerSize + 1)
return;
if (std::memcmp(current, "CONTENT_LENGTH", 15) != 0)
goto event_read_failed;
if (std::memcmp(current, "CONTENT_LENGTH", 15) != 0)
goto event_read_failed;
contentSize = strtol(current + 15, &contentPos, 0);
char* contentPos;
contentSize = strtol(current + 15, &contentPos, 0);
if (*contentPos != '\0' || contentSize <= 0)
goto event_read_failed;
if (*contentPos != '\0' || contentSize <= 0 || contentSize > max_content_size)
goto event_read_failed;
// Start of the data.
current += headerSize + 1;
m_body = current + headerSize + 1;
headerSize = std::distance(m_buffer, m_body);
if (std::distance(current, m_position) < contentSize)
if ((unsigned int)(contentSize + headerSize) < m_bufferSize) {
m_bufferSize = contentSize + headerSize;
} else if ((unsigned int)(contentSize + headerSize) <= default_buffer_size) {
m_bufferSize = contentSize;
std::memmove(m_buffer, m_body, std::distance(m_body, m_position));
m_position = m_buffer + std::distance(m_body, m_position);
m_body = m_buffer;
} else {
char* tmp = new char[(m_bufferSize = contentSize)];
std::memcpy(tmp, m_body, std::distance(m_body, m_position));
delete [] m_buffer;
m_position = tmp + std::distance(m_body, m_position);
m_buffer = tmp;
m_body = tmp;
}
}
if ((unsigned int)std::distance(m_buffer, m_position) != m_bufferSize)
return;
control->poll()->remove_read(this);
control->poll()->insert_write(this);
if (!m_parent->receive_call(this, current, contentSize))
// Close if the call failed, else stay open to write back data.
if (!m_parent->receive_call(this, m_body, m_bufferSize - std::distance(m_buffer, m_body)))
close();
return;
@@ -175,7 +199,8 @@ SCgiTask::event_error() {
bool
SCgiTask::receive_write(const char* buffer, uint32_t length) {
if (length + 256 > m_bufferSize) {
// Need to cast due to a bug in MacOSX gcc-4.0.1.
if (length + 256 > std::max(m_bufferSize, (unsigned int)default_buffer_size)) {
delete [] m_buffer;
m_buffer = new char[length + 256];
}
@@ -187,7 +212,6 @@ SCgiTask::receive_write(const char* buffer, uint32_t length) {
m_bufferSize = length + headerSize;
std::memcpy(m_buffer + headerSize, buffer, length);
event_write();
return true;
+6
View File
@@ -49,6 +49,10 @@ class SCgi;
class SCgiTask : public torrent::Event {
public:
static const unsigned int default_buffer_size = 2047;
static const int max_header_size = 2000;
static const int max_content_size = (128 << 10);
SCgiTask() { m_fileDesc = -1; }
bool is_open() const { return m_fileDesc != -1; }
@@ -70,6 +74,8 @@ private:
char* m_buffer;
char* m_position;
char* m_body;
unsigned int m_bufferSize;
};