]> git.sesse.net Git - vlc/blobdiff - src/misc/httpd.c
* httpd: Do not close the connection on timeout for rtsp.
[vlc] / src / misc / httpd.c
index 9ccc76f023bbbbaf030cd0c9dd3346ffcca17854..f08bc498213c65a750fae1de0b1198ac5ac57cd7 100644 (file)
 #include <stdlib.h>
 #include <vlc/vlc.h>
 
+#ifdef ENABLE_HTTPD
+
 #include "vlc_httpd.h"
 #include "network.h"
 
 #include <string.h>
 #include <errno.h>
+
 #ifdef HAVE_UNISTD_H
 #   include <unistd.h>
 #endif
-#include <fcntl.h>
+
+#ifdef HAVE_FCNTL_H
+#   include <fcntl.h>
+#endif
 
 #if defined( UNDER_CE )
 #   include <winsock.h>
 static const struct in6_addr in6addr_any = {{IN6ADDR_ANY_INIT}};
 #endif
 
+#ifndef PF_INET
+#    define PF_INET AF_INET                                          /* BeOS */
+#endif
+
 #if 0
 typedef struct httpd_t          httpd_t;
 
@@ -367,6 +377,7 @@ static struct
     { ".jpg",   "image/jpeg" },
     { ".jpeg",  "image/jpeg" },
     { ".png",   "image/png" },
+    { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
 
     /* media mime */
     { ".avi",   "video/avi" },
@@ -1003,7 +1014,7 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
                 const struct sockaddr_in6 *p_hsock, *p_sock;
 
                 p_hsock = (const struct sockaddr_in6 *)&httpd->host[i]->sock;
-                p_sock = (const struct sockaddr_in6 *)res->ai_addr;
+                p_sock = (const struct sockaddr_in6 *)ptr->ai_addr;
 
                 if( memcmp( &p_hsock->sin6_addr, &in6addr_any,
                             sizeof( struct in6_addr ) ) &&
@@ -1012,7 +1023,7 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
                                       sizeof( struct in6_addr ) ) ) )
                     continue; /* does not match */
             }
-            else if( res->ai_family == PF_INET6 )
+            else if( ptr->ai_family == PF_INET6 )
                 continue;
             else
 #endif
@@ -1021,14 +1032,14 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
                 const struct sockaddr_in *p_hsock, *p_sock;
 
                 p_hsock = (const struct sockaddr_in *)&httpd->host[i]->sock;
-                p_sock = (const struct sockaddr_in *)res->ai_addr;
+                p_sock = (const struct sockaddr_in *)ptr->ai_addr;
 
                 if( p_hsock->sin_addr.s_addr != INADDR_ANY &&
                     ( p_sock->sin_family != AF_INET ||
                       p_hsock->sin_addr.s_addr != p_sock->sin_addr.s_addr ) )
                     continue; /* does not match */
             }
-            else if( res->ai_family == PF_INET )
+            else if( ptr->ai_family == PF_INET )
                 continue;
             else
             {
@@ -1049,7 +1060,7 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
         }
 
         /* create the listening socket */
-        fd = socket( res->ai_family, res->ai_socktype, res->ai_protocol );
+        fd = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
         if( fd == -1 )
             continue;
 
@@ -1064,7 +1075,7 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
         }
 
         /* bind it */
-        if( bind( fd, res->ai_addr, res->ai_addrlen ) )
+        if( bind( fd, ptr->ai_addr, ptr->ai_addrlen ) )
         {
             msg_Err( p_this, "cannot bind socket" );
             goto socket_error;
@@ -1101,17 +1112,19 @@ httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host, int i_port )
             goto socket_error;
         }
 
-        continue;
+        break; // success
 
 socket_error:
-        close( fd );
+        net_Close( fd );
         fd = -1;
     }
 
-    freeaddrinfo( res );
 
     if( fd == -1 )
+    {
+        freeaddrinfo( res );
         goto error;
+    }
 
     /* create the new host */
     host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
@@ -1120,13 +1133,15 @@ socket_error:
     host->i_ref = 1;
     host->fd = fd;
 
