]> git.sesse.net Git - vlc/blobdiff - src/misc/httpd.c
* : make jump hotkeys time interval user configurable
[vlc] / src / misc / httpd.c
index 91649d91c219593720a6b967790ef936ba745aec..f9ec0c5861c151e1483ea90e75453cc2dde93a3e 100644 (file)
@@ -30,6 +30,7 @@
 #include "vlc_httpd.h"
 #include "network.h"
 #include "vlc_tls.h"
+#include "vlc_acl.h"
 
 #include <string.h>
 #include <errno.h>
 #   endif
 #endif
 
+#if defined( WIN32 )
+/* We need HUGE buffer otherwise TCP throughput is very limited */
+#define HTTPD_CL_BUFSIZE 1000000
+#else
+#define HTTPD_CL_BUFSIZE 10000
+#endif
+
 #if 0
 typedef struct httpd_t          httpd_t;
 
@@ -234,11 +242,10 @@ struct httpd_url_t
 
     vlc_mutex_t lock;
 
-    char    *psz_url;
-    char    *psz_user;
-    char    *psz_password;
-    char    **ppsz_hosts;
-    int     i_hosts;
+    char      *psz_url;
+    char      *psz_user;
+    char      *psz_password;
+    vlc_acl_t *p_acl;
 
     struct
     {
@@ -364,8 +371,8 @@ static void b64_decode( char *dest, char *src )
 
 static struct
 {
-    char *psz_ext;
-    char *psz_mime;
+    const char *psz_ext;
+    const char *psz_mime;
 } http_mime[] =
 {
     { ".htm",   "text/html" },
@@ -409,7 +416,7 @@ static struct
     /* end */
     { NULL,     NULL }
 };
-static char *httpd_MimeFromUrl( char *psz_url )
+static const char *httpd_MimeFromUrl( const char *psz_url )
 {
 
     char *psz_ext;
@@ -431,7 +438,7 @@ static char *httpd_MimeFromUrl( char *psz_url )
 }
 
 /*****************************************************************************
- * High Level Funtions: httpd_file_t
+ * High Level Functions: httpd_file_t
  *****************************************************************************/
 struct httpd_file_t
 {
@@ -449,6 +456,9 @@ struct httpd_file_t
 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, 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;
+    int *pi_body, i_body;
 
     if( answer == NULL || query == NULL )
     {
@@ -466,15 +476,30 @@ static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
 
     if( query->i_type != HTTPD_MSG_HEAD )
     {
-        char *psz_args = query->psz_args;
-        if( query->i_type == HTTPD_MSG_POST )
-        {
-            /* Check that */
-            psz_args = query->p_body;
-        }
-        file->pf_fill( file->p_sys, file, psz_args, &answer->p_body,
-                       &answer->i_body );
+        pp_body = &answer->p_body;
+        pi_body = &answer->i_body;
+    }
+    else
+    {
+        /* The file still needs to be executed. */
+        p_body = NULL;
+        i_body = 0;
+        pp_body = &p_body;
+        pi_body = &i_body;
+    }
+
+    if( query->i_type == HTTPD_MSG_POST )
+    {
+        /* msg_Warn not supported */
+    }
+
+    file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
+
+    if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
+    {
+        free( p_body );
     }
+
     /* We respect client request */
     if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
     {
@@ -489,16 +514,15 @@ static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
 
 
 httpd_file_t *httpd_FileNew( httpd_host_t *host,
-                             char *psz_url, char *psz_mime,
-                             char *psz_user, char *psz_password,
-                             char **ppsz_hosts, int i_hosts,
-                             httpd_file_callback_t pf_fill,
+                             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 )
 {
     httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
 
     if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
-                                          psz_password, ppsz_hosts, i_hosts )
+                                          psz_password, p_acl )
         ) == NULL )
     {
         free( file );
@@ -539,7 +563,165 @@ void httpd_FileDelete( httpd_file_t *file )
 }
 
 /*****************************************************************************
- * High Level Funtions: httpd_redirect_t
+ * High Level Functions: httpd_handler_t (for CGIs)
+ *****************************************************************************/
+struct httpd_handler_t
+{
+    httpd_url_t *url;
+
+    httpd_handler_callback_t pf_fill;
+    httpd_handler_sys_t      *p_sys;
+
+};
+
+static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
+{
+    httpd_handler_t *handler = (httpd_handler_t*)p_sys;
+    uint8_t *psz_args = query->psz_args;
+    char psz_remote_addr[100];
+
+    if( answer == NULL || query == NULL )
+    {
+        return VLC_SUCCESS;
+    }
+    answer->i_proto  = HTTPD_PROTO_NONE;
+    answer->i_type   = HTTPD_MSG_ANSWER;
+
+    /* We do it ourselves, thanks */
+    answer->i_status = 0;
+    answer->psz_status = NULL;
+
+    switch( cl->sock.ss_family )
+    {
+#ifdef HAVE_INET_PTON
+    case AF_INET:
+        inet_ntop( cl->sock.ss_family,
+                   &((struct sockaddr_in *)&cl->sock)->sin_addr,
+                   psz_remote_addr, sizeof(psz_remote_addr) );
+        break;
+    case AF_INET6:
+        inet_ntop( cl->sock.ss_family,
+                   &((struct sockaddr_in6 *)&cl->sock)->sin6_addr,
+                   psz_remote_addr, sizeof(psz_remote_addr) );
+        break;
+#else
+    case AF_INET:
+    {
+        char *psz_tmp = inet_ntoa( ((struct sockaddr_in *)&cl->sock)->sin_addr );
+        strncpy( psz_remote_addr, psz_tmp, sizeof(psz_remote_addr) );
+        break;
+    }
+#endif
+    default:
+        psz_remote_addr[0] = '\0';
+    }
+
+    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,
+                      &answer->p_body, &answer->i_body );
+
+    if( query->i_type == HTTPD_MSG_HEAD )
+    {
+        char *p = answer->p_body;
+        while ( (p = strchr( p, '\r' )) != NULL )
+        {
+            if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
+                 && p[3] && p[3] == '\n' )
+            {
+                break;
+            }
+        }
+        if( p != NULL )
+        {
+            p[4] = '\0';
+            answer->i_body = strlen(answer->p_body) + 1;
+            answer->p_body = realloc( answer->p_body, answer->i_body );
+        }
+    }
+
+    if( strncmp( answer->p_body, "HTTP/1.", 7 ) )
+    {
+        int i_status, i_headers;
+        char *psz_headers, *psz_new, *psz_status;
+        char psz_code[12];
+        if( !strncmp( answer->p_body, "Status: ", 8 ) )
+        {
+            /* Apache-style */
+            i_status = strtol( &answer->p_body[8], &psz_headers, 0 );
+            if( *psz_headers ) psz_headers++;
+            if( *psz_headers ) psz_headers++;
+            i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
+        }
+        else
+        {
+            i_status = 200;
+            psz_headers = answer->p_body;
+            i_headers = answer->i_body;
+        }
+        switch( i_status )
+        {
+        case 200:
+            psz_status = "OK";
+            break;
+        case 401:
+            psz_status = "Unauthorized";
+            break;
+        default:
+            psz_status = "Undefined";
+            break;
+        }
+        snprintf( psz_code, sizeof(psz_code), "%d", i_status );
+        answer->i_body = sizeof("HTTP/1.0  \r\n") + strlen(psz_code)
+                           + strlen(psz_status) + i_headers - 1;
+        psz_new = malloc( answer->i_body + 1);
+        sprintf( psz_new, "HTTP/1.0 %s %s\r\n", psz_code, psz_status );
+        memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
+        free( answer->p_body );
+        answer->p_body = psz_new;
+    }
+
+    return VLC_SUCCESS;
+}
+
+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 )
+{
+    httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
+
+    if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
+                                             psz_password, p_acl )
+        ) == NULL )
+    {
+        free( handler );
+        return NULL;
+    }
+
+    handler->pf_fill = pf_fill;
+    handler->p_sys   = p_sys;
+
+    httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
+                    (httpd_callback_sys_t*)handler );
+    httpd_UrlCatch( handler->url, HTTPD_MSG_GET,  httpd_HandlerCallBack,
+                    (httpd_callback_sys_t*)handler );
+    httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
+                    (httpd_callback_sys_t*)handler );
+
+    return handler;
+}
+
+void httpd_HandlerDelete( httpd_handler_t *handler )
+{
+    httpd_UrlDelete( handler->url );
+    free( handler );
+}
+
+/*****************************************************************************
+ * High Level Functions: httpd_redirect_t
  *****************************************************************************/
 struct httpd_redirect_t
 {
@@ -565,16 +747,22 @@ static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
     answer->psz_status = strdup( "Moved Permanently" );
 
     p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
-    p += sprintf( p, "<html>\n" );
-    p += sprintf( p, "<head>\n" );
-    p += sprintf( p, "<title>Redirection</title>\n" );
-    p += sprintf( p, "</head>\n" );
-    p += sprintf( p, "<body>\n" );
-    p += sprintf( p, "<h1><center>You should be <a href=\"%s\">redirected</a></center></h1>\n", rdir->psz_dst );
-    p += sprintf( p, "<hr />\n" );
-    p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
-    p += sprintf( p, "</body>\n" );
-    p += sprintf( p, "</html>\n" );
+    p += sprintf( (char *)p,
+        "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
+        "<!DOCTYPE html PUBLIC \"-//W3C//DTD  XHTML 1.0 Strict//EN\" "
+        "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
+        "<html>\n"
+        "<head>\n"
+        "<title>Redirection</title>\n"
+        "</head>\n"
+        "<body>\n"
+        "<h1>You should be " 
+        "<a href=\"%s\">redirected</a></h1>\n"
+        "<hr />\n"
+        "<p><a href=\"http://www.videolan.org\">VideoLAN</a>\n</p>"
+        "<hr />\n"
+        "</body>\n"
+        "</html>\n", rdir->psz_dst );
     answer->i_body = p - answer->p_body;
 
     /* XXX check if it's ok or we need to set an absolute url */
@@ -585,13 +773,12 @@ static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
     return VLC_SUCCESS;
 }
 
-httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, char *psz_url_dst,
-                                     char *psz_url_src )
+httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
+                                     const char *psz_url_src )
 {
     httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
 
-    if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL,
-                                           NULL, 0 ) ) )
+    if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
     {
         free( rdir );
         return NULL;
@@ -674,9 +861,9 @@ static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
 
         i_pos   = answer->i_body_offset % stream->i_buffer_size;
         i_write = stream->i_buffer_pos - answer->i_body_offset;
-        if( i_write > 10000 )
+        if( i_write > HTTPD_CL_BUFSIZE )
         {
-            i_write = 10000;
+            i_write = HTTPD_CL_BUFSIZE;
         }
         else if( i_write <= 0 )
         {
@@ -765,14 +952,14 @@ static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
 }
 
 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
-                                 char *psz_url, char *psz_mime,
-                                 char *psz_user, char *psz_password,
-                                 char **ppsz_hosts, int i_hosts )
+                                 const char *psz_url, const char *psz_mime,
+                                 const char *psz_user, const char *psz_password,
+                                 const vlc_acl_t *p_acl )
 {
     httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
 
     if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
-                                            psz_password, ppsz_hosts, i_hosts )
+                                            psz_password, p_acl )
         ) == NULL )
     {
         free( stream );
@@ -897,6 +1084,9 @@ httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
     vlc_value_t  lockval;
     int i;
 
+    if( psz_hostname == NULL )
+        psz_hostname = "";
+
     psz_host = strdup( psz_hostname );
     if( psz_host == NULL )
     {
@@ -1084,10 +1274,10 @@ void httpd_HostDelete( httpd_host_t *host )
     vlc_mutex_destroy( &host->lock );
     vlc_object_destroy( host );
 
+    vlc_object_release( httpd );
     if( httpd->i_host <= 0 )
     {
         msg_Info( httpd, "httpd doesn't reference any host, deleting" );
-        vlc_object_release( httpd );
         vlc_object_detach( httpd );
         vlc_object_destroy( httpd );
     }
@@ -1095,10 +1285,9 @@ void httpd_HostDelete( httpd_host_t *host )
 }
 
 /* register a new url */
-static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
-                                         char *psz_user, char *psz_password,
-                                         char **ppsz_hosts, int i_hosts,
-                                         vlc_bool_t b_check )
+static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
+                                         const char *psz_user, const char *psz_password,
+                                         const vlc_acl_t *p_acl, vlc_bool_t b_check )
 {
     httpd_url_t *url;
     int         i;
@@ -1125,12 +1314,7 @@ static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
     url->psz_url = strdup( psz_url );
     url->psz_user = strdup( psz_user ? psz_user : "" );
     url->psz_password = strdup( psz_password ? psz_password : "" );
-    url->i_hosts = 0;
-    url->ppsz_hosts = NULL;
-    for( i = 0; i < i_hosts; i++ )
-    {
-        TAB_APPEND( url->i_hosts, url->ppsz_hosts, strdup(ppsz_hosts[i]) );
-    }
+    url->p_acl = ACL_Duplicate( host, p_acl );
     for( i = 0; i < HTTPD_MSG_MAX; i++ )
     {
         url->catch[i].cb = NULL;
@@ -1143,20 +1327,20 @@ static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
     return url;
 }
 
-httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
-                           char *psz_user, char *psz_password,
-                           char **ppsz_hosts, int i_hosts )
+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 httpd_UrlNewPrivate( host, psz_url, psz_user,
-                                psz_password, ppsz_hosts, i_hosts, VLC_FALSE );
+                                psz_password, p_acl, VLC_FALSE );
 }
 
-httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
-                                 char *psz_user, char *psz_password,
-                                 char **ppsz_hosts, int i_hosts )
+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 httpd_UrlNewPrivate( host, psz_url, psz_user,
-                                psz_password, ppsz_hosts, i_hosts, VLC_TRUE );
+                                psz_password, p_acl, VLC_TRUE );
 }
 
 /* register callback on a url */
@@ -1185,10 +1369,7 @@ void httpd_UrlDelete( httpd_url_t *url )
     free( url->psz_url );
     free( url->psz_user );
     free( url->psz_password );
-    for( i = 0; i < url->i_hosts; i++ )
-    {
-        TAB_REMOVE( url->i_hosts, url->ppsz_hosts, url->ppsz_hosts[0] );
-    }
+    ACL_Destroy( url->p_acl );
 
     for( i = 0; i < host->i_client; i++ )
     {
@@ -1311,7 +1492,7 @@ static void httpd_ClientInit( httpd_client_t *cl )
     cl->i_state = HTTPD_CLIENT_RECEIVING;
     cl->i_activity_date = mdate();
     cl->i_activity_timeout = I64C(10000000);
-    cl->i_buffer_size = 10000;
+    cl->i_buffer_size = HTTPD_CL_BUFSIZE;
     cl->i_buffer = 0;
     cl->p_buffer = malloc( cl->i_buffer_size );
     cl->i_mode   = HTTPD_CLIENT_FILE;
@@ -1331,29 +1512,14 @@ void httpd_ClientModeBidir( httpd_client_t *cl )
     cl->i_mode   = HTTPD_CLIENT_BIDIR;
 }
 
