Changeset 6845


Ignore:
Timestamp:
Oct 3, 2008, 5:38:14 PM (13 years ago)
Author:
charles
Message:

(rpc) more robust `deflate' wrangling, possibly fixing ticket #1319

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/rpc-server.c

    r6835 r6845  
    210210
    211211#ifdef HAVE_LIBZ
    212 static void
     212static int
    213213compress_evbuf( struct evbuffer * evbuf )
    214214{
    215     static struct evbuffer * tmp;
    216     static z_stream          stream;
    217     static unsigned char     buffer[2048];
    218 
    219     if( !tmp )
    220     {
    221         tmp = evbuffer_new( );
    222         deflateInit( &stream, Z_BEST_COMPRESSION );
    223     }
    224 
    225     deflateReset( &stream );
     215    int err = 0;
     216    struct evbuffer  * out;
     217    static z_stream    stream;
     218
     219    stream.zalloc = Z_NULL;
     220    stream.zfree = Z_NULL;
     221    stream.opaque = Z_NULL;
     222    deflateInit( &stream, Z_DEFAULT_COMPRESSION );
     223
    226224    stream.next_in = EVBUFFER_DATA( evbuf );
    227225    stream.avail_in = EVBUFFER_LENGTH( evbuf );
    228 
    229     do
    230     {
    231         stream.next_out = buffer;
    232         stream.avail_out = sizeof( buffer );
    233         if( deflate( &stream, Z_FULL_FLUSH ) == Z_OK )
    234             evbuffer_add( tmp, buffer, sizeof( buffer ) - stream.avail_out );
    235         else
     226    out = evbuffer_new( );
     227
     228    while( !err ) {
     229        unsigned char buf[1024];
     230        int state;
     231        stream.next_out = buf;
     232        stream.avail_out = sizeof( buf );
     233        state = deflate( &stream, Z_FINISH );
     234        if( ( state != Z_OK ) && ( state != Z_STREAM_END ) ) {
     235            tr_nerr( MY_NAME, _( "Error deflating file: %s" ), zError( err ) );
     236            err = state;
    236237            break;
    237     }
    238     while( stream.avail_out == 0 );
    239 
    240 /*fprintf( stderr, "deflated response from %zu to %zu bytes\n", EVBUFFER_LENGTH(
    241   evbuf ), EVBUFFER_LENGTH( tmp ) );*/
    242     evbuffer_drain( evbuf, EVBUFFER_LENGTH( evbuf ) );
    243     evbuffer_add_buffer( evbuf, tmp );
    244 }
    245 
     238        }
     239        evbuffer_add( out, buf, sizeof(buf) - stream.avail_out );
     240        if( state == Z_STREAM_END )
     241            break;
     242    }
     243
     244    if( !err ) {
     245        fprintf( stderr, "deflated response from %zu bytes to %zu\n",
     246                 EVBUFFER_LENGTH( evbuf ),
     247                 EVBUFFER_LENGTH( out ) );
     248        evbuffer_drain( evbuf, EVBUFFER_LENGTH( evbuf ) );
     249        evbuffer_add_buffer( evbuf, out );
     250    }
     251
     252    deflateEnd( &stream );
     253    evbuffer_free( out );
     254    return err;
     255}
    246256#endif
    247257
     
    255265    const int    do_deflate = accept_encoding && strstr( accept_encoding,
    256266                                                         "deflate" );
    257     if( do_deflate )
    258     {
    259         evhttp_add_header( req->output_headers, "Content-Encoding",
    260                            "deflate" );
    261         compress_evbuf( response );
    262     }
     267    if( do_deflate && !compress_evbuf( response ) )
     268        evhttp_add_header( req->output_headers, "Content-Encoding", "deflate" );
    263269#endif
    264270}
Note: See TracChangeset for help on using the changeset viewer.