# include "config.h"
#endif
-#include <vlc/vlc.h>
+#include <vlc_common.h>
#include <vlc_httpd.h>
-#ifdef ENABLE_HTTPD
-
#include <assert.h>
#include <vlc_network.h>
httpd_t *httpd;
/* ref count */
- int i_ref;
+ unsigned i_ref;
/* address/port and socket for listening at connections */
char *psz_hostname;
unsigned nfd;
vlc_mutex_t lock;
+ vlc_cond_t wait;
/* all registered url (becarefull that 2 httpd_url_t could point at the same url)
* This will slow down the url research but make my live easier
/*****************************************************************************
* Various functions
*****************************************************************************/
-static struct
+static const struct
{
const char psz_ext[8];
const char *psz_mime;
{ ".mpe", "video/mpeg" },
{ ".mov", "video/quicktime" },
{ ".moov", "video/quicktime" },
+ { ".oga", "audio/ogg" },
{ ".ogg", "application/ogg" },
{ ".ogm", "application/ogg" },
+ { ".ogv", "video/ogg" },
+ { ".ogx", "application/ogg" },
+ { ".spx", "audio/ogg" },
{ ".wav", "audio/wav" },
{ ".wma", "audio/x-ms-wma" },
{ ".wmv", "video/x-ms-wmv" },
/*****************************************************************************
* Low level
*****************************************************************************/
-static void httpd_HostThread( httpd_host_t * );
+static void* httpd_HostThread( vlc_object_t * );
/* create a new host */
httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
psz_host = strdup( psz_hostname );
if( psz_host == NULL )
- {
- msg_Err( p_this, "memory error" );
return NULL;
- }
/* to be sure to avoid multiple creation */
var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
ptrval.p_address = httpd;
libvlc_priv (p_this->p_libvlc)->p_httpd = httpd;
- vlc_object_yield( httpd );
+ vlc_object_hold( httpd );
vlc_object_attach( httpd, p_this->p_libvlc );
}
|| strcmp( host->psz_hostname, psz_hostname ) )
continue;
- /* yep found */
+ /* Increase existing matching host reference count.
+ * The reference count is written under both the global httpd and the
+ * host lock. It is read with either or both locks held. The global
+ * lock is always acquired first. */
+ vlc_mutex_lock( &host->lock );
host->i_ref++;
+ vlc_mutex_unlock( &host->lock );
vlc_mutex_unlock( lockval.p_address );
return host;
/* create the new host */
host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
- VLC_OBJECT_HTTPD_HOST,
+ VLC_OBJECT_GENERIC,
psz_object_type );
if (host == NULL)
goto error;
- vlc_object_lock( host );
- if( vlc_object_waitpipe( VLC_OBJECT( host ) ) == -1 )
- {
- msg_Err( host, "signaling pipe error: %m" );
- vlc_object_unlock( host );
- goto error;
- }
- vlc_object_unlock( host );
-
host->httpd = httpd;
vlc_mutex_init( &host->lock );
+ vlc_cond_init( &host->wait );
host->i_ref = 1;
host->fds = net_ListenTCP( p_this, psz_host, i_port );
}
for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
+ if( vlc_object_waitpipe( VLC_OBJECT( host ) ) == -1 )
+ {
+ msg_Err( host, "signaling pipe error: %m" );
+ goto error;
+ }
+
host->i_port = i_port;
host->psz_hostname = psz_host;
/* create the thread */
if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
- VLC_THREAD_PRIORITY_LOW, false ) )
+ VLC_THREAD_PRIORITY_LOW ) )
{
msg_Err( p_this, "cannot spawn http host thread" );
goto error;
free( psz_host );
if( httpd->i_host <= 0 )
{
+ libvlc_priv (httpd->p_libvlc)->p_httpd = NULL;
vlc_object_release( httpd );
vlc_object_detach( httpd );
vlc_object_release( httpd );
if( host != NULL )
{
net_ListenClose( host->fds );
+ vlc_cond_destroy( &host->wait );
vlc_mutex_destroy( &host->lock );
vlc_object_release( host );
}
var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
vlc_mutex_lock( lockval.p_address );
+ vlc_mutex_lock( &host->lock );
host->i_ref--;
+ if( host->i_ref == 0 )
+ vlc_cond_signal( &host->wait );
+ vlc_mutex_unlock( &host->lock );
if( host->i_ref > 0 )
{
/* still used */
net_ListenClose( host->fds );
free( host->psz_hostname );
+ vlc_cond_destroy( &host->wait );
vlc_mutex_destroy( &host->lock );
vlc_object_release( host );
}
TAB_APPEND( host->i_url, host->url, url );
+ vlc_cond_signal( &host->wait );
vlc_mutex_unlock( &host->lock );
return url;
vlc_mutex_unlock( &host->lock );
}
-void httpd_MsgInit( httpd_message_t *msg )
+static void httpd_MsgInit( httpd_message_t *msg )
{
msg->cl = NULL;
msg->i_type = HTTPD_MSG_NONE;
msg->p_body = NULL;
}
-void httpd_MsgClean( httpd_message_t *msg )
+static void httpd_MsgClean( httpd_message_t *msg )
{
int i;
return cl;
}
-static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
+static
+ssize_t httpd_NetRecv (httpd_client_t *cl, uint8_t *p, size_t i_len)
{
tls_session_t *p_tls;
+ ssize_t val;
p_tls = cl->p_tls;
- if( p_tls != NULL)
- return tls_Recv( p_tls, p, i_len );
-
- return recv( cl->fd, p, i_len, 0 );
+ do
+ val = p_tls ? tls_Recv (p_tls, p, i_len)
+ : recv (cl->fd, p, i_len, 0);
+ while (val == -1 && errno == EINTR);
+ return val;
}
-static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
+static
+ssize_t httpd_NetSend (httpd_client_t *cl, const uint8_t *p, size_t i_len)
{
tls_session_t *p_tls;
+ ssize_t val;
p_tls = cl->p_tls;
- if( p_tls != NULL)
- return tls_Send( p_tls, p, i_len );
-
- return send( cl->fd, p, i_len, 0 );
+ do
+ val = p_tls ? tls_Send( p_tls, p, i_len )
+ : send (cl->fd, p, i_len, 0);
+ while (val == -1 && errno == EINTR);
+ return val;
}
{
*p2++ = '\0';
}
- if( !strncasecmp( p, "rtsp:", 5 ) )
- {
- /* for rtsp url, you have rtsp://localhost:port/path */
- p += 5;
- while( *p == '/' ) p++;
- while( *p && *p != '/' ) p++;
+ if( !strncasecmp( p, ( cl->query.i_proto
+ == HTTPD_PROTO_HTTP ) ? "http" : "rtsp", 4 )
+ && p[4 + !!strchr( "sS", p[4] )] == ':' )
+ { /* Skip hier-part of URL (if present) */
+ p = strchr( p, ':' ) + 1; /* skip URI scheme */
+ if( !strncmp( p, "//", 2 ) ) /* skip authority */
+ { /* see RFC3986 ยง3.2 */
+ p += 2;
+ while( *p && !strchr( "/?#", *p ) ) p++;
+ }
}
cl->query.psz_url = strdup( p );
if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
#if defined( WIN32 ) || defined( UNDER_CE )
if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
#else
- if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
+ if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
#endif
{
if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
#if defined( WIN32 ) || defined( UNDER_CE )
if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
#else
- if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
+ if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
#endif
{
/* error */
}
}
-static void httpd_HostThread( httpd_host_t *host )
+static void* httpd_HostThread( vlc_object_t *p_this )
{
+ httpd_host_t *host = (httpd_host_t *)p_this;
tls_session_t *p_tls = NULL;
counter_t *p_total_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
counter_t *p_active_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
- int evfd;
- bool b_die;
-
-retry:
- vlc_object_lock( host );
- evfd = vlc_object_waitpipe( VLC_OBJECT( host ) );
- b_die = !vlc_object_alive( host );
- vlc_object_unlock( host );
+ int canc = vlc_savecancel ();
+ int evfd = vlc_object_waitpipe( VLC_OBJECT( host ) );
- while( !b_die )
+ for( ;; )
{
- if( host->i_url <= 0 )
- {
- /* 0.2s (FIXME: use a condition variable) */
- msleep( 200000 );
- goto retry;
- }
-
/* prepare a new TLS session */
if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
p_tls = tls_ServerSessionPrepare( host->p_tls );
/* add all socket that should be read/write and close dead connection */
vlc_mutex_lock( &host->lock );
+ while( host->i_url <= 0 && host->i_ref > 0 )
+ vlc_cond_wait( &host->wait, &host->lock );
+
mtime_t now = mdate();
bool b_low_delay = false;
}
if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
- ( ( cl->answer.i_version == 0 && b_keepalive ) ||
- ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
+ ( ( cl->query.i_version == 0 && b_keepalive ) ||
+ ( cl->query.i_version == 1 && !b_connection ) ) ) ||
( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
!b_query && !b_connection ) )
{
continue;
}
- vlc_object_lock( host );
if( ufd[nfd - 1].revents )
- {
- b_die = !vlc_object_alive( host );
- if( !b_die )
- vlc_object_wait( host );
- }
- vlc_object_unlock( host );
+ break;
/* Handle client sockets */
vlc_mutex_lock( &host->lock );
now = mdate();
+ nfd = host->nfd;
for( int i_client = 0; i_client < host->i_client; i_client++ )
{
httpd_client_t *cl = host->client[i_client];
- const struct pollfd *pufd = &ufd[host->nfd + i_client];
+ const struct pollfd *pufd = &ufd[nfd];
assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
if( cl->fd != pufd->fd )
continue; // we were not waiting for this client
+ ++nfd;
if( pufd->revents == 0 )
continue; // no event received
stats_CounterClean( p_total_counter );
if( p_active_counter )
stats_CounterClean( p_active_counter );
-}
-
-#else /* ENABLE_HTTPD */
-
-/* We just define an empty wrapper */
-httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, const char *b,
- int c,
- const char *e, const char *f,
- const char *g, const char* h)
-{
- msg_Err( a, "HTTP daemon support is disabled" );
- return NULL;
-}
-
-httpd_host_t *httpd_HostNew( vlc_object_t *a, const char *b,
- int c )
-{
- msg_Err( a, "HTTP daemon support is disabled" );
- return NULL;
-}
-
-void httpd_HostDelete( httpd_host_t *a )
-{
-}
-
-httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
- const char *psz_user, const char *psz_password,
- const vlc_acl_t *p_acl )
-{
- return NULL;
-}
-
-httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
- const char *psz_user, const char *psz_password,
- const vlc_acl_t *p_acl )
-{
- return NULL;
-}
-
-int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
- httpd_callback_sys_t *d )
-{
- return 0;
-}
-
-void httpd_UrlDelete( httpd_url_t *a )
-{
-}
-
-char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
-{
- return NULL;
-}
-
-char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
-{
+ vlc_restorecancel (canc);
return NULL;
}
-
-void httpd_ClientModeStream( httpd_client_t *a )
-{
-}
-
-void httpd_ClientModeBidir( httpd_client_t *a )
-{
-}
-
-httpd_file_sys_t *httpd_FileDelete( httpd_file_t *file )
-{
- return NULL;
-}
-
-httpd_file_t *httpd_FileNew( httpd_host_t *host,
- const char *psz_url, const char *psz_mime,
- const char *psz_user, const char *psz_password,
- const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
- httpd_file_sys_t *p_sys )
-{
- return NULL;
-}
-
-httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
- const char *psz_user,
- const char *psz_password,
- const vlc_acl_t *p_acl,
- httpd_handler_callback_t pf_fill,
- httpd_handler_sys_t *p_sys )
-{
- return NULL;
-}
-
-httpd_handler_sys_t *httpd_HandlerDelete( httpd_handler_t *handler )
-{
- return NULL;
-}
-
-void httpd_RedirectDelete( httpd_redirect_t *a )
-{
-}
-
-httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
- const char *psz_url_src )
-{
- return NULL;
-}
-
-void httpd_StreamDelete( httpd_stream_t *a )
-{
-}
-
-int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
-{
- return 0;
-}
-
-int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
-{
- return 0;
-}
-
-httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
- const char *psz_url, const char *psz_mime,
- const char *psz_user, const char *psz_password,
- const vlc_acl_t *p_acl )
-{
- return NULL;
-}
-
-void httpd_MsgInit ( httpd_message_t *a )
-{
-}
-
-void httpd_MsgAdd ( httpd_message_t *a, const char *b, const char *c, ... )
-{
-}
-
-const char *httpd_MsgGet( const httpd_message_t *msg, const char *name )
-{
- return "";
-}
-
-void httpd_MsgClean( httpd_message_t *a )
-{
-}
-
-#endif /* ENABLE_HTTPD */