1 | /* |
---|
2 | * This file Copyright (C) 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 12177 2011-03-16 18:04:23Z jordan $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <string.h> /* strlen(), strstr() */ |
---|
14 | #include <stdlib.h> /* getenv() */ |
---|
15 | |
---|
16 | #ifdef WIN32 |
---|
17 | #include <ws2tcpip.h> |
---|
18 | #else |
---|
19 | #include <sys/select.h> |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <curl/curl.h> |
---|
23 | |
---|
24 | #include <event2/buffer.h> |
---|
25 | |
---|
26 | #include "transmission.h" |
---|
27 | #include "list.h" |
---|
28 | #include "net.h" /* tr_address */ |
---|
29 | #include "platform.h" /* mutex */ |
---|
30 | #include "session.h" |
---|
31 | #include "trevent.h" /* tr_runInEventThread() */ |
---|
32 | #include "utils.h" |
---|
33 | #include "version.h" /* User-Agent */ |
---|
34 | #include "web.h" |
---|
35 | |
---|
36 | #if LIBCURL_VERSION_NUM >= 0x070F06 /* CURLOPT_SOCKOPT* was added in 7.15.6 */ |
---|
37 | #define USE_LIBCURL_SOCKOPT |
---|
38 | #endif |
---|
39 | |
---|
40 | enum |
---|
41 | { |
---|
42 | THREADFUNC_MAX_SLEEP_MSEC = 1000, |
---|
43 | }; |
---|
44 | |
---|
45 | #if 0 |
---|
46 | #define dbgmsg(...) \ |
---|
47 | do { \ |
---|
48 | fprintf( stderr, __VA_ARGS__ ); \ |
---|
49 | fprintf( stderr, "\n" ); \ |
---|
50 | } while( 0 ) |
---|
51 | #else |
---|
52 | #define dbgmsg( ... ) \ |
---|
53 | do { \ |
---|
54 | if( tr_deepLoggingIsActive( ) ) \ |
---|
55 | tr_deepLog( __FILE__, __LINE__, "web", __VA_ARGS__ ); \ |
---|
56 | } while( 0 ) |
---|
57 | #endif |
---|
58 | |
---|
59 | /*** |
---|
60 | **** |
---|
61 | ***/ |
---|
62 | |
---|
63 | struct tr_web |
---|
64 | { |
---|
65 | int close_mode; |
---|
66 | tr_list * tasks; |
---|
67 | tr_lock * taskLock; |
---|
68 | }; |
---|
69 | |
---|
70 | |
---|
71 | /*** |
---|
72 | **** |
---|
73 | ***/ |
---|
74 | |
---|
75 | struct tr_web_task |
---|
76 | { |
---|
77 | long code; |
---|
78 | long timeout_secs; |
---|
79 | tr_bool did_connect; |
---|
80 | tr_bool did_timeout; |
---|
81 | struct evbuffer * response; |
---|
82 | struct evbuffer * freebuf; |
---|
83 | char * url; |
---|
84 | char * range; |
---|
85 | char * cookies; |
---|
86 | tr_session * session; |
---|
87 | tr_web_done_func * done_func; |
---|
88 | void * done_func_user_data; |
---|
89 | }; |
---|
90 | |
---|
91 | static void |
---|
92 | task_free( struct tr_web_task * task ) |
---|
93 | { |
---|
94 | if( task->freebuf ) |
---|
95 | evbuffer_free( task->freebuf ); |
---|
96 | tr_free( task->cookies ); |
---|
97 | tr_free( task->range ); |
---|
98 | tr_free( task->url ); |
---|
99 | tr_free( task ); |
---|
100 | } |
---|
101 | |
---|
102 | /*** |
---|
103 | **** |
---|
104 | ***/ |
---|
105 | |
---|
106 | static size_t |
---|
107 | writeFunc( void * ptr, size_t size, size_t nmemb, void * vtask ) |
---|
108 | { |
---|
109 | const size_t byteCount = size * nmemb; |
---|
110 | struct tr_web_task * task = vtask; |
---|
111 | evbuffer_add( task->response, ptr, byteCount ); |
---|
112 | dbgmsg( "wrote %zu bytes to task %p's buffer", byteCount, task ); |
---|
113 | return byteCount; |
---|
114 | } |
---|
115 | |
---|
116 | #ifdef USE_LIBCURL_SOCKOPT |
---|
117 | static int |
---|
118 | sockoptfunction( void * vtask, curl_socket_t fd, curlsocktype purpose UNUSED ) |
---|
119 | { |
---|
120 | struct tr_web_task * task = vtask; |
---|
121 | const tr_bool isScrape = strstr( task->url, "scrape" ) != NULL; |
---|
122 | const tr_bool isAnnounce = strstr( task->url, "announce" ) != NULL; |
---|
123 | |
---|
124 | /* announce and scrape requests have tiny payloads. */ |
---|
125 | if( isScrape || isAnnounce ) |
---|
126 | { |
---|
127 | const int sndbuf = 1024; |
---|
128 | const int rcvbuf = isScrape ? 2048 : 3072; |
---|
129 | setsockopt( fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf) ); |
---|
130 | setsockopt( fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf) ); |
---|
131 | } |
---|
132 | |
---|
133 | /* return nonzero if this function encountered an error */ |
---|
134 | return 0; |
---|
135 | } |
---|
136 | #endif |
---|
137 | |
---|
138 | static long |
---|
139 | getTimeoutFromURL( const struct tr_web_task * task ) |
---|
140 | { |
---|
141 | long timeout; |
---|
142 | const tr_session * session = task->session; |
---|
143 | |
---|
144 | if( !session || session->isClosed ) timeout = 20L; |
---|
145 | else if( strstr( task->url, "scrape" ) != NULL ) timeout = 30L; |
---|
146 | else if( strstr( task->url, "announce" ) != NULL ) timeout = 90L; |
---|
147 | else timeout = 240L; |
---|
148 | |
---|
149 | return timeout; |
---|
150 | } |
---|
151 | |
---|
152 | static CURL * |
---|
153 | createEasy( tr_session * s, struct tr_web_task * task ) |
---|
154 | { |
---|
155 | const tr_address * addr; |
---|
156 | tr_bool is_default_value; |
---|
157 | CURL * e = curl_easy_init( ); |
---|
158 | const long verbose = getenv( "TR_CURL_VERBOSE" ) != NULL; |
---|
159 | char * cookie_filename = tr_buildPath( s->configDir, "cookies.txt", NULL ); |
---|
160 | |
---|
161 | task->timeout_secs = getTimeoutFromURL( task ); |
---|
162 | |
---|
163 | curl_easy_setopt( e, CURLOPT_AUTOREFERER, 1L ); |
---|
164 | curl_easy_setopt( e, CURLOPT_COOKIEFILE, cookie_filename ); |
---|
165 | curl_easy_setopt( e, CURLOPT_ENCODING, "gzip;q=1.0, deflate, identity" ); |
---|
166 | curl_easy_setopt( e, CURLOPT_FOLLOWLOCATION, 1L ); |
---|
167 | curl_easy_setopt( e, CURLOPT_MAXREDIRS, -1L ); |
---|
168 | curl_easy_setopt( e, CURLOPT_NOSIGNAL, 1L ); |
---|
169 | curl_easy_setopt( e, CURLOPT_PRIVATE, task ); |
---|
170 | #ifdef USE_LIBCURL_SOCKOPT |
---|
171 | curl_easy_setopt( e, CURLOPT_SOCKOPTFUNCTION, sockoptfunction ); |
---|
172 | curl_easy_setopt( e, CURLOPT_SOCKOPTDATA, task ); |
---|
173 | #endif |
---|
174 | curl_easy_setopt( e, CURLOPT_SSL_VERIFYHOST, 0L ); |
---|
175 | curl_easy_setopt( e, CURLOPT_SSL_VERIFYPEER, 0L ); |
---|
176 | curl_easy_setopt( e, CURLOPT_TIMEOUT, task->timeout_secs ); |
---|
177 | curl_easy_setopt( e, CURLOPT_URL, task->url ); |
---|
178 | curl_easy_setopt( e, CURLOPT_USERAGENT, TR_NAME "/" SHORT_VERSION_STRING ); |
---|
179 | curl_easy_setopt( e, CURLOPT_VERBOSE, verbose ); |
---|
180 | curl_easy_setopt( e, CURLOPT_WRITEDATA, task ); |
---|
181 | curl_easy_setopt( e, CURLOPT_WRITEFUNCTION, writeFunc ); |
---|
182 | |
---|
183 | if((( addr = tr_sessionGetPublicAddress( s, TR_AF_INET, &is_default_value ))) && !is_default_value ) |
---|
184 | curl_easy_setopt( e, CURLOPT_INTERFACE, tr_ntop_non_ts( addr ) ); |
---|
185 | else if ((( addr = tr_sessionGetPublicAddress( s, TR_AF_INET6, &is_default_value ))) && !is_default_value ) |
---|
186 | curl_easy_setopt( e, CURLOPT_INTERFACE, tr_ntop_non_ts( addr ) ); |
---|
187 | |
---|
188 | if( task->cookies != NULL ) |
---|
189 | curl_easy_setopt( e, CURLOPT_COOKIE, task->cookies ); |
---|
190 | |
---|
191 | if( task->range ) |
---|
192 | curl_easy_setopt( e, CURLOPT_RANGE, task->range ); |
---|
193 | |
---|
194 | if( s->curl_easy_config_func != NULL ) |
---|
195 | s->curl_easy_config_func( s, e, task->url ); |
---|
196 | |
---|
197 | tr_free( cookie_filename ); |
---|
198 | return e; |
---|
199 | } |
---|
200 | |
---|
201 | /*** |
---|
202 | **** |
---|
203 | ***/ |
---|
204 | |
---|
205 | static void |
---|
206 | task_finish_func( void * vtask ) |
---|
207 | { |
---|
208 | struct tr_web_task * task = vtask; |
---|
209 | dbgmsg( "finished web task %p; got %ld", task, task->code ); |
---|
210 | |
---|
211 | if( task->done_func != NULL ) |
---|
212 | task->done_func( task->session, |
---|
213 | task->did_connect, |
---|
214 | task->did_timeout, |
---|
215 | task->code, |
---|
216 | evbuffer_pullup( task->response, -1 ), |
---|
217 | evbuffer_get_length( task->response ), |
---|
218 | task->done_func_user_data ); |
---|
219 | |
---|
220 | task_free( task ); |
---|
221 | } |
---|
222 | |
---|
223 | /**** |
---|
224 | ***** |
---|
225 | ****/ |
---|
226 | |
---|
227 | void |
---|
228 | tr_webRun( tr_session * session, |
---|
229 | const char * url, |
---|
230 | const char * range, |
---|
231 | const char * cookies, |
---|
232 | tr_web_done_func done_func, |
---|
233 | void * done_func_user_data ) |
---|
234 | { |
---|
235 | tr_webRunWithBuffer( session, url, range, cookies, |
---|
236 | done_func, done_func_user_data, |
---|
237 | NULL ); |
---|
238 | } |
---|
239 | |
---|
240 | void |
---|
241 | tr_webRunWithBuffer( tr_session * session, |
---|
242 | const char * url, |
---|
243 | const char * range, |
---|
244 | const char * cookies, |
---|
245 | tr_web_done_func done_func, |
---|
246 | void * done_func_user_data, |
---|
247 | struct evbuffer * buffer ) |
---|
248 | { |
---|
249 | struct tr_web * web = session->web; |
---|
250 | |
---|
251 | if( web != NULL ) |
---|
252 | { |
---|
253 | struct tr_web_task * task = tr_new0( struct tr_web_task, 1 ); |
---|
254 | |
---|
255 | task->session = session; |
---|
256 | task->url = tr_strdup( url ); |
---|
257 | task->range = tr_strdup( range ); |
---|
258 | task->cookies = tr_strdup( cookies); |
---|
259 | task->done_func = done_func; |
---|
260 | task->done_func_user_data = done_func_user_data; |
---|
261 | task->response = buffer ? buffer : evbuffer_new( ); |
---|
262 | task->freebuf = buffer ? NULL : task->response; |
---|
263 | |
---|
264 | tr_lockLock( web->taskLock ); |
---|
265 | tr_list_append( &web->tasks, task ); |
---|
266 | tr_lockUnlock( web->taskLock ); |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * Portability wrapper for select(). |
---|
272 | * |
---|
273 | * http://msdn.microsoft.com/en-us/library/ms740141%28VS.85%29.aspx |
---|
274 | * On win32, any two of the parameters, readfds, writefds, or exceptfds, |
---|
275 | * can be given as null. At least one must be non-null, and any non-null |
---|
276 | * descriptor set must contain at least one handle to a socket. |
---|
277 | */ |
---|
278 | static void |
---|
279 | tr_select( int nfds, |
---|
280 | fd_set * r_fd_set, fd_set * w_fd_set, fd_set * c_fd_set, |
---|
281 | struct timeval * t ) |
---|
282 | { |
---|
283 | #ifdef WIN32 |
---|
284 | if( !r_fd_set->fd_count && !w_fd_set->fd_count && !c_fd_set->fd_count ) |
---|
285 | { |
---|
286 | const long int msec = t->tv_sec*1000 + t->tv_usec/1000; |
---|
287 | tr_wait_msec( msec ); |
---|
288 | } |
---|
289 | else if( select( 0, r_fd_set->fd_count ? r_fd_set : NULL, |
---|
290 | w_fd_set->fd_count ? w_fd_set : NULL, |
---|
291 | c_fd_set->fd_count ? c_fd_set : NULL, t ) < 0 ) |
---|
292 | { |
---|
293 | char errstr[512]; |
---|
294 | const int e = EVUTIL_SOCKET_ERROR( ); |
---|
295 | tr_net_strerror( errstr, sizeof( errstr ), e ); |
---|
296 | dbgmsg( "Error: select (%d) %s", e, errstr ); |
---|
297 | } |
---|
298 | #else |
---|
299 | select( nfds, r_fd_set, w_fd_set, c_fd_set, t ); |
---|
300 | #endif |
---|
301 | } |
---|
302 | |
---|
303 | static void |
---|
304 | tr_webThreadFunc( void * vsession ) |
---|
305 | { |
---|
306 | CURLM * multi; |
---|
307 | struct tr_web * web; |
---|
308 | int taskCount = 0; |
---|
309 | struct tr_web_task * task; |
---|
310 | tr_session * session = vsession; |
---|
311 | |
---|
312 | /* try to enable ssl for https support; but if that fails, |
---|
313 | * try a plain vanilla init */ |
---|
314 | if( curl_global_init( CURL_GLOBAL_SSL ) ) |
---|
315 | curl_global_init( 0 ); |
---|
316 | |
---|
317 | web = tr_new0( struct tr_web, 1 ); |
---|
318 | web->close_mode = ~0; |
---|
319 | web->taskLock = tr_lockNew( ); |
---|
320 | web->tasks = NULL; |
---|
321 | multi = curl_multi_init( ); |
---|
322 | session->web = web; |
---|
323 | |
---|
324 | for( ;; ) |
---|
325 | { |
---|
326 | long msec; |
---|
327 | int unused; |
---|
328 | CURLMsg * msg; |
---|
329 | CURLMcode mcode; |
---|
330 | |
---|
331 | if( web->close_mode == TR_WEB_CLOSE_NOW ) |
---|
332 | break; |
---|
333 | if( ( web->close_mode == TR_WEB_CLOSE_WHEN_IDLE ) && !taskCount ) |
---|
334 | break; |
---|
335 | |
---|
336 | /* add tasks from the queue */ |
---|
337 | tr_lockLock( web->taskLock ); |
---|
338 | while(( task = tr_list_pop_front( &web->tasks ))) |
---|
339 | { |
---|
340 | dbgmsg( "adding task to curl: [%s]", task->url ); |
---|
341 | curl_multi_add_handle( multi, createEasy( session, task )); |
---|
342 | /*fprintf( stderr, "adding a task.. taskCount is now %d\n", taskCount );*/ |
---|
343 | ++taskCount; |
---|
344 | } |
---|
345 | tr_lockUnlock( web->taskLock ); |
---|
346 | |
---|
347 | /* maybe wait a little while before calling curl_multi_perform() */ |
---|
348 | msec = 0; |
---|
349 | curl_multi_timeout( multi, &msec ); |
---|
350 | if( msec < 0 ) |
---|
351 | msec = THREADFUNC_MAX_SLEEP_MSEC; |
---|
352 | if( msec > 0 ) |
---|
353 | { |
---|
354 | int usec; |
---|
355 | int max_fd; |
---|
356 | struct timeval t; |
---|
357 | fd_set r_fd_set, w_fd_set, c_fd_set; |
---|
358 | |
---|
359 | max_fd = 0; |
---|
360 | FD_ZERO( &r_fd_set ); |
---|
361 | FD_ZERO( &w_fd_set ); |
---|
362 | FD_ZERO( &c_fd_set ); |
---|
363 | curl_multi_fdset( multi, &r_fd_set, &w_fd_set, &c_fd_set, &max_fd ); |
---|
364 | |
---|
365 | if( msec > THREADFUNC_MAX_SLEEP_MSEC ) |
---|
366 | msec = THREADFUNC_MAX_SLEEP_MSEC; |
---|
367 | |
---|
368 | usec = msec * 1000; |
---|
369 | t.tv_sec = usec / 1000000; |
---|
370 | t.tv_usec = usec % 1000000; |
---|
371 | |
---|
372 | tr_select( max_fd+1, &r_fd_set, &w_fd_set, &c_fd_set, &t ); |
---|
373 | } |
---|
374 | |
---|
375 | /* call curl_multi_perform() */ |
---|
376 | do { |
---|
377 | mcode = curl_multi_perform( multi, &unused ); |
---|
378 | } while( mcode == CURLM_CALL_MULTI_PERFORM ); |
---|
379 | |
---|
380 | /* pump completed tasks from the multi */ |
---|
381 | while(( msg = curl_multi_info_read( multi, &unused ))) |
---|
382 | { |
---|
383 | if(( msg->msg == CURLMSG_DONE ) && ( msg->easy_handle != NULL )) |
---|
384 | { |
---|
385 | double total_time; |
---|
386 | struct tr_web_task * task; |
---|
387 | long req_bytes_sent; |
---|
388 | CURL * e = msg->easy_handle; |
---|
389 | curl_easy_getinfo( e, CURLINFO_PRIVATE, (void*)&task ); |
---|
390 | curl_easy_getinfo( e, CURLINFO_RESPONSE_CODE, &task->code ); |
---|
391 | curl_easy_getinfo( e, CURLINFO_REQUEST_SIZE, &req_bytes_sent ); |
---|
392 | curl_easy_getinfo( e, CURLINFO_TOTAL_TIME, &total_time ); |
---|
393 | task->did_connect = task->code>0 || req_bytes_sent>0; |
---|
394 | task->did_timeout = !task->code && ( total_time >= task->timeout_secs ); |
---|
395 | curl_multi_remove_handle( multi, e ); |
---|
396 | curl_easy_cleanup( e ); |
---|
397 | /*fprintf( stderr, "removing a completed task.. taskCount is now %d (response code: %d, response len: %d)\n", taskCount, (int)task->code, (int)evbuffer_get_length(task->response) );*/ |
---|
398 | tr_runInEventThread( task->session, task_finish_func, task ); |
---|
399 | --taskCount; |
---|
400 | } |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | /* Discard any remaining tasks. |
---|
405 | * This is rare, but can happen on shutdown with unresponsive trackers. */ |
---|
406 | while(( task = tr_list_pop_front( &web->tasks ))) { |
---|
407 | dbgmsg( "Discarding task \"%s\"", task->url ); |
---|
408 | task_free( task ); |
---|
409 | } |
---|
410 | |
---|
411 | /* cleanup */ |
---|
412 | curl_multi_cleanup( multi ); |
---|
413 | tr_lockFree( web->taskLock ); |
---|
414 | tr_free( web ); |
---|
415 | session->web = NULL; |
---|
416 | } |
---|
417 | |
---|
418 | void |
---|
419 | tr_webInit( tr_session * session ) |
---|
420 | { |
---|
421 | tr_threadNew( tr_webThreadFunc, session ); |
---|
422 | } |
---|
423 | |
---|
424 | void |
---|
425 | tr_webClose( tr_session * session, tr_web_close_mode close_mode ) |
---|
426 | { |
---|
427 | if( session->web != NULL ) |
---|
428 | { |
---|
429 | session->web->close_mode = close_mode; |
---|
430 | |
---|
431 | if( close_mode == TR_WEB_CLOSE_NOW ) |
---|
432 | while( session->web != NULL ) |
---|
433 | tr_wait_msec( 100 ); |
---|
434 | } |
---|
435 | } |
---|
436 | |
---|
437 | /***** |
---|
438 | ****** |
---|
439 | ****** |
---|
440 | *****/ |
---|
441 | |
---|
442 | const char * |
---|
443 | tr_webGetResponseStr( long code ) |
---|
444 | { |
---|
445 | switch( code ) |
---|
446 | { |
---|
447 | case 0: return "No Response"; |
---|
448 | case 101: return "Switching Protocols"; |
---|
449 | case 200: return "OK"; |
---|
450 | case 201: return "Created"; |
---|
451 | case 202: return "Accepted"; |
---|
452 | case 203: return "Non-Authoritative Information"; |
---|
453 | case 204: return "No Content"; |
---|
454 | case 205: return "Reset Content"; |
---|
455 | case 206: return "Partial Content"; |
---|
456 | case 300: return "Multiple Choices"; |
---|
457 | case 301: return "Moved Permanently"; |
---|
458 | case 302: return "Found"; |
---|
459 | case 303: return "See Other"; |
---|
460 | case 304: return "Not Modified"; |
---|
461 | case 305: return "Use Proxy"; |
---|
462 | case 306: return "(Unused)"; |
---|
463 | case 307: return "Temporary Redirect"; |
---|
464 | case 400: return "Bad Request"; |
---|
465 | case 401: return "Unauthorized"; |
---|
466 | case 402: return "Payment Required"; |
---|
467 | case 403: return "Forbidden"; |
---|
468 | case 404: return "Not Found"; |
---|
469 | case 405: return "Method Not Allowed"; |
---|
470 | case 406: return "Not Acceptable"; |
---|
471 | case 407: return "Proxy Authentication Required"; |
---|
472 | case 408: return "Request Timeout"; |
---|
473 | case 409: return "Conflict"; |
---|
474 | case 410: return "Gone"; |
---|
475 | case 411: return "Length Required"; |
---|
476 | case 412: return "Precondition Failed"; |
---|
477 | case 413: return "Request Entity Too Large"; |
---|
478 | case 414: return "Request-URI Too Long"; |
---|
479 | case 415: return "Unsupported Media Type"; |
---|
480 | case 416: return "Requested Range Not Satisfiable"; |
---|
481 | case 417: return "Expectation Failed"; |
---|
482 | case 500: return "Internal Server Error"; |
---|
483 | case 501: return "Not Implemented"; |
---|
484 | case 502: return "Bad Gateway"; |
---|
485 | case 503: return "Service Unavailable"; |
---|
486 | case 504: return "Gateway Timeout"; |
---|
487 | case 505: return "HTTP Version Not Supported"; |
---|
488 | default: return "Unknown Error"; |
---|
489 | } |
---|
490 | } |
---|
491 | |
---|
492 | void |
---|
493 | tr_http_escape( struct evbuffer * out, |
---|
494 | const char * str, int len, tr_bool escape_slashes ) |
---|
495 | { |
---|
496 | const char * end; |
---|
497 | |
---|
498 | if( ( len < 0 ) && ( str != NULL ) ) |
---|
499 | len = strlen( str ); |
---|
500 | |
---|
501 | for( end=str+len; str && str!=end; ++str ) { |
---|
502 | if( ( *str == ',' ) |
---|
503 | || ( *str == '-' ) |
---|
504 | || ( *str == '.' ) |
---|
505 | || ( ( '0' <= *str ) && ( *str <= '9' ) ) |
---|
506 | || ( ( 'A' <= *str ) && ( *str <= 'Z' ) ) |
---|
507 | || ( ( 'a' <= *str ) && ( *str <= 'z' ) ) |
---|
508 | || ( ( *str == '/' ) && ( !escape_slashes ) ) ) |
---|
509 | evbuffer_add( out, str, 1 ); |
---|
510 | else |
---|
511 | evbuffer_add_printf( out, "%%%02X", (unsigned)(*str&0xFF) ); |
---|
512 | } |
---|
513 | } |
---|
514 | |
---|
515 | char * |
---|
516 | tr_http_unescape( const char * str, int len ) |
---|
517 | { |
---|
518 | char * tmp = curl_unescape( str, len ); |
---|
519 | char * ret = tr_strdup( tmp ); |
---|
520 | curl_free( tmp ); |
---|
521 | return ret; |
---|
522 | } |
---|
523 | |
---|
524 | static int |
---|
525 | is_rfc2396_alnum( uint8_t ch ) |
---|
526 | { |
---|
527 | return ( '0' <= ch && ch <= '9' ) |
---|
528 | || ( 'A' <= ch && ch <= 'Z' ) |
---|
529 | || ( 'a' <= ch && ch <= 'z' ) |
---|
530 | || ch == '.' |
---|
531 | || ch == '-' |
---|
532 | || ch == '_' |
---|
533 | || ch == '~'; |
---|
534 | } |
---|
535 | |
---|
536 | void |
---|
537 | tr_http_escape_sha1( char * out, const uint8_t * sha1_digest ) |
---|
538 | { |
---|
539 | const uint8_t * in = sha1_digest; |
---|
540 | const uint8_t * end = in + SHA_DIGEST_LENGTH; |
---|
541 | |
---|
542 | while( in != end ) |
---|
543 | if( is_rfc2396_alnum( *in ) ) |
---|
544 | *out++ = (char) *in++; |
---|
545 | else |
---|
546 | out += tr_snprintf( out, 4, "%%%02x", (unsigned int)*in++ ); |
---|
547 | |
---|
548 | *out = '\0'; |
---|
549 | } |
---|