1 | /* |
---|
2 | * This file Copyright (C) 2008-2009 Charles Kerr <charles@transmissionbt.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: web.c 9065 2009-09-08 05:18:46Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <stdlib.h> /* bsearch */ |
---|
15 | |
---|
16 | #include <event.h> |
---|
17 | |
---|
18 | #define CURL_DISABLE_TYPECHECK /* otherwise -Wunreachable-code goes insane */ |
---|
19 | #include <curl/curl.h> |
---|
20 | |
---|
21 | #include "transmission.h" |
---|
22 | #include "list.h" |
---|
23 | #include "net.h" /* socklen_t */ |
---|
24 | #include "session.h" |
---|
25 | #include "trevent.h" |
---|
26 | #include "utils.h" |
---|
27 | #include "version.h" |
---|
28 | #include "web.h" |
---|
29 | |
---|
30 | static tr_bool |
---|
31 | useCurlMultiSocketAction( void ) |
---|
32 | { |
---|
33 | static tr_bool tested = FALSE; |
---|
34 | static tr_bool useMultiSocketAction; |
---|
35 | |
---|
36 | if( !tested ) |
---|
37 | { |
---|
38 | curl_version_info_data * data = curl_version_info( CURLVERSION_NOW ); |
---|
39 | tr_inf( "Using libcurl %s", data->version ); |
---|
40 | /* Use curl_multi_socket_action() instead of curl_multi_perform() |
---|
41 | * if libcurl >= 7.18.2. See http://trac.transmissionbt.com/ticket/1844 */ |
---|
42 | useMultiSocketAction = data->version_num >= 0x071202; |
---|
43 | tested = TRUE; |
---|
44 | } |
---|
45 | |
---|
46 | return useMultiSocketAction; |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | enum |
---|
51 | { |
---|
52 | /* arbitrary number */ |
---|
53 | MAX_CONCURRENT_TASKS = 100, |
---|
54 | |
---|
55 | /* arbitrary number */ |
---|
56 | DEFAULT_TIMER_MSEC = 2500 |
---|
57 | }; |
---|
58 | |
---|
59 | #if 0 |
---|
60 | #define dbgmsg(...) \ |
---|
61 | do { \ |
---|
62 | fprintf( stderr, __VA_ARGS__ ); \ |
---|
63 | fprintf( stderr, "\n" ); \ |
---|
64 | } while( 0 ) |
---|
65 | #else |
---|
66 | #define dbgmsg( ... ) \ |
---|
67 | do { \ |
---|
68 | if( tr_deepLoggingIsActive( ) ) \ |
---|
69 | tr_deepLog( __FILE__, __LINE__, "web", __VA_ARGS__ ); \ |
---|
70 | } while( 0 ) |
---|
71 | #endif |
---|
72 | |
---|
73 | struct tr_web_sockinfo |
---|
74 | { |
---|
75 | int fd; |
---|
76 | tr_bool evset; |
---|
77 | struct event ev; |
---|
78 | }; |
---|
79 | |
---|
80 | struct tr_web |
---|
81 | { |
---|
82 | tr_bool closing; |
---|
83 | int prev_running; |
---|
84 | int still_running; |
---|
85 | long timer_ms; |
---|
86 | CURLM * multi; |
---|
87 | tr_session * session; |
---|
88 | struct event timer_event; |
---|
89 | tr_list * easy_queue; |
---|
90 | tr_list * fds; |
---|
91 | }; |
---|
92 | |
---|
93 | /*** |
---|
94 | **** |
---|
95 | ***/ |
---|
96 | |
---|
97 | static struct tr_web_sockinfo * |
---|
98 | getSockinfo( tr_web * web, int fd, tr_bool createIfMissing ) |
---|
99 | { |
---|
100 | tr_list * l; |
---|
101 | |
---|
102 | for( l=web->fds; l!=NULL; l=l->next ) { |
---|
103 | struct tr_web_sockinfo * s = l->data; |
---|
104 | if( s->fd == fd ) { |
---|
105 | dbgmsg( "looked up sockinfo %p for fd %d", s, fd ); |
---|
106 | return s; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | if( createIfMissing ) { |
---|
111 | struct tr_web_sockinfo * s = tr_new0( struct tr_web_sockinfo, 1 ); |
---|
112 | s->fd = fd; |
---|
113 | tr_list_prepend( &web->fds, s ); |
---|
114 | dbgmsg( "created sockinfo %p for fd %d... we now have %d sockinfos", s, fd, tr_list_size(web->fds) ); |
---|
115 | return s; |
---|
116 | } |
---|
117 | |
---|
118 | return NULL; |
---|
119 | } |
---|
120 | |
---|
121 | static void |
---|
122 | clearSockinfoEvent( struct tr_web_sockinfo * s ) |
---|
123 | { |
---|
124 | if( s && s->evset ) |
---|
125 | { |
---|
126 | dbgmsg( "clearing libevent polling for sockinfo %p, fd %d", s, s->fd ); |
---|
127 | event_del( &s->ev ); |
---|
128 | s->evset = FALSE; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | static void |
---|
133 | purgeSockinfo( tr_web * web, int fd ) |
---|
134 | { |
---|
135 | struct tr_web_sockinfo * s = getSockinfo( web, fd, FALSE ); |
---|
136 | |
---|
137 | if( s != NULL ) |
---|
138 | { |
---|
139 | tr_list_remove_data( &web->fds, s ); |
---|
140 | clearSockinfoEvent( s ); |
---|
141 | dbgmsg( "freeing sockinfo %p, fd %d", s, s->fd ); |
---|
142 | tr_free( s ); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | /*** |
---|
147 | **** |
---|
148 | ***/ |
---|
149 | |
---|
150 | struct tr_web_task |
---|
151 | { |
---|
152 | unsigned long tag; |
---|
153 | struct evbuffer * response; |
---|
154 | char * url; |
---|
155 | char * range; |
---|
156 | tr_session * session; |
---|
157 | tr_web_done_func * done_func; |
---|
158 | void * done_func_user_data; |
---|
159 | }; |
---|
160 | |
---|
161 | static size_t |
---|
162 | writeFunc( void * ptr, size_t size, size_t nmemb, void * task ) |
---|
163 | { |
---|
164 | const size_t byteCount = size * nmemb; |
---|
165 | evbuffer_add( ((struct tr_web_task*)task)->response, ptr, byteCount ); |
---|
166 | dbgmsg( "wrote %zu bytes to task %p's buffer", byteCount, task ); |
---|
167 | return byteCount; |
---|
168 | } |
---|
169 | |
---|
170 | static int |
---|
171 | getCurlProxyType( tr_proxy_type t ) |
---|
172 | { |
---|
173 | switch( t ) |
---|
174 | { |
---|
175 | case TR_PROXY_SOCKS4: return CURLPROXY_SOCKS4; |
---|
176 | case TR_PROXY_SOCKS5: return CURLPROXY_SOCKS5; |
---|
177 | default: return CURLPROXY_HTTP; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | static void |
---|
182 | addTask( void * vtask ) |
---|
183 | { |
---|
184 | struct tr_web_task * task = vtask; |
---|
185 | const tr_session * session = task->session; |
---|
186 | |
---|
187 | if( session && session->web ) |
---|
188 | { |
---|
189 | struct tr_web * web = session->web; |
---|
190 | CURL * easy; |
---|
191 | |
---|
192 | dbgmsg( "adding task #%lu [%s]", task->tag, task->url ); |
---|
193 | |
---|
194 | easy = curl_easy_init( ); |
---|
195 | |
---|
196 | if( !task->range && session->isProxyEnabled ) { |
---|
197 | curl_easy_setopt( easy, CURLOPT_PROXY, session->proxy ); |
---|
198 | curl_easy_setopt( easy, CURLOPT_PROXYAUTH, CURLAUTH_ANY ); |
---|
199 | curl_easy_setopt( easy, CURLOPT_PROXYPORT, session->proxyPort ); |
---|
200 | curl_easy_setopt( easy, CURLOPT_PROXYTYPE, |
---|
201 | getCurlProxyType( session->proxyType ) ); |
---|
202 | } |
---|
203 | if( !task->range && session->isProxyAuthEnabled ) { |
---|
204 | char * str = tr_strdup_printf( "%s:%s", session->proxyUsername, |
---|
205 | session->proxyPassword ); |
---|
206 | curl_easy_setopt( easy, CURLOPT_PROXYUSERPWD, str ); |
---|
207 | tr_free( str ); |
---|
208 | } |
---|
209 | |
---|
210 | curl_easy_setopt( easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); |
---|
211 | curl_easy_setopt( easy, CURLOPT_DNS_CACHE_TIMEOUT, 360L ); |
---|
212 | curl_easy_setopt( easy, CURLOPT_CONNECTTIMEOUT, 60L ); |
---|
213 | |
---|
214 | /* set a time limit for announces & scrapes */ |
---|
215 | if( strstr( task->url, "announce" ) ) |
---|
216 | curl_easy_setopt( easy, CURLOPT_TIMEOUT, 120L ); |
---|
217 | else if( strstr( task->url, "scrape" ) ) |
---|
218 | curl_easy_setopt( easy, CURLOPT_TIMEOUT, 30L ); |
---|
219 | |
---|
220 | curl_easy_setopt( easy, CURLOPT_FOLLOWLOCATION, 1L ); |
---|
221 | curl_easy_setopt( easy, CURLOPT_FORBID_REUSE, 1L ); |
---|
222 | curl_easy_setopt( easy, CURLOPT_MAXREDIRS, 16L ); |
---|
223 | curl_easy_setopt( easy, CURLOPT_NOSIGNAL, 1L ); |
---|
224 | curl_easy_setopt( easy, CURLOPT_PRIVATE, task ); |
---|
225 | curl_easy_setopt( easy, CURLOPT_SSL_VERIFYHOST, 0L ); |
---|
226 | curl_easy_setopt( easy, CURLOPT_SSL_VERIFYPEER, 0L ); |
---|
227 | curl_easy_setopt( easy, CURLOPT_URL, task->url ); |
---|
228 | curl_easy_setopt( easy, CURLOPT_USERAGENT, |
---|
229 | TR_NAME "/" LONG_VERSION_STRING ); |
---|
230 | curl_easy_setopt( easy, CURLOPT_VERBOSE, |
---|
231 | getenv( "TR_CURL_VERBOSE" ) != NULL ); |
---|
232 | curl_easy_setopt( easy, CURLOPT_WRITEDATA, task ); |
---|
233 | curl_easy_setopt( easy, CURLOPT_WRITEFUNCTION, writeFunc ); |
---|
234 | if( task->range ) |
---|
235 | curl_easy_setopt( easy, CURLOPT_RANGE, task->range ); |
---|
236 | else /* don't set encoding on webseeds; it messes up binary data */ |
---|
237 | curl_easy_setopt( easy, CURLOPT_ENCODING, "" ); |
---|
238 | |
---|
239 | if( web->still_running >= MAX_CONCURRENT_TASKS ) { |
---|
240 | tr_list_append( &web->easy_queue, easy ); |
---|
241 | dbgmsg( ">> enqueueing a task... size is now %d", tr_list_size( web->easy_queue ) ); |
---|
242 | } else { |
---|
243 | const CURLMcode mcode = curl_multi_add_handle( web->multi, easy ); |
---|
244 | tr_assert( mcode == CURLM_OK, "curl_multi_add_handle() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) ); |
---|
245 | if( mcode == CURLM_OK ) |
---|
246 | ++web->still_running; |
---|
247 | else |
---|
248 | tr_err( "%s", curl_multi_strerror( mcode ) ); |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | /*** |
---|
254 | **** |
---|
255 | ***/ |
---|
256 | |
---|
257 | static void |
---|
258 | task_free( struct tr_web_task * task ) |
---|
259 | { |
---|
260 | evbuffer_free( task->response ); |
---|
261 | tr_free( task->range ); |
---|
262 | tr_free( task->url ); |
---|
263 | tr_free( task ); |
---|
264 | } |
---|
265 | |
---|
266 | static void |
---|
267 | task_finish( struct tr_web_task * task, long response_code ) |
---|
268 | { |
---|
269 | dbgmsg( "finished a web task... response code is %ld", response_code ); |
---|
270 | dbgmsg( "===================================================" ); |
---|
271 | task->done_func( task->session, |
---|
272 | response_code, |
---|
273 | EVBUFFER_DATA( task->response ), |
---|
274 | EVBUFFER_LENGTH( task->response ), |
---|
275 | task->done_func_user_data ); |
---|
276 | task_free( task ); |
---|
277 | } |
---|
278 | |
---|
279 | static void |
---|
280 | remove_finished_tasks( tr_web * g ) |
---|
281 | { |
---|
282 | CURL * easy; |
---|
283 | |
---|
284 | do |
---|
285 | { |
---|
286 | CURLMsg * msg; |
---|
287 | int msgs_left; |
---|
288 | |
---|
289 | easy = NULL; |
---|
290 | while(( msg = curl_multi_info_read( g->multi, &msgs_left ))) { |
---|
291 | if( msg->msg == CURLMSG_DONE ) { |
---|
292 | easy = msg->easy_handle; |
---|
293 | break; |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | if( easy ) { |
---|
298 | long code; |
---|
299 | long fd; |
---|
300 | struct tr_web_task * task; |
---|
301 | CURLcode ecode; |
---|
302 | CURLMcode mcode; |
---|
303 | |
---|
304 | ecode = curl_easy_getinfo( easy, CURLINFO_PRIVATE, (void*)&task ); |
---|
305 | tr_assert( ecode == CURLE_OK, "curl_easy_getinfo() failed: %d (%s)", ecode, curl_easy_strerror( ecode ) ); |
---|
306 | |
---|
307 | ecode = curl_easy_getinfo( easy, CURLINFO_RESPONSE_CODE, &code ); |
---|
308 | tr_assert( ecode == CURLE_OK, "curl_easy_getinfo() failed: %d (%s)", ecode, curl_easy_strerror( ecode ) ); |
---|
309 | |
---|
310 | ecode = curl_easy_getinfo( easy, CURLINFO_LASTSOCKET, &fd ); |
---|
311 | tr_assert( ecode == CURLE_OK, "curl_easy_getinfo() failed: %d (%s)", ecode, curl_easy_strerror( ecode ) ); |
---|
312 | if( fd != -1L ) |
---|
313 | purgeSockinfo( g, fd ); |
---|
314 | |
---|
315 | mcode = curl_multi_remove_handle( g->multi, easy ); |
---|
316 | tr_assert( mcode == CURLM_OK, "curl_multi_remove_handle() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) ); |
---|
317 | |
---|
318 | curl_easy_cleanup( easy ); |
---|
319 | task_finish( task, code ); |
---|
320 | } |
---|
321 | } |
---|
322 | while ( easy ); |
---|
323 | |
---|
324 | g->prev_running = g->still_running; |
---|
325 | } |
---|
326 | |
---|
327 | static void |
---|
328 | stop_timer( tr_web* g ) |
---|
329 | { |
---|
330 | if( evtimer_pending( &g->timer_event, NULL ) ) |
---|
331 | { |
---|
332 | dbgmsg( "deleting the pending global timer" ); |
---|
333 | evtimer_del( &g->timer_event ); |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | static void |
---|
338 | restart_timer( tr_web * g ) |
---|
339 | { |
---|
340 | struct timeval interval; |
---|
341 | |
---|
342 | assert( tr_amInEventThread( g->session ) ); |
---|
343 | assert( g->session != NULL ); |
---|
344 | assert( g->session->events != NULL ); |
---|
345 | |
---|
346 | stop_timer( g ); |
---|
347 | dbgmsg( "adding a timeout for %ld seconds from now", g->timer_ms/1000L ); |
---|
348 | tr_timevalMsec( g->timer_ms, &interval ); |
---|
349 | evtimer_add( &g->timer_event, &interval ); |
---|
350 | } |
---|
351 | |
---|
352 | static void |
---|
353 | add_tasks_from_queue( tr_web * g ) |
---|
354 | { |
---|
355 | while( ( g->still_running < MAX_CONCURRENT_TASKS ) |
---|
356 | && ( tr_list_size( g->easy_queue ) > 0 ) ) |
---|
357 | { |
---|
358 | CURL * easy = tr_list_pop_front( &g->easy_queue ); |
---|
359 | if( easy ) |
---|
360 | { |
---|
361 | const CURLMcode rc = curl_multi_add_handle( g->multi, easy ); |
---|
362 | if( rc != CURLM_OK ) |
---|
363 | tr_err( "%s", curl_multi_strerror( rc ) ); |
---|
364 | else { |
---|
365 | dbgmsg( "pumped the task queue, %d remain", |
---|
366 | tr_list_size( g->easy_queue ) ); |
---|
367 | ++g->still_running; |
---|
368 | } |
---|
369 | } |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | static void |
---|
374 | web_close( tr_web * g ) |
---|
375 | { |
---|
376 | CURLMcode mcode; |
---|
377 | |
---|
378 | stop_timer( g ); |
---|
379 | |
---|
380 | mcode = curl_multi_cleanup( g->multi ); |
---|
381 | tr_assert( mcode == CURLM_OK, "curl_multi_cleanup() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) ); |
---|
382 | if( mcode != CURLM_OK ) |
---|
383 | tr_err( "%s", curl_multi_strerror( mcode ) ); |
---|
384 | |
---|
385 | tr_free( g ); |
---|
386 | } |
---|
387 | |
---|
388 | /* note: this function can free the tr_web if its 'closing' flag is set |
---|
389 | and no tasks remain. callers must not reference their g pointer |
---|
390 | after calling this function */ |
---|
391 | static void |
---|
392 | tr_multi_perform( tr_web * g, int fd ) |
---|
393 | { |
---|
394 | int closed = FALSE; |
---|
395 | CURLMcode mcode; |
---|
396 | |
---|
397 | dbgmsg( "check_run_count: prev_running %d, still_running %d", |
---|
398 | g->prev_running, g->still_running ); |
---|
399 | |
---|
400 | /* invoke libcurl's processing */ |
---|
401 | if( useCurlMultiSocketAction( ) ) |
---|
402 | { |
---|
403 | do { |
---|
404 | dbgmsg( "calling curl_multi_socket_action..." ); |
---|
405 | mcode = curl_multi_socket_action( g->multi, fd, 0, &g->still_running ); |
---|
406 | fd = CURL_SOCKET_TIMEOUT; |
---|
407 | dbgmsg( "done calling curl_multi_socket_action..." ); |
---|
408 | } while( mcode == CURLM_CALL_MULTI_SOCKET ); |
---|
409 | } |
---|
410 | else |
---|
411 | { |
---|
412 | do { |
---|
413 | dbgmsg( "calling curl_multi_perform..." ); |
---|
414 | mcode = curl_multi_perform( g->multi, &g->still_running ); |
---|
415 | dbgmsg( "done calling curl_multi_perform..." ); |
---|
416 | } while( mcode == CURLM_CALL_MULTI_PERFORM ); |
---|
417 | } |
---|
418 | tr_assert( mcode == CURLM_OK, "curl_multi_perform() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) ); |
---|
419 | if( mcode != CURLM_OK ) |
---|
420 | tr_err( "%s", curl_multi_strerror( mcode ) ); |
---|
421 | |
---|
422 | remove_finished_tasks( g ); |
---|
423 | |
---|
424 | add_tasks_from_queue( g ); |
---|
425 | |
---|
426 | if( !g->still_running ) { |
---|
427 | assert( tr_list_size( g->fds ) == 0 ); |
---|
428 | stop_timer( g ); |
---|
429 | if( g->closing ) { |
---|
430 | web_close( g ); |
---|
431 | closed = TRUE; |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | if( !closed ) |
---|
436 | restart_timer( g ); |
---|
437 | } |
---|
438 | |
---|
439 | /* libevent says that sock is ready to be processed, so wake up libcurl */ |
---|
440 | static void |
---|
441 | event_cb( int fd, short kind UNUSED, void * g ) |
---|
442 | { |
---|
443 | tr_multi_perform( g, fd ); |
---|
444 | } |
---|
445 | |
---|
446 | /* libevent says that timer_ms have passed, so wake up libcurl */ |
---|
447 | static void |
---|
448 | timer_cb( int socket UNUSED, short action UNUSED, void * g ) |
---|
449 | { |
---|
450 | dbgmsg( "libevent timer is done" ); |
---|
451 | tr_multi_perform( g, CURL_SOCKET_TIMEOUT ); |
---|
452 | } |
---|
453 | |
---|
454 | /* CURLMOPT_SOCKETFUNCTION */ |
---|
455 | static int |
---|
456 | sock_cb( CURL * e UNUSED, |
---|
457 | curl_socket_t fd, |
---|
458 | int action, |
---|
459 | void * vweb, |
---|
460 | void * unused UNUSED) |
---|
461 | { |
---|
462 | struct tr_web * web = vweb; |
---|
463 | dbgmsg( "sock_cb: action is %d, fd is %d", action, (int)fd ); |
---|
464 | |
---|
465 | if( action == CURL_POLL_REMOVE ) |
---|
466 | { |
---|
467 | purgeSockinfo( web, fd ); |
---|
468 | } |
---|
469 | else |
---|
470 | { |
---|
471 | struct tr_web_sockinfo * sockinfo = getSockinfo( web, fd, TRUE ); |
---|
472 | const int kind = EV_PERSIST |
---|
473 | | (( action & CURL_POLL_IN ) ? EV_READ : 0 ) |
---|
474 | | (( action & CURL_POLL_OUT ) ? EV_WRITE : 0 ); |
---|
475 | dbgmsg( "setsock: fd is %d, curl action is %d, libevent action is %d", fd, action, kind ); |
---|
476 | assert( tr_amInEventThread( web->session ) ); |
---|
477 | assert( kind != EV_PERSIST ); |
---|
478 | |
---|
479 | /* clear any old polling on this fd */ |
---|
480 | clearSockinfoEvent( sockinfo ); |
---|
481 | |
---|
482 | /* set the new polling on this fd */ |
---|
483 | dbgmsg( "enabling (libevent %d, libcurl %d) polling on sockinfo %p, fd %d", action, kind, sockinfo, fd ); |
---|
484 | event_set( &sockinfo->ev, fd, kind, event_cb, web ); |
---|
485 | event_add( &sockinfo->ev, NULL ); |
---|
486 | sockinfo->evset = TRUE; |
---|
487 | } |
---|
488 | |
---|
489 | return 0; |
---|
490 | } |
---|
491 | |
---|
492 | |
---|
493 | /* libcurl documentation: "If 0, it means you should proceed immediately |
---|
494 | * without waiting for anything. If it returns -1, there's no timeout at all |
---|
495 | * set ... (but) you must not wait too long (more than a few seconds perhaps) |
---|
496 | * before you call curl_multi_perform() again." */ |
---|
497 | static void |
---|
498 | multi_timer_cb( CURLM *multi UNUSED, long timer_ms, void * vg ) |
---|
499 | { |
---|
500 | tr_web * g = vg; |
---|
501 | |
---|
502 | if( timer_ms < 1 ) { |
---|
503 | if( timer_ms == 0 ) /* call it immediately */ |
---|
504 | timer_cb( 0, 0, g ); |
---|
505 | timer_ms = DEFAULT_TIMER_MSEC; |
---|
506 | } |
---|
507 | |
---|
508 | g->timer_ms = timer_ms; |
---|
509 | restart_timer( g ); |
---|
510 | } |
---|
511 | |
---|
512 | /**** |
---|
513 | ***** |
---|
514 | ****/ |
---|
515 | |
---|
516 | void |
---|
517 | tr_webRun( tr_session * session, |
---|
518 | const char * url, |
---|
519 | const char * range, |
---|
520 | tr_web_done_func done_func, |
---|
521 | void * done_func_user_data ) |
---|
522 | { |
---|
523 | if( session->web ) |
---|
524 | { |
---|
525 | static unsigned long tag = 0; |
---|
526 | struct tr_web_task * task; |
---|
527 | |
---|
528 | task = tr_new0( struct tr_web_task, 1 ); |
---|
529 | task->session = session; |
---|
530 | task->url = tr_strdup( url ); |
---|
531 | task->range = tr_strdup( range ); |
---|
532 | task->done_func = done_func; |
---|
533 | task->done_func_user_data = done_func_user_data; |
---|
534 | task->tag = ++tag; |
---|
535 | task->response = evbuffer_new( ); |
---|
536 | |
---|
537 | tr_runInEventThread( session, addTask, task ); |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | tr_web* |
---|
542 | tr_webInit( tr_session * session ) |
---|
543 | { |
---|
544 | CURLMcode mcode; |
---|
545 | static int curlInited = FALSE; |
---|
546 | tr_web * web; |
---|
547 | |
---|
548 | /* call curl_global_init if we haven't done it already. |
---|
549 | * try to enable ssl for https support; but if that fails, |
---|
550 | * try a plain vanilla init */ |
---|
551 | if( curlInited == FALSE ) { |
---|
552 | curlInited = TRUE; |
---|
553 | if( curl_global_init( CURL_GLOBAL_SSL ) ) |
---|
554 | curl_global_init( 0 ); |
---|
555 | } |
---|
556 | |
---|
557 | web = tr_new0( struct tr_web, 1 ); |
---|
558 | web->multi = curl_multi_init( ); |
---|
559 | web->session = session; |
---|
560 | web->timer_ms = DEFAULT_TIMER_MSEC; /* overwritten by multi_timer_cb() */ |
---|
561 | |
---|
562 | evtimer_set( &web->timer_event, timer_cb, web ); |
---|
563 | mcode = curl_multi_setopt( web->multi, CURLMOPT_SOCKETDATA, web ); |
---|
564 | tr_assert( mcode == CURLM_OK, "curl_mutli_setopt() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) ); |
---|
565 | mcode = curl_multi_setopt( web->multi, CURLMOPT_SOCKETFUNCTION, sock_cb ); |
---|
566 | tr_assert( mcode == CURLM_OK, "curl_mutli_setopt() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) ); |
---|
567 | mcode = curl_multi_setopt( web->multi, CURLMOPT_TIMERDATA, web ); |
---|
568 | tr_assert( mcode == CURLM_OK, "curl_mutli_setopt() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) ); |
---|
569 | mcode = curl_multi_setopt( web->multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb ); |
---|
570 | tr_assert( mcode == CURLM_OK, "curl_mutli_setopt() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) ); |
---|
571 | |
---|
572 | return web; |
---|
573 | } |
---|
574 | |
---|
575 | void |
---|
576 | tr_webClose( tr_web ** web_in ) |
---|
577 | { |
---|
578 | tr_web * web = *web_in; |
---|
579 | *web_in = NULL; |
---|
580 | if( web->still_running < 1 ) |
---|
581 | web_close( web ); |
---|
582 | else |
---|
583 | web->closing = 1; |
---|
584 | } |
---|
585 | |
---|
586 | /***** |
---|
587 | ****** |
---|
588 | ****** |
---|
589 | *****/ |
---|
590 | |
---|
591 | static struct http_msg { |
---|
592 | long code; |
---|
593 | const char * text; |
---|
594 | } http_msg[] = { |
---|
595 | { 0, "No Response" }, |
---|
596 | { 101, "Switching Protocols" }, |
---|
597 | { 200, "OK" }, |
---|
598 | { 201, "Created" }, |
---|
599 | { 202, "Accepted" }, |
---|
600 | { 203, "Non-Authoritative Information" }, |
---|
601 | { 204, "No Content" }, |
---|
602 | { 205, "Reset Content" }, |
---|
603 | { 206, "Partial Content" }, |
---|
604 | { 300, "Multiple Choices" }, |
---|
605 | { 301, "Moved Permanently" }, |
---|
606 | { 302, "Found" }, |
---|
607 | { 303, "See Other" }, |
---|
608 | { 304, "Not Modified" }, |
---|
609 | { 305, "Use Proxy" }, |
---|
610 | { 306, "(Unused)" }, |
---|
611 | { 307, "Temporary Redirect" }, |
---|
612 | { 400, "Bad Request" }, |
---|
613 | { 401, "Unauthorized" }, |
---|
614 | { 402, "Payment Required" }, |
---|
615 | { 403, "Forbidden" }, |
---|
616 | { 404, "Not Found" }, |
---|
617 | { 405, "Method Not Allowed" }, |
---|
618 | { 406, "Not Acceptable" }, |
---|
619 | { 407, "Proxy Authentication Required" }, |
---|
620 | { 408, "Request Timeout" }, |
---|
621 | { 409, "Conflict" }, |
---|
622 | { 410, "Gone" }, |
---|
623 | { 411, "Length Required" }, |
---|
624 | { 412, "Precondition Failed" }, |
---|
625 | { 413, "Request Entity Too Large" }, |
---|
626 | { 414, "Request-URI Too Long" }, |
---|
627 | { 415, "Unsupported Media Type" }, |
---|
628 | { 416, "Requested Range Not Satisfiable" }, |
---|
629 | { 417, "Expectation Failed" }, |
---|
630 | { 500, "Internal Server Error" }, |
---|
631 | { 501, "Not Implemented" }, |
---|
632 | { 502, "Bad Gateway" }, |
---|
633 | { 503, "Service Unavailable" }, |
---|
634 | { 504, "Gateway Timeout" }, |
---|
635 | { 505, "HTTP Version Not Supported" } |
---|
636 | }; |
---|
637 | |
---|
638 | static int |
---|
639 | compareResponseCodes( const void * va, const void * vb ) |
---|
640 | { |
---|
641 | const long a = *(const long*) va; |
---|
642 | const struct http_msg * b = vb; |
---|
643 | return a - b->code; |
---|
644 | } |
---|
645 | |
---|
646 | const char * |
---|
647 | tr_webGetResponseStr( long code ) |
---|
648 | { |
---|
649 | struct http_msg * msg = bsearch( &code, |
---|
650 | http_msg, |
---|
651 | sizeof( http_msg ) / sizeof( http_msg[0] ), |
---|
652 | sizeof( http_msg[0] ), |
---|
653 | compareResponseCodes ); |
---|
654 | return msg ? msg->text : "Unknown Error"; |
---|
655 | } |
---|