add support for compressing the content served by the rpc server -- rpc responses and clutch html/css/js files

This commit is contained in:
Charles Kerr 2008-10-01 15:53:56 +00:00
parent 859af09f37
commit 585a9783ad
7 changed files with 127 additions and 13 deletions

3
NEWS
View File

@ -3,8 +3,9 @@ NEWS file for Transmission <http://www.transmissionbt.com/>
1.40 (2008/mm/dd)
http://trac.transmissionbt.com/query?group=component&milestone=1.40
- All Platforms
+ Lazy bitfields
+ Rewritten bandwidth management code
+ Lazy bitfields
+ Support compression when serving Web Interface content and RPC responses
- Mac
+ Option to automatically update the blocklist weekly
+ Sparkle updated to 1.5

View File

@ -68,6 +68,7 @@ AC_SEARCH_LIBS([socket], [socket net])
AC_SEARCH_LIBS([gethostbyname], [nsl bind])
PKG_CHECK_MODULES(OPENSSL, [openssl >= $OPENSSL_MINIMUM], , [CHECK_SSL()])
PKG_CHECK_MODULES(LIBCURL, [libcurl >= $CURL_MINIMUM])
CHECK_ZLIB()
AC_SYS_LARGEFILE

View File

@ -1056,6 +1056,9 @@ processRequests( const char * host,
curl = curl_easy_init( );
curl_easy_setopt( curl, CURLOPT_VERBOSE, debug );
#ifdef HAVE_LIBZ
curl_easy_setopt( curl, CURLOPT_ENCODING, "deflate" );
#endif
curl_easy_setopt( curl, CURLOPT_USERAGENT,
MY_NAME "/" LONG_VERSION_STRING );
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writeFunc );

View File