-char* httpd_ClientIP( httpd_client_t *cl )
+char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
 {
-    int i;
-    char sz_ip[NI_MAXNUMERICHOST + 2];
-
-    i = vlc_getnameinfo( (const struct sockaddr *)&cl->sock, cl->i_sock_size,
-                         sz_ip+1, NI_MAXNUMERICHOST, NULL, NI_NUMERICHOST );
-
-    if( i != 0 )
-        return NULL;
-
-    /* semi-colon in address => must add bracket for HTTP */        
-    if( strchr( sz_ip + 1, ':' ) != NULL )
-    {
-        sz_ip[0] = '[';
-        i = strlen( sz_ip );
-        sz_ip[i++] = ']';
-        sz_ip[i] = '\0';
+    return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
+}
 
-        return strdup(sz_ip);
-    }
-    
-    return strdup(sz_ip + 1);
+char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
+{
+    return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
 }
 
 static void httpd_ClientClean( httpd_client_t *cl )
@@ -1394,7 +1560,7 @@ static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
 }
 
 
-static int httpd_NetRecv( httpd_client_t *cl, char *p, int i_len )
+static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
 {
     tls_session_t *p_tls;
     
@@ -1406,7 +1572,7 @@ static int httpd_NetRecv( httpd_client_t *cl, char *p, int i_len )
 }
 
 
-static int httpd_NetSend( httpd_client_t *cl, const char *p, int i_len )
+static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
 {
     tls_session_t *p_tls;
 
@@ -1424,7 +1590,7 @@ static void httpd_ClientRecv( httpd_client_t *cl )
 
     if( cl->query.i_proto == HTTPD_PROTO_NONE )
     {
-        /* enought to see if it's rtp over rtsp or RTSP/HTTP */
+        /* enough to see if it's rtp over rtsp or RTSP/HTTP */
         i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
                                4 - cl->i_buffer );
         if( i_len > 0 )
@@ -1447,19 +1613,19 @@ static void httpd_ClientRecv( httpd_client_t *cl )
 
                 cl->i_buffer      = 0;
             }
