2008-04-25 18:35:48 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
|
2008-04-24 01:42:53 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2008-10-15 16:43:51 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-04-24 01:42:53 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-04-25 19:46:36 +00:00
|
|
|
* $Id$
|
2008-04-24 01:42:53 +00:00
|
|
|
*/
|
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
#include <assert.h>
|
2008-04-25 04:26:04 +00:00
|
|
|
#include <stdlib.h> /* bsearch */
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
#include <event.h>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2008-10-15 16:43:51 +00:00
|
|
|
#include "list.h"
|
2008-04-25 18:35:48 +00:00
|
|
|
#include "trevent.h"
|
2008-04-24 01:42:53 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "web.h"
|
|
|
|
|
2008-10-15 17:12:53 +00:00
|
|
|
#if LIBCURL_VERSION_NUM < 0x071003
|
|
|
|
#define curl_multi_socket_action(m,fd,mask,i) curl_multi_socket((m),(fd),(i))
|
|
|
|
#endif
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
#define dbgmsg( ... ) tr_deepLog( __FILE__, __LINE__, "web", __VA_ARGS__ )
|
2008-04-25 02:57:33 +00:00
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
struct tr_web
|
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
unsigned int dying : 1;
|
|
|
|
int prev_running;
|
|
|
|
int still_running;
|
|
|
|
CURLM * multi;
|
|
|
|
tr_session * session;
|
|
|
|
struct event timer_event;
|
2008-04-24 01:42:53 +00:00
|
|
|
};
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
struct tr_web_task
|
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
unsigned long tag;
|
|
|
|
struct evbuffer * response;
|
|
|
|
char * url;
|
|
|
|
char * range;
|
|
|
|
tr_session * session;
|
|
|
|
tr_web_done_func * done_func;
|
|
|
|
void * done_func_user_data;
|
2008-04-24 01:42:53 +00:00
|
|
|
};
|
|
|
|
|
2008-04-25 18:35:48 +00:00
|
|
|
static size_t
|
2008-10-15 16:43:51 +00:00
|
|
|
writeFunc( void * ptr, size_t size, size_t nmemb, void * task )
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2008-04-25 18:35:48 +00:00
|
|
|
const size_t byteCount = size * nmemb;
|
2008-10-15 16:43:51 +00:00
|
|
|
evbuffer_add( ((struct tr_web_task*)task)->response, ptr, byteCount );
|
2008-04-25 18:35:48 +00:00
|
|
|
return byteCount;
|
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2008-06-11 20:45:53 +00:00
|
|
|
static int
|
|
|
|
getCurlProxyType( tr_proxy_type t )
|
|
|
|
{
|
|
|
|
switch( t )
|
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
case TR_PROXY_SOCKS4: return CURLPROXY_SOCKS4;
|
|
|
|
case TR_PROXY_SOCKS5: return CURLPROXY_SOCKS5;
|
|
|
|
default: return CURLPROXY_HTTP;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-11 20:45:53 +00:00
|
|
|
|
2008-04-25 18:35:48 +00:00
|
|
|
static void
|
|
|
|
addTask( void * vtask )
|
|
|
|
{
|
|
|
|
struct tr_web_task * task = vtask;
|
2008-10-15 16:43:51 +00:00
|
|
|
const tr_handle * session = task->session;
|
2008-04-27 18:27:32 +00:00
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
if( session && session->web )
|
2008-04-27 18:27:32 +00:00
|
|
|
{
|
2008-06-10 16:16:31 +00:00
|
|
|
struct tr_web * web = session->web;
|
2008-10-15 16:43:51 +00:00
|
|
|
CURL * ch;
|
|
|
|
CURLMcode rc;
|
2008-08-06 23:33:29 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
dbgmsg( "adding task #%lu [%s]", task->tag, task->url );
|
2008-04-27 18:27:32 +00:00
|
|
|
|
|
|
|
ch = curl_easy_init( );
|
2008-06-10 16:16:31 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
if( !task->range && session->isProxyEnabled ) {
|
2008-06-10 16:16:31 +00:00
|
|
|
curl_easy_setopt( ch, CURLOPT_PROXY, session->proxy );
|
2008-07-15 01:03:03 +00:00
|
|
|
curl_easy_setopt( ch, CURLOPT_PROXYPORT, session->proxyPort );
|
2008-10-15 16:43:51 +00:00
|
|
|
curl_easy_setopt( ch, CURLOPT_PROXYTYPE, getCurlProxyType( session->proxyType ) );
|
2008-06-10 16:16:31 +00:00
|
|
|
curl_easy_setopt( ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
|
|
|
|
}
|
2008-10-15 16:43:51 +00:00
|
|
|
if( !task->range && session->isProxyAuthEnabled ) {
|
|
|
|
char * str = tr_strdup_printf( "%s:%s", session->proxyUsername, session->proxyPassword );
|
2008-06-10 16:16:31 +00:00
|
|
|
curl_easy_setopt( ch, CURLOPT_PROXYUSERPWD, str );
|
|
|
|
tr_free( str );
|
|
|
|
}
|
|
|
|
|
2008-04-27 18:27:32 +00:00
|
|
|
curl_easy_setopt( ch, CURLOPT_PRIVATE, task );
|
|
|
|
curl_easy_setopt( ch, CURLOPT_URL, task->url );
|
|
|
|
curl_easy_setopt( ch, CURLOPT_WRITEFUNCTION, writeFunc );
|
|
|
|
curl_easy_setopt( ch, CURLOPT_WRITEDATA, task );
|
2008-10-15 16:43:51 +00:00
|
|
|
curl_easy_setopt( ch, CURLOPT_USERAGENT, TR_NAME "/" LONG_VERSION_STRING );
|
2008-06-05 16:35:33 +00:00
|
|
|
curl_easy_setopt( ch, CURLOPT_SSL_VERIFYHOST, 0 );
|
2008-04-27 18:27:32 +00:00
|
|
|
curl_easy_setopt( ch, CURLOPT_SSL_VERIFYPEER, 0 );
|
|
|
|
curl_easy_setopt( ch, CURLOPT_FORBID_REUSE, 1 );
|
|
|
|
curl_easy_setopt( ch, CURLOPT_NOSIGNAL, 1 );
|
|
|
|
curl_easy_setopt( ch, CURLOPT_FOLLOWLOCATION, 1 );
|
|
|
|
curl_easy_setopt( ch, CURLOPT_MAXREDIRS, 5 );
|
|
|
|
curl_easy_setopt( ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
|
2008-10-15 16:43:51 +00:00
|
|
|
curl_easy_setopt( ch, CURLOPT_VERBOSE, getenv( "TR_CURL_VERBOSE" ) != NULL );
|
2008-06-07 21:26:41 +00:00
|
|
|
if( task->range )
|
|
|
|
curl_easy_setopt( ch, CURLOPT_RANGE, task->range );
|
|
|
|
else /* don't set encoding if range is sent; it messes up binary data */
|
|
|
|
curl_easy_setopt( ch, CURLOPT_ENCODING, "" );
|
2008-10-15 16:43:51 +00:00
|
|
|
|
|
|
|
rc = curl_multi_add_handle( web->multi, ch );
|
|
|
|
if( rc != CURLM_OK )
|
|
|
|
tr_err( "%s", curl_multi_strerror( rc ) );
|
2008-04-27 18:27:32 +00:00
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
}
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2008-04-27 18:27:32 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
struct tr_web_sockinfo
|
|
|
|
{
|
|
|
|
struct event ev;
|
|
|
|
int evset;
|
|
|
|
};
|
2008-04-27 18:27:32 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
static void
|
|
|
|
finish_task( struct tr_web_task * task, long response_code )
|
|
|
|
{
|
|
|
|
dbgmsg( "finished a web task... response code is %ld", response_code );
|
|
|
|
dbgmsg( "===================================================" );
|
|
|
|
task->done_func( task->session,
|
|
|
|
response_code,
|
|
|
|
EVBUFFER_DATA( task->response ),
|
|
|
|
EVBUFFER_LENGTH( task->response ),
|
|
|
|
task->done_func_user_data );
|
|
|
|
evbuffer_free( task->response );
|
|
|
|
tr_free( task->range );
|
|
|
|
tr_free( task->url );
|
|
|
|
tr_free( task );
|
2008-04-24 01:42:53 +00:00
|
|
|
}
|
|
|
|
|
2008-04-24 19:38:59 +00:00
|
|
|
static void
|
2008-08-06 23:33:29 +00:00
|
|
|
webDestroy( tr_web * web )
|
2008-04-24 19:38:59 +00:00
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
timeout_del( &web->timer_event );
|
|
|
|
curl_multi_cleanup( web->multi );
|
2008-08-06 23:33:29 +00:00
|
|
|
tr_free( web );
|
2008-07-29 00:51:07 +00:00
|
|
|
}
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/* note: this function can free the tr_web if it's been flagged for deletion
|
|
|
|
and there are no more tasks remaining. so, callers need to make sure to
|
|
|
|
not reference their g pointer after calling this function */
|
2008-07-29 00:51:07 +00:00
|
|
|
static void
|
2008-10-15 16:43:51 +00:00
|
|
|
check_run_count( tr_web * g )
|
2008-07-29 00:51:07 +00:00
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
dbgmsg( "check_run_count: prev_running %d, still_running %d",
|
|
|
|
g->prev_running, g->still_running );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
if( g->prev_running > g->still_running )
|
|
|
|
{
|
|
|
|
CURLMsg * msg;
|
|
|
|
int msgs_left;
|
|
|
|
CURL * easy;
|
|
|
|
CURLcode res;
|
|
|
|
|
|
|
|
do{
|
|
|
|
easy = NULL;
|
|
|
|
while(( msg = curl_multi_info_read( g->multi, &msgs_left ))) {
|
|
|
|
if( msg->msg == CURLMSG_DONE ) {
|
|
|
|
easy = msg->easy_handle;
|
|
|
|
res = msg->data.result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( easy ) {
|
|
|
|
long code;
|
|
|
|
struct tr_web_task * task;
|
|
|
|
curl_easy_getinfo( easy, CURLINFO_PRIVATE, (void*)&task );
|
|
|
|
curl_easy_getinfo( easy, CURLINFO_RESPONSE_CODE, &code );
|
|
|
|
curl_multi_remove_handle( g->multi, easy );
|
|
|
|
curl_easy_cleanup( easy );
|
|
|
|
finish_task( task, code );
|
|
|
|
}
|
|
|
|
} while ( easy );
|
|
|
|
}
|
2008-07-29 00:51:07 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
g->prev_running = g->still_running;
|
2008-07-29 00:51:07 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
if( g->still_running <= 0 ) {
|
|
|
|
if( evtimer_pending( &g->timer_event, NULL ) ) {
|
|
|
|
dbgmsg( "deleting the pending global timer" );
|
|
|
|
evtimer_del( &g->timer_event );
|
|
|
|
}
|
|
|
|
}
|
2008-07-29 00:51:07 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
if( g->dying && ( g->still_running < 1 ) ) {
|
|
|
|
dbgmsg( "destroying the web global now that all the tasks are done" );
|
|
|
|
webDestroy( g );
|
|
|
|
}
|
|
|
|
}
|
2008-07-29 00:51:07 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/* libevent says that sock is ready to be processed, so wake up libcurl */
|
|
|
|
static void
|
|
|
|
event_cb( int fd, short kind, void * vg )
|
|
|
|
{
|
|
|
|
tr_web * g = vg;
|
|
|
|
CURLMcode rc;
|
|
|
|
int error = 0;
|
|
|
|
int mask;
|
|
|
|
socklen_t errsz = sizeof( error );
|
|
|
|
|
|
|
|
getsockopt( fd, SOL_SOCKET, SO_ERROR, &error, &errsz );
|
|
|
|
if( error )
|
|
|
|
mask = CURL_CSELECT_ERR;
|
|
|
|
else {
|
|
|
|
mask = 0;
|
|
|
|
if( kind & EV_READ ) mask |= CURL_CSELECT_IN;
|
|
|
|
if( kind & EV_WRITE ) mask |= CURL_CSELECT_OUT;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2008-10-15 16:43:51 +00:00
|
|
|
|
|
|
|
do {
|
2008-10-15 17:53:42 +00:00
|
|
|
dbgmsg( "event_cb calling socket_action fd %d, mask %d", fd, mask );
|
2008-10-15 16:43:51 +00:00
|
|
|
rc = curl_multi_socket_action( g->multi, fd, mask, &g->still_running );
|
|
|
|
} while( rc == CURLM_CALL_MULTI_PERFORM );
|
|
|
|
if( rc != CURLM_OK )
|
|
|
|
tr_err( "%s", curl_multi_strerror( rc ) );
|
|
|
|
|
|
|
|
check_run_count( g );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* libevent says that timeout_ms have passed, so wake up libcurl */
|
|
|
|
static void
|
|
|
|
timer_cb( int socket UNUSED, short action UNUSED, void * vg )
|
|
|
|
{
|
|
|
|
tr_web * g = vg;
|
|
|
|
CURLMcode rc;
|
|
|
|
dbgmsg( "libevent timer is done" );
|
|
|
|
|
|
|
|
do {
|
|
|
|
dbgmsg( "timer_cb calling CURL_SOCKET_TIMEOUT" );
|
2008-10-15 23:21:53 +00:00
|
|
|
rc = curl_multi_socket_action( g->multi, CURL_SOCKET_TIMEOUT, 0,
|
2008-10-15 16:43:51 +00:00
|
|
|
&g->still_running );
|
|
|
|
} while( rc == CURLM_CALL_MULTI_PERFORM );
|
|
|
|
|
|
|
|
if( rc != CURLM_OK )
|
|
|
|
tr_err( "%s", curl_multi_strerror( rc ) );
|
|
|
|
|
|
|
|
check_run_count( g );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remsock( struct tr_web_sockinfo * f )
|
|
|
|
{
|
|
|
|
if( f ) {
|
|
|
|
dbgmsg( "deleting sockinfo %p", f );
|
|
|
|
if( f->evset )
|
|
|
|
event_del( &f->ev );
|
|
|
|
tr_free( f );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2008-10-15 16:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setsock( curl_socket_t sockfd,
|
|
|
|
int action,
|
|
|
|
struct tr_web * g,
|
|
|
|
struct tr_web_sockinfo * f )
|
|
|
|
{
|
|
|
|
const int kind = (action & CURL_POLL_IN ? EV_READ : 0)
|
|
|
|
| (action & CURL_POLL_OUT ? EV_WRITE : 0);
|
|
|
|
dbgmsg( "setsock: fd is %d, curl action is %d, libevent action is %d", sockfd, action, kind );
|
|
|
|
if( f->evset )
|
|
|
|
event_del( &f->ev );
|
|
|
|
event_set( &f->ev, sockfd, kind, event_cb, g );
|
|
|
|
f->evset = 1;
|
|
|
|
event_add( &f->ev, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addsock( curl_socket_t sockfd,
|
|
|
|
int action,
|
|
|
|
struct tr_web * g )
|
|
|
|
{
|
|
|
|
struct tr_web_sockinfo * f = tr_new0( struct tr_web_sockinfo, 1 );
|
|
|
|
dbgmsg( "creating a sockinfo %p for fd %d", f, sockfd );
|
|
|
|
setsock( sockfd, action, g, f );
|
|
|
|
curl_multi_assign( g->multi, sockfd, f );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CURLMOPT_SOCKETFUNCTION */
|
|
|
|
static int
|
|
|
|
sock_cb( CURL * e UNUSED,
|
|
|
|
curl_socket_t s,
|
|
|
|
int what,
|
|
|
|
void * vg,
|
|
|
|
void * vf)
|
|
|
|
{
|
|
|
|
struct tr_web * g = vg;
|
|
|
|
struct tr_web_sockinfo * f = vf;
|
|
|
|
dbgmsg( "sock_cb: what is %d, sockinfo is %p", what, f );
|
|
|
|
|
|
|
|
if( what == CURL_POLL_REMOVE )
|
|
|
|
remsock( f );
|
|
|
|
else if( !f )
|
|
|
|
addsock( s, what, g );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
2008-10-15 16:43:51 +00:00
|
|
|
setsock( s, what, g, f );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* libcurl wants us to tell it when timeout_ms have passed */
|
|
|
|
static void
|
|
|
|
multi_timer_cb( CURLM *multi UNUSED, long timeout_ms, void * vweb )
|
|
|
|
{
|
|
|
|
tr_web * web = vweb;
|
|
|
|
struct timeval timeout;
|
|
|
|
dbgmsg( "adding a timeout for %ld seconds from now", timeout_ms/1000l );
|
|
|
|
tr_timevalMsec( timeout_ms, &timeout );
|
|
|
|
timeout_add( &web->timer_event, &timeout );
|
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_webRun( tr_session * session,
|
|
|
|
const char * url,
|
|
|
|
const char * range,
|
|
|
|
tr_web_done_func done_func,
|
|
|
|
void * done_func_user_data )
|
|
|
|
{
|
|
|
|
if( session->web )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
static unsigned long tag = 0;
|
|
|
|
struct tr_web_task * task;
|
|
|
|
|
|
|
|
task = tr_new0( struct tr_web_task, 1 );
|
|
|
|
task->session = session;
|
|
|
|
task->url = tr_strdup( url );
|
|
|
|
task->range = tr_strdup( range );
|
|
|
|
task->done_func = done_func;
|
|
|
|
task->done_func_user_data = done_func_user_data;
|
|
|
|
task->tag = ++tag;
|
|
|
|
task->response = evbuffer_new( );
|
|
|
|
|
|
|
|
tr_runInEventThread( session, addTask, task );
|
2008-08-06 23:33:29 +00:00
|
|
|
}
|
2008-05-06 15:52:57 +00:00
|
|
|
}
|
2008-04-24 19:38:59 +00:00
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
tr_web*
|
|
|
|
tr_webInit( tr_session * session )
|
|
|
|
{
|
|
|
|
static int curlInited = FALSE;
|
2008-10-15 16:43:51 +00:00
|
|
|
tr_web * web;
|
2008-04-24 01:42:53 +00:00
|
|
|
|
|
|
|
/* call curl_global_init if we haven't done it already.
|
|
|
|
* try to enable ssl for https support; but if that fails,
|
2008-10-15 16:43:51 +00:00
|
|
|
* try a plain vanilla init */
|
|
|
|
if( curlInited == FALSE ) {
|
2008-04-24 01:42:53 +00:00
|
|
|
curlInited = TRUE;
|
|
|
|
if( curl_global_init( CURL_GLOBAL_SSL ) )
|
|
|
|
curl_global_init( 0 );
|
|
|
|
}
|
2008-10-15 16:43:51 +00:00
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
web = tr_new0( struct tr_web, 1 );
|
2008-10-15 16:43:51 +00:00
|
|
|
web->multi = curl_multi_init( );
|
2008-04-24 01:42:53 +00:00
|
|
|
web->session = session;
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
timeout_set( &web->timer_event, timer_cb, web );
|
|
|
|
curl_multi_setopt( web->multi, CURLMOPT_SOCKETDATA, web );
|
|
|
|
curl_multi_setopt( web->multi, CURLMOPT_SOCKETFUNCTION, sock_cb );
|
|
|
|
curl_multi_setopt( web->multi, CURLMOPT_TIMERDATA, web );
|
|
|
|
curl_multi_setopt( web->multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb );
|
2008-04-24 01:42:53 +00:00
|
|
|
|
|
|
|
return web;
|
|
|
|
}
|
2008-04-25 04:26:04 +00:00
|
|
|
|
2008-04-25 19:46:36 +00:00
|
|
|
void
|
2008-05-06 15:52:57 +00:00
|
|
|
tr_webClose( tr_web ** web_in )
|
2008-04-25 19:46:36 +00:00
|
|
|
{
|
2008-05-06 15:52:57 +00:00
|
|
|
tr_web * web = *web_in;
|
|
|
|
*web_in = NULL;
|
2008-10-15 16:43:51 +00:00
|
|
|
if( web->still_running < 1 )
|
2008-08-06 23:33:29 +00:00
|
|
|
webDestroy( web );
|
|
|
|
else
|
|
|
|
web->dying = 1;
|
2008-04-25 19:46:36 +00:00
|
|
|
}
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/*****
|
|
|
|
******
|
|
|
|
******
|
|
|
|
*****/
|
2008-04-25 04:26:04 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
static struct http_msg {
|
|
|
|
long code;
|
|
|
|
const char * text;
|
2008-04-25 04:26:04 +00:00
|
|
|
} http_msg[] = {
|
2008-10-15 16:43:51 +00:00
|
|
|
{ 101, "Switching Protocols" },
|
|
|
|
{ 200, "OK" },
|
|
|
|
{ 201, "Created" },
|
|
|
|
{ 202, "Accepted" },
|
|
|
|
{ 203, "Non-Authoritative Information" },
|
|
|
|
{ 204, "No Content" },
|
|
|
|
{ 205, "Reset Content" },
|
|
|
|
{ 206, "Partial Content" },
|
|
|
|
{ 300, "Multiple Choices" },
|
|
|
|
{ 301, "Moved Permanently" },
|
|
|
|
{ 302, "Found" },
|
|
|
|
{ 303, "See Other" },
|
|
|
|
{ 304, "Not Modified" },
|
|
|
|
{ 305, "Use Proxy" },
|
|
|
|
{ 306, "(Unused)" },
|
|
|
|
{ 307, "Temporary Redirect" },
|
|
|
|
{ 400, "Bad Request" },
|
|
|
|
{ 401, "Unauthorized" },
|
|
|
|
{ 402, "Payment Required" },
|
|
|
|
{ 403, "Forbidden" },
|
|
|
|
{ 404, "Not Found" },
|
|
|
|
{ 405, "Method Not Allowed" },
|
|
|
|
{ 406, "Not Acceptable" },
|
|
|
|
{ 407, "Proxy Authentication Required" },
|
|
|
|
{ 408, "Request Timeout" },
|
|
|
|
{ 409, "Conflict" },
|
|
|
|
{ 410, "Gone" },
|
|
|
|
{ 411, "Length Required" },
|
|
|
|
{ 412, "Precondition Failed" },
|
|
|
|
{ 413, "Request Entity Too Large" },
|
|
|
|
{ 414, "Request-URI Too Long" },
|
|
|
|
{ 415, "Unsupported Media Type" },
|
2008-04-25 04:26:04 +00:00
|
|
|
{ 416, "Requested Range Not Satisfiable" },
|
2008-10-15 16:43:51 +00:00
|
|
|
{ 417, "Expectation Failed" },
|
|
|
|
{ 500, "Internal Server Error" },
|
|
|
|
{ 501, "Not Implemented" },
|
|
|
|
{ 502, "Bad Gateway" },
|
|
|
|
{ 503, "Service Unavailable" },
|
|
|
|
{ 504, "Gateway Timeout" },
|
|
|
|
{ 505, "HTTP Version Not Supported" },
|
|
|
|
{ 0, NULL }
|
2008-04-25 04:26:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2008-10-15 16:43:51 +00:00
|
|
|
compareResponseCodes( const void * va, const void * vb )
|
2008-04-25 04:26:04 +00:00
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
const long a = *(const long*) va;
|
2008-04-25 04:26:04 +00:00
|
|
|
const struct http_msg * b = vb;
|
|
|
|
return a - b->code;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
tr_webGetResponseStr( long code )
|
|
|
|
{
|
|
|
|
struct http_msg * msg = bsearch( &code,
|
2008-10-15 16:43:51 +00:00
|
|
|
http_msg,
|
|
|
|
sizeof( http_msg ) / sizeof( http_msg[0] ),
|
2008-04-25 04:26:04 +00:00
|
|
|
sizeof( http_msg[0] ),
|
|
|
|
compareResponseCodes );
|
|
|
|
return msg ? msg->text : "Unknown Error";
|
|
|
|
}
|