@ -19,6 +19,10 @@
#include <fcntl.h> /* open */
#include <unistd.h> /* close */
#ifdef HAVE_LIBZ
#include <zlib.h>
#endif
#include <libevent/event.h>
#include <libevent/evhttp.h>
@ -203,6 +207,51 @@ mimetype_guess( const char * path )
return "application/octet-stream";
}
#ifdef HAVE_LIBZ
static void
compress_evbuf( struct evbuffer * evbuf )
{
static struct evbuffer *tmp;
static z_stream stream;
static unsigned char buffer[2048];
if( !tmp ) {
tmp = evbuffer_new( );
deflateInit( &stream, Z_BEST_COMPRESSION );
}
deflateReset( &stream );
stream.next_in = EVBUFFER_DATA(evbuf);
stream.avail_in = EVBUFFER_LENGTH(evbuf);
do {
stream.next_out = buffer;
stream.avail_out = sizeof( buffer );
if( deflate( &stream, Z_FULL_FLUSH ) == Z_OK )
evbuffer_add( tmp, buffer, sizeof( buffer ) - stream.avail_out );
else
break;
} while (stream.avail_out == 0);
/*fprintf( stderr, "deflated response from %zu to %zu bytes\n", EVBUFFER_LENGTH( evbuf ), EVBUFFER_LENGTH( tmp ) );*/
evbuffer_drain(evbuf, EVBUFFER_LENGTH(evbuf));
evbuffer_add_buffer(evbuf, tmp);
}
#endif
static void
maybe_deflate_response( struct evhttp_request * req, struct evbuffer * response )
{
#ifdef HAVE_LIBZ
const char * accept_encoding = evhttp_find_header( req->input_headers, "Accept-Encoding" );
const int do_deflate = accept_encoding && strstr( accept_encoding, "deflate" );
if( do_deflate ) {
evhttp_add_header( req->output_headers, "Content-Encoding", "deflate" );
compress_evbuf( response );
}
#endif
}
static void
serve_file( struct evhttp_request * req,
const char * path )
@ -226,6 +275,7 @@ serve_file( struct evhttp_request * req,
evhttp_add_header( req->output_headers, "Content-Type",
mimetype_guess(
path ) );
maybe_deflate_response( req, buf );
evhttp_send_reply( req, HTTP_OK, "OK", buf );
evbuffer_free( buf );
close( fd );
@ -265,6 +315,7 @@ handle_clutch( struct evhttp_request * req,
evbuffer_free( buf );
}
static void
handle_rpc( struct evhttp_request * req,
struct tr_rpc_server * server )
@ -294,6 +345,7 @@ handle_rpc( struct evhttp_request * req,
buf = evbuffer_new( );
evbuffer_add( buf, response, len );
maybe_deflate_response( req, buf );
evhttp_add_header( req->output_headers, "Content-Type",
"application/json; charset=UTF-8" );
evhttp_send_reply( req, HTTP_OK, "OK", buf );
@ -328,7 +380,6 @@ handle_request( struct evhttp_request * req,
void * arg )
{
struct tr_rpc_server * server = arg;
fprintf( stderr, "%s:%d Got request: \"%s\"\n", __FILE__, __LINE__, req->uri );
if( req && req->evcon )
{
@ -394,7 +445,6 @@ handle_request( struct evhttp_request * req,
tr_free( user );
}
fprintf( stderr, "%s:%d\n", __FILE__, __LINE__ );
}
static void
@ -402,15 +452,10 @@ startServer( void * vserver )
{
tr_rpc_server * server = vserver;
fprintf( stderr, "%s:%d in startServer; current context is %p\n", __FILE__, __LINE__, server->httpd );
if( !server->httpd )
{
int i;
server->httpd = evhttp_new( tr_eventGetBase( server->session ) );
fprintf( stderr, "%s:%d in startServer; new context is %p\n", __FILE__, __LINE__, server->httpd );
i = evhttp_bind_socket( server->httpd, "0.0.0.0", server->port );
fprintf( stderr, "evhttp_bind_socket returned %d\n", i );
evhttp_bind_socket( server->httpd, "0.0.0.0", server->port );
evhttp_set_gencb( server->httpd, handle_request, server );
}
}

64
m4/check-zlib.m4 Normal file
View File

@ -0,0 +1,64 @@
AC_DEFUN([CHECK_ZLIB],
#
# http://autoconf-archive.cryp.to/check_zlib.html
# Handle user hints
#
[AC_MSG_CHECKING(if zlib is wanted)
AC_ARG_WITH(zlib,
[ --with-zlib=DIR root directory path of zlib installation [defaults to
/usr/local or /usr if not found in /usr/local]
--without-zlib to disable zlib usage completely],
[if test "$withval" != no ; then
AC_MSG_RESULT(yes)
if test -d "$withval"
then
ZLIB_HOME="$withval"
else
AC_MSG_WARN([Sorry, $withval does not exist, checking usual places])
fi
else
AC_MSG_RESULT(no)
fi])
ZLIB_HOME=/usr/local
if test ! -f "${ZLIB_HOME}/include/zlib.h"
then
ZLIB_HOME=/usr
fi
#
# Locate zlib, if wanted
#
if test -n "${ZLIB_HOME}"
then
ZLIB_OLD_LDFLAGS=$LDFLAGS
ZLIB_OLD_CPPFLAGS=$LDFLAGS
LDFLAGS="$LDFLAGS -L${ZLIB_HOME}/lib"
CPPFLAGS="$CPPFLAGS -I${ZLIB_HOME}/include"
AC_LANG_SAVE
AC_LANG_C
AC_CHECK_LIB(z, inflateEnd, [zlib_cv_libz=yes], [zlib_cv_libz=no])
AC_CHECK_HEADER(zlib.h, [zlib_cv_zlib_h=yes], [zlib_cv_zlib_h=no])
AC_LANG_RESTORE
if test "$zlib_cv_libz" = "yes" -a "$zlib_cv_zlib_h" = "yes"
then
#
# If both library and header were found, use them
#
AC_CHECK_LIB(z, inflateEnd)
AC_MSG_CHECKING(zlib in ${ZLIB_HOME})
AC_MSG_RESULT(ok)
else
#
# If either header or library was not found, revert and bomb
#
AC_MSG_CHECKING(zlib in ${ZLIB_HOME})
LDFLAGS="$ZLIB_OLD_LDFLAGS"
CPPFLAGS="$ZLIB_OLD_CPPFLAGS"
AC_MSG_RESULT(failed)
AC_MSG_ERROR(either specify a valid zlib installation with --with-zlib=DIR or disable zlib usage with --without-zlib)
fi
fi
])

View File

@ -230,7 +230,7 @@
#define TIME_WITH_SYS_TIME 1
/* Version number of package */
#define VERSION "1.4.1-beta"
#define VERSION "1.4.8-stable"
/* Define to appropriate substitue if compiler doesnt have __func__ */
/* #undef __func__ */
@ -251,4 +251,4 @@
/* #undef size_t */
/* Define to unsigned int if you dont have it */
/* #undef socklen_t */
/* #undef socklen_t */

View File

@ -237,7 +237,7 @@
#define _EVENT_TIME_WITH_SYS_TIME 1
/* Version number of package */
#define _EVENT_VERSION "1.4.1-beta"
#define _EVENT_VERSION "1.4.8-stable"
/* Define to appropriate substitue if compiler doesnt have __func__ */
/* #undef _EVENT___func__ */
@ -259,4 +259,4 @@
/* Define to unsigned int if you dont have it */
/* #undef _EVENT_socklen_t */
#endif
#endif