static int test_url (void) { int port; char * scheme; char * host; char * path; char * str; const char * url; url = "http://1"; check (tr_urlParse (url, TR_BAD_SIZE, &scheme, &host, &port, &path)); check_streq ("http", scheme); check_streq ("1", host); check_streq ("/", path); check_int_eq (80, port); tr_free (scheme); tr_free (path); tr_free (host); url = "http://www.some-tracker.org/some/path"; check (tr_urlParse (url, TR_BAD_SIZE, &scheme, &host, &port, &path)); check_streq ("http", scheme); check_streq ("www.some-tracker.org", host); check_streq ("/some/path", path); check_int_eq (80, port); tr_free (scheme); tr_free (path); tr_free (host); url = "http://www.some-tracker.org:80/some/path"; check (tr_urlParse (url, TR_BAD_SIZE, &scheme, &host, &port, &path)); check_streq ("http", scheme); check_streq ("www.some-tracker.org", host); check_streq ("/some/path", path); check_int_eq (80, port); tr_free (scheme); tr_free (path); tr_free (host); url = "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2"; str = tr_http_unescape (url, strlen (url)); check_streq ("http://www.example.com/~user/?test=1&test1=2", str); tr_free (str); return 0; }
static void doDNS( void * vtask ) { tr_address addr; int port = -1; char * host = NULL; struct hostent *he; struct sockaddr_in adr; struct tr_web_task * task = vtask; tr_dns_result lookup_result = TR_DNS_UNTESTED; assert( task->resolved_host == NULL ); if( !tr_urlParse( task->url, -1, NULL, &host, &port, NULL ) ) { task->port = port; task->host = host; /* If 'host' is an IPv4 or IPv6 address in text form, use it as-is. * Otherwise, see if its resolved name is in our DNS cache */ if( tr_pton( task->host, &addr ) != NULL ) { task->resolved_host = task->host; lookup_result = TR_DNS_OK; } else { if( (he = gethostbyname(host)) ) { memcpy(&adr.sin_addr, he->h_addr_list[0], he->h_length); task->resolved_host = strdup(inet_ntoa(adr.sin_addr)); lookup_result = TR_DNS_OK; } } } if( lookup_result != TR_DNS_UNTESTED ) { addTask( task ); } else if( !host || evdns_resolve_ipv4( host, 0, dns_ipv4_done_cb, task ) ) { dns_ipv4_done_cb( DNS_ERR_UNKNOWN, DNS_IPv4_A, 0, 0, NULL, task ); } }
/* pattern-matching text; ie, legaltorrents.com */ char* gtr_get_host_from_url( const char * url ) { char * h = NULL; char * name; tr_urlParse( url, -1, NULL, &h, NULL, NULL ); if( tr_addressIsIP( h ) ) name = g_strdup( h ); else { const char * first_dot = strchr( h, '.' ); const char * last_dot = strrchr( h, '.' ); if( ( first_dot ) && ( last_dot ) && ( first_dot != last_dot ) ) name = g_strdup( first_dot + 1 ); else name = g_strdup( h ); } tr_free( h ); return name; }