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