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