Changeset 12396


Ignore:
Timestamp:
Apr 28, 2011, 6:37:27 PM (10 years ago)
Author:
jordan
Message:

(trunk gtk) gtr_get_host_from_url(): avoid a couple of malloc() + free() calls.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/util.c

    r12306 r12396  
    209209gtr_get_host_from_url( char * buf, size_t buflen, const char * url )
    210210{
    211     char * h = NULL;
    212 
    213     tr_urlParse( url, -1, NULL, &h, NULL, NULL );
    214 
    215     if( tr_addressIsIP( h ) )
     211    char host[1024];
     212    const char * pch;
     213
     214    if(( pch = strstr( url, "://" ))) {
     215        const size_t hostlen = strcspn( pch+3, ":/" );
     216        const size_t copylen = MIN( hostlen, sizeof(host)-1 );
     217        memcpy( host, pch+3, copylen );
     218        host[copylen] = '\0';
     219    } else {
     220        *host = '\0';
     221    }
     222
     223    if( tr_addressIsIP( host ) )
    216224        g_strlcpy( buf, url, buflen );
    217225    else {
    218         const char * first_dot = strchr( h, '.' );
    219         const char * last_dot = strrchr( h, '.' );
     226        const char * first_dot = strchr( host, '.' );
     227        const char * last_dot = strrchr( host, '.' );
    220228        if( ( first_dot ) && ( last_dot ) && ( first_dot != last_dot ) )
    221229            g_strlcpy( buf, first_dot + 1, buflen );
    222230        else
    223             g_strlcpy( buf, h, buflen );
    224     }
    225 
    226     tr_free( h );
     231            g_strlcpy( buf, host, buflen );
     232    }
    227233}
    228234
Note: See TracChangeset for help on using the changeset viewer.