Changeset 6800


Ignore:
Timestamp:
Sep 26, 2008, 12:58:06 AM (13 years ago)
Author:
charles
Message:

(libT) more rpc-server cleanup

File:
1 edited

Legend:

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

    r6798 r6800  
    1515#include <limits.h> /* INT_MAX */
    1616
    17 #include <sys/types.h>
    18 #include <sys/stat.h>
    19 #include <fcntl.h> /* open */
    20 #include <unistd.h> /* close */
     17#include <sys/types.h> /* open */
     18#include <sys/stat.h>  /* open */
     19#include <fcntl.h>     /* open */
     20#include <unistd.h>    /* close */
    2121
    2222#include <libevent/event.h>
     
    3434#define MY_NAME "RPC Server"
    3535#define MY_REALM "Transmission"
    36 #define MAX_TOKEN_LEN 16
    3736#define TR_N_ELEMENTS( ary ) ( sizeof( ary ) / sizeof( *ary ) )
    38 
    39 struct acl_addr
    40 {
    41     char flag; /* '+' to allow, '-' to deny */
    42     char quads[4][MAX_TOKEN_LEN];
    43 };
    4437
    4538struct tr_rpc_server
     
    8275
    8376static int
    84 parseAddress( const char * addr, struct acl_addr * setme, const char ** end )
    85 {
    86     const char * pch;
    87 
    88     memset( setme, 0, sizeof( struct acl_addr ) );
    89 
    90     if( !addr ) return 0;
    91 
    92     if(!((pch = strchr( addr, '.' )))) return 0;
    93     if( pch-addr > MAX_TOKEN_LEN ) return 0;
    94     memcpy( setme->quads[0], addr, pch-addr );
    95     addr = pch + 1;
    96 
    97     if(!((pch = strchr( addr, '.' )))) return 0;
    98     if( pch-addr > MAX_TOKEN_LEN ) return 0;
    99     memcpy( setme->quads[1], addr, pch-addr );
    100     addr = pch + 1;
    101 
    102     if(!((pch = strchr( addr, '.' )))) return 0;
    103     if( pch-addr > MAX_TOKEN_LEN ) return 0;
    104     memcpy( setme->quads[2], addr, pch-addr );
    105     addr = pch + 1;
    106 
    107     while( *pch && *pch!=',' ) ++pch;
    108     if( pch-addr > MAX_TOKEN_LEN ) return 0;
    109     memcpy( setme->quads[3], addr, pch-addr );
    110 
    111     *end = pch;
    112     return 1;
    113 }
    114 
    115 static int
    11677isAddressAllowed( const tr_rpc_server * server,
    11778                  const char * address )
    11879{
    11980    const char * acl;
    120     struct acl_addr tmp;
    121     const char * end;
    122     const int parsed = parseAddress( address, &tmp, &end );
    123 
    124     if( !parsed )
    125         return 0;
    12681
    12782    for( acl=server->acl; acl && *acl; )
     
    161116    if( req->type != EVHTTP_REQ_POST )
    162117    {
    163         send_simple_response( req, HTTP_BADREQUEST, NULL );
     118        send_simple_response( req, 405, NULL );
    164119    }
    165120    else
     
    283238        else
    284239        {
    285             char size[12];
    286             struct evbuffer * buf = evbuffer_new();
    287 
     240            struct evbuffer * buf = evbuffer_new( );
    288241            evbuffer_read(buf, fd, INT_MAX );
    289242            evhttp_add_header(req->output_headers, "Content-Type", mimetype_guess( path ) );
    290             snprintf(size, sizeof(size), "%zu", EVBUFFER_LENGTH( buf ) );
    291             evhttp_add_header(req->output_headers, "Content-Length", size );
    292243            evhttp_send_reply( req, HTTP_OK, "OK", buf );
    293 
    294244            evbuffer_free(buf);
    295245            close( fd );
     
    311261        evbuffer_add_printf( buf, "index.html" );
    312262    else {
    313         const char * pch;
    314         if(( pch = strchr( uri, '?' )))
     263        const char * pch = strchr( uri, '?' );
     264        if( pch )
    315265            evbuffer_add_printf( buf, "%*.*s", (int)(pch-uri), (int)(pch-uri), uri );
    316266        else
     
    319269                             
    320270    if( strstr( (const char *)EVBUFFER_DATA( buf ), ".." ) )
    321         serve_file( req, "/dev/null" );
     271        send_simple_response( req, 401, NULL );
    322272    else
    323273        serve_file( req, (const char *)EVBUFFER_DATA( buf ) );
Note: See TracChangeset for help on using the changeset viewer.