/* Thread used to set up a connection */ void *setup_thread( void *c ) { conn_t *conn = c; int oldstate; /* Allow this thread to be killed at any time. */ pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, &oldstate ); pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate ); if( conn_setup( conn ) ) { conn->last_transfer = gettime(); if( conn_exec( conn ) ) { conn->last_transfer = gettime(); conn->enabled = 1; conn->state = 0; return( NULL ); } } conn_disconnect( conn ); conn->state = 0; return( NULL ); }
void *setup_thread_cb( void *c ) #endif { conn_t *conn = c; int oldstate; /* Allow this thread to be killed at any time. */ #if !WIN32 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate); #endif if (conn_setup(conn)) { conn->last_transfer = gettime(); if (conn_exec(conn)) { conn->last_transfer = gettime(); conn->enabled = 1; conn->state = 0; #if WIN32 return 0; #else return NULL; #endif } } conn_disconnect(conn); conn->state = 0; #if WIN32 return 0; #else return NULL; #endif }
int search_makelist( search_t *results, char *url ) { int i, size = 8192, j = 0; char *s, *s1, *s2, *s3; conn_t conn[1]; double t; memset( conn, 0, sizeof( conn_t ) ); conn->conf = results->conf; t = gettime(); if( !conn_set( conn, url ) ) return( -1 ); if( !conn_init( conn ) ) return( -1 ); if( !conn_info( conn ) ) return( -1 ); strcpy( results[0].url, url ); results[0].speed = 1 + 1000 * ( gettime() - t ); results[0].size = conn->size; s = malloc( size ); sprintf( s, "http://www.filesearching.com/cgi-bin/s?q=%s&w=a&l=en&" "t=f&e=on&m=%i&o=n&s1=%lld&s2=%lld&x=15&y=15", conn->file, results->conf->search_amount, conn->size, conn->size ); conn_disconnect( conn ); memset( conn, 0, sizeof( conn_t ) ); conn->conf = results->conf; if( !conn_set( conn, s ) ) { free( s ); return( 1 ); } if( !conn_setup( conn ) ) { free( s ); return( 1 ); } if( !conn_exec( conn ) ) { free( s ); return( 1 ); } while( ( i = read( conn->fd, s + j, size - j ) ) > 0 ) { j += i; if( j + 10 >= size ) { size *= 2; s = realloc( s, size ); memset( s + size / 2, 0, size / 2 ); } } conn_disconnect( conn ); s1 = strstr( s, "<pre class=list" ); s1 = strchr( s1, '\n' ) + 1; if( strstr( s1, "</pre>" ) == NULL ) { /* Incomplete list */ free( s ); return( 1 ); } for( i = 1; strncmp( s1, "</pre>", 6 ) && i < results->conf->search_amount && *s1; i ++ ) { s3 = strchr( s1, '\n' ); *s3 = 0; s2 = strrstr( s1, "<a href=" ) + 8; *s3 = '\n'; s3 = strchr( s2, ' ' ); *s3 = 0; if( strcmp( results[0].url, s2 ) ) { strncpy( results[i].url, s2, MAX_STRING ); results[i].size = results[0].size; results[i].conf = results->conf; } else { /* The original URL might show up */ i --; } for( s1 = s3; *s1 != '\n'; s1 ++ ); s1 ++; } free( s ); return( i ); }
/* Get file size and other information */ int conn_info( conn_t *conn ) { /* It's all a bit messed up.. But it works. */ if( conn->proto == PROTO_FTP && !conn->proxy ) { ftp_command( conn->ftp, "REST %lld", 1 ); if( ftp_wait( conn->ftp ) / 100 == 3 || conn->ftp->status / 100 == 2 ) { conn->supported = 1; ftp_command( conn->ftp, "REST %lld", 0 ); ftp_wait( conn->ftp ); } else { conn->supported = 0; } if( !ftp_cwd( conn->ftp, conn->dir ) ) return( 0 ); conn->size = ftp_size( conn->ftp, conn->file, MAX_REDIR ); if( conn->size < 0 ) conn->supported = 0; if( conn->size == -1 ) return( 0 ); else if( conn->size == -2 ) conn->size = INT_MAX; } else { char s[MAX_STRING], *t; long long int i = 0; do { conn->currentbyte = 1; if( !conn_setup( conn ) ) return( 0 ); conn_exec( conn ); conn_disconnect( conn ); /* Code 3xx == redirect */ if( conn->http->status / 100 != 3 ) break; if( ( t = http_header( conn->http, "location:" ) ) == NULL ) return( 0 ); sscanf( t, "%1023s", s ); // Warning: truncating to MAX_STRING if( strstr( s, "://" ) == NULL) { sprintf( conn->http->headers, "%s%s", conn_url( conn ), s ); strncpy( s, conn->http->headers, MAX_STRING ); } else if( s[0] == '/' ) { sprintf( conn->http->headers, "http://%s:%i%s", conn->host, conn->port, s ); strncpy( s, conn->http->headers, MAX_STRING ); } conn_set( conn, s ); i ++; } while( conn->http->status / 100 == 3 && i < MAX_REDIR ); if( i == MAX_REDIR ) { sprintf( conn->message, _("Too many redirects.\n") ); return( 0 ); } conn->size = http_size( conn->http ); if( conn->http->status == 206 && conn->size >= 0 ) { conn->supported = 1; conn->size ++; } else if( conn->http->status == 200 || conn->http->status == 206 ) { conn->supported = 0; conn->size = INT_MAX; } else { t = strchr( conn->message, '\n' ); if( t == NULL ) sprintf( conn->message, _("Unknown HTTP error.\n") ); else *t = 0; return( 0 ); } } return( 1 ); }