-            else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
+            else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
             {
                 cl->query.i_proto = HTTPD_PROTO_HTTP;
                 cl->query.i_type  = HTTPD_MSG_ANSWER;
             }
-            else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
+            else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
             {
                 cl->query.i_proto = HTTPD_PROTO_RTSP;
                 cl->query.i_type  = HTTPD_MSG_ANSWER;
             }
-            else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
-                     !strncmp( cl->p_buffer, "HEAD", 4 ) ||
-                     !strncmp( cl->p_buffer, "POST", 4 ) )
+            else if( !memcmp( cl->p_buffer, "GET", 3 ) ||
+                     !memcmp( cl->p_buffer, "HEAD", 4 ) ||
+                     !memcmp( cl->p_buffer, "POST", 4 ) )
             {
                 cl->query.i_proto = HTTPD_PROTO_HTTP;
                 cl->query.i_type  = HTTPD_MSG_NONE;
@@ -1502,8 +1668,8 @@ static void httpd_ClientRecv( httpd_client_t *cl )
                 cl->i_buffer_size += 1024;
                 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
             }
-            if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
-                ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
+            if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
+                ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
             {
                 char *p;
 
@@ -1512,8 +1678,12 @@ static void httpd_ClientRecv( httpd_client_t *cl )
 
                 if( cl->query.i_type == HTTPD_MSG_ANSWER )
                 {
+                    /* FIXME:
+                     * assume strlen( "HTTP/1.x" ) = 8
+                     */
                     cl->query.i_status =
-                        strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
+                        strtol( (char *)&cl->p_buffer[8],
+                                &p, 0 );
                     while( *p == ' ' )
                     {
                         p++;
@@ -1552,10 +1722,10 @@ static void httpd_ClientRecv( httpd_client_t *cl )
 
                     for( i = 0; msg_type[i].name != NULL; i++ )
                     {
-                        if( !strncmp( cl->p_buffer, msg_type[i].name,
+                        if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
                                       strlen( msg_type[i].name ) ) )
                         {
-                            p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
+                            p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
                             cl->query.i_type = msg_type[i].i_type;
                             if( cl->query.i_proto != msg_type[i].i_proto )
                             {
@@ -1568,11 +1738,11 @@ static void httpd_ClientRecv( httpd_client_t *cl )
                     }
                     if( p == NULL )
                     {
-                        if( strstr( cl->p_buffer, "HTTP/1." ) )
+                        if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
                         {
                             cl->query.i_proto = HTTPD_PROTO_HTTP;
                         }
-                        else if( strstr( cl->p_buffer, "RTSP/1." ) )
+                        else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
                         {
                             cl->query.i_proto = HTTPD_PROTO_RTSP;
                         }
@@ -1602,7 +1772,7 @@ static void httpd_ClientRecv( httpd_client_t *cl )
                         if( ( p3 = strchr( cl->query.psz_url, '?' ) )  )
                         {
                             *p3++ = '\0';
-                            cl->query.psz_args = strdup( p3 );
+                            cl->query.psz_args = (uint8_t *)strdup( p3 );
                         }
                         if( p2 )
                         {
@@ -1772,7 +1942,7 @@ static void httpd_ClientSend( httpd_client_t *cl )
             free( cl->p_buffer );
             cl->p_buffer = malloc( i_size );
         }
-        p = cl->p_buffer;
+        p = (char *)cl->p_buffer;
 
         p += sprintf( p, "%s/1.%d %d %s\r\n",
                       cl->answer.i_proto ==  HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
@@ -1794,7 +1964,7 @@ static void httpd_ClientSend( httpd_client_t *cl )
 
     i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
                            cl->i_buffer_size - cl->i_buffer );
-    if( i_len > 0 )
+    if( i_len >= 0 )
     {
         cl->i_activity_date = mdate();
         cl->i_buffer += i_len;
@@ -2038,16 +2208,20 @@ static void httpd_HostThread( httpd_host_t *host )
 
                         p = answer->p_body = malloc( 1000 );
 
-                        p += sprintf( p, "<html>\n" );
-                        p += sprintf( p, "<head>\n" );
-                        p += sprintf( p, "<title>Error 501</title>\n" );
-                        p += sprintf( p, "</head>\n" );
-                        p += sprintf( p, "<body>\n" );
-                        p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
-                        p += sprintf( p, "<hr />\n" );
-                        p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
-                        p += sprintf( p, "</body>\n" );
-                        p += sprintf( p, "</html>\n" );
+                        p += sprintf( (char *)p,
+                            "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
+                            "<!DOCTYPE html PUBLIC \"-//W3C//DTD  XHTML 1.0 Strict//EN\" "
+                            "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
+                            "<html>\n"
+                            "<head>\n"
+                            "<title>Error 501</title>\n"
+                            "</head>\n"
+                            "<body>\n"
+                            "<h1>501 Unimplemented</h1>\n"
+                            "<hr />\n"
+                            "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
+                            "</body>\n"
+                            "</html>\n" );
 
                         answer->i_body = p - answer->p_body;
                         httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
@@ -2071,21 +2245,20 @@ static void httpd_HostThread( httpd_host_t *host )
                         {
                             if( url->catch[i_msg].cb )
                             {
-                                if( answer && url->i_hosts )
+                                if( answer && ( url->p_acl != NULL ) )
                                 {
-                                    char *ip = httpd_ClientIP( cl );
-                                    if( ip != NULL )
+                                    char ip[NI_MAXNUMERICHOST];
+
+                                    if( httpd_ClientIP( cl, ip ) != NULL )
                                     {
-                                        if( net_CheckIP( host, ip,
-                                                         url->ppsz_hosts,
-                                                         url->i_hosts ) <= 0 )
+                                        if( ACL_Check( url->p_acl, ip ) )
                                         {
                                             b_hosts_failed = VLC_TRUE;
-                                            free( ip );
                                             break;
                                         }
-                                        free( ip );
                                     }
+                                    else
+                                        b_hosts_failed = VLC_TRUE;
                                 }
 
                                 if( answer && ( *url->psz_user || *url->psz_password ) )
@@ -2127,6 +2300,14 @@ static void httpd_HostThread( httpd_host_t *host )
 
                                 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
                                 {
+                                    if( answer->i_proto == HTTPD_PROTO_NONE )
+                                    {
+                                        /* Raw answer from a CGI */
+                                        cl->i_buffer = cl->i_buffer_size;
+                                    }
+                                    else
+                                        cl->i_buffer = -1;
+
                                     /* only one url can answer */
                                     answer = NULL;
                                     if( cl->url == NULL )
@@ -2151,32 +2332,41 @@ static void httpd_HostThread( httpd_host_t *host )
                             answer->i_status = 403;
                             answer->psz_status = strdup( "Forbidden" );
 
-                            p += sprintf( p, "<html>\n" );
-                            p += sprintf( p, "<head>\n" );
-                            p += sprintf( p, "<title>Error 403</title>\n" );
-                            p += sprintf( p, "</head>\n" );
-                            p += sprintf( p, "<body>\n" );
-                            p += sprintf( p, "<h1><center> 403 Forbidden (%s)</center></h1>\n", query->psz_url );
-                            p += sprintf( p, "<hr />\n" );
-                            p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
-                            p += sprintf( p, "</body>\n" );
-                            p += sprintf( p, "</html>\n" );
+                            /* FIXME: lots of code duplication */
+                            p += sprintf( (char *)p,
+                                "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
+                                "<!DOCTYPE html PUBLIC \"-//W3C//DTD  XHTML 1.0 Strict//EN\" "
+                                "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
+                                "<html>\n"
+                                "<head>\n"
+                                "<title>Error 403</title>\n"
+                                "</head>\n"
+                                "<body>\n"
+                                "<h1>403 Forbidden (%s)</h1>\n"
+                                "<hr />\n"
+                                "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
+                                "</body>\n"
+                                "</html>\n", query->psz_url );
                         }
                         else if( b_auth_failed )
                         {
                             answer->i_status = 401;
                             answer->psz_status = strdup( "Authorization Required" );
 
-                            p += sprintf( p, "<html>\n" );
-                            p += sprintf( p, "<head>\n" );
-                            p += sprintf( p, "<title>Error 401</title>\n" );
-                            p += sprintf( p, "</head>\n" );
-                            p += sprintf( p, "<body>\n" );
-                            p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
-                            p += sprintf( p, "<hr />\n" );
-                            p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
-                            p += sprintf( p, "</body>\n" );
-                            p += sprintf( p, "</html>\n" );
+                            p += sprintf( (char *)p,
+                                "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
+                                "<!DOCTYPE html PUBLIC \"-//W3C//DTD  XHTML 1.0 Strict//EN\" "
+                                "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
+                                "<html>\n"
+                                "<head>\n"
+                                "<title>Error 401</title>\n"
+                                "</head>\n"
+                                "<body>\n"
+                                "<h1>401 Authorization Required (%s)</h1>\n"
+                                "<hr />\n"
+                                "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
+                                "</body>\n"
+                                "</html>\n", query->psz_url );
                         }
                         else
                         {
@@ -2184,22 +2374,27 @@ static void httpd_HostThread( httpd_host_t *host )
                             answer->i_status = 404;
                             answer->psz_status = strdup( "Not found" );
 
-                            p += sprintf( p, "<html>\n" );
-                            p += sprintf( p, "<head>\n" );
-                            p += sprintf( p, "<title>Error 404</title>\n" );
-                            p += sprintf( p, "</head>\n" );
-                            p += sprintf( p, "<body>\n" );
-                            p += sprintf( p, "<h1><center> 404 Resource not found(%s)</center></h1>\n", query->psz_url );
-                            p += sprintf( p, "<hr />\n" );
-                            p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
-                            p += sprintf( p, "</body>\n" );
-                            p += sprintf( p, "</html>\n" );
+                            p += sprintf( (char *)p,
+                                "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
+                                "<!DOCTYPE html PUBLIC \"-//W3C//DTD  XHTML 1.0 Strict//EN\" "
+                                "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
+                                "<html>\n"
+                                "<head>\n"
+                                "<title>Error 404</title>\n"
+                                "</head>\n"
+                                "<body>\n"
+                                "<h1>404 Resource not found(%s)</h1>\n"
+                                "<hr />\n"
+                                "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
+                                "</body>\n"
+                                "</html>\n", query->psz_url );
                         }
 
                         answer->i_body = p - answer->p_body;
+                        cl->i_buffer = -1;  /* Force the creation of the answer in httpd_ClientSend */
                         httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
                     }
-                    cl->i_buffer = -1;  /* Force the creation of the answer in httpd_ClientSend */
+
                     cl->i_state = HTTPD_CLIENT_SENDING;
                 }
             }
@@ -2321,7 +2516,7 @@ static void httpd_HostThread( httpd_host_t *host )
         {
             if( FD_ISSET( fd, &fds_read ) )
             {
-                int     i_sock_size = sizeof( struct sockaddr_storage );
+                socklen_t i_sock_size = sizeof( struct sockaddr_storage );
                 struct  sockaddr_storage sock;
     
                 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
@@ -2427,15 +2622,28 @@ httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
     msg_Err( a, "HTTP daemon support is disabled" );
     return 0;
 }
-void httpd_HostDelete( httpd_host_t *a ){}
-httpd_url_t *httpd_UrlNew( httpd_host_t *a, char *b ){ return 0; }
-httpd_url_t *httpd_UrlNewUnique( httpd_host_t *a, char *b, char *c,
-                                 char *d ){ return 0; }
+void httpd_HostDelete( httpd_host_t *a )
+{
+}
+httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
+                           char *psz_user, char *psz_password,
+                           const vlc_acl_t *p_acl )
+{
+    return NULL;
+}
+httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
+                                 char *psz_user, 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( httpd_client_t *a ){ return 0; }
+char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip ) { return NULL; }
+char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip ) { return NULL; }
+
 void httpd_ClientModeStream( httpd_client_t *a ){}
 void httpd_ClientModeBidir( httpd_client_t *a ){}
 
@@ -2444,6 +2652,17 @@ httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
                              char *e, httpd_file_callback_t f,
                              httpd_file_sys_t *g ){ return 0; }
 
+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;
+}
+void httpd_HandlerDelete( httpd_handler_t *handler ) {}
+
 void httpd_RedirectDelete( httpd_redirect_t *a ){}
 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
                                      char *b, char *c ){ return 0; }