]> git.sesse.net Git - vlc/blobdiff - src/network/httpd.c
httpd: full URL is also valid for HTTP (fixes #2320)
[vlc] / src / network / httpd.c
index 6ddd9dab8de4f4a7cb13bb01fbb34ca0476d490a..4dce0e8ec2f758e78c76ae8fd5c4e99018ff29dc 100644 (file)
 # 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>
@@ -189,7 +187,7 @@ struct httpd_client_t
 /*****************************************************************************
  * Various functions
  *****************************************************************************/
-static struct
+static const struct
 {
     const char psz_ext[8];
     const char *psz_mime;
@@ -394,7 +392,6 @@ httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
                     httpd_message_t *answer, const httpd_message_t *query )
 {
     httpd_file_t *file = (httpd_file_t*)p_sys;
-    uint8_t *psz_args = query->psz_args;
     uint8_t **pp_body, *p_body;
     const char *psz_connection;
     int *pi_body, i_body;
@@ -431,6 +428,7 @@ httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
         /* msg_Warn not supported */
     }
 
+    uint8_t *psz_args = query->psz_args;
     file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
 
     if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
@@ -520,7 +518,6 @@ httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
                        httpd_message_t *answer, const httpd_message_t *query )
 {
     httpd_handler_t *handler = (httpd_handler_t*)p_sys;
-    uint8_t *psz_args = query->psz_args;
     char psz_remote_addr[NI_MAXNUMERICHOST];
 
     if( answer == NULL || query == NULL )
@@ -536,6 +533,7 @@ httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
     if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
         *psz_remote_addr = '\0';
 
+    uint8_t *psz_args = query->psz_args;
     handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
                       query->i_type, query->p_body, query->i_body,
                       psz_remote_addr, NULL,
@@ -960,7 +958,7 @@ void httpd_StreamDelete( httpd_stream_t *stream )
 /*****************************************************************************
  * 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,
@@ -989,10 +987,7 @@ httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
 
     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 );
@@ -1018,7 +1013,7 @@ httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
 
         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 );
     }
 
@@ -1069,7 +1064,7 @@ httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
 
     /* 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;
@@ -1123,6 +1118,7 @@ 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 );
@@ -1310,7 +1306,7 @@ void httpd_UrlDelete( httpd_url_t *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;
@@ -1334,7 +1330,7 @@ void httpd_MsgInit( httpd_message_t *msg )
     msg->p_body        = NULL;
 }
 
-void httpd_MsgClean( httpd_message_t *msg )
+static void httpd_MsgClean( httpd_message_t *msg )
 {
     int i;
 
@@ -1457,26 +1453,32 @@ static httpd_client_t *httpd_ClientNew( int fd, tls_session_t *p_tls, mtime_t no
     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;
 }
 
 
@@ -1741,12 +1743,16 @@ static void httpd_ClientRecv( httpd_client_t *cl )
                         {
                             *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, '?' ) )  )
@@ -1830,7 +1836,7 @@ static void httpd_ClientRecv( httpd_client_t *cl )
 #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 )
@@ -1970,7 +1976,7 @@ static void httpd_ClientSend( httpd_client_t *cl )
 #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 */
@@ -2016,13 +2022,15 @@ static void httpd_ClientTlsHsOut( httpd_client_t *cl )
     }
 }
 
-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;
+    int canc = vlc_savecancel ();
 
 retry:
     vlc_object_lock( host );
@@ -2346,8 +2354,8 @@ retry:
                     }
 
                     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 ) )
                     {
@@ -2453,22 +2461,24 @@ retry:
         {
             b_die = !vlc_object_alive( host );
             if( !b_die )
-                b_die = vlc_object_wait( host );
+                vlc_object_wait( host );
         }
         vlc_object_unlock( host );
 
         /* 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
 
@@ -2564,149 +2574,6 @@ retry:
         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 )
-{
-    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 )
-{
+    vlc_restorecancel (canc);
     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 */