1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * RĂ©mi Denis-Courmont <rem # videolan.org>
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"
46 #if defined( UNDER_CE )
48 #elif defined( WIN32 )
49 # include <winsock2.h>
50 # include <ws2tcpip.h>
52 # include <netdb.h> /* hostent ... */
53 # include <sys/socket.h>
54 /* FIXME: should not be needed */
55 # include <netinet/in.h>
56 # ifdef HAVE_ARPA_INET_H
57 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
62 typedef struct httpd_t httpd_t;
64 typedef struct httpd_host_t httpd_host_t;
65 typedef struct httpd_url_t httpd_url_t;
66 typedef struct httpd_client_t httpd_client_t;
75 /* channel communication */
91 /* just to track the count of MSG */
104 httpd_client_t *cl; /* NULL if not throught a connection e vlc internal */
116 char *psz_args; /* FIXME find a clean way to handle GET(psz_args) and POST(body) through the same code */
118 /* for rtp over rtsp */
128 int64_t i_body_offset;
134 typedef struct httpd_callback_sys_t httpd_callback_sys_t;
135 /* answer could be null, int this case no answer is requested */
136 typedef int (*httpd_callback_t)( httpd_callback_sys_t *, httpd_client_t *, httpd_message_t *answer, httpd_message_t *query );
139 /* create a new host */
140 httpd_host_t *httpd_HostNew( vlc_object_t *, char *psz_host, int i_port );
142 void httpd_HostDelete( httpd_host_t * );
144 /* register a new url */
145 httpd_url_t *httpd_UrlNew( httpd_host_t *, char *psz_url );
146 /* register callback on a url */
147 int httpd_UrlCatch( httpd_url_t *, int i_msg,
148 httpd_callback_t, httpd_callback_sys_t * );
150 void httpd_UrlDelete( httpd_url_t * );
153 void httpd_ClientModeStream( httpd_client_t *cl );
154 void httpd_ClientModeBidir( httpd_client_t *cl );
155 static void httpd_ClientClean( httpd_client_t *cl );
158 typedef struct httpd_file_t httpd_file_t;
159 typedef struct httpd_file_sys_t httpd_file_sys_t;
160 typedef int (*httpd_file_callback_t)( httpd_file_sys_t*, httpd_file_t *, uint8_t *psz_request, uint8_t **pp_data, int *pi_data );
161 httpd_file_t *httpd_FileNew( httpd_host_t *,
162 char *psz_url, char *psz_mime,
163 char *psz_user, char *psz_password,
164 httpd_file_callback_t pf_fill,
165 httpd_file_sys_t *p_sys );
166 void httpd_FileDelete( httpd_file_t * );
168 typedef struct httpd_redirect_t httpd_redirect_t;
169 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *, char *psz_url_dst, char *psz_url_src );
170 void httpd_RedirectDelete( httpd_redirect_t * );
173 typedef struct httpd_stream_t httpd_stream_t;
174 httpd_stream_t *httpd_StreamNew( httpd_host_t * );
175 int httpd_StreamHeader( httpd_stream_t *, uint8_t *p_data, int i_data );
176 int httpd_StreamSend( httpd_stream_t *, uint8_t *p_data, int i_data );
177 void httpd_StreamDelete( httpd_stream_t * );
180 /* Msg functions facilities */
181 void httpd_MsgInit( httpd_message_t * );
182 void httpd_MsgAdd( httpd_message_t *, char *psz_name, char *psz_value, ... );
183 /* return "" if not found. The string is not allocated */
184 char *httpd_MsgGet( httpd_message_t *, char *psz_name );
185 void httpd_MsgClean( httpd_message_t * );
193 /* vlc_mutex_t lock; */
199 static void httpd_ClientClean( httpd_client_t *cl );
201 /* each host run in his own thread */
211 /* address/port and socket for listening at connections */
218 /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
219 * This will slow down the url research but make my live easier
220 * All url will have their cb trigger, but only the first one can answer
226 httpd_client_t **client;
246 httpd_callback_sys_t *p_sys;
247 } catch[HTTPD_MSG_MAX];
253 HTTPD_CLIENT_RECEIVING,
254 HTTPD_CLIENT_RECEIVE_DONE,
256 HTTPD_CLIENT_SENDING,
257 HTTPD_CLIENT_SEND_DONE,
259 HTTPD_CLIENT_WAITING,
263 HTTPD_CLIENT_TLS_HS_IN,
264 HTTPD_CLIENT_TLS_HS_OUT
269 HTTPD_CLIENT_FILE, /* default */
270 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
271 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
274 struct httpd_client_t
280 struct sockaddr_storage sock;
286 int b_read_waiting; /* stop as soon as possible sending */
288 mtime_t i_activity_date;
289 mtime_t i_activity_timeout;
291 /* buffer for reading header */
297 httpd_message_t query; /* client -> httpd */
298 httpd_message_t answer; /* httpd -> client */
301 tls_session_t *p_tls;
305 /*****************************************************************************
307 *****************************************************************************/
308 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
309 static void b64_decode( char *dest, char *src )
314 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
315 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
316 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
317 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
318 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
319 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
320 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
321 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
322 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
323 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
324 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
325 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
326 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
327 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
328 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
329 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
332 for( i_level = 0; *src != '\0'; src++ )
336 c = b64[(unsigned int)*src];
348 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
352 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
356 *dest++ = ( ( last &0x03 ) << 6 ) | c;
368 const char *psz_mime;
371 { ".htm", "text/html" },
372 { ".html", "text/html" },
373 { ".txt", "text/plain" },
374 { ".xml", "text/xml" },
375 { ".dtd", "text/dtd" },
377 { ".css", "text/css" },
380 { ".gif", "image/gif" },
381 { ".jpe", "image/jpeg" },
382 { ".jpg", "image/jpeg" },
383 { ".jpeg", "image/jpeg" },
384 { ".png", "image/png" },
385 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
388 { ".avi", "video/avi" },
389 { ".asf", "video/x-ms-asf" },
390 { ".m1a", "audio/mpeg" },
391 { ".m2a", "audio/mpeg" },
392 { ".m1v", "video/mpeg" },
393 { ".m2v", "video/mpeg" },
394 { ".mp2", "audio/mpeg" },
395 { ".mp3", "audio/mpeg" },
396 { ".mpa", "audio/mpeg" },
397 { ".mpg", "video/mpeg" },
398 { ".mpeg", "video/mpeg" },
399 { ".mpe", "video/mpeg" },
400 { ".mov", "video/quicktime" },
401 { ".moov", "video/quicktime" },
402 { ".ogg", "application/ogg" },
403 { ".ogm", "application/ogg" },
404 { ".wav", "audio/wav" },
405 { ".wma", "audio/x-ms-wma" },
406 { ".wmv", "video/x-ms-wmv" },
412 static const char *httpd_MimeFromUrl( const char *psz_url )
417 psz_ext = strrchr( psz_url, '.' );
422 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
424 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
426 return http_mime[i].psz_mime;
430 return "application/octet-stream";
433 /*****************************************************************************
434 * High Level Functions: httpd_file_t
435 *****************************************************************************/
443 httpd_file_callback_t pf_fill;
444 httpd_file_sys_t *p_sys;
449 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
451 httpd_file_t *file = (httpd_file_t*)p_sys;
452 uint8_t *psz_args = query->psz_args;
453 uint8_t **pp_body, *p_body;
454 int *pi_body, i_body;
456 if( answer == NULL || query == NULL )
460 answer->i_proto = HTTPD_PROTO_HTTP;
461 answer->i_version= query->i_version;
462 answer->i_type = HTTPD_MSG_ANSWER;
464 answer->i_status = 200;
465 answer->psz_status = strdup( "OK" );
467 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
468 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
470 if( query->i_type != HTTPD_MSG_HEAD )
472 pp_body = &answer->p_body;
473 pi_body = &answer->i_body;
477 /* The file still needs to be executed. */
484 if( query->i_type == HTTPD_MSG_POST )
486 /* msg_Warn not supported */
489 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
491 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
496 /* We respect client request */
497 if( strcmp( httpd_MsgGet( &cl->query, "Connection" ), "" ) )
499 httpd_MsgAdd( answer, "Connection",
500 httpd_MsgGet( &cl->query, "Connection" ) );
503 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
509 httpd_file_t *httpd_FileNew( httpd_host_t *host,
510 const char *psz_url, const char *psz_mime,
511 const char *psz_user, const char *psz_password,
512 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
513 httpd_file_sys_t *p_sys )
515 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
517 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
518 psz_password, p_acl )
525 file->psz_url = strdup( psz_url );
526 if( psz_mime && *psz_mime )
528 file->psz_mime = strdup( psz_mime );
532 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
535 file->pf_fill = pf_fill;
538 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
539 (httpd_callback_sys_t*)file );
540 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
541 (httpd_callback_sys_t*)file );
542 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
543 (httpd_callback_sys_t*)file );
548 void httpd_FileDelete( httpd_file_t *file )
550 httpd_UrlDelete( file->url );
552 free( file->psz_url );
553 free( file->psz_mime );
558 /*****************************************************************************
559 * High Level Functions: httpd_handler_t (for CGIs)
560 *****************************************************************************/
561 struct httpd_handler_t
565 httpd_handler_callback_t pf_fill;
566 httpd_handler_sys_t *p_sys;
570 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
572 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
573 uint8_t *psz_args = query->psz_args;
574 char psz_remote_addr[100];
576 if( answer == NULL || query == NULL )
580 answer->i_proto = HTTPD_PROTO_NONE;
581 answer->i_type = HTTPD_MSG_ANSWER;
583 /* We do it ourselves, thanks */
584 answer->i_status = 0;
585 answer->psz_status = NULL;
587 switch( cl->sock.ss_family )
589 #ifdef HAVE_INET_PTON
591 inet_ntop( cl->sock.ss_family,
592 &((struct sockaddr_in *)&cl->sock)->sin_addr,
593 psz_remote_addr, sizeof(psz_remote_addr) );
596 inet_ntop( cl->sock.ss_family,
597 &((struct sockaddr_in6 *)&cl->sock)->sin6_addr,
598 psz_remote_addr, sizeof(psz_remote_addr) );
603 char *psz_tmp = inet_ntoa( ((struct sockaddr_in *)&cl->sock)->sin_addr );
604 strncpy( psz_remote_addr, psz_tmp, sizeof(psz_remote_addr) );
609 psz_remote_addr[0] = '\0';
612 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
613 query->i_type, query->p_body, query->i_body,
614 psz_remote_addr, NULL,
615 &answer->p_body, &answer->i_body );
617 if( query->i_type == HTTPD_MSG_HEAD )
619 char *p = answer->p_body;
620 while ( (p = strchr( p, '\r' )) != NULL )
622 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
623 && p[3] && p[3] == '\n' )
631 answer->i_body = strlen(answer->p_body) + 1;
632 answer->p_body = realloc( answer->p_body, answer->i_body );
636 if( strncmp( answer->p_body, "HTTP/1.", 7 ) )
638 int i_status, i_headers;
639 char *psz_headers, *psz_new, *psz_status;
641 if( !strncmp( answer->p_body, "Status: ", 8 ) )
644 i_status = strtol( &answer->p_body[8], &psz_headers, 0 );
645 if( *psz_headers ) psz_headers++;
646 if( *psz_headers ) psz_headers++;
647 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
652 psz_headers = answer->p_body;
653 i_headers = answer->i_body;
661 psz_status = "Unauthorized";
664 psz_status = "Undefined";
667 snprintf( psz_code, sizeof(psz_code), "%d", i_status );
668 answer->i_body = sizeof("HTTP/1.0 \r\n") + strlen(psz_code)
669 + strlen(psz_status) + i_headers - 1;
670 psz_new = malloc( answer->i_body + 1);
671 sprintf( psz_new, "HTTP/1.0 %s %s\r\n", psz_code, psz_status );
672 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
673 free( answer->p_body );
674 answer->p_body = psz_new;
680 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
681 const char *psz_user,
682 const char *psz_password,
683 const vlc_acl_t *p_acl,
684 httpd_handler_callback_t pf_fill,
685 httpd_handler_sys_t *p_sys )
687 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
689 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
690 psz_password, p_acl )
697 handler->pf_fill = pf_fill;
698 handler->p_sys = p_sys;
700 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
701 (httpd_callback_sys_t*)handler );
702 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
703 (httpd_callback_sys_t*)handler );
704 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
705 (httpd_callback_sys_t*)handler );
710 void httpd_HandlerDelete( httpd_handler_t *handler )
712 httpd_UrlDelete( handler->url );
716 /*****************************************************************************
717 * High Level Functions: httpd_redirect_t
718 *****************************************************************************/
719 struct httpd_redirect_t
725 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
726 httpd_client_t *cl, httpd_message_t *answer,
727 httpd_message_t *query )
729 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
732 if( answer == NULL || query == NULL )
736 answer->i_proto = query->i_proto;
737 answer->i_version= query->i_version;
738 answer->i_type = HTTPD_MSG_ANSWER;
739 answer->i_status = 301;
740 answer->psz_status = strdup( "Moved Permanently" );
742 p = answer->p_body = malloc( 1000 + strlen( rdir->psz_dst ) );
743 p += sprintf( (char *)p,
744 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
745 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
746 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
749 "<title>Redirection</title>\n"
753 "<a href=\"%s\">redirected</a></h1>\n"
755 "<p><a href=\"http://www.videolan.org\">VideoLAN</a>\n</p>"
758 "</html>\n", rdir->psz_dst );
759 answer->i_body = p - answer->p_body;
761 /* XXX check if it's ok or we need to set an absolute url */
762 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
764 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
769 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
770 const char *psz_url_src )
772 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
774 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
779 rdir->psz_dst = strdup( psz_url_dst );
781 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
782 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
783 (httpd_callback_sys_t*)rdir );
784 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
785 (httpd_callback_sys_t*)rdir );
786 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
787 (httpd_callback_sys_t*)rdir );
788 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
789 (httpd_callback_sys_t*)rdir );
793 void httpd_RedirectDelete( httpd_redirect_t *rdir )
795 httpd_UrlDelete( rdir->url );
796 free( rdir->psz_dst );
800 /*****************************************************************************
801 * High Level Funtions: httpd_stream_t
802 *****************************************************************************/
803 struct httpd_stream_t
810 /* Header to send as first packet */
814 /* circular buffer */
815 int i_buffer_size; /* buffer size, can't be reallocated smaller */
816 uint8_t *p_buffer; /* buffer */
817 int64_t i_buffer_pos; /* absolute position from begining */
818 int64_t i_buffer_last_pos; /* a new connection will start with that */
821 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
822 httpd_client_t *cl, httpd_message_t *answer,
823 httpd_message_t *query )
825 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
827 if( answer == NULL || query == NULL || cl == NULL )
831 if( answer->i_body_offset > 0 )
837 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
838 answer->i_body_offset );
841 if( answer->i_body_offset >= stream->i_buffer_pos )
843 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
844 return VLC_EGENERIC; /* wait, no data available */
846 if( answer->i_body_offset + stream->i_buffer_size <
847 stream->i_buffer_pos )
849 /* this client isn't fast enough */
850 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
851 answer->i_body_offset, stream->i_buffer_last_pos );
852 answer->i_body_offset = stream->i_buffer_last_pos;
855 i_pos = answer->i_body_offset % stream->i_buffer_size;
856 i_write = stream->i_buffer_pos - answer->i_body_offset;
857 if( i_write > 10000 )
861 else if( i_write <= 0 )
863 return VLC_EGENERIC; /* wait, no data available */
866 /* Don't go past the end of the circular buffer */
867 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
869 /* using HTTPD_MSG_ANSWER -> data available */
870 answer->i_proto = HTTPD_PROTO_HTTP;
871 answer->i_version= 0;
872 answer->i_type = HTTPD_MSG_ANSWER;
874 answer->i_body = i_write;
875 answer->p_body = malloc( i_write );
876 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
878 answer->i_body_offset += i_write;
884 answer->i_proto = HTTPD_PROTO_HTTP;
885 answer->i_version= 0;
886 answer->i_type = HTTPD_MSG_ANSWER;
888 answer->i_status = 200;
889 answer->psz_status = strdup( "OK" );
891 if( query->i_type != HTTPD_MSG_HEAD )
893 httpd_ClientModeStream( cl );
894 vlc_mutex_lock( &stream->lock );
895 /* Send the header */
896 if( stream->i_header > 0 )
898 answer->i_body = stream->i_header;
899 answer->p_body = malloc( stream->i_header );
900 memcpy( answer->p_body, stream->p_header, stream->i_header );
902 answer->i_body_offset = stream->i_buffer_last_pos;
903 vlc_mutex_unlock( &stream->lock );
907 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
908 answer->i_body_offset = 0;
911 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
913 vlc_bool_t b_xplaystream = VLC_FALSE;
916 httpd_MsgAdd( answer, "Content-type", "%s",
917 "application/octet-stream" );
918 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
919 httpd_MsgAdd( answer, "Pragma", "no-cache" );
920 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
921 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
923 /* Check if there is a xPlayStrm=1 */
924 for( i = 0; i < query->i_name; i++ )
926 if( !strcasecmp( query->name[i], "Pragma" ) &&
927 strstr( query->value[i], "xPlayStrm=1" ) )
929 b_xplaystream = VLC_TRUE;
935 answer->i_body_offset = 0;
940 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
942 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
947 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
948 const char *psz_url, const char *psz_mime,
949 const char *psz_user, const char *psz_password,
950 const vlc_acl_t *p_acl )
952 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
954 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
955 psz_password, p_acl )
961 vlc_mutex_init( host, &stream->lock );
962 if( psz_mime && *psz_mime )
964 stream->psz_mime = strdup( psz_mime );
968 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
970 stream->i_header = 0;
971 stream->p_header = NULL;
972 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
973 stream->p_buffer = malloc( stream->i_buffer_size );
974 /* We set to 1 to make life simpler
975 * (this way i_body_offset can never be 0) */
976 stream->i_buffer_pos = 1;
977 stream->i_buffer_last_pos = 1;
979 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
980 (httpd_callback_sys_t*)stream );
981 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
982 (httpd_callback_sys_t*)stream );
983 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
984 (httpd_callback_sys_t*)stream );
989 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
991 vlc_mutex_lock( &stream->lock );
992 if( stream->p_header )
994 free( stream->p_header );
995 stream->p_header = NULL;
997 stream->i_header = i_data;
1000 stream->p_header = malloc( i_data );
1001 memcpy( stream->p_header, p_data, i_data );
1003 vlc_mutex_unlock( &stream->lock );
1008 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
1013 if( i_data < 0 || p_data == NULL )
1017 vlc_mutex_lock( &stream->lock );
1019 /* save this pointer (to be used by new connection) */
1020 stream->i_buffer_last_pos = stream->i_buffer_pos;
1022 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
1028 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
1030 /* Ok, we can't go past the end of our buffer */
1031 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
1033 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
1038 stream->i_buffer_pos += i_data;
1040 vlc_mutex_unlock( &stream->lock );
1044 void httpd_StreamDelete( httpd_stream_t *stream )
1046 httpd_UrlDelete( stream->url );
1047 vlc_mutex_destroy( &stream->lock );
1048 if( stream->psz_mime ) free( stream->psz_mime );
1049 if( stream->p_header ) free( stream->p_header );
1050 if( stream->p_buffer ) free( stream->p_buffer );
1055 /*****************************************************************************
1057 *****************************************************************************/
1058 static void httpd_HostThread( httpd_host_t * );
1060 /* create a new host */
1061 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
1064 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
1068 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
1070 const char *psz_cert, const char *psz_key,
1071 const char *psz_ca, const char *psz_crl )
1075 tls_server_t *p_tls;
1077 vlc_value_t lockval;
1080 if( psz_hostname == NULL )
1083 psz_host = strdup( psz_hostname );
1084 if( psz_host == NULL )
1086 msg_Err( p_this, "memory error" );
1090 /* to be sure to avoid multiple creation */
1091 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
1092 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
1093 vlc_mutex_lock( lockval.p_address );
1095 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
1097 msg_Info( p_this, "creating httpd" );
1098 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
1100 vlc_mutex_unlock( lockval.p_address );
1108 vlc_object_yield( httpd );
1109 vlc_object_attach( httpd, p_this->p_vlc );
1112 /* verify if it already exist */
1113 for( i = httpd->i_host - 1; i >= 0; i-- )
1115 host = httpd->host[i];
1117 /* cannot mix TLS and non-TLS hosts */
1118 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1119 || ( host->i_port != i_port )
1120 || strcmp( host->psz_hostname, psz_hostname ) )
1126 vlc_mutex_unlock( lockval.p_address );
1132 /* determine TLS configuration */
1133 if ( psz_cert != NULL )
1135 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1136 if ( p_tls == NULL )
1138 msg_Err( p_this, "TLS initialization error" );
1142 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1144 msg_Err( p_this, "TLS CA error" );
1148 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1150 msg_Err( p_this, "TLS CRL error" );
1157 /* create the new host */
1158 host = vlc_object_create( p_this, sizeof( httpd_host_t ) );
1159 host->httpd = httpd;
1160 vlc_mutex_init( httpd, &host->lock );
1163 host->fd = net_ListenTCP( p_this, psz_host, i_port );
1164 if( host->fd == NULL )
1166 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1170 host->i_port = i_port;
1171 host->psz_hostname = psz_host;
1176 host->client = NULL;
1178 host->p_tls = p_tls;
1180 /* create the thread */
1181 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1182 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1184 msg_Err( p_this, "cannot spawn http host thread" );
1188 /* now add it to httpd */
1189 TAB_APPEND( httpd->i_host, httpd->host, host );
1190 vlc_mutex_unlock( lockval.p_address );
1196 if( httpd->i_host <= 0 )
1198 vlc_object_release( httpd );
1199 vlc_object_detach( httpd );
1200 vlc_object_destroy( httpd );
1202 vlc_mutex_unlock( lockval.p_address );
1206 net_ListenClose( host->fd );
1207 vlc_mutex_destroy( &host->lock );
1208 vlc_object_destroy( host );
1212 tls_ServerDelete( p_tls );
1218 void httpd_HostDelete( httpd_host_t *host )
1220 httpd_t *httpd = host->httpd;
1221 vlc_value_t lockval;
1224 msg_Dbg( host, "httpd_HostDelete" );
1226 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1227 vlc_mutex_lock( lockval.p_address );
1230 if( host->i_ref > 0 )
1233 vlc_mutex_unlock( lockval.p_address );
1234 msg_Dbg( host, "httpd_HostDelete: host still used" );
1237 TAB_REMOVE( httpd->i_host, httpd->host, host );
1239 msg_Dbg( host, "httpd_HostDelete: host removed from http" );
1242 vlc_thread_join( host );
1244 msg_Dbg( host, "httpd_HostDelete: host thread joined" );
1246 for( i = 0; i < host->i_url; i++ )
1248 msg_Err( host, "url still registered:%s", host->url[i]->psz_url );
1250 for( i = 0; i < host->i_client; i++ )
1252 httpd_client_t *cl = host->client[i];
1253 msg_Warn( host, "client still connected" );
1254 httpd_ClientClean( cl );
1255 TAB_REMOVE( host->i_client, host->client, cl );
1261 if( host->p_tls != NULL)
1262 tls_ServerDelete( host->p_tls );
1264 net_ListenClose( host->fd );
1265 free( host->psz_hostname );
1267 vlc_mutex_destroy( &host->lock );
1268 vlc_object_destroy( host );
1270 if( httpd->i_host <= 0 )
1272 msg_Info( httpd, "httpd doesn't reference any host, deleting" );
1273 vlc_object_release( httpd );
1274 vlc_object_detach( httpd );
1275 vlc_object_destroy( httpd );
1277 vlc_mutex_unlock( lockval.p_address );
1280 /* register a new url */
1281 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1282 const char *psz_user, const char *psz_password,
1283 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1288 vlc_mutex_lock( &host->lock );
1291 for( i = 0; i < host->i_url; i++ )
1293 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1295 msg_Warn( host->httpd,
1296 "cannot add '%s' (url already defined)", psz_url );
1297 vlc_mutex_unlock( &host->lock );
1303 url = malloc( sizeof( httpd_url_t ) );
1306 vlc_mutex_init( host->httpd, &url->lock );
1307 url->psz_url = strdup( psz_url );
1308 url->psz_user = strdup( psz_user ? psz_user : "" );
1309 url->psz_password = strdup( psz_password ? psz_password : "" );
1310 url->p_acl = ACL_Duplicate( host, p_acl );
1311 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1313 url->catch[i].cb = NULL;
1314 url->catch[i].p_sys = NULL;
1317 TAB_APPEND( host->i_url, host->url, url );
1318 vlc_mutex_unlock( &host->lock );
1323 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1324 const char *psz_user, const char *psz_password,
1325 const vlc_acl_t *p_acl )
1327 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1328 psz_password, p_acl, VLC_FALSE );
1331 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1332 const char *psz_user, const char *psz_password,
1333 const vlc_acl_t *p_acl )
1335 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1336 psz_password, p_acl, VLC_TRUE );
1339 /* register callback on a url */
1340 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1341 httpd_callback_sys_t *p_sys )
1343 vlc_mutex_lock( &url->lock );
1344 url->catch[i_msg].cb = cb;
1345 url->catch[i_msg].p_sys= p_sys;
1346 vlc_mutex_unlock( &url->lock );
1353 void httpd_UrlDelete( httpd_url_t *url )
1355 httpd_host_t *host = url->host;
1358 vlc_mutex_lock( &host->lock );
1359 TAB_REMOVE( host->i_url, host->url, url );
1361 vlc_mutex_destroy( &url->lock );
1362 free( url->psz_url );
1363 free( url->psz_user );
1364 free( url->psz_password );
1365 ACL_Destroy( url->p_acl );
1367 for( i = 0; i < host->i_client; i++ )
1369 httpd_client_t *client = host->client[i];
1371 if( client->url == url )
1373 /* TODO complete it */
1374 msg_Warn( host, "force closing connections" );
1375 httpd_ClientClean( client );
1376 TAB_REMOVE( host->i_client, host->client, client );
1382 vlc_mutex_unlock( &host->lock );
1385 void httpd_MsgInit( httpd_message_t *msg )
1388 msg->i_type = HTTPD_MSG_NONE;
1389 msg->i_proto = HTTPD_PROTO_NONE;
1390 msg->i_version = -1;
1393 msg->psz_status = NULL;
1395 msg->psz_url = NULL;
1396 msg->psz_args = NULL;
1398 msg->i_channel = -1;
1405 msg->i_body_offset = 0;
1410 void httpd_MsgClean( httpd_message_t *msg )
1414 if( msg->psz_status )
1416 free( msg->psz_status );
1420 free( msg->psz_url );
1424 free( msg->psz_args );
1426 for( i = 0; i < msg->i_name; i++ )
1428 free( msg->name[i] );
1429 free( msg->value[i] );
1441 free( msg->p_body );
1443 httpd_MsgInit( msg );
1446 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1450 for( i = 0; i < msg->i_name; i++ )
1452 if( !strcasecmp( msg->name[i], name ))
1454 return msg->value[i];
1459 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1464 va_start( args, psz_value );
1465 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
1466 vasprintf( &value, psz_value, args );
1469 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1470 value = calloc( i_size, sizeof( char ) );
1471 vsnprintf( value, i_size, psz_value, args );
1472 value[i_size - 1] = 0;
1477 name = strdup( name );
1479 TAB_APPEND( msg->i_name, msg->name, name );
1480 TAB_APPEND( msg->i_value, msg->value, value );
1483 static void httpd_ClientInit( httpd_client_t *cl )
1485 cl->i_state = HTTPD_CLIENT_RECEIVING;
1486 cl->i_activity_date = mdate();
1487 cl->i_activity_timeout = I64C(10000000);
1488 cl->i_buffer_size = 10000;
1490 cl->p_buffer = malloc( cl->i_buffer_size );
1491 cl->i_mode = HTTPD_CLIENT_FILE;
1492 cl->b_read_waiting = VLC_FALSE;
1494 httpd_MsgInit( &cl->query );
1495 httpd_MsgInit( &cl->answer );
1498 void httpd_ClientModeStream( httpd_client_t *cl )
1500 cl->i_mode = HTTPD_CLIENT_STREAM;
1503 void httpd_ClientModeBidir( httpd_client_t *cl )
1505 cl->i_mode = HTTPD_CLIENT_BIDIR;
1508 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
1510 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1513 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
1515 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1518 static void httpd_ClientClean( httpd_client_t *cl )
1522 if( cl->p_tls != NULL )
1523 tls_ServerSessionClose( cl->p_tls );
1524 net_Close( cl->fd );
1528 httpd_MsgClean( &cl->answer );
1529 httpd_MsgClean( &cl->query );
1533 free( cl->p_buffer );
1534 cl->p_buffer = NULL;
1538 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1540 tls_session_t *p_tls )
1542 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1545 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1546 cl->i_sock_size = i_sock_size;
1550 httpd_ClientInit( cl );
1556 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1558 tls_session_t *p_tls;
1562 return tls_Recv( p_tls, p, i_len );
1564 return recv( cl->fd, p, i_len, 0 );
1568 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1570 tls_session_t *p_tls;
1574 return tls_Send( p_tls, p, i_len );
1576 return send( cl->fd, p, i_len, 0 );
1580 static void httpd_ClientRecv( httpd_client_t *cl )
1584 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1586 /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1587 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1591 cl->i_buffer += i_len;
1594 if( cl->i_buffer >= 4 )
1596 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1598 if( cl->p_buffer[0] == '$' )
1600 /* RTSP (rtp over rtsp) */
1601 cl->query.i_proto = HTTPD_PROTO_RTSP;
1602 cl->query.i_type = HTTPD_MSG_CHANNEL;
1603 cl->query.i_channel = cl->p_buffer[1];
1604 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1605 cl->query.p_body = malloc( cl->query.i_body );
1609 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1611 cl->query.i_proto = HTTPD_PROTO_HTTP;
1612 cl->query.i_type = HTTPD_MSG_ANSWER;
1614 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1616 cl->query.i_proto = HTTPD_PROTO_RTSP;
1617 cl->query.i_type = HTTPD_MSG_ANSWER;
1619 else if( !memcmp( cl->p_buffer, "GET", 3 ) ||
1620 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1621 !memcmp( cl->p_buffer, "POST", 4 ) )
1623 cl->query.i_proto = HTTPD_PROTO_HTTP;
1624 cl->query.i_type = HTTPD_MSG_NONE;
1628 cl->query.i_proto = HTTPD_PROTO_RTSP;
1629 cl->query.i_type = HTTPD_MSG_NONE;
1633 else if( cl->query.i_body > 0 )
1635 /* we are reading the body of a request or a channel */
1636 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1637 cl->query.i_body - cl->i_buffer );
1640 cl->i_buffer += i_len;
1642 if( cl->i_buffer >= cl->query.i_body )
1644 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1649 /* we are reading a header -> char by char */
1652 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1659 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1661 cl->i_buffer_size += 1024;
1662 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1664 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1665 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1669 /* we have finished the header so parse it and set i_body */
1670 cl->p_buffer[cl->i_buffer] = '\0';
1672 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1675 * assume strlen( "HTTP/1.x" ) = 8
1677 cl->query.i_status =
1678 strtol( (char *)&cl->p_buffer[8],
1684 cl->query.psz_status = strdup( p );
1696 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1697 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1698 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1700 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1701 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1702 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1703 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1704 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1705 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1707 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1712 cl->query.i_type = HTTPD_MSG_NONE;
1714 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1716 for( i = 0; msg_type[i].name != NULL; i++ )
1718 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1719 strlen( msg_type[i].name ) ) )
1721 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1722 cl->query.i_type = msg_type[i].i_type;
1723 if( cl->query.i_proto != msg_type[i].i_proto )
1726 cl->query.i_proto = HTTPD_PROTO_NONE;
1727 cl->query.i_type = HTTPD_MSG_NONE;
1734 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1736 cl->query.i_proto = HTTPD_PROTO_HTTP;
1738 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1740 cl->query.i_proto = HTTPD_PROTO_RTSP;
1752 p2 = strchr( p, ' ' );
1757 if( !strncasecmp( p, "rtsp:", 5 ) )
1759 /* for rtsp url, you have rtsp://localhost:port/path */
1761 while( *p == '/' ) p++;
1762 while( *p && *p != '/' ) p++;
1764 cl->query.psz_url = strdup( p );
1765 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1768 cl->query.psz_args = (uint8_t *)strdup( p3 );
1776 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1778 cl->query.i_proto = HTTPD_PROTO_HTTP;
1779 cl->query.i_version = atoi( p2+7 );
1781 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1783 cl->query.i_proto = HTTPD_PROTO_RTSP;
1784 cl->query.i_version = atoi( p2+7 );
1792 p = strchr( p, '\n' );
1796 while( *p == '\n' || *p == '\r' )
1800 while( p && *p != '\0' )
1803 char *eol = p = strchr( p, '\n' );
1806 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1811 if( ( colon = strchr( line, ':' ) ) )
1817 while( *colon == ' ' )
1821 name = strdup( line );
1822 value = strdup( colon );
1824 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1825 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1827 if( !strcasecmp( name, "Content-Length" ) )
1829 cl->query.i_body = atol( value );
1836 while( *p == '\n' || *p == '\r' )
1843 if( cl->query.i_body > 0 )
1845 /* TODO Mhh, handle the case client will only send a request and close the connection
1846 * to mark and of body (probably only RTSP) */
1847 cl->query.p_body = malloc( cl->query.i_body );
1852 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1858 /* check if the client is to be set to dead */
1859 #if defined( WIN32 ) || defined( UNDER_CE )
1860 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1862 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1865 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1867 /* connection closed -> end of data */
1868 if( cl->query.i_body > 0 )
1870 cl->query.i_body = cl->i_buffer;
1872 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1876 cl->i_state = HTTPD_CLIENT_DEAD;
1879 cl->i_activity_date = mdate();
1881 /* XXX: for QT I have to disable timeout. Try to find why */
1882 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1883 cl->i_activity_timeout = 0;
1885 /* Debugging only */
1886 /*if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1890 fprintf( stderr, "received new request\n" );
1891 fprintf( stderr, " - proto=%s\n",
1892 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1893 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1894 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1895 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1897 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1898 cl->query.psz_status );
1900 else if( cl->query.i_type != HTTPD_MSG_NONE )
1902 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1904 for( i = 0; i < cl->query.i_name; i++ )
1906 fprintf( stderr, " - option name='%s' value='%s'\n",
1907 cl->query.name[i], cl->query.value[i] );
1913 static void httpd_ClientSend( httpd_client_t *cl )
1918 if( cl->i_buffer < 0 )
1920 /* We need to create the header */
1924 i_size = strlen( "HTTP/1.") + 10 + 10 +
1925 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1926 for( i = 0; i < cl->answer.i_name; i++ )
1928 i_size += strlen( cl->answer.name[i] ) + 2 +
1929 strlen( cl->answer.value[i] ) + 2;
1932 if( cl->i_buffer_size < i_size )
1934 cl->i_buffer_size = i_size;
1935 free( cl->p_buffer );
1936 cl->p_buffer = malloc( i_size );
1938 p = (char *)cl->p_buffer;
1940 p += sprintf( p, "%s/1.%d %d %s\r\n",
1941 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1942 cl->answer.i_version,
1943 cl->answer.i_status, cl->answer.psz_status );
1944 for( i = 0; i < cl->answer.i_name; i++ )
1946 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1947 cl->answer.value[i] );
1949 p += sprintf( p, "\r\n" );
1952 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1954 /*fprintf( stderr, "sending answer\n" );
1955 fprintf( stderr, "%s", cl->p_buffer );*/
1958 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1959 cl->i_buffer_size - cl->i_buffer );
1962 cl->i_activity_date = mdate();
1963 cl->i_buffer += i_len;
1965 if( cl->i_buffer >= cl->i_buffer_size )
1967 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1968 !cl->b_read_waiting )
1970 /* catch more body data */
1971 int i_msg = cl->query.i_type;
1972 int64_t i_offset = cl->answer.i_body_offset;
1974 httpd_MsgClean( &cl->answer );
1975 cl->answer.i_body_offset = i_offset;
1977 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1978 &cl->answer, &cl->query );
1981 if( cl->answer.i_body > 0 )
1983 /* send the body data */
1984 free( cl->p_buffer );
1985 cl->p_buffer = cl->answer.p_body;
1986 cl->i_buffer_size = cl->answer.i_body;
1989 cl->answer.i_body = 0;
1990 cl->answer.p_body = NULL;
1995 cl->i_state = HTTPD_CLIENT_SEND_DONE;
2001 #if defined( WIN32 ) || defined( UNDER_CE )
2002 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
2004 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
2008 cl->i_state = HTTPD_CLIENT_DEAD;
2013 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
2015 switch( tls_SessionContinueHandshake( cl->p_tls ) )
2018 cl->i_state = HTTPD_CLIENT_RECEIVING;
2022 cl->i_state = HTTPD_CLIENT_DEAD;
2027 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
2031 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
2033 switch( tls_SessionContinueHandshake( cl->p_tls ) )
2036 cl->i_state = HTTPD_CLIENT_RECEIVING;
2040 cl->i_state = HTTPD_CLIENT_DEAD;
2045 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
2050 static void httpd_HostThread( httpd_host_t *host )
2052 tls_session_t *p_tls = NULL;
2054 while( !host->b_die )
2056 struct timeval timeout;
2059 /* FIXME: (too) many int variables */
2061 int i_handle_max = 0;
2064 int b_low_delay = 0;
2066 if( host->i_url <= 0 )
2073 /* built a set of handle to select */
2074 FD_ZERO( &fds_read );
2075 FD_ZERO( &fds_write );
2079 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2081 FD_SET( fd, &fds_read );
2082 if( fd > i_handle_max )
2086 /* prepare a new TLS session */
2087 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
2088 p_tls = tls_ServerSessionPrepare( host->p_tls );
2090 /* add all socket that should be read/write and close dead connection */
2091 vlc_mutex_lock( &host->lock );
2092 for( i_client = 0; i_client < host->i_client; i_client++ )
2094 httpd_client_t *cl = host->client[i_client];
2096 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
2097 ( cl->i_state == HTTPD_CLIENT_DEAD ||
2098 ( cl->i_activity_timeout > 0 &&
2099 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
2101 httpd_ClientClean( cl );
2102 TAB_REMOVE( host->i_client, host->client, cl );
2107 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2108 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2110 FD_SET( cl->fd, &fds_read );
2111 i_handle_max = __MAX( i_handle_max, cl->fd );
2113 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2114 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2116 FD_SET( cl->fd, &fds_write );
2117 i_handle_max = __MAX( i_handle_max, cl->fd );
2119 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2121 httpd_message_t *answer = &cl->answer;
2122 httpd_message_t *query = &cl->query;
2123 int i_msg = query->i_type;
2125 httpd_MsgInit( answer );
2127 /* Handle what we received */
2128 if( cl->i_mode != HTTPD_CLIENT_BIDIR &&
2129 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2131 /* we can only receive request from client when not
2134 cl->i_state = HTTPD_CLIENT_DEAD;
2136 else if( i_msg == HTTPD_MSG_ANSWER )
2138 /* We are in BIDIR mode, trigger the callback and then
2139 * check for new data */
2140 if( cl->url && cl->url->catch[i_msg].cb )
2142 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2145 cl->i_state = HTTPD_CLIENT_WAITING;
2147 else if( i_msg == HTTPD_MSG_CHANNEL )
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_OPTIONS )
2163 answer->i_proto = query->i_proto ;
2164 answer->i_type = HTTPD_MSG_ANSWER;
2165 answer->i_version= 0;
2166 answer->i_status = 200;
2167 answer->psz_status = strdup( "Ok" );
2170 answer->p_body = NULL;
2172 i_cseq = atoi( httpd_MsgGet( query, "Cseq" ) );
2173 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2174 httpd_MsgAdd( answer, "Server", "VLC Server" );
2175 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2176 "TEARDOWN, PLAY, PAUSE" );
2177 httpd_MsgAdd( answer, "Content-Length", "%d",
2180 cl->i_buffer = -1; /* Force the creation of the answer in
2181 * httpd_ClientSend */
2182 cl->i_state = HTTPD_CLIENT_SENDING;
2184 else if( i_msg == HTTPD_MSG_NONE )
2186 if( query->i_proto == HTTPD_PROTO_NONE )
2189 cl->i_state = HTTPD_CLIENT_DEAD;
2196 answer->i_proto = query->i_proto ;
2197 answer->i_type = HTTPD_MSG_ANSWER;
2198 answer->i_version= 0;
2199 answer->i_status = 501;
2200 answer->psz_status = strdup( "Unimplemented" );
2202 p = answer->p_body = malloc( 1000 );
2204 p += sprintf( (char *)p,
2205 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2206 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2207 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2210 "<title>Error 501</title>\n"
2213 "<h1>501 Unimplemented</h1>\n"
2215 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2219 answer->i_body = p - answer->p_body;
2220 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2222 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2223 cl->i_state = HTTPD_CLIENT_SENDING;
2228 vlc_bool_t b_auth_failed = VLC_FALSE;
2229 vlc_bool_t b_hosts_failed = VLC_FALSE;
2232 /* Search the url and trigger callbacks */
2233 for( i = 0; i < host->i_url; i++ )
2235 httpd_url_t *url = host->url[i];
2237 if( !strcmp( url->psz_url, query->psz_url ) )
2239 if( url->catch[i_msg].cb )
2241 if( answer && ( url->p_acl != NULL ) )
2243 char ip[NI_MAXNUMERICHOST];
2245 if( httpd_ClientIP( cl, ip ) != NULL )
2247 if( ACL_Check( url->p_acl, ip ) )
2249 b_hosts_failed = VLC_TRUE;
2254 b_hosts_failed = VLC_TRUE;
2257 if( answer && ( *url->psz_user || *url->psz_password ) )
2259 /* create the headers */
2260 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2264 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2265 auth = malloc( strlen(b64) + 1 );
2267 if( !strncasecmp( b64, "BASIC", 5 ) )
2270 while( *b64 == ' ' )
2274 b64_decode( auth, b64 );
2280 if( strcmp( id, auth ) )
2282 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2283 /* We fail for all url */
2284 b_auth_failed = VLC_TRUE;
2294 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2296 if( answer->i_proto == HTTPD_PROTO_NONE )
2298 /* Raw answer from a CGI */
2299 cl->i_buffer = cl->i_buffer_size;
2304 /* only one url can answer */
2306 if( cl->url == NULL )
2318 answer->i_proto = query->i_proto;
2319 answer->i_type = HTTPD_MSG_ANSWER;
2320 answer->i_version= 0;
2321 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2323 if( b_hosts_failed )
2325 answer->i_status = 403;
2326 answer->psz_status = strdup( "Forbidden" );
2328 /* FIXME: lots of code duplication */
2329 p += sprintf( (char *)p,
2330 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2331 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2332 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2335 "<title>Error 403</title>\n"
2338 "<h1>403 Forbidden (%s)</h1>\n"
2340 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2342 "</html>\n", query->psz_url );
2344 else if( b_auth_failed )
2346 answer->i_status = 401;
2347 answer->psz_status = strdup( "Authorization Required" );
2349 p += sprintf( (char *)p,
2350 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2351 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2352 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2355 "<title>Error 401</title>\n"
2358 "<h1>401 Authorization Required (%s)</h1>\n"
2360 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2362 "</html>\n", query->psz_url );
2366 /* no url registered */
2367 answer->i_status = 404;
2368 answer->psz_status = strdup( "Not found" );
2370 p += sprintf( (char *)p,
2371 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2372 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2373 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2376 "<title>Error 404</title>\n"
2379 "<h1>404 Resource not found(%s)</h1>\n"
2381 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2383 "</html>\n", query->psz_url );
2386 answer->i_body = p - answer->p_body;
2387 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2388 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2391 cl->i_state = HTTPD_CLIENT_SENDING;
2394 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2396 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2399 if( ( cl->query.i_proto == HTTPD_PROTO_HTTP &&
2400 ( ( cl->answer.i_version == 0 && !strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Keep-Alive" ) ) ||
2401 ( cl->answer.i_version == 1 && strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) ) ) ||
2402 ( cl->query.i_proto == HTTPD_PROTO_RTSP &&
2403 strcasecmp( httpd_MsgGet( &cl->query, "Connection" ), "Close" ) &&
2404 strcasecmp( httpd_MsgGet( &cl->answer, "Connection" ), "Close" ) ) )
2406 httpd_MsgClean( &cl->query );
2407 httpd_MsgInit( &cl->query );
2410 cl->i_buffer_size = 1000;
2411 free( cl->p_buffer );
2412 cl->p_buffer = malloc( cl->i_buffer_size );
2413 cl->i_state = HTTPD_CLIENT_RECEIVING;
2417 cl->i_state = HTTPD_CLIENT_DEAD;
2419 httpd_MsgClean( &cl->answer );
2421 else if( cl->b_read_waiting )
2423 /* we have a message waiting for us to read it */
2424 httpd_MsgClean( &cl->answer );
2425 httpd_MsgClean( &cl->query );
2428 cl->i_buffer_size = 1000;
2429 free( cl->p_buffer );
2430 cl->p_buffer = malloc( cl->i_buffer_size );
2431 cl->i_state = HTTPD_CLIENT_RECEIVING;
2432 cl->b_read_waiting = VLC_FALSE;
2436 int64_t i_offset = cl->answer.i_body_offset;
2437 httpd_MsgClean( &cl->answer );
2439 cl->answer.i_body_offset = i_offset;
2440 free( cl->p_buffer );
2441 cl->p_buffer = NULL;
2443 cl->i_buffer_size = 0;
2445 cl->i_state = HTTPD_CLIENT_WAITING;
2448 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2450 int64_t i_offset = cl->answer.i_body_offset;
2451 int i_msg = cl->query.i_type;
2453 httpd_MsgInit( &cl->answer );
2454 cl->answer.i_body_offset = i_offset;
2456 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2457 &cl->answer, &cl->query );
2458 if( cl->answer.i_type != HTTPD_MSG_NONE )
2460 /* we have new data, so reenter send mode */
2462 cl->p_buffer = cl->answer.p_body;
2463 cl->i_buffer_size = cl->answer.i_body;
2464 cl->answer.p_body = NULL;
2465 cl->answer.i_body = 0;
2466 cl->i_state = HTTPD_CLIENT_SENDING;
2470 /* we shouldn't wait too long */
2471 b_low_delay = VLC_TRUE;
2475 /* Special for BIDIR mode we also check reading */
2476 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2477 cl->i_state == HTTPD_CLIENT_SENDING )
2479 FD_SET( cl->fd, &fds_read );
2480 i_handle_max = __MAX( i_handle_max, cl->fd );
2483 vlc_mutex_unlock( &host->lock );
2485 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2487 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2489 i_ret = select( i_handle_max + 1,
2490 &fds_read, &fds_write, NULL, &timeout );
2492 if( i_ret == -1 && errno != EINTR )
2494 #if defined(WIN32) || defined(UNDER_CE)
2495 msg_Warn( host, "cannot select sockets (%d)", WSAGetLastError( ) );
2497 msg_Warn( host, "cannot select sockets : %s", strerror( errno ) );
2502 else if( i_ret <= 0 )
2507 /* accept new connections */
2508 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2510 if( FD_ISSET( fd, &fds_read ) )
2512 socklen_t i_sock_size = sizeof( struct sockaddr_storage );
2513 struct sockaddr_storage sock;
2515 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2520 /* set this new socket non-block */
2521 #if defined( WIN32 ) || defined( UNDER_CE )
2523 unsigned long i_dummy = 1;
2524 ioctlsocket( fd, FIONBIO, &i_dummy );
2527 fcntl( fd, F_SETFL, O_NONBLOCK );
2532 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2535 msg_Err( host, "Rejecting TLS connection" );
2541 case 1: /* missing input - most likely */
2542 i_state = HTTPD_CLIENT_TLS_HS_IN;
2545 case 2: /* missing output */
2546 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2555 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2557 vlc_mutex_lock( &host->lock );
2558 TAB_APPEND( host->i_client, host->client, cl );
2559 vlc_mutex_unlock( &host->lock );
2562 cl->i_state = i_state; // override state for TLS
2568 /* now try all others socket */
2569 vlc_mutex_lock( &host->lock );
2570 for( i_client = 0; i_client < host->i_client; i_client++ )
2572 httpd_client_t *cl = host->client[i_client];
2573 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2575 httpd_ClientRecv( cl );
2577 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2579 httpd_ClientSend( cl );
2581 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2583 httpd_ClientTlsHsIn( cl );
2585 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2587 httpd_ClientTlsHsOut( cl );
2590 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2591 cl->i_state == HTTPD_CLIENT_SENDING &&
2592 FD_ISSET( cl->fd, &fds_read ) )
2594 cl->b_read_waiting = VLC_TRUE;
2597 vlc_mutex_unlock( &host->lock );
2601 tls_ServerSessionClose( p_tls );
2604 #else /* ENABLE_HTTPD */
2606 /* We just define an empty wrapper */
2607 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2610 msg_Err( a, "HTTP daemon support is disabled" );
2613 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2615 msg_Err( a, "HTTP daemon support is disabled" );
2618 void httpd_HostDelete( httpd_host_t *a )
2621 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2622 char *psz_user, char *psz_password,
2623 const vlc_acl_t *p_acl )
2627 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2628 char *psz_user, char *psz_password,
2629 const vlc_acl_t *p_acl )
2633 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2634 httpd_callback_sys_t *d ){ return 0; }
2635 void httpd_UrlDelete( httpd_url_t *a ){}
2637 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip ) { return NULL; }
2638 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip ) { return NULL; }
2640 void httpd_ClientModeStream( httpd_client_t *a ){}
2641 void httpd_ClientModeBidir( httpd_client_t *a ){}
2643 void httpd_FileDelete( httpd_file_t *a ){}
2644 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2645 char *e, httpd_file_callback_t f,
2646 httpd_file_sys_t *g ){ return 0; }
2648 void httpd_RedirectDelete( httpd_redirect_t *a ){}
2649 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2650 char *b, char *c ){ return 0; }
2652 void httpd_StreamDelete( httpd_stream_t *a ){}
2653 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2654 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c ){ return 0; }
2655 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2656 char *d, char *e ){ return 0; }
2658 void httpd_MsgInit ( httpd_message_t *a ){}
2659 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... ){}
2660 char *httpd_MsgGet ( httpd_message_t *a, char *b ){ return 0; }
2661 void httpd_MsgClean( httpd_message_t *a ){}
2663 #endif /* ENABLE_HTTPD */