1 | /* |
---|
2 | * This file Copyright (C) 2008-2010 Mnemosyne LLC |
---|
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 9910 2010-01-10 19:52:01Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | |
---|
15 | #include <curl/curl.h> |
---|
16 | #include <event.h> |
---|
17 | |
---|
18 | #include "transmission.h" |
---|
19 | #include "net.h" |
---|
20 | #include "session.h" |
---|
21 | #include "trevent.h" |
---|
22 | #include "utils.h" |
---|
23 | #include "version.h" |
---|
24 | #include "web.h" |
---|
25 | |
---|
26 | enum |
---|
27 | { |
---|
28 | TR_MEMORY_TRASH = 0xCC, |
---|
29 | |
---|
30 | DEFAULT_TIMER_MSEC = 1500 /* arbitrary */ |
---|
31 | }; |
---|
32 | |
---|
33 | #if 0 |
---|
34 | #define dbgmsg(...) \ |
---|
35 | do { \ |
---|
36 | fprintf( stderr, __VA_ARGS__ ); \ |
---|
37 | fprintf( stderr, "\n" ); \ |
---|
38 | } while( 0 ) |
---|
39 | #else |
---|
40 | #define dbgmsg( ... ) \ |
---|
41 | do { \ |
---|
42 | if( tr_deepLoggingIsActive( ) ) \ |
---|
43 | tr_deepLog( __FILE__, __LINE__, "web", __VA_ARGS__ ); \ |
---|
44 | } while( 0 ) |
---|
45 | #endif |
---|
46 | |
---|
47 | /*** |
---|
48 | **** |
---|
49 | ***/ |
---|
50 | |
---|
51 | struct tr_web |
---|
52 | { |
---|
53 | tr_bool closing; |
---|
54 | tr_bool haveAddr; |
---|
55 | int taskCount; |
---|
56 | long timer_msec; |
---|
57 | CURLM * multi; |
---|
58 | tr_session * session; |
---|
59 | tr_address addr; |
---|
60 | struct event timer_event; |
---|
61 | }; |
---|
62 | |
---|
63 | static void |
---|
64 | web_free( tr_web * g ) |
---|
65 | { |
---|
66 | curl_multi_cleanup( g->multi ); |
---|
67 | evtimer_del( &g->timer_event ); |
---|
68 | memset( g, TR_MEMORY_TRASH, sizeof( struct tr_web ) ); |
---|
69 | tr_free( g ); |
---|
70 | } |
---|
71 | |
---|
72 | /*** |
---|
73 | **** |
---|
74 | ***/ |
---|
75 | |
---|
76 | struct tr_web_task |
---|
77 | { |
---|
78 | unsigned long tag; |
---|
79 | struct evbuffer * response; |
---|
80 | char * url; |
---|
81 | char * range; |
---|
82 | tr_session * session; |
---|
83 | tr_web_done_func * done_func; |
---|
84 | void * done_func_user_data; |
---|
85 | struct event timer_event; |
---|
86 | CURL * easy; |
---|
87 | CURLM * multi; |
---|
88 | }; |
---|
89 | |
---|
90 | static void |
---|
91 | task_free( struct tr_web_task * task ) |
---|
92 | { |
---|
93 | evtimer_del( &task->timer_event ); |
---|
94 | evbuffer_free( task->response ); |
---|
95 | tr_free( task->range ); |
---|
96 | tr_free( task->url ); |
---|
97 | memset( task, TR_MEMORY_TRASH, sizeof( struct tr_web_task ) ); |
---|
98 | tr_free( task ); |
---|
99 | } |
---|
100 | |
---|
101 | /*** |
---|
102 | **** |
---|
103 | ***/ |
---|
104 | |
---|
105 | static size_t |
---|
106 | writeFunc( void * ptr, size_t size, size_t nmemb, void * vtask ) |
---|
107 | { |
---|
108 | const size_t byteCount = size * nmemb; |
---|
109 | struct tr_web_task * task = vtask; |
---|
110 | evbuffer_add( task->response, ptr, byteCount ); |
---|
111 | dbgmsg( "wrote %zu bytes to task %p's buffer", byteCount, task ); |
---|
112 | return byteCount; |
---|
113 | } |
---|
114 | |
---|
115 | static int |
---|
116 | sockoptfunction( void * vtask, curl_socket_t fd, curlsocktype purpose UNUSED ) |
---|
117 | { |
---|
118 | struct tr_web_task * task = vtask; |
---|
119 | const tr_bool isScrape = strstr( task->url, "scrape" ) != NULL; |
---|
120 | const tr_bool isAnnounce = strstr( task->url, "announce" ) != NULL; |
---|
121 | |
---|
122 | /* announce and scrape requests have tiny payloads. */ |
---|
123 | if( isScrape || isAnnounce ) |
---|
124 | { |
---|
125 | const int sndbuf = 1024; |
---|
126 | const int rcvbuf = isScrape ? 2048 : 3072; |
---|
127 | setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf) ); |
---|
128 | setsockopt( fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf) ); |
---|
129 | } |
---|
130 | |
---|
131 | /* return nonzero if this function encountered an error */ |
---|
132 | return 0; |
---|
133 | } |
---|
134 | |
---|
135 | #if 0 |
---|
136 | static int |
---|
137 | getCurlProxyType( tr_proxy_type t ) |
---|
138 | { |
---|
139 | if( t == TR_PROXY_SOCKS4 ) return CURLPROXY_SOCKS4; |
---|
140 | if( t == TR_PROXY_SOCKS5 ) return CURLPROXY_SOCKS5; |
---|
141 | return CURLPROXY_HTTP; |
---|
142 | } |
---|
143 | #endif |
---|
144 | |
---|
145 | static int |
---|
146 | getTimeoutFromURL( const char * url ) |
---|
147 | { |
---|
148 | if( strstr( url, "scrape" ) != NULL ) return 20; |
---|
149 | if( strstr( url, "announce" ) != NULL ) return 30; |
---|
150 | return 240; |
---|
151 | } |
---|
152 | |
---|
153 | static void task_timeout_cb( int fd UNUSED, short what UNUSED, void * task ); |
---|
154 | |
---|
155 | static void |
---|
156 | addTask( void * vtask ) |
---|
157 | { |
---|
158 | struct tr_web_task * task = vtask; |
---|
159 | const tr_session * session = task->session; |
---|
160 | |
---|
161 | if( session && session->web ) |
---|
162 | { |
---|
163 | CURL * e = curl_easy_init( ); |
---|
164 | struct tr_web * web = session->web; |
---|
165 | const int timeout = getTimeoutFromURL( task->url ); |
---|
166 | const long verbose = getenv( "TR_CURL_VERBOSE" ) != NULL; |
---|
167 | const char * user_agent = TR_NAME "/" LONG_VERSION_STRING; |
---|
168 | |
---|
169 | dbgmsg( "adding task #%lu [%s]", task->tag, task->url ); |
---|
170 | |
---|
171 | /* experimentally disable proxies to see if that has any effect on the libevent crashes */ |
---|
172 | #if 0 |
---|
173 | if( !task->range && session->isProxyEnabled ) { |
---|
174 | curl_easy_setopt( e, CURLOPT_PROXY, session->proxy ); |
---|
175 | curl_easy_setopt( e, CURLOPT_PROXYAUTH, CURLAUTH_ANY ); |
---|
176 | curl_easy_setopt( e, CURLOPT_PROXYPORT, session->proxyPort ); |
---|
177 | curl_easy_setopt( e, CURLOPT_PROXYTYPE, |
---|
178 | getCurlProxyType( session->proxyType ) ); |
---|
179 | } |
---|
180 | if( !task->range && session->isProxyAuthEnabled ) { |
---|
181 | char * str = tr_strdup_printf( "%s:%s", session->proxyUsername, |
---|
182 | session->proxyPassword ); |
---|
183 | curl_easy_setopt( e, CURLOPT_PROXYUSERPWD, str ); |
---|
184 | tr_free( str ); |
---|
185 | } |
---|
186 | #endif |
---|
187 | |
---|
188 | task->easy = e; |
---|
189 | task->multi = web->multi; |
---|
190 | |
---|
191 | /* use our own timeout instead of CURLOPT_TIMEOUT because the latter |
---|
192 | * doesn't play nicely with curl_multi. See curl bug #2501457 */ |
---|
193 | evtimer_set( &task->timer_event, task_timeout_cb, task ); |
---|
194 | tr_timerAdd( &task->timer_event, timeout, 0 ); |
---|
195 | |
---|
196 | curl_easy_setopt( e, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); |
---|
197 | curl_easy_setopt( e, CURLOPT_SOCKOPTFUNCTION, sockoptfunction ); |
---|
198 | curl_easy_setopt( e, CURLOPT_SOCKOPTDATA, task ); |
---|
199 | curl_easy_setopt( e, CURLOPT_WRITEDATA, task ); |
---|
200 | curl_easy_setopt( e, CURLOPT_WRITEFUNCTION, writeFunc ); |
---|
201 | curl_easy_setopt( e, CURLOPT_DNS_CACHE_TIMEOUT, 1800L ); |
---|
202 | curl_easy_setopt( e, CURLOPT_FOLLOWLOCATION, 1L ); |
---|
203 | curl_easy_setopt( e, CURLOPT_AUTOREFERER, 1L ); |
---|
204 | curl_easy_setopt( e, CURLOPT_FORBID_REUSE, 1L ); |
---|
205 | curl_easy_setopt( e, CURLOPT_MAXREDIRS, -1L ); |
---|
206 | curl_easy_setopt( e, CURLOPT_NOSIGNAL, 1L ); |
---|
207 | curl_easy_setopt( e, CURLOPT_PRIVATE, task ); |
---|
208 | curl_easy_setopt( e, CURLOPT_SSL_VERIFYHOST, 0L ); |
---|
209 | curl_easy_setopt( e, CURLOPT_SSL_VERIFYPEER, 0L ); |
---|
210 | curl_easy_setopt( e, CURLOPT_URL, task->url ); |
---|
211 | curl_easy_setopt( e, CURLOPT_USERAGENT, user_agent ); |
---|
212 | curl_easy_setopt( e, CURLOPT_VERBOSE, verbose ); |
---|
213 | if( web->haveAddr ) |
---|
214 | curl_easy_setopt( e, CURLOPT_INTERFACE, tr_ntop_non_ts( &web->addr ) ); |
---|
215 | if( task->range ) |
---|
216 | curl_easy_setopt( e, CURLOPT_RANGE, task->range ); |
---|
217 | |
---|
218 | if( curl_multi_add_handle( web->multi, e ) == CURLM_OK ) |
---|
219 | ++web->taskCount; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | /*** |
---|
224 | **** |
---|
225 | ***/ |
---|
226 | |
---|
227 | static void |
---|
228 | task_finish( struct tr_web_task * task, long response_code ) |
---|
229 | { |
---|
230 | dbgmsg( "finished web task %lu; got %ld", task->tag, response_code ); |
---|
231 | |
---|
232 | if( task->done_func != NULL ) |
---|
233 | task->done_func( task->session, |
---|
234 | response_code, |
---|
235 | EVBUFFER_DATA( task->response ), |
---|
236 | EVBUFFER_LENGTH( task->response ), |
---|
237 | task->done_func_user_data ); |
---|
238 | task_free( task ); |
---|
239 | } |
---|
240 | |
---|
241 | static void |
---|
242 | remove_task( struct tr_web_task * task ) |
---|
243 | { |
---|
244 | long code; |
---|
245 | |
---|
246 | curl_easy_getinfo( task->easy, CURLINFO_RESPONSE_CODE, &code ); |
---|
247 | curl_multi_remove_handle( task->multi, task->easy ); |
---|
248 | curl_easy_cleanup( task->easy ); |
---|
249 | task_finish( task, code ); |
---|
250 | } |
---|
251 | |
---|
252 | static void |
---|
253 | task_timeout_cb( int fd UNUSED, short what UNUSED, void * task ) |
---|
254 | { |
---|
255 | remove_task( task ); |
---|
256 | } |
---|
257 | |
---|
258 | static void |
---|
259 | remove_finished_tasks( tr_web * g ) |
---|
260 | { |
---|
261 | CURLMsg * msg; |
---|
262 | int msgs_left; |
---|
263 | |
---|
264 | while(( msg = curl_multi_info_read( g->multi, &msgs_left ))) { |
---|
265 | if(( msg->msg == CURLMSG_DONE ) && ( msg->easy_handle != NULL )) { |
---|
266 | struct tr_web_task * task; |
---|
267 | CURL * e = msg->easy_handle; |
---|
268 | curl_easy_getinfo( e, CURLINFO_PRIVATE, (void*)&task ); |
---|
269 | assert( e == task->easy ); |
---|
270 | remove_task( task ); |
---|
271 | } |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | static void |
---|
276 | restart_timer( tr_web * g ) |
---|
277 | { |
---|
278 | dbgmsg( "adding a timeout for %.1f seconds from now", g->timer_msec/1000.0 ); |
---|
279 | evtimer_del( &g->timer_event ); |
---|
280 | tr_timerAddMsec( &g->timer_event, g->timer_msec ); |
---|
281 | } |
---|
282 | |
---|
283 | static void |
---|
284 | tr_multi_perform( tr_web * g, int fd, int curl_what ) |
---|
285 | { |
---|
286 | CURLMcode m; |
---|
287 | |
---|
288 | dbgmsg( "check_run_count: %d taskCount", g->taskCount ); |
---|
289 | |
---|
290 | /* invoke libcurl's processing */ |
---|
291 | do |
---|
292 | m = curl_multi_socket_action( g->multi, fd, curl_what, &g->taskCount ); |
---|
293 | while( m == CURLM_CALL_MULTI_SOCKET ); |
---|
294 | |
---|
295 | remove_finished_tasks( g ); |
---|
296 | |
---|
297 | if( g->closing && !g->taskCount ) |
---|
298 | web_free( g ); |
---|
299 | else |
---|
300 | restart_timer( g ); |
---|
301 | } |
---|
302 | |
---|
303 | /* libevent says that sock is ready to be processed, so wake up libcurl */ |
---|
304 | static void |
---|
305 | event_cb( int fd, short ev_what, void * g ) |
---|
306 | { |
---|
307 | int curl_what = 0; |
---|
308 | if( ev_what & EV_READ ) curl_what |= CURL_POLL_IN; |
---|
309 | if( ev_what & EV_WRITE ) curl_what |= CURL_POLL_OUT; |
---|
310 | tr_multi_perform( g, fd, curl_what ); |
---|
311 | } |
---|
312 | |
---|
313 | /* CURLMOPT_SOCKETFUNCTION */ |
---|
314 | static int |
---|
315 | sock_cb( CURL * e UNUSED, curl_socket_t fd, int curl_what, |
---|
316 | void * vweb, void * vevent ) |
---|
317 | { |
---|
318 | /*static int num_events = 0;*/ |
---|
319 | struct tr_web * web = vweb; |
---|
320 | struct event * io_event = vevent; |
---|
321 | dbgmsg( "sock_cb: curl_what %d, fd %d, io_event %p", |
---|
322 | curl_what, (int)fd, io_event ); |
---|
323 | |
---|
324 | if( io_event != NULL ) |
---|
325 | event_del( io_event ); |
---|
326 | |
---|
327 | if( curl_what & ( CURL_POLL_IN | CURL_POLL_OUT ) ) |
---|
328 | { |
---|
329 | const short ev_what = EV_PERSIST |
---|
330 | | (( curl_what & CURL_POLL_IN ) ? EV_READ : 0 ) |
---|
331 | | (( curl_what & CURL_POLL_OUT ) ? EV_WRITE : 0 ); |
---|
332 | |
---|
333 | if( io_event == NULL ) { |
---|
334 | io_event = tr_new0( struct event, 1 ); |
---|
335 | curl_multi_assign( web->multi, fd, io_event ); |
---|
336 | /*fprintf( stderr, "+1 io_events to %d\n", ++num_events );*/ |
---|
337 | } |
---|
338 | |
---|
339 | dbgmsg( "enabling (libevent %hd, libcurl %d) on io_event %p, fd %d", |
---|
340 | ev_what, curl_what, io_event, fd ); |
---|
341 | event_set( io_event, fd, ev_what, event_cb, web ); |
---|
342 | assert( io_event->ev_base != NULL ); |
---|
343 | event_add( io_event, NULL ); |
---|
344 | } |
---|
345 | |
---|
346 | if( ( io_event != NULL ) && ( curl_what & CURL_POLL_REMOVE ) ) |
---|
347 | { |
---|
348 | memset( io_event, TR_MEMORY_TRASH, sizeof( struct event ) ); |
---|
349 | tr_free( io_event ); |
---|
350 | /*fprintf( stderr, "-1 io_events to %d\n", --num_events );*/ |
---|
351 | } |
---|
352 | |
---|
353 | return 0; /* libcurl documentation: "The callback MUST return 0." */ |
---|
354 | } |
---|
355 | |
---|
356 | /* libevent says that timer_msec have passed, so wake up libcurl */ |
---|
357 | static void |
---|
358 | libevent_timer_cb( int fd UNUSED, short what UNUSED, void * g ) |
---|
359 | { |
---|
360 | dbgmsg( "libevent timer is done" ); |
---|
361 | tr_multi_perform( g, CURL_SOCKET_TIMEOUT, 0 ); |
---|
362 | } |
---|
363 | |
---|
364 | /* libcurl documentation: "If 0, it means you should proceed immediately |
---|
365 | * without waiting for anything. If it returns -1, there's no timeout at all |
---|
366 | * set ... (but) you must not wait too long (more than a few seconds perhaps) |
---|
367 | * before you call curl_multi_perform() again." */ |
---|
368 | static void |
---|
369 | multi_timer_cb( CURLM * multi UNUSED, long timer_msec, void * vg ) |
---|
370 | { |
---|
371 | tr_web * g = vg; |
---|
372 | |
---|
373 | g->timer_msec = timer_msec > 0 ? timer_msec : DEFAULT_TIMER_MSEC; |
---|
374 | |
---|
375 | if( timer_msec < 1 ) |
---|
376 | tr_multi_perform( g, CURL_SOCKET_TIMEOUT, 0 ); |
---|
377 | else |
---|
378 | restart_timer( g ); |
---|
379 | } |
---|
380 | |
---|
381 | /**** |
---|
382 | ***** |
---|
383 | ****/ |
---|
384 | |
---|
385 | void |
---|
386 | tr_webRun( tr_session * session, |
---|
387 | const char * url, |
---|
388 | const char * range, |
---|
389 | tr_web_done_func done_func, |
---|
390 | void * done_func_user_data ) |
---|
391 | { |
---|
392 | if( session->web != NULL ) |
---|
393 | { |
---|
394 | static unsigned long tag = 0; |
---|
395 | struct tr_web_task * task = tr_new0( struct tr_web_task, 1 ); |
---|
396 | task->session = session; |
---|
397 | task->url = tr_strdup( url ); |
---|
398 | task->range = tr_strdup( range ); |
---|
399 | task->done_func = done_func; |
---|
400 | task->done_func_user_data = done_func_user_data; |
---|
401 | task->tag = ++tag; |
---|
402 | task->response = evbuffer_new( ); |
---|
403 | tr_runInEventThread( session, addTask, task ); |
---|
404 | } |
---|
405 | } |
---|
406 | |
---|
407 | void |
---|
408 | tr_webSetInterface( tr_web * web, const tr_address * addr ) |
---|
409 | { |
---|
410 | if(( web->haveAddr = ( addr != NULL ))) |
---|
411 | web->addr = *addr; |
---|
412 | } |
---|
413 | |
---|
414 | tr_web* |
---|
415 | tr_webInit( tr_session * session ) |
---|
416 | { |
---|
417 | tr_web * web; |
---|
418 | |
---|
419 | /* try to enable ssl for https support; but if that fails, |
---|
420 | * try a plain vanilla init */ |
---|
421 | if( curl_global_init( CURL_GLOBAL_SSL ) ) |
---|
422 | curl_global_init( 0 ); |
---|
423 | |
---|
424 | web = tr_new0( struct tr_web, 1 ); |
---|
425 | web->session = session; |
---|
426 | web->timer_msec = DEFAULT_TIMER_MSEC; /* overwritten by multi_timer_cb() */ |
---|
427 | evtimer_set( &web->timer_event, libevent_timer_cb, web ); |
---|
428 | |
---|
429 | web->multi = curl_multi_init( ); |
---|
430 | curl_multi_setopt( web->multi, CURLMOPT_SOCKETDATA, web ); |
---|
431 | curl_multi_setopt( web->multi, CURLMOPT_SOCKETFUNCTION, sock_cb ); |
---|
432 | curl_multi_setopt( web->multi, CURLMOPT_TIMERDATA, web ); |
---|
433 | curl_multi_setopt( web->multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb ); |
---|
434 | |
---|
435 | return web; |
---|
436 | } |
---|
437 | |
---|
438 | void |
---|
439 | tr_webClose( tr_web ** web_in ) |
---|
440 | { |
---|
441 | tr_web * web = *web_in; |
---|
442 | *web_in = NULL; |
---|
443 | if( web->taskCount < 1 ) |
---|
444 | web_free( web ); |
---|
445 | else |
---|
446 | web->closing = 1; |
---|
447 | } |
---|
448 | |
---|
449 | /***** |
---|
450 | ****** |
---|
451 | ****** |
---|
452 | *****/ |
---|
453 | |
---|
454 | const char * |
---|
455 | tr_webGetResponseStr( long code ) |
---|
456 | { |
---|
457 | switch( code ) |
---|
458 | { |
---|
459 | case 0: return "No Response"; |
---|
460 | case 101: return "Switching Protocols"; |
---|
461 | case 200: return "OK"; |
---|
462 | case 201: return "Created"; |
---|
463 | case 202: return "Accepted"; |
---|
464 | case 203: return "Non-Authoritative Information"; |
---|
465 | case 204: return "No Content"; |
---|
466 | case 205: return "Reset Content"; |
---|
467 | case 206: return "Partial Content"; |
---|
468 | case 300: return "Multiple Choices"; |
---|
469 | case 301: return "Moved Permanently"; |
---|
470 | case 302: return "Found"; |
---|
471 | case 303: return "See Other"; |
---|
472 | case 304: return "Not Modified"; |
---|
473 | case 305: return "Use Proxy"; |
---|
474 | case 306: return "(Unused)"; |
---|
475 | case 307: return "Temporary Redirect"; |
---|
476 | case 400: return "Bad Request"; |
---|
477 | case 401: return "Unauthorized"; |
---|
478 | case 402: return "Payment Required"; |
---|
479 | case 403: return "Forbidden"; |
---|
480 | case 404: return "Not Found"; |
---|
481 | case 405: return "Method Not Allowed"; |
---|
482 | case 406: return "Not Acceptable"; |
---|
483 | case 407: return "Proxy Authentication Required"; |
---|
484 | case 408: return "Request Timeout"; |
---|
485 | case 409: return "Conflict"; |
---|
486 | case 410: return "Gone"; |
---|
487 | case 411: return "Length Required"; |
---|
488 | case 412: return "Precondition Failed"; |
---|
489 | case 413: return "Request Entity Too Large"; |
---|
490 | case 414: return "Request-URI Too Long"; |
---|
491 | case 415: return "Unsupported Media Type"; |
---|
492 | case 416: return "Requested Range Not Satisfiable"; |
---|
493 | case 417: return "Expectation Failed"; |
---|
494 | case 500: return "Internal Server Error"; |
---|
495 | case 501: return "Not Implemented"; |
---|
496 | case 502: return "Bad Gateway"; |
---|
497 | case 503: return "Service Unavailable"; |
---|
498 | case 504: return "Gateway Timeout"; |
---|
499 | case 505: return "HTTP Version Not Supported"; |
---|
500 | default: return "Unknown Error"; |
---|
501 | } |
---|
502 | } |
---|
503 | |
---|
504 | void |
---|
505 | tr_http_escape( struct evbuffer * out, |
---|
506 | const char * str, int len, tr_bool escape_slashes ) |
---|
507 | { |
---|
508 | int i; |
---|
509 | |
---|
510 | if( ( len < 0 ) && ( str != NULL ) ) |
---|
511 | len = strlen( str ); |
---|
512 | |
---|
513 | for( i = 0; i < len; i++ ) { |
---|
514 | if( str[i] == ',' || str[i] == '-' || str[i] == '.' |
---|
515 | || ( '0' <= str[i] && str[i] <= '9' ) |
---|
516 | || ( 'A' <= str[i] && str[i] <= 'Z' ) |
---|
517 | || ( 'a' <= str[i] && str[i] <= 'z' ) |
---|
518 | || ( str[i] == '/' && !escape_slashes ) ) |
---|
519 | evbuffer_add( out, &str[i], 1 ); |
---|
520 | else |
---|
521 | evbuffer_add_printf( out, "%%%02X", (unsigned)(str[i]&0xFF) ); |
---|
522 | } |
---|
523 | } |
---|
524 | |
---|
525 | char * |
---|
526 | tr_http_unescape( const char * str, int len ) |
---|
527 | { |
---|
528 | char * tmp = curl_unescape( str, len ); |
---|
529 | char * ret = tr_strdup( tmp ); |
---|
530 | curl_free( tmp ); |
---|
531 | return ret; |
---|
532 | } |
---|