1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Remi Denis-Courmont <courmisch@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
30 #include "vlc_httpd.h"
45 #if defined( UNDER_CE )
47 #elif defined( WIN32 )
48 # include <winsock2.h>
49 # include <ws2tcpip.h>
51 # include <netdb.h> /* hostent ... */
52 # include <sys/socket.h>
53 # include <netinet/in.h>
54 # ifdef HAVE_ARPA_INET_H
55 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
60 static const struct in6_addr in6addr_any = {{IN6ADDR_ANY_INIT}};
64 # define PF_INET AF_INET /* BeOS */
68 typedef struct httpd_t httpd_t;
70 typedef struct httpd_host_t httpd_host_t;
71 typedef struct httpd_url_t httpd_url_t;
72 typedef struct httpd_client_t httpd_client_t;
81 /* channel communication */
97 /* just to track the count of MSG */
110 httpd_client_t *cl; /* NULL if not throught a connection e vlc internal */
122 char *psz_args; /* FIXME find a clean way to handle GET(psz_args) and POST(body) through the same code */
124 /* for rtp over rtsp */
134 int64_t i_body_offset;
140 typedef struct httpd_callback_sys_t httpd_callback_sys_t;
141 /* answer could be null, int this case no answer is requested */
142 typedef int (*httpd_callback_t)( httpd_callback_sys_t *, httpd_client_t *, httpd_message_t *answer, httpd_message_t *query );
145 /* create a new host */
146 httpd_host_t *httpd_HostNew( vlc_object_t *, char *psz_host, int i_port );
148 void httpd_HostDelete( httpd_host_t * );
150 /* register a new url */
151 httpd_url_t *httpd_UrlNew( httpd_host_t *, char *psz_url );
152 /* register callback on a url */
153 int httpd_UrlCatch( httpd_url_t *, int i_msg,
154 httpd_callback_t, httpd_callback_sys_t * );
156 void httpd_UrlDelete( httpd_url_t * );
159 void httpd_ClientModeStream( httpd_client_t *cl );
160 void httpd_ClientModeBidir( httpd_client_t *cl );
161 static void httpd_ClientClean( httpd_client_t *cl );
164 typedef struct httpd_file_t httpd_file_t;
165 typedef struct httpd_file_sys_t httpd_file_sys_t;
166 typedef int (*httpd_file_callback_t)( httpd_file_sys_t*, httpd_file_t *, uint8_t *psz_request, uint8_t **pp_data, int *pi_data );
167 httpd_file_t *httpd_FileNew( httpd_host_t *,
168 char *psz_url, char *psz_mime,
169 char *psz_user, char *psz_password,
170 httpd_file_callback_t pf_fill,
171 httpd_file_sys_t *p_sys );
172 void httpd_FileDelete( httpd_file_t * );
174 typedef struct httpd_redirect_t httpd_redirect_t;
175 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *, char *psz_url_dst, char *psz_url_src );
176 void httpd_RedirectDelete( httpd_redirect_t * );
179 typedef struct httpd_stream_t httpd_stream_t;
180 httpd_stream_t *httpd_StreamNew( httpd_host_t * );
181 int httpd_StreamHeader( httpd_stream_t *, uint8_t *p_data, int i_data );
182 int httpd_StreamSend( httpd_stream_t *, uint8_t *p_data, int i_data );
183 void httpd_StreamDelete( httpd_stream_t * );
186 /* Msg functions facilities */
187 void httpd_MsgInit( httpd_message_t * );
188 void httpd_MsgAdd( httpd_message_t *, char *psz_name, char *psz_value, ... );
189 /* return "" if not found. The string is not allocated */
190 char *httpd_MsgGet( httpd_message_t *, char *psz_name );
191 void httpd_MsgClean( httpd_message_t * );
199 /* vlc_mutex_t lock; */
205 static void httpd_ClientClean( httpd_client_t *cl );
207 /* each host run in his own thread */
217 /* address/port and socket for listening at connections */
218 struct sockaddr_storage sock;
224 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
225 * This will slow down the url research but make my live easier
226 * All url will have their cb trigger, but only the first one can answer
232 httpd_client_t **client;
251 httpd_callback_sys_t *p_sys;
252 } catch[HTTPD_MSG_MAX];
258 HTTPD_CLIENT_RECEIVING,
259 HTTPD_CLIENT_RECEIVE_DONE,
261 HTTPD_CLIENT_SENDING,
262 HTTPD_CLIENT_SEND_DONE,
264 HTTPD_CLIENT_WAITING,
268 HTTPD_CLIENT_TLS_HS_IN,
269 HTTPD_CLIENT_TLS_HS_OUT
274 HTTPD_CLIENT_FILE, /* default */
275 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
276 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
279 struct httpd_client_t
285 struct sockaddr_storage sock;
291 int b_read_waiting; /* stop as soon as possible sending */
293 mtime_t i_activity_date;
294 mtime_t i_activity_timeout;
296 /* buffer for reading header */
302 httpd_message_t query; /* client -> httpd */
303 httpd_message_t answer; /* httpd -> client */
306 tls_session_t *p_tls;
310 /*****************************************************************************
312 *****************************************************************************/
313 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
314 static void b64_decode( char *dest, char *src )
319 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
320 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
321 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
322 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
323 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
324 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
325 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
326 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
327 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
328 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
329 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
330 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
331 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
332 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
333 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
334 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
337 for( i_level = 0; *src != '\0'; src++ )
341 c = b64[(unsigned int)*src];
353 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
357 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
361 *dest++ = ( ( last &0x03 ) << 6 ) | c;
376 { ".htm", "text/html" },
377 { ".html", "text/html" },
378 { ".txt", "text/plain" },
379 { ".xml", "text/xml" },
380 { ".dtd", "text/dtd" },
382 { ".css", "text/css" },
385 { ".gif", "image/gif" },
386 { ".jpe", "image/jpeg" },
387 { ".jpg", "image/jpeg" },
388 { ".jpeg", "image/jpeg" },
389 { ".png", "image/png" },
390 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
393 { ".avi", "video/avi" },
394 { ".asf", "video/x-ms-asf" },
395 { ".m1a", "audio/mpeg" },
396 { ".m2a", "audio/mpeg" },
397 { ".m1v", "video/mpeg" },
398 { ".m2v", "video/mpeg" },
399 { ".mp2", "audio/mpeg" },
400 { ".mp3", "audio/mpeg" },
401 { ".mpa", "audio/mpeg" },
402 { ".mpg", "video/mpeg" },
403 { ".mpeg", "video/mpeg" },
404 { ".mpe", "video/mpeg" },
405 { ".mov", "video/quicktime" },
406 { ".moov", "video/quicktime" },
407 { ".ogg", "application/ogg" },
408 { ".ogm", "application/ogg" },
409 { ".wav", "audio/wav" },
410 { ".wma", "audio/x-ms-wma" },
411 { ".wmv", "video/x-ms-wmv" },
417 static char *httpd_MimeFromUrl( char *psz_url )
422 psz_ext = strrchr( psz_url, '.' );
427 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
429 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
431 return http_mime[i].psz_mime;
435 return "application/octet-stream";
438 /*****************************************************************************
439 * High Level Funtions: httpd_file_t
440 *****************************************************************************/
448 httpd_file_callback_t pf_fill;
449 httpd_file_sys_t *p_sys;
454 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
456 httpd_file_t *file = (httpd_file_t*)p_sys;
458 if( answer == NULL || query == NULL )
462 answer->i_proto = HTTPD_PROTO_HTTP;
463 answer->i_version= query->i_version;
464 answer->i_type = HTTPD_MSG_ANSWER;
466 answer->i_status = 200;
467 answer->psz_status = strdup( "OK" );
469 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
470 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
472 if( query->i_type != HTTPD_MSG_HEAD )
474 char *psz_args = query->psz_args;
475 if( query->i_type == HTTPD_MSG_POST )
478 psz_args = query->p_body;
480 file->pf_fill( file->p_sys, file, psz_args, &answer->p_body,
483 /* We respect client request */
484 if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
486 httpd_MsgAdd( answer, "Connection",
487 httpd_MsgGet( &cl->query, "Connection" ) );
490 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
496 httpd_file_t *httpd_FileNew( httpd_host_t *host,
497 char *psz_url, char *psz_mime,
498 char *psz_user, char *psz_password,
499 httpd_file_callback_t pf_fill,
500 httpd_file_sys_t *p_sys )
502 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
504 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
505 psz_password ) ) == NULL )
511 file->psz_url = strdup( psz_url );
512 if( psz_mime && *psz_mime )
514 file->psz_mime = strdup( psz_mime );
518 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
521 file->pf_fill = pf_fill;
524 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
525 (httpd_callback_sys_t*)file );
526 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
527 (httpd_callback_sys_t*)file );
528 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
529 (httpd_callback_sys_t*)file );
534 void httpd_FileDelete( httpd_file_t *file )
536 httpd_UrlDelete( file->url );
538 free( file->psz_url );
539 free( file->psz_mime );
544 /*****************************************************************************
545 * High Level Funtions: httpd_redirect_t
546 *****************************************************************************/
547 struct httpd_redirect_t
553 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
554 httpd_client_t *cl, httpd_message_t *answer,
555 httpd_message_t *query )
557 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
560 if( answer == NULL || query == NULL )
564 answer->i_proto = query->i_proto;
565 answer->i_version= query->i_version;
566 answer->i_type = HTTPD_MSG_ANSWER;
567 answer->i_status = 301;
568 answer->psz_status = strdup( "Moved Permanently" );
570 p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
571 p += sprintf( p, "<html>\n" );
572 p += sprintf( p, "<head>\n" );
573 p += sprintf( p, "<title>Redirection</title>\n" );
574 p += sprintf( p, "</head>\n" );
575 p += sprintf( p, "<body>\n" );
576 p += sprintf( p, "<h1><center>You should be <a href=\"%s\">redirected</a></center></h1>\n", rdir->psz_dst );
577 p += sprintf( p, "<hr />\n" );
578 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
579 p += sprintf( p, "</body>\n" );
580 p += sprintf( p, "</html>\n" );
581 answer->i_body = p - answer->p_body;
583 /* XXX check if it's ok or we need to set an absolute url */
584 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
586 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
591 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, char *psz_url_dst,
594 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
596 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL ) ) )
601 rdir->psz_dst = strdup( psz_url_dst );
603 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
604 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
605 (httpd_callback_sys_t*)rdir );
606 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
607 (httpd_callback_sys_t*)rdir );
608 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
609 (httpd_callback_sys_t*)rdir );
610 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
611 (httpd_callback_sys_t*)rdir );
615 void httpd_RedirectDelete( httpd_redirect_t *rdir )
617 httpd_UrlDelete( rdir->url );
618 free( rdir->psz_dst );
622 /*****************************************************************************
623 * High Level Funtions: httpd_stream_t
624 *****************************************************************************/
625 struct httpd_stream_t
632 /* Header to send as first packet */
636 /* circular buffer */
637 int i_buffer_size; /* buffer size, can't be reallocated smaller */
638 uint8_t *p_buffer; /* buffer */
639 int64_t i_buffer_pos; /* absolute position from begining */
640 int64_t i_buffer_last_pos; /* a new connection will start with that */
643 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
644 httpd_client_t *cl, httpd_message_t *answer,
645 httpd_message_t *query )
647 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
649 if( answer == NULL || query == NULL || cl == NULL )
653 if( answer->i_body_offset > 0 )
659 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
660 answer->i_body_offset );
663 if( answer->i_body_offset >= stream->i_buffer_pos )
665 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
666 return VLC_EGENERIC; /* wait, no data available */
668 if( answer->i_body_offset + stream->i_buffer_size <
669 stream->i_buffer_pos )
671 /* this client isn't fast enough */
672 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
673 answer->i_body_offset, stream->i_buffer_last_pos );
674 answer->i_body_offset = stream->i_buffer_last_pos;
677 i_pos = answer->i_body_offset % stream->i_buffer_size;
678 i_write = stream->i_buffer_pos - answer->i_body_offset;
679 if( i_write > 10000 )
683 else if( i_write <= 0 )
685 return VLC_EGENERIC; /* wait, no data available */
688 /* Don't go past the end of the circular buffer */
689 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
691 /* using HTTPD_MSG_ANSWER -> data available */
692 answer->i_proto = HTTPD_PROTO_HTTP;
693 answer->i_version= 0;
694 answer->i_type = HTTPD_MSG_ANSWER;
696 answer->i_body = i_write;
697 answer->p_body = malloc( i_write );
698 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
700 answer->i_body_offset += i_write;
706 answer->i_proto = HTTPD_PROTO_HTTP;
707 answer->i_version= 0;
708 answer->i_type = HTTPD_MSG_ANSWER;
710 answer->i_status = 200;
711 answer->psz_status = strdup( "OK" );
713 if( query->i_type != HTTPD_MSG_HEAD )
715 httpd_ClientModeStream( cl );
716 vlc_mutex_lock( &stream->lock );
717 /* Send the header */
718 if( stream->i_header > 0 )
720 answer->i_body = stream->i_header;
721 answer->p_body = malloc( stream->i_header );
722 memcpy( answer->p_body, stream->p_header, stream->i_header );
724 answer->i_body_offset = stream->i_buffer_last_pos;
725 vlc_mutex_unlock( &stream->lock );
729 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
730 answer->i_body_offset = 0;
733 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
735 vlc_bool_t b_xplaystream = VLC_FALSE;
738 httpd_MsgAdd( answer, "Content-type", "%s",
739 "application/octet-stream" );
740 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
741 httpd_MsgAdd( answer, "Pragma", "no-cache" );
742 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
743 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
745 /* Check if there is a xPlayStrm=1 */
746 for( i = 0; i < query->i_name; i++ )
748 if( !strcasecmp( query->name[i], "Pragma" ) &&
749 strstr( query->value[i], "xPlayStrm=1" ) )
751 b_xplaystream = VLC_TRUE;
757 answer->i_body_offset = 0;
762 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
764 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
769 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
770 char *psz_url, char *psz_mime,
771 char *psz_user, char *psz_password )
773 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
775 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
776 psz_password ) ) == NULL )
781 vlc_mutex_init( host, &stream->lock );
782 if( psz_mime && *psz_mime )
784 stream->psz_mime = strdup( psz_mime );
788 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
790 stream->i_header = 0;
791 stream->p_header = NULL;
792 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
793 stream->p_buffer = malloc( stream->i_buffer_size );
794 /* We set to 1 to make life simpler
795 * (this way i_body_offset can never be 0) */
796 stream->i_buffer_pos = 1;
797 stream->i_buffer_last_pos = 1;
799 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
800 (httpd_callback_sys_t*)stream );
801 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
802 (httpd_callback_sys_t*)stream );
803 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
804 (httpd_callback_sys_t*)stream );
809 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
811 vlc_mutex_lock( &stream->lock );
812 if( stream->p_header )
814 free( stream->p_header );
815 stream->p_header = NULL;
817 stream->i_header = i_data;
820 stream->p_header = malloc( i_data );
821 memcpy( stream->p_header, p_data, i_data );
823 vlc_mutex_unlock( &stream->lock );
828 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
833 if( i_data < 0 || p_data == NULL )
837 vlc_mutex_lock( &stream->lock );
839 /* save this pointer (to be used by new connection) */
840 stream->i_buffer_last_pos = stream->i_buffer_pos;
842 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
848 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
850 /* Ok, we can't go past the end of our buffer */
851 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
853 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
858 stream->i_buffer_pos += i_data;
860 vlc_mutex_unlock( &stream->lock );
864 void httpd_StreamDelete( httpd_stream_t *stream )
866 httpd_UrlDelete( stream->url );
867 vlc_mutex_destroy( &stream->lock );
868 if( stream->psz_mime ) free( stream->psz_mime );
869 if( stream->p_header ) free( stream->p_header );
870 if( stream->p_buffer ) free( stream->p_buffer );
875 /*****************************************************************************
877 *****************************************************************************/
878 #define LISTEN_BACKLOG 100
880 #if defined(HAVE_GETNAMEINFO) && !defined(HAVE_GETADDRINFO)
882 * For now, VLC's configure script does not check for getaddrinfo(),
883 * but it should be present if getnameinfo() is (the opposite is untrue, with
884 * Debian potato as an example)
886 # define HAVE_GETADDRINFO 1
889 static void httpd_HostThread( httpd_host_t * );
890 static int GetAddrPort( const struct sockaddr_storage *p_ss );
892 #ifndef HAVE_GETADDRINFO
893 struct httpd_addrinfo
899 struct sockaddr *ai_addr;
901 struct httpd_addrinfo *ai_next;
903 # define addrinfo httpd_addrinfo
905 static int BuildAddr( struct sockaddr_in * p_socket,
906 const char * psz_address, int i_port );
910 /* create a new host */
911 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, char *psz_host,
914 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL );
917 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, char *psz_host,
918 int i_port, tls_server_t *p_tls )
921 httpd_host_t *host = NULL;
924 struct addrinfo *res, *ptr;
927 #ifdef HAVE_GETADDRINFO
931 struct addrinfo hints;
933 memset( &hints, 0, sizeof( hints ) );
937 * For now, keep IPv4 by default. That said, it should be safe to use
938 * IPv6 by default *on the server side*, as, apart from NetBSD, most
939 * systems accept IPv4 clients on IPv6 listening sockets.
941 hints.ai_family = PF_INET;
945 /* Check if ipv4 or ipv6 were forced */
946 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
947 var_Get( p_this, "ipv4", &val );
949 hints.ai_family = PF_INET;
951 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
952 var_Get( p_this, "ipv6", &val );
954 hints.ai_family = PF_INET6;
956 hints.ai_socktype = SOCK_STREAM;
957 hints.ai_flags = AI_PASSIVE;
959 if (*psz_host == '\0')
962 snprintf( psz_port, sizeof( psz_port ), "%d", i_port );
963 psz_port[sizeof( psz_port ) - 1] = '\0';
965 if( getaddrinfo( psz_host, psz_port, &hints, &res ) )
967 msg_Err( p_this, "cannot resolve %s:%d", psz_host, i_port );
973 struct sockaddr_in sock;
974 struct httpd_addrinfo info;
976 info.ai_family = PF_INET;
977 info.ai_socktype = SOCK_STREAM;
978 info.ai_protocol = 0;
979 info.ai_addr = (struct sockaddr *)&sock;
980 info.ai_addrlen = sizeof( sock );
985 if( BuildAddr( &sock, psz_host, i_port ) )
987 msg_Err( p_this, "cannot build address for %s:%d", psz_host, i_port );
991 # define freeaddrinfo( r ) (void)0;
994 /* to be sure to avoid multiple creation */
995 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
996 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
997 vlc_mutex_lock( lockval.p_address );
999 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
1001 msg_Info( p_this, "creating httpd" );
1002 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
1004 vlc_mutex_unlock( lockval.p_address );
1005 freeaddrinfo( res );
1012 vlc_object_yield( httpd );
1013 vlc_object_attach( httpd, p_this->p_vlc );
1016 for( ptr = res; (ptr != NULL) && (fd == -1); ptr = ptr->ai_next )
1020 if( ((unsigned)ptr->ai_addrlen) > sizeof( struct sockaddr_storage ) )
1022 msg_Dbg( p_this, "socket address too big" );
1026 /* verify if it already exist */
1027 for( i = 0; i < httpd->i_host; i++ )
1029 if( GetAddrPort (&httpd->host[i]->sock) != i_port )
1032 /* Cannot re-use host if it uses TLS/SSL */
1033 if( httpd->host[i]->p_tls != NULL )
1037 if( httpd->host[i]->sock.ss_family == AF_INET6 )
1039 const struct sockaddr_in6 *p_hsock, *p_sock;
1041 p_hsock = (const struct sockaddr_in6 *)&httpd->host[i]->sock;
1042 p_sock = (const struct sockaddr_in6 *)ptr->ai_addr;
1044 if( memcmp( &p_hsock->sin6_addr, &in6addr_any,
1045 sizeof( struct in6_addr ) ) &&
1046 ( p_sock->sin6_family != AF_INET6 ||
1047 memcmp( &p_hsock->sin6_addr, &p_sock->sin6_addr,
1048 sizeof( struct in6_addr ) ) ) )
1049 continue; /* does not match */
1051 else if( ptr->ai_family == PF_INET6 )
1055 if( httpd->host[i]->sock.ss_family == AF_INET )
1057 const struct sockaddr_in *p_hsock, *p_sock;
1059 p_hsock = (const struct sockaddr_in *)&httpd->host[i]->sock;
1060 p_sock = (const struct sockaddr_in *)ptr->ai_addr;
1062 if( p_hsock->sin_addr.s_addr != INADDR_ANY &&
1063 ( p_sock->sin_family != AF_INET ||
1064 p_hsock->sin_addr.s_addr != p_sock->sin_addr.s_addr ) )
1065 continue; /* does not match */
1067 else if( ptr->ai_family == PF_INET )
1071 msg_Dbg( p_this, "host with unknown address family" );
1075 freeaddrinfo( res );
1078 host = httpd->host[i];
1081 vlc_mutex_unlock( lockval.p_address );
1083 msg_Dbg( p_this, "host already registered" );
1087 /* create the listening socket */
1088 fd = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
1095 if( setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
1096 (void *)&dummy, sizeof( dummy ) ) < 0 )
1098 msg_Warn( p_this, "cannot configure socket (SO_REUSEADDR)" );
1103 if( bind( fd, ptr->ai_addr, ptr->ai_addrlen ) )
1105 msg_Err( p_this, "cannot bind socket" );
1108 /* set to non-blocking */
1109 #if defined( WIN32 ) || defined( UNDER_CE )
1111 unsigned long i_dummy = 1;
1112 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
1114 msg_Err( p_this, "cannot set socket to non-blocking mode" );
1120 unsigned int i_flags;
1121 if( ( i_flags = fcntl( fd, F_GETFL, 0 ) ) < 0 )
1123 msg_Err( p_this, "cannot F_GETFL socket" );
1126 if( fcntl( fd, F_SETFL, i_flags | O_NONBLOCK ) < 0 )
1128 msg_Err( p_this, "cannot F_SETFL O_NONBLOCK" );
1134 if( listen( fd, LISTEN_BACKLOG ) < 0 )
1136 msg_Err( p_this, "cannot listen socket" );
1150 freeaddrinfo( res );
1154 /* create the new host */
1155 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
1156 host->httpd = httpd;
1157 vlc_mutex_init( httpd, &host->lock );
1161 memcpy( &host->sock, ptr->ai_addr, ptr->ai_addrlen );
1162 host->i_sock_size = ptr->ai_addrlen;
1166 host->client = NULL;
1168 freeaddrinfo( res );
1169 host->p_tls = p_tls;
1171 /* create the thread */
1172 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1173 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1175 msg_Err( p_this, "cannot spawn http host thread" );
1179 /* now add it to httpd */
1180 TAB_APPEND( httpd->i_host, httpd->host, host );
1181 vlc_mutex_unlock( lockval.p_address );
1186 vlc_mutex_unlock( lockval.p_address );
1193 vlc_mutex_destroy( &host->lock );
1194 vlc_object_destroy( host );
1197 /* TODO destroy no more used httpd TODO */
1198 vlc_object_release( httpd );
1203 void httpd_HostDelete( httpd_host_t *host )
1205 httpd_t *httpd = host->httpd;
1206 vlc_value_t lockval;
1209 msg_Dbg( host, "httpd_HostDelete" );
1211 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1212 vlc_mutex_lock( lockval.p_address );
1214 vlc_object_release( httpd );
1217 if( host->i_ref > 0 )
1220 vlc_mutex_unlock( lockval.p_address );
1221 msg_Dbg( host, "httpd_HostDelete: host still used" );
1224 TAB_REMOVE( httpd->i_host, httpd->host, host );
1226 msg_Dbg( host, "httpd_HostDelete: host removed from http" );
1229 vlc_thread_join( host );
1231 msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1233 for( i = 0; i < host->i_url; i++ )
1235 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1237 for( i = 0; i < host->i_client; i++ )
1239 httpd_client_t *cl = host->client[i];
1240 msg_Warn( host, "client still connected" );
1241 httpd_ClientClean( cl );
1242 TAB_REMOVE( host->i_client, host->client, cl );
1248 if( host->p_tls != NULL)
1249 tls_ServerDelete( host->p_tls );
1250 net_Close( host->fd );
1251 vlc_mutex_destroy( &host->lock );
1252 vlc_object_destroy( host );
1254 if( httpd->i_host <= 0 )
1256 msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1257 vlc_object_detach( httpd );
1258 vlc_object_destroy( httpd );
1260 vlc_mutex_unlock( lockval.p_address );
1263 /* register a new url */
1264 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url,
1265 char *psz_user, char *psz_password,
1266 vlc_bool_t b_check )
1271 vlc_mutex_lock( &host->lock );
1274 for( i = 0; i < host->i_url; i++ )
1276 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1278 msg_Warn( host->httpd,
1279 "cannot add '%s' (url already defined)", psz_url );
1280 vlc_mutex_unlock( &host->lock );
1286 url = malloc( sizeof( httpd_url_t ) );
1289 vlc_mutex_init( host->httpd, &url->lock );
1290 url->psz_url = strdup( psz_url );
1291 url->psz_user = strdup( psz_user ? psz_user : "" );
1292 url->psz_password = strdup( psz_password ? psz_password : "" );
1293 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1295 url->catch[i].cb = NULL;
1296 url->catch[i].p_sys = NULL;
1299 TAB_APPEND( host->i_url, host->url, url );
1300 vlc_mutex_unlock( &host->lock );
1305 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
1306 char *psz_user, char *psz_password )
1308 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1309 psz_password, VLC_FALSE );
1312 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
1313 char *psz_user, char *psz_password )
1315 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1316 psz_password, VLC_TRUE );
1319 /* register callback on a url */
1320 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1321 httpd_callback_sys_t *p_sys )
1323 vlc_mutex_lock( &url->lock );
1324 url->catch[i_msg].cb = cb;
1325 url->catch[i_msg].p_sys= p_sys;
1326 vlc_mutex_unlock( &url->lock );
1333 void httpd_UrlDelete( httpd_url_t *url )
1335 httpd_host_t *host = url->host;
1338 vlc_mutex_lock( &host->lock );
1339 TAB_REMOVE( host->i_url, host->url, url );
1341 vlc_mutex_destroy( &url->lock );
1342 free( url->psz_url );
1343 free( url->psz_user );
1344 free( url->psz_password );
1346 for( i = 0; i < host->i_client; i++ )
1348 httpd_client_t *client = host->client[i];
1350 if( client->url == url )
1352 /* TODO complete it */
1353 msg_Warn( host, "force closing connections" );
1354 httpd_ClientClean( client );
1355 TAB_REMOVE( host->i_client, host->client, client );
1361 vlc_mutex_unlock( &host->lock );
1364 void httpd_MsgInit( httpd_message_t *msg )
1367 msg->i_type = HTTPD_MSG_NONE;
1368 msg->i_proto = HTTPD_PROTO_NONE;
1369 msg->i_version = -1;
1372 msg->psz_status = NULL;
1374 msg->psz_url = NULL;
1375 msg->psz_args = NULL;
1377 msg->i_channel = -1;
1384 msg->i_body_offset = 0;
1389 void httpd_MsgClean( httpd_message_t *msg )
1393 if( msg->psz_status )
1395 free( msg->psz_status );
1399 free( msg->psz_url );
1403 free( msg->psz_args );
1405 for( i = 0; i < msg->i_name; i++ )
1407 free( msg->name[i] );
1408 free( msg->value[i] );
1420 free( msg->p_body );
1422 httpd_MsgInit( msg );
1425 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1429 for( i = 0; i < msg->i_name; i++ )
1431 if( !strcasecmp( msg->name[i], name ))
1433 return msg->value[i];
1438 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1443 va_start( args, psz_value );
1444 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1445 vasprintf( &value, psz_value, args );
1448 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1449 value = calloc( i_size, sizeof( char ) );
1450 vsnprintf( value, i_size, psz_value, args );
1451 value[i_size - 1] = 0;
1456 name = strdup( name );
1458 TAB_APPEND( msg->i_name, msg->name, name );
1459 TAB_APPEND( msg->i_value, msg->value, value );
1462 static void httpd_ClientInit( httpd_client_t *cl )
1464 cl->i_state = HTTPD_CLIENT_RECEIVING;
1465 cl->i_activity_date = mdate();
1466 cl->i_activity_timeout = I64C(10000000);
1467 cl->i_buffer_size = 10000;
1469 cl->p_buffer = malloc( cl->i_buffer_size );
1470 cl->i_mode = HTTPD_CLIENT_FILE;
1471 cl->b_read_waiting = VLC_FALSE;
1473 httpd_MsgInit( &cl->query );
1474 httpd_MsgInit( &cl->answer );
1477 void httpd_ClientModeStream( httpd_client_t *cl )
1479 cl->i_mode = HTTPD_CLIENT_STREAM;
1482 void httpd_ClientModeBidir( httpd_client_t *cl )
1484 cl->i_mode = HTTPD_CLIENT_BIDIR;
1487 char* httpd_ClientIP( httpd_client_t *cl )
1489 #ifdef HAVE_GETNAMEINFO
1490 char sz_ip[INET6_ADDRSTRLEN + 2];
1493 if( (cl->sock.ss_family == AF_INET6) &&
1494 IN6_IS_ADDR_V4MAPPED( &((const struct sockaddr_in6 *)
1495 &cl->sock)->sin6_addr) )
1497 /* If client is using IPv4 but server is using IPv6 */
1498 struct sockaddr_in a;
1500 memset( &a, 0, sizeof( a ) );
1501 a.sin_family = AF_INET;
1502 a.sin_port = ((const struct sockaddr_in6 *)&cl->sock)->sin6_port;
1503 a.sin_addr.s_addr = ((const uint32_t *)&((const struct sockaddr_in6 *)
1504 &cl->sock)->sin6_addr)[3];
1505 i = getnameinfo( (const struct sockaddr *)&a, sizeof( a ),
1506 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST );
1509 i = getnameinfo( (const struct sockaddr *)&cl->sock, cl->i_sock_size,
1510 &sz_ip[1], INET6_ADDRSTRLEN, NULL, 0,
1514 /* FIXME: msg_Err */
1517 if( strchr( &sz_ip[1], ':' ) != NULL )
1520 i = strlen( sz_ip );
1524 return strdup( sz_ip );
1527 return strdup( &sz_ip[1] );
1530 /* FIXME not thread safe */
1531 return strdup( inet_ntoa( ((const struct sockaddr_in *)&cl->sock)->sin_addr ) );
1535 static void httpd_ClientClean( httpd_client_t *cl )
1539 if( cl->p_tls != NULL )
1540 tls_SessionClose( cl->p_tls );
1541 net_Close( cl->fd );
1545 httpd_MsgClean( &cl->answer );
1546 httpd_MsgClean( &cl->query );
1550 free( cl->p_buffer );
1551 cl->p_buffer = NULL;
1555 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1557 tls_session_t *p_tls )
1559 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1562 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1563 cl->i_sock_size = i_sock_size;
1567 httpd_ClientInit( cl );
1573 static int httpd_NetRecv( httpd_client_t *cl, char *p, int i_len )
1575 tls_session_t *p_tls;
1579 return tls_Recv( p_tls, p, i_len );
1581 return recv( cl->fd, p, i_len, 0 );
1585 static int httpd_NetSend( httpd_client_t *cl, const char *p, int i_len )
1587 tls_session_t *p_tls;
1591 return tls_Send( p_tls, p, i_len );
1593 return send( cl->fd, p, i_len, 0 );
1597 static void httpd_ClientRecv( httpd_client_t *cl )
1601 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1603 /* enought to see if it's rtp over rtsp or RTSP/HTTP */
1604 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1608 cl->i_buffer += i_len;
1611 if( cl->i_buffer >= 4 )
1613 fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );
1615 if( cl->p_buffer[0] == '$' )
1617 /* RTSP (rtp over rtsp) */
1618 cl->query.i_proto = HTTPD_PROTO_RTSP;
1619 cl->query.i_type = HTTPD_MSG_CHANNEL;
1620 cl->query.i_channel = cl->p_buffer[1];
1621 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1622 cl->query.p_body = malloc( cl->query.i_body );
1626 else if( !strncmp( cl->p_buffer, "HTTP", 4 ) )
1628 cl->query.i_proto = HTTPD_PROTO_HTTP;
1629 cl->query.i_type = HTTPD_MSG_ANSWER;
1631 else if( !strncmp( cl->p_buffer, "RTSP", 4 ) )
1633 cl->query.i_proto = HTTPD_PROTO_RTSP;
1634 cl->query.i_type = HTTPD_MSG_ANSWER;
1636 else if( !strncmp( cl->p_buffer, "GET", 3 ) ||
1637 !strncmp( cl->p_buffer, "HEAD", 4 ) ||
1638 !strncmp( cl->p_buffer, "POST", 4 ) )
1640 cl->query.i_proto = HTTPD_PROTO_HTTP;
1641 cl->query.i_type = HTTPD_MSG_NONE;
1645 cl->query.i_proto = HTTPD_PROTO_RTSP;
1646 cl->query.i_type = HTTPD_MSG_NONE;
1650 else if( cl->query.i_body > 0 )
1652 /* we are reading the body of a request or a channel */
1653 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1654 cl->query.i_body - cl->i_buffer );
1657 cl->i_buffer += i_len;
1659 if( cl->i_buffer >= cl->query.i_body )
1661 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1666 /* we are reading a header -> char by char */
1669 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1676 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1678 cl->i_buffer_size += 1024;
1679 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1681 if( ( cl->i_buffer >= 2 && !strncmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1682 ( cl->i_buffer >= 4 && !strncmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1686 /* we have finished the header so parse it and set i_body */
1687 cl->p_buffer[cl->i_buffer] = '\0';
1689 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1691 cl->query.i_status =
1692 strtol( &cl->p_buffer[strlen( "HTTP/1.x" )], &p, 0 );
1697 cl->query.psz_status = strdup( p );
1709 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1710 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1711 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1713 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1714 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1715 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1716 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1717 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1718 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1720 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1725 cl->query.i_type = HTTPD_MSG_NONE;
1727 fprintf( stderr, "received new request=%s\n", cl->p_buffer);
1729 for( i = 0; msg_type[i].name != NULL; i++ )
1731 if( !strncmp( cl->p_buffer, msg_type[i].name,
1732 strlen( msg_type[i].name ) ) )
1734 p = &cl->p_buffer[strlen(msg_type[i].name) + 1 ];
1735 cl->query.i_type = msg_type[i].i_type;
1736 if( cl->query.i_proto != msg_type[i].i_proto )
1739 cl->query.i_proto = HTTPD_PROTO_NONE;
1740 cl->query.i_type = HTTPD_MSG_NONE;
1747 if( strstr( cl->p_buffer, "HTTP/1." ) )
1749 cl->query.i_proto = HTTPD_PROTO_HTTP;
1751 else if( strstr( cl->p_buffer, "RTSP/1." ) )
1753 cl->query.i_proto = HTTPD_PROTO_RTSP;
1765 p2 = strchr( p, ' ' );
1770 if( !strncasecmp( p, "rtsp:", 5 ) )
1772 /* for rtsp url, you have rtsp://localhost:port/path */
1774 while( *p == '/' ) p++;
1775 while( *p && *p != '/' ) p++;
1777 cl->query.psz_url = strdup( p );
1778 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1781 cl->query.psz_args = strdup( p3 );
1789 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1791 cl->query.i_proto = HTTPD_PROTO_HTTP;
1792 cl->query.i_version = atoi( p2+7 );
1794 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1796 cl->query.i_proto = HTTPD_PROTO_RTSP;
1797 cl->query.i_version = atoi( p2+7 );
1805 p = strchr( p, '\n' );
1809 while( *p == '\n' || *p == '\r' )
1813 while( p && *p != '\0' )
1816 char *eol = p = strchr( p, '\n' );
1819 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1824 if( ( colon = strchr( line, ':' ) ) )
1830 while( *colon == ' ' )
1834 name = strdup( line );
1835 value = strdup( colon );
1837 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1838 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1840 if( !strcasecmp( name, "Content-Length" ) )
1842 cl->query.i_body = atol( value );
1849 while( *p == '\n' || *p == '\r' )
1856 if( cl->query.i_body > 0 )
1858 /* TODO Mhh, handle the case client will only send a request and close the connection
1859 * to mark and of body (probably only RTSP) */
1860 cl->query.p_body = malloc( cl->query.i_body );
1865 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1871 /* check if the client is to be set to dead */
1872 #if defined( WIN32 ) || defined( UNDER_CE )
1873 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1875 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1878 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1880 /* connection closed -> end of data */
1881 if( cl->query.i_body > 0 )
1883 cl->query.i_body = cl->i_buffer;
1885 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1889 cl->i_state = HTTPD_CLIENT_DEAD;
1892 cl->i_activity_date = mdate();
1894 /* XXX: for QT I have to disable timeout. Try to find why */
1895 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1896 cl->i_activity_timeout = 0;
1898 /* Debugging only */
1899 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1903 fprintf( stderr, "received new request\n" );
1904 fprintf( stderr, " - proto=%s\n",
1905 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1906 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1907 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1908 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1910 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1911 cl->query.psz_status );
1913 else if( cl->query.i_type != HTTPD_MSG_NONE )
1915 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1917 for( i = 0; i < cl->query.i_name; i++ )
1919 fprintf( stderr, " - option name='%s' value='%s'\n",
1920 cl->query.name[i], cl->query.value[i] );
1926 static void httpd_ClientSend( httpd_client_t *cl )
1931 if( cl->i_buffer < 0 )
1933 /* We need to create the header */
1937 i_size = strlen( "HTTP/1.") + 10 + 10 +
1938 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1939 for( i = 0; i < cl->answer.i_name; i++ )
1941 i_size += strlen( cl->answer.name[i] ) + 2 +
1942 strlen( cl->answer.value[i] ) + 2;
1945 if( cl->i_buffer_size < i_size )
1947 cl->i_buffer_size = i_size;
1948 free( cl->p_buffer );
1949 cl->p_buffer = malloc( i_size );
1953 p += sprintf( p, "%s/1.%d %d %s\r\n",
1954 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1955 cl->answer.i_version,
1956 cl->answer.i_status, cl->answer.psz_status );
1957 for( i = 0; i < cl->answer.i_name; i++ )
1959 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1960 cl->answer.value[i] );
1962 p += sprintf( p, "\r\n" );
1965 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1967 fprintf( stderr, "sending answer\n" );
1968 fprintf( stderr, "%s", cl->p_buffer );
1971 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1972 cl->i_buffer_size - cl->i_buffer );
1975 cl->i_activity_date = mdate();
1976 cl->i_buffer += i_len;
1978 if( cl->i_buffer >= cl->i_buffer_size )
1980 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1981 !cl->b_read_waiting )
1983 /* catch more body data */
1984 int i_msg = cl->query.i_type;
1985 int64_t i_offset = cl->answer.i_body_offset;
1987 httpd_MsgClean( &cl->answer );
1988 cl->answer.i_body_offset = i_offset;
1990 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1991 &cl->answer, &cl->query );
1994 if( cl->answer.i_body > 0 )
1996 /* send the body data */
1997 free( cl->p_buffer );
1998 cl->p_buffer = cl->answer.p_body;
1999 cl->i_buffer_size = cl->answer.i_body;
2002 cl->answer.i_body = 0;
2003 cl->answer.p_body = NULL;
2008 cl->i_state = HTTPD_CLIENT_SEND_DONE;
2014 #if defined( WIN32 ) || defined( UNDER_CE )
2015 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
2017 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
2021 cl->i_state = HTTPD_CLIENT_DEAD;
2026 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
2028 switch( tls_SessionContinueHandshake( cl->p_tls ) )
2031 cl->i_state = HTTPD_CLIENT_RECEIVING;
2035 cl->i_state = HTTPD_CLIENT_DEAD;
2039 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
2043 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
2045 switch( tls_SessionContinueHandshake( cl->p_tls ) )
2048 cl->i_state = HTTPD_CLIENT_RECEIVING;
2052 cl->i_state = HTTPD_CLIENT_DEAD;
2056 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
2061 static void httpd_HostThread( httpd_host_t *host )
2063 tls_session_t *p_tls = NULL;
2065 while( !host->b_die )
2067 struct timeval timeout;
2070 int i_handle_max = 0;
2073 int b_low_delay = 0;
2075 if( host->i_url <= 0 )
2082 /* built a set of handle to select */
2083 FD_ZERO( &fds_read );
2084 FD_ZERO( &fds_write );
2086 FD_SET( host->fd, &fds_read );
2087 i_handle_max = host->fd;
2089 /* prepare a new TLS session */
2090 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2091 p_tls = tls_ServerSessionPrepare( host->p_tls );
2093 /* add all socket that should be read/write and close dead connection */
2094 vlc_mutex_lock( &host->lock );
2095 for( i_client = 0; i_client < host->i_client; i_client++ )
2097 httpd_client_t *cl = host->client[i_client];
2099 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2100 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2101 ( cl->i_activity_timeout > 0 &&
2102 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
2106 // FIXME: it sucks to allocate memory on the stack for debug
2107 ip = httpd_ClientIP( cl );
2108 msg_Dbg( host, "connection closed(%s)",
2109 (ip != NULL) ? ip : "unknown" );
2112 httpd_ClientClean( cl );
2113 TAB_REMOVE( host->i_client, host->client, cl );
2118 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2119 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2121 FD_SET( cl->fd, &fds_read );
2122 i_handle_max = __MAX( i_handle_max, cl->fd );
2124 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2125 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2127 FD_SET( cl->fd, &fds_write );
2128 i_handle_max = __MAX( i_handle_max, cl->fd );
2130 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2132 httpd_message_t *answer = &cl->answer;
2133 httpd_message_t *query = &cl->query;
2134 int i_msg = query->i_type;
2136 httpd_MsgInit( answer );
2138 /* Handle what we received */
2139 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
2140 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2142 /* we can only receive request from client when not
2145 cl->i_state = HTTPD_CLIENT_DEAD;
2147 else if( i_msg == HTTPD_MSG_ANSWER )
2149 /* We are in BIDIR mode, trigger the callback and then
2150 * check for new data */
2151 if( cl->url && cl->url->catch[i_msg].cb )
2153 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2156 cl->i_state = HTTPD_CLIENT_WAITING;
2158 else if( i_msg == HTTPD_MSG_CHANNEL )
2160 /* We are in BIDIR mode, trigger the callback and then
2161 * check for new data */
2162 if( cl->url && cl->url->catch[i_msg].cb )
2164 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2167 cl->i_state = HTTPD_CLIENT_WAITING;
2169 else if( i_msg == HTTPD_MSG_OPTIONS )
2174 answer->i_proto = query->i_proto ;
2175 answer->i_type = HTTPD_MSG_ANSWER;
2176 answer->i_version= 0;
2177 answer->i_status = 200;
2178 answer->psz_status = strdup( "Ok" );
2181 answer->p_body = NULL;
2183 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2184 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2185 httpd_MsgAdd( answer, "Server", "VLC Server" );
2186 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2187 "TEARDOWN, PLAY, PAUSE" );
2188 httpd_MsgAdd( answer, "Content-Length", "%d",
2191 cl->i_buffer = -1; /* Force the creation of the answer in
2192 * httpd_ClientSend */
2193 cl->i_state = HTTPD_CLIENT_SENDING;
2195 else if( i_msg == HTTPD_MSG_NONE )
2197 if( query->i_proto == HTTPD_PROTO_NONE )
2200 cl->i_state = HTTPD_CLIENT_DEAD;
2207 answer->i_proto = query->i_proto ;
2208 answer->i_type = HTTPD_MSG_ANSWER;
2209 answer->i_version= 0;
2210 answer->i_status = 501;
2211 answer->psz_status = strdup( "Unimplemented" );
2213 p = answer->p_body = malloc( 1000 );
2215 p += sprintf( p, "<html>\n" );
2216 p += sprintf( p, "<head>\n" );
2217 p += sprintf( p, "<title>Error 501</title>\n" );
2218 p += sprintf( p, "</head>\n" );
2219 p += sprintf( p, "<body>\n" );
2220 p += sprintf( p, "<h1><center> 501 Unimplemented</center></h1>\n" );
2221 p += sprintf( p, "<hr />\n" );
2222 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2223 p += sprintf( p, "</body>\n" );
2224 p += sprintf( p, "</html>\n" );
2226 answer->i_body = p - answer->p_body;
2227 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2229 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2230 cl->i_state = HTTPD_CLIENT_SENDING;
2235 vlc_bool_t b_auth_failed = VLC_FALSE;
2238 /* Search the url and trigger callbacks */
2239 for( i = 0; i < host->i_url; i++ )
2241 httpd_url_t *url = host->url[i];
2243 if( !strcmp( url->psz_url, query->psz_url ) )
2245 if( url->catch[i_msg].cb )
2247 if( answer && ( *url->psz_user || *url->psz_password ) )
2249 /* create the headers */
2250 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2254 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2255 auth = malloc( strlen(b64) + 1 );
2257 if( !strncasecmp( b64, "BASIC", 5 ) )
2260 while( *b64 == ' ' )
2264 b64_decode( auth, b64 );
2270 if( strcmp( id, auth ) )
2272 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2273 /* We fail for all url */
2274 b_auth_failed = VLC_TRUE;
2284 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2286 /* only one url can answer */
2288 if( cl->url == NULL )
2300 answer->i_proto = query->i_proto;
2301 answer->i_type = HTTPD_MSG_ANSWER;
2302 answer->i_version= 0;
2303 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2307 answer->i_status = 401;
2308 answer->psz_status = strdup( "Authorization Required" );
2310 p += sprintf( p, "<html>\n" );
2311 p += sprintf( p, "<head>\n" );
2312 p += sprintf( p, "<title>Error 401</title>\n" );
2313 p += sprintf( p, "</head>\n" );
2314 p += sprintf( p, "<body>\n" );
2315 p += sprintf( p, "<h1><center> 401 Authorization Required (%s)</center></h1>\n", query->psz_url );
2316 p += sprintf( p, "<hr />\n" );
2317 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2318 p += sprintf( p, "</body>\n" );
2319 p += sprintf( p, "</html>\n" );
2323 /* no url registered */
2324 answer->i_status = 404;
2325 answer->psz_status = strdup( "Not found" );
2327 p += sprintf( p, "<html>\n" );
2328 p += sprintf( p, "<head>\n" );
2329 p += sprintf( p, "<title>Error 404</title>\n" );
2330 p += sprintf( p, "</head>\n" );
2331 p += sprintf( p, "<body>\n" );
2332 p += sprintf( p, "<h1><center> 404 Resource not found(%s)</center></h1>\n", query->psz_url );
2333 p += sprintf( p, "<hr />\n" );
2334 p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" );
2335 p += sprintf( p, "</body>\n" );
2336 p += sprintf( p, "</html>\n" );
2339 answer->i_body = p - answer->p_body;
2340 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2342 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2343 cl->i_state = HTTPD_CLIENT_SENDING;
2346 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2348 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2351 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2352 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2353 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2354 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2355 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2356 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2358 httpd_MsgClean( &cl->query );
2359 httpd_MsgInit( &cl->query );
2362 cl->i_buffer_size = 1000;
2363 free( cl->p_buffer );
2364 cl->p_buffer = malloc( cl->i_buffer_size );
2365 cl->i_state = HTTPD_CLIENT_RECEIVING;
2369 cl->i_state = HTTPD_CLIENT_DEAD;
2371 httpd_MsgClean( &cl->answer );
2373 else if( cl->b_read_waiting )
2375 /* we have a message waiting for us to read it */
2376 httpd_MsgClean( &cl->answer );
2377 httpd_MsgClean( &cl->query );
2380 cl->i_buffer_size = 1000;
2381 free( cl->p_buffer );
2382 cl->p_buffer = malloc( cl->i_buffer_size );
2383 cl->i_state = HTTPD_CLIENT_RECEIVING;
2384 cl->b_read_waiting = VLC_FALSE;
2388 int64_t i_offset = cl->answer.i_body_offset;
2389 httpd_MsgClean( &cl->answer );
2391 cl->answer.i_body_offset = i_offset;
2392 free( cl->p_buffer );
2393 cl->p_buffer = NULL;
2395 cl->i_buffer_size = 0;
2397 cl->i_state = HTTPD_CLIENT_WAITING;
2400 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2402 int64_t i_offset = cl->answer.i_body_offset;
2403 int i_msg = cl->query.i_type;
2405 httpd_MsgInit( &cl->answer );
2406 cl->answer.i_body_offset = i_offset;
2408 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2409 &cl->answer, &cl->query );
2410 if( cl->answer.i_type != HTTPD_MSG_NONE )
2412 /* we have new data, so reenter send mode */
2414 cl->p_buffer = cl->answer.p_body;
2415 cl->i_buffer_size = cl->answer.i_body;
2416 cl->answer.p_body = NULL;
2417 cl->answer.i_body = 0;
2418 cl->i_state = HTTPD_CLIENT_SENDING;
2422 /* we shouldn't wait too long */
2423 b_low_delay = VLC_TRUE;
2427 /* Special for BIDIR mode we also check reading */
2428 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2429 cl->i_state == HTTPD_CLIENT_SENDING )
2431 FD_SET( cl->fd, &fds_read );
2432 i_handle_max = __MAX( i_handle_max, cl->fd );
2435 vlc_mutex_unlock( &host->lock );
2437 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2439 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2441 i_ret = select( i_handle_max + 1,
2442 &fds_read, &fds_write, NULL, &timeout );
2444 if( i_ret == -1 && errno != EINTR )
2446 msg_Warn( host, "cannot select sockets" );
2450 else if( i_ret <= 0 )
2455 /* accept new connections */
2456 if( FD_ISSET( host->fd, &fds_read ) )
2458 int i_sock_size = sizeof( struct sockaddr_storage );
2459 struct sockaddr_storage sock;
2462 fd = accept( host->fd, (struct sockaddr *)&sock, &i_sock_size );
2467 /* set this new socket non-block */
2468 #if defined( WIN32 ) || defined( UNDER_CE )
2470 unsigned long i_dummy = 1;
2471 ioctlsocket( fd, FIONBIO, &i_dummy );
2474 fcntl( fd, F_SETFL, O_NONBLOCK );
2477 /* FIXME: that MUST be non-blocking */
2480 switch ( tls_SessionHandshake( p_tls, fd ) )
2483 msg_Err( host, "Rejecting TLS connection" );
2488 case 1: /* missing input - most likely */
2489 i_state = HTTPD_CLIENT_TLS_HS_IN;
2492 case 2: /* missing output */
2493 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2503 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2505 vlc_mutex_lock( &host->lock );
2506 TAB_APPEND( host->i_client, host->client, cl );
2507 vlc_mutex_unlock( &host->lock );
2510 cl->i_state = i_state; // override state for TLS
2512 // FIXME: it sucks to allocate memory for debug
2513 ip = httpd_ClientIP( cl );
2514 msg_Dbg( host, "new connection (%s)",
2515 ip != NULL ? ip : "unknown" );
2521 /* now try all others socket */
2522 vlc_mutex_lock( &host->lock );
2523 for( i_client = 0; i_client < host->i_client; i_client++ )
2525 httpd_client_t *cl = host->client[i_client];
2526 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2528 httpd_ClientRecv( cl );
2530 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2532 httpd_ClientSend( cl );
2534 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2536 httpd_ClientTlsHsIn( cl );
2538 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2540 httpd_ClientTlsHsOut( cl );
2543 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2544 cl->i_state == HTTPD_CLIENT_SENDING &&
2545 FD_ISSET( cl->fd, &fds_read ) )
2547 cl->b_read_waiting = VLC_TRUE;
2550 vlc_mutex_unlock( &host->lock );
2554 #ifndef HAVE_GETADDRINFO
2555 static int BuildAddr( struct sockaddr_in * p_socket,
2556 const char * psz_address, int i_port )
2559 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
2560 p_socket->sin_family = AF_INET; /* family */
2561 p_socket->sin_port = htons( (uint16_t)i_port );
2564 p_socket->sin_addr.s_addr = INADDR_ANY;
2568 struct hostent * p_hostent;
2570 /* Try to convert address directly from in_addr - this will work if
2571 * psz_address is dotted decimal. */
2572 #ifdef HAVE_ARPA_INET_H
2573 if( !inet_aton( psz_address, &p_socket->sin_addr ) )
2575 p_socket->sin_addr.s_addr = inet_addr( psz_address );
2577 /* if( p_socket->sin_addr.s_addr == INADDR_NONE )*/
2578 if( p_socket->sin_addr.s_addr == INADDR_BROADCAST )
2581 /* We have a fqdn, try to find its address */
2582 if ( (p_hostent = gethostbyname( psz_address )) == NULL )
2587 /* Copy the first address of the host in the socket address */
2588 memcpy( &((struct sockaddr_in *)p_socket)->sin_addr, p_hostent->h_addr_list[0],
2589 p_hostent->h_length );
2596 static int GetAddrPort( const struct sockaddr_storage *p_ss )
2600 switch (p_ss->ss_family)
2604 i_port = ((const struct sockaddr_in6 *)p_ss)->sin6_port;
2609 i_port = ((const struct sockaddr_in *)p_ss)->sin_port;
2616 return ntohs( i_port );
2619 #else /* ENABLE_HTTPD */
2621 /* We just define an empty wrapper */
2622 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2625 msg_Err( a, "HTTP daemon support is disabled" );
2628 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2630 msg_Err( a, "HTTP daemon support is disabled" );
2633 void httpd_HostDelete( httpd_host_t *a ){}
2634 httpd_url_t *httpd_UrlNew( httpd_host_t *a, char *b ){ return 0; }
2635 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *a, char *b, char *c,
2636 char *d ){ return 0; }
2637 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2638 httpd_callback_sys_t *d ){ return 0; }
2639 void httpd_UrlDelete( httpd_url_t *a ){}
2641 char *httpd_ClientIP( httpd_client_t *a ){ return 0; }
2642 void httpd_ClientModeStream( httpd_client_t *a ){}
2643 void httpd_ClientModeBidir( httpd_client_t *a ){}
2645 void httpd_FileDelete( httpd_file_t *a ){}
2646 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2647 char *e, httpd_file_callback_t f,
2648 httpd_file_sys_t *g ){ return 0; }
2650 void httpd_RedirectDelete( httpd_redirect_t *a ){}
2651 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2652 char *b, char *c ){ return 0; }
2654 void httpd_StreamDelete( httpd_stream_t *a ){}
2655 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2656 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2657 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2658 char *d, char *e ){ return 0; }
2660 void httpd_MsgInit ( httpd_message_t *a ){}
2661 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... ){}
2662 char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
2663 void httpd_MsgClean( httpd_message_t *a ){}
2665 #endif /* ENABLE_HTTPD */