Changeset 9750
- Timestamp:
- Dec 14, 2009, 12:54:30 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/web.c
r9749 r9750 55 55 }; 56 56 57 static void 58 web_close( tr_web * g ) 59 { 60 curl_multi_cleanup( g->multi ); 61 evtimer_del( &g->timer_event ); 62 tr_free( g ); 63 } 64 57 65 /*** 58 66 **** … … 70 78 }; 71 79 80 static void 81 task_free( struct tr_web_task * task ) 82 { 83 evbuffer_free( task->response ); 84 tr_free( task->range ); 85 tr_free( task->url ); 86 tr_free( task ); 87 } 88 89 /*** 90 **** 91 ***/ 92 72 93 static size_t 73 94 writeFunc( void * ptr, size_t size, size_t nmemb, void * vtask ) … … 123 144 { 124 145 CURLMcode mcode; 125 CURL * e asy= curl_easy_init( );146 CURL * e = curl_easy_init( ); 126 147 struct tr_web * web = session->web; 127 148 const long timeout = getTimeoutFromURL( task->url ); … … 132 153 133 154 if( !task->range && session->isProxyEnabled ) { 134 curl_easy_setopt( e asy, CURLOPT_PROXY, session->proxy );135 curl_easy_setopt( e asy, CURLOPT_PROXYAUTH, CURLAUTH_ANY );136 curl_easy_setopt( e asy, CURLOPT_PROXYPORT, session->proxyPort );137 curl_easy_setopt( e asy, CURLOPT_PROXYTYPE,155 curl_easy_setopt( e, CURLOPT_PROXY, session->proxy ); 156 curl_easy_setopt( e, CURLOPT_PROXYAUTH, CURLAUTH_ANY ); 157 curl_easy_setopt( e, CURLOPT_PROXYPORT, session->proxyPort ); 158 curl_easy_setopt( e, CURLOPT_PROXYTYPE, 138 159 getCurlProxyType( session->proxyType ) ); 139 160 } … … 141 162 char * str = tr_strdup_printf( "%s:%s", session->proxyUsername, 142 163 session->proxyPassword ); 143 curl_easy_setopt( e asy, CURLOPT_PROXYUSERPWD, str );164 curl_easy_setopt( e, CURLOPT_PROXYUSERPWD, str ); 144 165 tr_free( str ); 145 166 } 146 167 147 curl_easy_setopt( e asy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );148 curl_easy_setopt( e asy, CURLOPT_TIMEOUT, timeout );149 curl_easy_setopt( e asy, CURLOPT_CONNECTTIMEOUT, timeout-5 );150 curl_easy_setopt( e asy, CURLOPT_SOCKOPTFUNCTION, sockoptfunction );151 curl_easy_setopt( e asy, CURLOPT_SOCKOPTDATA, task );152 curl_easy_setopt( e asy, CURLOPT_WRITEDATA, task );153 curl_easy_setopt( e asy, CURLOPT_WRITEFUNCTION, writeFunc );154 curl_easy_setopt( e asy, CURLOPT_DNS_CACHE_TIMEOUT, 1800L );155 curl_easy_setopt( e asy, CURLOPT_FOLLOWLOCATION, 1L );156 curl_easy_setopt( e asy, CURLOPT_AUTOREFERER, 1L );157 curl_easy_setopt( e asy, CURLOPT_FORBID_REUSE, 1L );158 curl_easy_setopt( e asy, CURLOPT_MAXREDIRS, -1L );159 curl_easy_setopt( e asy, CURLOPT_NOSIGNAL, 1L );160 curl_easy_setopt( e asy, CURLOPT_PRIVATE, task );161 curl_easy_setopt( e asy, CURLOPT_SSL_VERIFYHOST, 0L );162 curl_easy_setopt( e asy, CURLOPT_SSL_VERIFYPEER, 0L );163 curl_easy_setopt( e asy, CURLOPT_URL, task->url );164 curl_easy_setopt( e asy, CURLOPT_USERAGENT, user_agent );165 curl_easy_setopt( e asy, CURLOPT_VERBOSE, verbose );168 curl_easy_setopt( e, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); 169 curl_easy_setopt( e, CURLOPT_TIMEOUT, timeout ); 170 curl_easy_setopt( e, CURLOPT_CONNECTTIMEOUT, timeout-5 ); 171 curl_easy_setopt( e, CURLOPT_SOCKOPTFUNCTION, sockoptfunction ); 172 curl_easy_setopt( e, CURLOPT_SOCKOPTDATA, task ); 173 curl_easy_setopt( e, CURLOPT_WRITEDATA, task ); 174 curl_easy_setopt( e, CURLOPT_WRITEFUNCTION, writeFunc ); 175 curl_easy_setopt( e, CURLOPT_DNS_CACHE_TIMEOUT, 1800L ); 176 curl_easy_setopt( e, CURLOPT_FOLLOWLOCATION, 1L ); 177 curl_easy_setopt( e, CURLOPT_AUTOREFERER, 1L ); 178 curl_easy_setopt( e, CURLOPT_FORBID_REUSE, 1L ); 179 curl_easy_setopt( e, CURLOPT_MAXREDIRS, -1L ); 180 curl_easy_setopt( e, CURLOPT_NOSIGNAL, 1L ); 181 curl_easy_setopt( e, CURLOPT_PRIVATE, task ); 182 curl_easy_setopt( e, CURLOPT_SSL_VERIFYHOST, 0L ); 183 curl_easy_setopt( e, CURLOPT_SSL_VERIFYPEER, 0L ); 184 curl_easy_setopt( e, CURLOPT_URL, task->url ); 185 curl_easy_setopt( e, CURLOPT_USERAGENT, user_agent ); 186 curl_easy_setopt( e, CURLOPT_VERBOSE, verbose ); 166 187 if( web->haveAddr ) 167 curl_easy_setopt( easy, CURLOPT_INTERFACE, 168 tr_ntop_non_ts( &web->addr ) ); 188 curl_easy_setopt( e, CURLOPT_INTERFACE, tr_ntop_non_ts( &web->addr ) ); 169 189 if( task->range ) 170 curl_easy_setopt( e asy, CURLOPT_RANGE, task->range );190 curl_easy_setopt( e, CURLOPT_RANGE, task->range ); 171 191 else /* don't set encoding on webseeds; it messes up binary data */ 172 curl_easy_setopt( e asy, CURLOPT_ENCODING, "" );173 174 mcode = curl_multi_add_handle( web->multi, e asy);192 curl_easy_setopt( e, CURLOPT_ENCODING, "" ); 193 194 mcode = curl_multi_add_handle( web->multi, e ); 175 195 ++web->taskCount; 176 196 /*tr_multi_perform( web, CURL_SOCKET_TIMEOUT );*/ … … 181 201 **** 182 202 ***/ 183 184 static void185 task_free( struct tr_web_task * task )186 {187 evbuffer_free( task->response );188 tr_free( task->range );189 tr_free( task->url );190 tr_free( task );191 }192 203 193 204 static void … … 215 226 long code; 216 227 struct tr_web_task * task; 217 CURL * e asy= msg->easy_handle;218 curl_easy_getinfo( e asy, CURLINFO_PRIVATE, (void*)&task );219 curl_easy_getinfo( e asy, CURLINFO_RESPONSE_CODE, &code );220 curl_multi_remove_handle( g->multi, e asy);221 curl_easy_cleanup( e asy);228 CURL * e = msg->easy_handle; 229 curl_easy_getinfo( e, CURLINFO_PRIVATE, (void*)&task ); 230 curl_easy_getinfo( e, CURLINFO_RESPONSE_CODE, &code ); 231 curl_multi_remove_handle( g->multi, e ); 232 curl_easy_cleanup( e ); 222 233 task_finish( task, code ); 223 234 } … … 233 244 } 234 245 235 static void236 web_close( tr_web * g )237 {238 curl_multi_cleanup( g->multi );239 evtimer_del( &g->timer_event );240 tr_free( g );241 }242 243 /* note: this function can free the tr_web if its 'closing' flag is set244 and no tasks remain. callers must not reference their g pointer245 after calling this function */246 246 static tr_bool 247 247 tr_multi_perform( tr_web * g, int fd ) 248 248 { 249 CURLMcode mcode; 249 250 tr_bool closed = FALSE; 250 CURLMcode mcode;251 251 252 252 dbgmsg( "check_run_count: %d taskCount", g->taskCount ); … … 259 259 remove_finished_tasks( g ); 260 260 261 if( g->closing && !g->taskCount ) {261 if(( closed = g->closing && !g->taskCount )) 262 262 web_close( g ); 263 closed = TRUE; 264 } 265 266 if( !closed ) 263 else 267 264 restart_timer( g ); 268 265 … … 279 276 /* CURLMOPT_SOCKETFUNCTION */ 280 277 static int 281 sock_cb( CURL * easy UNUSED, curl_socket_t fd, int action, void * vweb, void * vevent ) 278 sock_cb( CURL * easy UNUSED, curl_socket_t fd, int action, 279 void * vweb, void * vevent ) 282 280 { 283 281 /*static int num_events = 0;*/ 284 282 struct tr_web * web = vweb; 285 283 struct event * io_event = vevent; 286 dbgmsg( "sock_cb: action is %d, fd is %d, io_event is %p", action, (int)fd, io_event ); 284 dbgmsg( "sock_cb: action is %d, fd is %d, io_event is %p", 285 action, (int)fd, io_event ); 287 286 288 287 if( action == CURL_POLL_REMOVE ) … … 296 295 } 297 296 } 298 else 297 else if( action & ( EV_READ | EV_WRITE ) ) 299 298 { 300 299 const short events = EV_PERSIST … … 360 359 void * done_func_user_data ) 361 360 { 362 if( session->web )361 if( session->web != NULL ) 363 362 { 364 363 static unsigned long tag = 0; … … 387 386 { 388 387 tr_web * web; 389 static int curlInited = FALSE; 390 391 /* call curl_global_init if we haven't done it already. 392 * try to enable ssl for https support; but if that fails, 388 389 /* try to enable ssl for https support; but if that fails, 393 390 * try a plain vanilla init */ 394 if( curlInited == FALSE ) { 395 curlInited = TRUE; 396 if( curl_global_init( CURL_GLOBAL_SSL ) ) 397 curl_global_init( 0 ); 398 } 391 if( curl_global_init( CURL_GLOBAL_SSL ) ) 392 curl_global_init( 0 ); 399 393 400 394 web = tr_new0( struct tr_web, 1 ); … … 428 422 *****/ 429 423 430 static const struct http_msg {431 long code;432 const char * text;433 } http_msg[] = {434 { 0, "No Response" },435 { 101, "Switching Protocols" },436 { 200, "OK" },437 { 201, "Created" },438 { 202, "Accepted" },439 { 203, "Non-Authoritative Information" },440 { 204, "No Content" },441 { 205, "Reset Content" },442 { 206, "Partial Content" },443 { 300, "Multiple Choices" },444 { 301, "Moved Permanently" },445 { 302, "Found" },446 { 303, "See Other" },447 { 304, "Not Modified" },448 { 305, "Use Proxy" },449 { 306, "(Unused)" },450 { 307, "Temporary Redirect" },451 { 400, "Bad Request" },452 { 401, "Unauthorized" },453 { 402, "Payment Required" },454 { 403, "Forbidden" },455 { 404, "Not Found" },456 { 405, "Method Not Allowed" },457 { 406, "Not Acceptable" },458 { 407, "Proxy Authentication Required" },459 { 408, "Request Timeout" },460 { 409, "Conflict" },461 { 410, "Gone" },462 { 411, "Length Required" },463 { 412, "Precondition Failed" },464 { 413, "Request Entity Too Large" },465 { 414, "Request-URI Too Long" },466 { 415, "Unsupported Media Type" },467 { 416, "Requested Range Not Satisfiable" },468 { 417, "Expectation Failed" },469 { 500, "Internal Server Error" },470 { 501, "Not Implemented" },471 { 502, "Bad Gateway" },472 { 503, "Service Unavailable" },473 { 504, "Gateway Timeout" },474 { 505, "HTTP Version Not Supported" }475 };476 477 424 const char * 478 425 tr_webGetResponseStr( long code ) 479 426 { 480 int i; 481 static const int n = sizeof( http_msg ) / sizeof( http_msg[0] ); 482 for( i=0; i<n; ++i ) 483 if( http_msg[i].code == code ) 484 return http_msg[i].text; 485 return "Unknown Error"; 427 switch( code ) 428 { 429 case 0: return "No Response"; 430 case 101: return "Switching Protocols"; 431 case 200: return "OK"; 432 case 201: return "Created"; 433 case 202: return "Accepted"; 434 case 203: return "Non-Authoritative Information"; 435 case 204: return "No Content"; 436 case 205: return "Reset Content"; 437 case 206: return "Partial Content"; 438 case 300: return "Multiple Choices"; 439 case 301: return "Moved Permanently"; 440 case 302: return "Found"; 441 case 303: return "See Other"; 442 case 304: return "Not Modified"; 443 case 305: return "Use Proxy"; 444 case 306: return "(Unused)"; 445 case 307: return "Temporary Redirect"; 446 case 400: return "Bad Request"; 447 case 401: return "Unauthorized"; 448 case 402: return "Payment Required"; 449 case 403: return "Forbidden"; 450 case 404: return "Not Found"; 451 case 405: return "Method Not Allowed"; 452 case 406: return "Not Acceptable"; 453 case 407: return "Proxy Authentication Required"; 454 case 408: return "Request Timeout"; 455 case 409: return "Conflict"; 456 case 410: return "Gone"; 457 case 411: return "Length Required"; 458 case 412: return "Precondition Failed"; 459 case 413: return "Request Entity Too Large"; 460 case 414: return "Request-URI Too Long"; 461 case 415: return "Unsupported Media Type"; 462 case 416: return "Requested Range Not Satisfiable"; 463 case 417: return "Expectation Failed"; 464 case 500: return "Internal Server Error"; 465 case 501: return "Not Implemented"; 466 case 502: return "Bad Gateway"; 467 case 503: return "Service Unavailable"; 468 case 504: return "Gateway Timeout"; 469 case 505: return "HTTP Version Not Supported"; 470 default: return "Unknown Error"; 471 } 486 472 } 487 473
Note: See TracChangeset
for help on using the changeset viewer.