-    memcpy( &host->sock, res->ai_addr, res->ai_addrlen );
-    host->i_sock_size = res->ai_addrlen;
+    memcpy( &host->sock, ptr->ai_addr, ptr->ai_addrlen );
+    host->i_sock_size = ptr->ai_addrlen;
     host->i_url     = 0;
     host->url       = NULL;
     host->i_client  = 0;
     host->client    = NULL;
-    
+
+    freeaddrinfo( res );
+
     /* create the thread */
     if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
@@ -1420,7 +1435,7 @@ static void httpd_ClientInit( httpd_client_t *cl )
 {
     cl->i_state = HTTPD_CLIENT_RECEIVING;
     cl->i_activity_date = mdate();
-    cl->i_activity_timeout = 50000000;
+    cl->i_activity_timeout = I64C(10000000);
     cl->i_buffer_size = 10000;
     cl->i_buffer = 0;
     cl->p_buffer = malloc( cl->i_buffer_size );
@@ -1471,8 +1486,6 @@ char* httpd_ClientIP( httpd_client_t *cl )
         /* FIXME: msg_Err */
         return NULL;
         
-    fprintf( stderr, "ClientIP = %s\n", &sz_ip[1]);
-    
     if( strchr( &sz_ip[1], ':' ) != NULL )
     {
         *sz_ip = '[';
@@ -1480,7 +1493,6 @@ char* httpd_ClientIP( httpd_client_t *cl )
         sz_ip[i++] = ']';
         sz_ip[i] = '\0';
        
-        fprintf( stderr, "ClientIP (with []) = %s\n", sz_ip);
         return strdup( sz_ip );
     }
     
@@ -1831,6 +1843,10 @@ static void httpd_ClientRecv( httpd_client_t *cl )
     }
     cl->i_activity_date = mdate();
 
+    /* XXX: for QT I have to disable timeout. Try to find why */
+    if( cl->query.i_proto == HTTPD_PROTO_RTSP )
+        cl->i_activity_timeout = 0;
+
     /* Debugging only */
     if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
     {
@@ -1992,7 +2008,8 @@ static void httpd_HostThread( httpd_host_t *host )
 
             if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
                 ( cl->i_state == HTTPD_CLIENT_DEAD ||
-                  cl->i_activity_date + cl->i_activity_timeout < mdate() ) ) )
+                  ( cl->i_activity_timeout > 0 &&
+                    cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
             {
                 char *ip;
 
@@ -2220,7 +2237,7 @@ static void httpd_HostThread( httpd_host_t *host )
                             p += sprintf( p, "<title>Error 404</title>\n" );
                             p += sprintf( p, "</head>\n" );
                             p += sprintf( p, "<body>\n" );
-                            p += sprintf( p, "<h1><center> 404 Ressource not found(%s)</center></h1>\n", query->psz_url );
+                            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" );
@@ -2351,7 +2368,7 @@ static void httpd_HostThread( httpd_host_t *host )
             int     fd;
 
             fd = accept( host->fd, (struct sockaddr *)&sock, &i_sock_size );
-            if( fd > 0 )
+            if( fd >= 0 )
             {
                 char *ip;
 
@@ -2457,3 +2474,45 @@ static int GetAddrPort( const struct sockaddr_storage *p_ss )
     
     return ntohs( i_port );
 }
+
+#else /* ENABLE_HTTPD */
+
+/* We just define an empty wrapper */
+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; }
+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; }
+void httpd_ClientModeStream( httpd_client_t *a ){}
+void httpd_ClientModeBidir( httpd_client_t *a ){}
+
+void httpd_FileDelete( httpd_file_t *a ){}
+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; }
+
+void httpd_RedirectDelete( httpd_redirect_t *a ){}
+httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
+                                     char *b, char *c ){ return 0; }
+
+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 *a, char *b, char *c,
+                                 char *d, char *e ){ return 0; }
+
+void httpd_MsgInit ( httpd_message_t *a ){}
+void httpd_MsgAdd  ( httpd_message_t *a, char *b, char *c, ... ){}
+char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
+void httpd_MsgClean( httpd_message_t *a ){}
+
+#endif /* ENABLE_HTTPD */