1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
34 #include "vlc_httpd.h"
50 #if defined( UNDER_CE )
52 #elif defined( WIN32 )
53 # include <winsock2.h>
55 # include <sys/socket.h>
59 /* We need HUGE buffer otherwise TCP throughput is very limited */
60 #define HTTPD_CL_BUFSIZE 1000000
62 #define HTTPD_CL_BUFSIZE 10000
65 static void httpd_ClientClean( httpd_client_t *cl );
81 httpd_callback_sys_t *p_sys;
82 } catch[HTTPD_MSG_MAX];
88 HTTPD_CLIENT_RECEIVING,
89 HTTPD_CLIENT_RECEIVE_DONE,
92 HTTPD_CLIENT_SEND_DONE,
98 HTTPD_CLIENT_TLS_HS_IN,
99 HTTPD_CLIENT_TLS_HS_OUT
105 HTTPD_CLIENT_FILE, /* default */
106 HTTPD_CLIENT_STREAM, /* regulary get data from cb */
107 HTTPD_CLIENT_BIDIR, /* check for reading and get data from cb */
110 struct httpd_client_t
116 struct sockaddr_storage sock;
122 int b_read_waiting; /* stop as soon as possible sending */
124 mtime_t i_activity_date;
125 mtime_t i_activity_timeout;
127 /* buffer for reading header */
133 httpd_message_t query; /* client -> httpd */
134 httpd_message_t answer; /* httpd -> client */
137 tls_session_t *p_tls;
141 /*****************************************************************************
143 *****************************************************************************/
144 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
145 static void b64_decode( char *dest, char *src )
150 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
151 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
152 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
153 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
154 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
155 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
156 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
157 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
158 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
159 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
160 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
161 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
162 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
163 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
164 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
165 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
168 for( i_level = 0; *src != '\0'; src++ )
172 c = b64[(unsigned int)*src];
184 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
188 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
192 *dest++ = ( ( last &0x03 ) << 6 ) | c;
204 const char *psz_mime;
207 { ".htm", "text/html" },
208 { ".html", "text/html" },
209 { ".txt", "text/plain" },
210 { ".xml", "text/xml" },
211 { ".dtd", "text/dtd" },
213 { ".css", "text/css" },
216 { ".gif", "image/gif" },
217 { ".jpe", "image/jpeg" },
218 { ".jpg", "image/jpeg" },
219 { ".jpeg", "image/jpeg" },
220 { ".png", "image/png" },
221 { ".mpjpeg","multipart/x-mixed-replace; boundary=This Random String" },
224 { ".avi", "video/avi" },
225 { ".asf", "video/x-ms-asf" },
226 { ".m1a", "audio/mpeg" },
227 { ".m2a", "audio/mpeg" },
228 { ".m1v", "video/mpeg" },
229 { ".m2v", "video/mpeg" },
230 { ".mp2", "audio/mpeg" },
231 { ".mp3", "audio/mpeg" },
232 { ".mpa", "audio/mpeg" },
233 { ".mpg", "video/mpeg" },
234 { ".mpeg", "video/mpeg" },
235 { ".mpe", "video/mpeg" },
236 { ".mov", "video/quicktime" },
237 { ".moov", "video/quicktime" },
238 { ".ogg", "application/ogg" },
239 { ".ogm", "application/ogg" },
240 { ".wav", "audio/wav" },
241 { ".wma", "audio/x-ms-wma" },
242 { ".wmv", "video/x-ms-wmv" },
249 static const char *httpd_MimeFromUrl( const char *psz_url )
254 psz_ext = strrchr( psz_url, '.' );
259 for( i = 0; http_mime[i].psz_ext != NULL ; i++ )
261 if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
263 return http_mime[i].psz_mime;
267 return "application/octet-stream";
274 const char *psz_reason;
277 static const http_status_info http_reason[] =
279 /*{ 100, "Continue" },
280 { 101, "Switching Protocols" },*/
284 { 203, "Non-Authoritative Information" },
285 { 204, "No Content" },
286 { 205, "Reset Content"Â },
287 { 206, "Partial Content" },
288 { 250, "Low on Storage Space" },
289 { 300, "Multiple Choices" }*/,
290 { 301, "Moved Permanently" }/*,
291 { 302, "Moved Temporarily" }, - aka "Found"
292 { 303, "See Other" },
293 { 304, "Not Modified" },
294 { 305, "Use Proxy" },
295 { 307, "Temporary Redirect" },
296 { 400, "Bad Request" }*/,
297 { 401, "Unauthorized" }/*,
298 { 402, "Payment Required" }*/,
299 { 403, "Forbidden" },
300 { 404, "Not Found" }/*,
301 { 405, "Method Not Allowed" },
302 { 406, "Not Acceptable" },
303 { 407, "Proxy Authentication Required" },
304 { 408, "Request Time-out" },
307 { 411, "Length Required" },
308 { 412, "Precondition Failed" },
309 { 413, "Request Entity Too Large" },
310 { 414, "Request-URI Too Large" },
311 { 415, "Unsupported Media Type" },
312 { 416, "Requested range not satisfiable" },
313 { 417, "Expectation Failed" },
314 { 451, "Parameter Not Understood" },
315 { 452, "Conference Not Found" },
316 { 453, "Not Enough Bandwidth" }*/,
317 { 454, "Session Not Found" }/*,
318 { 455, "Method Not Valid in This State" },
319 { 456, "Header Field Not Valid for Resource" },
320 { 457, "Invalid Range" },
321 { 458, "Parameter Is Read-Only" },
322 { 459, "Aggregate operation not allowed" },
323 { 460, "Only aggregate operation allowed" }*/,
324 { 461, "Unsupported transport" }/*,
325 { 462, "Destination unreachable" }*/,
326 { 500, "Internal Server Error" },
327 { 501, "Not Implemented" }/*,
328 { 502, "Bad Gateway" }*/,
329 { 503, "Service Unavailable" }/*,
330 { 504, "Gateway Time-out" },
331 { 505, "Protocol version not supported" }*/,
335 static const char *psz_fallback_reason[] =
336 { "Continue", "OK", "Found", "Client Error", "Server Error" };
338 static const char *httpd_ReasonFromCode( int i_code )
340 const http_status_info *p;
342 for (p = http_reason; p->i_code < i_code; p++);
344 if( p->i_code == i_code )
345 return p->psz_reason;
347 assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
348 return psz_fallback_reason[(i_code / 100) - 1];
352 /*****************************************************************************
353 * High Level Functions: httpd_file_t
354 *****************************************************************************/
362 httpd_file_callback_t pf_fill;
363 httpd_file_sys_t *p_sys;
367 static int httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
369 httpd_file_t *file = (httpd_file_t*)p_sys;
370 uint8_t *psz_args = query->psz_args;
371 uint8_t **pp_body, *p_body;
372 char *psz_connection = NULL;
373 int *pi_body, i_body;
375 if( answer == NULL || query == NULL )
379 answer->i_proto = HTTPD_PROTO_HTTP;
380 answer->i_version= query->i_version;
381 answer->i_type = HTTPD_MSG_ANSWER;
383 answer->i_status = 200;
384 answer->psz_status = strdup( "OK" );
386 httpd_MsgAdd( answer, "Content-type", "%s", file->psz_mime );
387 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
389 if( query->i_type != HTTPD_MSG_HEAD )
391 pp_body = &answer->p_body;
392 pi_body = &answer->i_body;
396 /* The file still needs to be executed. */
403 if( query->i_type == HTTPD_MSG_POST )
405 /* msg_Warn not supported */
408 file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
410 if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
415 /* We respect client request */
416 psz_connection = httpd_MsgGet( &cl->query, "Connection" );
417 if( psz_connection != NULL )
419 httpd_MsgAdd( answer, "Connection", psz_connection );
422 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
427 httpd_file_t *httpd_FileNew( httpd_host_t *host,
428 const char *psz_url, const char *psz_mime,
429 const char *psz_user, const char *psz_password,
430 const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
431 httpd_file_sys_t *p_sys )
433 httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
435 if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
436 psz_password, p_acl )
443 file->psz_url = strdup( psz_url );
444 if( psz_mime && *psz_mime )
446 file->psz_mime = strdup( psz_mime );
450 file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
453 file->pf_fill = pf_fill;
456 httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
457 (httpd_callback_sys_t*)file );
458 httpd_UrlCatch( file->url, HTTPD_MSG_GET, httpd_FileCallBack,
459 (httpd_callback_sys_t*)file );
460 httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
461 (httpd_callback_sys_t*)file );
466 void httpd_FileDelete( httpd_file_t *file )
468 httpd_UrlDelete( file->url );
470 free( file->psz_url );
471 free( file->psz_mime );
476 /*****************************************************************************
477 * High Level Functions: httpd_handler_t (for CGIs)
478 *****************************************************************************/
479 struct httpd_handler_t
483 httpd_handler_callback_t pf_fill;
484 httpd_handler_sys_t *p_sys;
488 static int httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl, httpd_message_t *answer, httpd_message_t *query )
490 httpd_handler_t *handler = (httpd_handler_t*)p_sys;
491 uint8_t *psz_args = query->psz_args;
492 char psz_remote_addr[NI_MAXNUMERICHOST];
494 if( answer == NULL || query == NULL )
498 answer->i_proto = HTTPD_PROTO_NONE;
499 answer->i_type = HTTPD_MSG_ANSWER;
501 /* We do it ourselves, thanks */
502 answer->i_status = 0;
503 answer->psz_status = NULL;
505 if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
506 *psz_remote_addr = '\0';
508 handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
509 query->i_type, query->p_body, query->i_body,
510 psz_remote_addr, NULL,
511 &answer->p_body, &answer->i_body );
513 if( query->i_type == HTTPD_MSG_HEAD )
515 char *p = (char *)answer->p_body;
516 while ( (p = strchr( p, '\r' )) != NULL )
518 if( p[1] && p[1] == '\n' && p[2] && p[2] == '\r'
519 && p[3] && p[3] == '\n' )
527 answer->i_body = strlen((char*)answer->p_body) + 1;
528 answer->p_body = realloc( answer->p_body, answer->i_body );
532 if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
534 int i_status, i_headers;
535 char *psz_headers, *psz_new, *psz_status;
537 if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
540 i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
541 if( *psz_headers ) psz_headers++;
542 if( *psz_headers ) psz_headers++;
543 i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
548 psz_headers = (char *)answer->p_body;
549 i_headers = answer->i_body;
557 psz_status = "Unauthorized";
560 if( (i_status < 0) || (i_status > 999) )
562 psz_status = "Undefined";
565 answer->i_body = sizeof("HTTP/1.0 xxx \r\n")
566 + strlen(psz_status) + i_headers - 1;
567 psz_new = (char *)malloc( answer->i_body + 1);
568 sprintf( psz_new, "HTTP/1.0 %03d %s\r\n", i_status, psz_status );
569 memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
570 free( answer->p_body );
571 answer->p_body = (uint8_t *)psz_new;
577 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
578 const char *psz_user,
579 const char *psz_password,
580 const vlc_acl_t *p_acl,
581 httpd_handler_callback_t pf_fill,
582 httpd_handler_sys_t *p_sys )
584 httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
586 if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
587 psz_password, p_acl )
594 handler->pf_fill = pf_fill;
595 handler->p_sys = p_sys;
597 httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
598 (httpd_callback_sys_t*)handler );
599 httpd_UrlCatch( handler->url, HTTPD_MSG_GET, httpd_HandlerCallBack,
600 (httpd_callback_sys_t*)handler );
601 httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
602 (httpd_callback_sys_t*)handler );
607 void httpd_HandlerDelete( httpd_handler_t *handler )
609 httpd_UrlDelete( handler->url );
613 /*****************************************************************************
614 * High Level Functions: httpd_redirect_t
615 *****************************************************************************/
616 struct httpd_redirect_t
622 static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
623 httpd_client_t *cl, httpd_message_t *answer,
624 httpd_message_t *query )
626 httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
629 if( answer == NULL || query == NULL )
633 answer->i_proto = query->i_proto;
634 answer->i_version= query->i_version;
635 answer->i_type = HTTPD_MSG_ANSWER;
636 answer->i_status = 301;
637 answer->psz_status = strdup( "Moved Permanently" );
639 answer->i_body = asprintf( &p_body,
640 "<?xml version=\"1.0\" encoding=\"ascii\" ?>\n"
641 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
642 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
645 "<title>Redirection</title>\n"
649 "<a href=\"%s\">redirected</a></h1>\n"
651 "<p><a href=\"http://www.videolan.org\">VideoLAN</a>\n</p>"
654 "</html>\n", rdir->psz_dst );
655 answer->p_body = (unsigned char *)p_body;
657 /* XXX check if it's ok or we need to set an absolute url */
658 httpd_MsgAdd( answer, "Location", "%s", rdir->psz_dst );
660 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
665 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
666 const char *psz_url_src )
668 httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
670 if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
675 rdir->psz_dst = strdup( psz_url_dst );
677 /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
678 httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
679 (httpd_callback_sys_t*)rdir );
680 httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
681 (httpd_callback_sys_t*)rdir );
682 httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
683 (httpd_callback_sys_t*)rdir );
684 httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
685 (httpd_callback_sys_t*)rdir );
689 void httpd_RedirectDelete( httpd_redirect_t *rdir )
691 httpd_UrlDelete( rdir->url );
692 free( rdir->psz_dst );
696 /*****************************************************************************
697 * High Level Funtions: httpd_stream_t
698 *****************************************************************************/
699 struct httpd_stream_t
706 /* Header to send as first packet */
710 /* circular buffer */
711 int i_buffer_size; /* buffer size, can't be reallocated smaller */
712 uint8_t *p_buffer; /* buffer */
713 int64_t i_buffer_pos; /* absolute position from begining */
714 int64_t i_buffer_last_pos; /* a new connection will start with that */
717 static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
718 httpd_client_t *cl, httpd_message_t *answer,
719 httpd_message_t *query )
721 httpd_stream_t *stream = (httpd_stream_t*)p_sys;
723 if( answer == NULL || query == NULL || cl == NULL )
728 if( answer->i_body_offset > 0 )
734 fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lld\n",
735 answer->i_body_offset );
738 if( answer->i_body_offset >= stream->i_buffer_pos )
740 /* fprintf( stderr, "httpd_StreamCallBack: no data\n" ); */
741 return VLC_EGENERIC; /* wait, no data available */
743 if( answer->i_body_offset + stream->i_buffer_size <
744 stream->i_buffer_pos )
746 /* this client isn't fast enough */
748 fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)\n",
749 answer->i_body_offset, stream->i_buffer_last_pos );
751 answer->i_body_offset = stream->i_buffer_last_pos;
754 i_pos = answer->i_body_offset % stream->i_buffer_size;
755 i_write = stream->i_buffer_pos - answer->i_body_offset;
756 if( i_write > HTTPD_CL_BUFSIZE )
758 i_write = HTTPD_CL_BUFSIZE;
760 else if( i_write <= 0 )
762 return VLC_EGENERIC; /* wait, no data available */
765 /* Don't go past the end of the circular buffer */
766 i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
768 /* using HTTPD_MSG_ANSWER -> data available */
769 answer->i_proto = HTTPD_PROTO_HTTP;
770 answer->i_version= 0;
771 answer->i_type = HTTPD_MSG_ANSWER;
773 answer->i_body = i_write;
774 answer->p_body = malloc( i_write );
775 memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
777 answer->i_body_offset += i_write;
783 answer->i_proto = HTTPD_PROTO_HTTP;
784 answer->i_version= 0;
785 answer->i_type = HTTPD_MSG_ANSWER;
787 answer->i_status = 200;
788 answer->psz_status = strdup( "OK" );
790 if( query->i_type != HTTPD_MSG_HEAD )
792 httpd_ClientModeStream( cl );
793 vlc_mutex_lock( &stream->lock );
794 /* Send the header */
795 if( stream->i_header > 0 )
797 answer->i_body = stream->i_header;
798 answer->p_body = malloc( stream->i_header );
799 memcpy( answer->p_body, stream->p_header, stream->i_header );
801 answer->i_body_offset = stream->i_buffer_last_pos;
802 vlc_mutex_unlock( &stream->lock );
806 httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
807 answer->i_body_offset = 0;
810 if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
812 vlc_bool_t b_xplaystream = VLC_FALSE;
815 httpd_MsgAdd( answer, "Content-type", "%s",
816 "application/octet-stream" );
817 httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
818 httpd_MsgAdd( answer, "Pragma", "no-cache" );
819 httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
820 httpd_MsgAdd( answer, "Pragma", "features=\"broadcast\"" );
822 /* Check if there is a xPlayStrm=1 */
823 for( i = 0; i < query->i_name; i++ )
825 if( !strcasecmp( query->name[i], "Pragma" ) &&
826 strstr( query->value[i], "xPlayStrm=1" ) )
828 b_xplaystream = VLC_TRUE;
834 answer->i_body_offset = 0;
839 httpd_MsgAdd( answer, "Content-type", "%s", stream->psz_mime );
841 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
846 httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
847 const char *psz_url, const char *psz_mime,
848 const char *psz_user, const char *psz_password,
849 const vlc_acl_t *p_acl )
851 httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
853 if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
854 psz_password, p_acl )
860 vlc_mutex_init( host, &stream->lock );
861 if( psz_mime && *psz_mime )
863 stream->psz_mime = strdup( psz_mime );
867 stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
869 stream->i_header = 0;
870 stream->p_header = NULL;
871 stream->i_buffer_size = 5000000; /* 5 Mo per stream */
872 stream->p_buffer = malloc( stream->i_buffer_size );
873 /* We set to 1 to make life simpler
874 * (this way i_body_offset can never be 0) */
875 stream->i_buffer_pos = 1;
876 stream->i_buffer_last_pos = 1;
878 httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
879 (httpd_callback_sys_t*)stream );
880 httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
881 (httpd_callback_sys_t*)stream );
882 httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
883 (httpd_callback_sys_t*)stream );
888 int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
890 vlc_mutex_lock( &stream->lock );
891 if( stream->p_header )
893 free( stream->p_header );
894 stream->p_header = NULL;
896 stream->i_header = i_data;
899 stream->p_header = malloc( i_data );
900 memcpy( stream->p_header, p_data, i_data );
902 vlc_mutex_unlock( &stream->lock );
907 int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
912 if( i_data < 0 || p_data == NULL )
916 vlc_mutex_lock( &stream->lock );
918 /* save this pointer (to be used by new connection) */
919 stream->i_buffer_last_pos = stream->i_buffer_pos;
921 i_pos = stream->i_buffer_pos % stream->i_buffer_size;
927 i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
929 /* Ok, we can't go past the end of our buffer */
930 memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
932 i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
937 stream->i_buffer_pos += i_data;
939 vlc_mutex_unlock( &stream->lock );
943 void httpd_StreamDelete( httpd_stream_t *stream )
945 httpd_UrlDelete( stream->url );
946 vlc_mutex_destroy( &stream->lock );
947 if( stream->psz_mime ) free( stream->psz_mime );
948 if( stream->p_header ) free( stream->p_header );
949 if( stream->p_buffer ) free( stream->p_buffer );
953 /*****************************************************************************
955 *****************************************************************************/
956 static void httpd_HostThread( httpd_host_t * );
958 /* create a new host */
959 httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
962 return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
966 httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
968 const char *psz_cert, const char *psz_key,
969 const char *psz_ca, const char *psz_crl )
978 if( psz_hostname == NULL )
981 psz_host = strdup( psz_hostname );
982 if( psz_host == NULL )
984 msg_Err( p_this, "memory error" );
988 /* to be sure to avoid multiple creation */
989 var_Create( p_this->p_libvlc, "httpd_mutex", VLC_VAR_MUTEX );
990 var_Get( p_this->p_libvlc, "httpd_mutex", &lockval );
991 vlc_mutex_lock( lockval.p_address );
993 if( !(httpd = vlc_object_find( p_this, VLC_OBJECT_HTTPD, FIND_ANYWHERE )) )
995 msg_Info( p_this, "creating httpd" );
996 if( ( httpd = vlc_object_create( p_this, VLC_OBJECT_HTTPD ) ) == NULL )
998 vlc_mutex_unlock( lockval.p_address );
1006 vlc_object_yield( httpd );
1007 vlc_object_attach( httpd, p_this->p_vlc );
1010 /* verify if it already exist */
1011 for( i = httpd->i_host - 1; i >= 0; i-- )
1013 host = httpd->host[i];
1015 /* cannot mix TLS and non-TLS hosts */
1016 if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
1017 || ( host->i_port != i_port )
1018 || strcmp( host->psz_hostname, psz_hostname ) )
1024 vlc_mutex_unlock( lockval.p_address );
1030 /* determine TLS configuration */
1031 if ( psz_cert != NULL )
1033 p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
1034 if ( p_tls == NULL )
1036 msg_Err( p_this, "TLS initialization error" );
1040 if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
1042 msg_Err( p_this, "TLS CA error" );
1046 if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
1048 msg_Err( p_this, "TLS CRL error" );
1055 /* create the new host */
1056 host = vlc_object_create( p_this, VLC_OBJECT_HTTPD_HOST );
1057 host->httpd = httpd;
1058 vlc_mutex_init( httpd, &host->lock );
1061 host->fd = net_ListenTCP( p_this, psz_host, i_port );
1062 if( host->fd == NULL )
1064 msg_Err( p_this, "cannot create socket(s) for HTTP host" );
1068 host->i_port = i_port;
1069 host->psz_hostname = psz_host;
1074 host->client = NULL;
1076 host->p_tls = p_tls;
1078 /* create the thread */
1079 if( vlc_thread_create( host, "httpd host thread", httpd_HostThread,
1080 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
1082 msg_Err( p_this, "cannot spawn http host thread" );
1086 /* now add it to httpd */
1087 TAB_APPEND( httpd->i_host, httpd->host, host );
1088 vlc_mutex_unlock( lockval.p_address );
1094 if( httpd->i_host <= 0 )
1096 vlc_object_release( httpd );
1097 vlc_object_detach( httpd );
1098 vlc_object_destroy( httpd );
1100 vlc_mutex_unlock( lockval.p_address );
1104 net_ListenClose( host->fd );
1105 vlc_mutex_destroy( &host->lock );
1106 vlc_object_destroy( host );
1110 tls_ServerDelete( p_tls );
1116 void httpd_HostDelete( httpd_host_t *host )
1118 httpd_t *httpd = host->httpd;
1119 vlc_value_t lockval;
1122 var_Get( httpd->p_libvlc, "httpd_mutex", &lockval );
1123 vlc_mutex_lock( lockval.p_address );
1126 if( host->i_ref > 0 )
1129 vlc_mutex_unlock( lockval.p_address );
1130 msg_Dbg( host, "httpd_HostDelete: host still used" );
1133 TAB_REMOVE( httpd->i_host, httpd->host, host );
1136 vlc_thread_join( host );
1138 msg_Dbg( host, "HTTP host removed" );
1140 for( i = 0; i < host->i_url; i++ )
1142 msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
1144 for( i = 0; i < host->i_client; i++ )
1146 httpd_client_t *cl = host->client[i];
1147 msg_Warn( host, "client still connected" );
1148 httpd_ClientClean( cl );
1149 TAB_REMOVE( host->i_client, host->client, cl );
1155 if( host->p_tls != NULL)
1156 tls_ServerDelete( host->p_tls );
1158 net_ListenClose( host->fd );
1159 free( host->psz_hostname );
1161 vlc_mutex_destroy( &host->lock );
1162 vlc_object_destroy( host );
1164 vlc_object_release( httpd );
1165 if( httpd->i_host <= 0 )
1167 msg_Dbg( httpd, "no host left, stopping httpd" );
1168 vlc_object_detach( httpd );
1169 vlc_object_destroy( httpd );
1171 vlc_mutex_unlock( lockval.p_address );
1174 /* register a new url */
1175 static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
1176 const char *psz_user, const char *psz_password,
1177 const vlc_acl_t *p_acl, vlc_bool_t b_check )
1182 vlc_mutex_lock( &host->lock );
1185 for( i = 0; i < host->i_url; i++ )
1187 if( !strcmp( psz_url, host->url[i]->psz_url ) )
1189 msg_Warn( host->httpd,
1190 "cannot add '%s' (url already defined)", psz_url );
1191 vlc_mutex_unlock( &host->lock );
1197 url = malloc( sizeof( httpd_url_t ) );
1200 vlc_mutex_init( host->httpd, &url->lock );
1201 url->psz_url = strdup( psz_url );
1202 url->psz_user = strdup( psz_user ? psz_user : "" );
1203 url->psz_password = strdup( psz_password ? psz_password : "" );
1204 url->p_acl = ACL_Duplicate( host, p_acl );
1205 for( i = 0; i < HTTPD_MSG_MAX; i++ )
1207 url->catch[i].cb = NULL;
1208 url->catch[i].p_sys = NULL;
1211 TAB_APPEND( host->i_url, host->url, url );
1212 vlc_mutex_unlock( &host->lock );
1217 httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
1218 const char *psz_user, const char *psz_password,
1219 const vlc_acl_t *p_acl )
1221 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1222 psz_password, p_acl, VLC_FALSE );
1225 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
1226 const char *psz_user, const char *psz_password,
1227 const vlc_acl_t *p_acl )
1229 return httpd_UrlNewPrivate( host, psz_url, psz_user,
1230 psz_password, p_acl, VLC_TRUE );
1233 /* register callback on a url */
1234 int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
1235 httpd_callback_sys_t *p_sys )
1237 vlc_mutex_lock( &url->lock );
1238 url->catch[i_msg].cb = cb;
1239 url->catch[i_msg].p_sys= p_sys;
1240 vlc_mutex_unlock( &url->lock );
1246 void httpd_UrlDelete( httpd_url_t *url )
1248 httpd_host_t *host = url->host;
1251 vlc_mutex_lock( &host->lock );
1252 TAB_REMOVE( host->i_url, host->url, url );
1254 vlc_mutex_destroy( &url->lock );
1255 free( url->psz_url );
1256 free( url->psz_user );
1257 free( url->psz_password );
1258 ACL_Destroy( url->p_acl );
1260 for( i = 0; i < host->i_client; i++ )
1262 httpd_client_t *client = host->client[i];
1264 if( client->url == url )
1266 /* TODO complete it */
1267 msg_Warn( host, "force closing connections" );
1268 httpd_ClientClean( client );
1269 TAB_REMOVE( host->i_client, host->client, client );
1275 vlc_mutex_unlock( &host->lock );
1278 void httpd_MsgInit( httpd_message_t *msg )
1281 msg->i_type = HTTPD_MSG_NONE;
1282 msg->i_proto = HTTPD_PROTO_NONE;
1283 msg->i_version = -1;
1286 msg->psz_status = NULL;
1288 msg->psz_url = NULL;
1289 msg->psz_args = NULL;
1291 msg->i_channel = -1;
1298 msg->i_body_offset = 0;
1303 void httpd_MsgClean( httpd_message_t *msg )
1307 if( msg->psz_status )
1309 free( msg->psz_status );
1313 free( msg->psz_url );
1317 free( msg->psz_args );
1319 for( i = 0; i < msg->i_name; i++ )
1321 free( msg->name[i] );
1322 free( msg->value[i] );
1334 free( msg->p_body );
1336 httpd_MsgInit( msg );
1339 char *httpd_MsgGet( httpd_message_t *msg, char *name )
1343 for( i = 0; i < msg->i_name; i++ )
1345 if( !strcasecmp( msg->name[i], name ))
1347 return msg->value[i];
1353 void httpd_MsgAdd( httpd_message_t *msg, char *name, char *psz_value, ... )
1358 va_start( args, psz_value );
1359 #if defined(HAVE_VASPRINTF) && !defined(__APPLE__) && !defined(SYS_BEOS)
1360 vasprintf( &value, psz_value, args );
1363 int i_size = strlen( psz_value ) + 4096; /* FIXME stupid system */
1364 value = calloc( i_size, sizeof( char ) );
1365 vsnprintf( value, i_size, psz_value, args );
1366 value[i_size - 1] = 0;
1371 name = strdup( name );
1373 TAB_APPEND( msg->i_name, msg->name, name );
1374 TAB_APPEND( msg->i_value, msg->value, value );
1377 static void httpd_ClientInit( httpd_client_t *cl )
1379 cl->i_state = HTTPD_CLIENT_RECEIVING;
1380 cl->i_activity_date = mdate();
1381 cl->i_activity_timeout = I64C(10000000);
1382 cl->i_buffer_size = HTTPD_CL_BUFSIZE;
1384 cl->p_buffer = malloc( cl->i_buffer_size );
1385 cl->i_mode = HTTPD_CLIENT_FILE;
1386 cl->b_read_waiting = VLC_FALSE;
1388 httpd_MsgInit( &cl->query );
1389 httpd_MsgInit( &cl->answer );
1392 void httpd_ClientModeStream( httpd_client_t *cl )
1394 cl->i_mode = HTTPD_CLIENT_STREAM;
1397 void httpd_ClientModeBidir( httpd_client_t *cl )
1399 cl->i_mode = HTTPD_CLIENT_BIDIR;
1402 char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
1404 return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1407 char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
1409 return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
1412 static void httpd_ClientClean( httpd_client_t *cl )
1416 if( cl->p_tls != NULL )
1417 tls_ServerSessionClose( cl->p_tls );
1418 net_Close( cl->fd );
1422 httpd_MsgClean( &cl->answer );
1423 httpd_MsgClean( &cl->query );
1427 free( cl->p_buffer );
1428 cl->p_buffer = NULL;
1432 static httpd_client_t *httpd_ClientNew( int fd, struct sockaddr_storage *sock,
1434 tls_session_t *p_tls )
1436 httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
1438 if( !cl ) return NULL;
1442 memcpy( &cl->sock, sock, sizeof( cl->sock ) );
1443 cl->i_sock_size = i_sock_size;
1447 httpd_ClientInit( cl );
1452 static int httpd_NetRecv( httpd_client_t *cl, uint8_t *p, int i_len )
1454 tls_session_t *p_tls;
1458 return tls_Recv( p_tls, p, i_len );
1460 return recv( cl->fd, p, i_len, 0 );
1463 static int httpd_NetSend( httpd_client_t *cl, const uint8_t *p, int i_len )
1465 tls_session_t *p_tls;
1469 return tls_Send( p_tls, p, i_len );
1471 return send( cl->fd, p, i_len, 0 );
1474 static void httpd_ClientRecv( httpd_client_t *cl )
1478 if( cl->query.i_proto == HTTPD_PROTO_NONE )
1480 /* enough to see if it's rtp over rtsp or RTSP/HTTP */
1481 i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
1485 cl->i_buffer += i_len;
1488 if( cl->i_buffer >= 4 )
1490 /*fprintf( stderr, "peek=%4.4s\n", cl->p_buffer );*/
1492 if( cl->p_buffer[0] == '$' )
1494 /* RTSP (rtp over rtsp) */
1495 cl->query.i_proto = HTTPD_PROTO_RTSP;
1496 cl->query.i_type = HTTPD_MSG_CHANNEL;
1497 cl->query.i_channel = cl->p_buffer[1];
1498 cl->query.i_body = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
1499 cl->query.p_body = malloc( cl->query.i_body );
1503 else if( !memcmp( cl->p_buffer, "HTTP", 4 ) )
1505 cl->query.i_proto = HTTPD_PROTO_HTTP;
1506 cl->query.i_type = HTTPD_MSG_ANSWER;
1508 else if( !memcmp( cl->p_buffer, "RTSP", 4 ) )
1510 cl->query.i_proto = HTTPD_PROTO_RTSP;
1511 cl->query.i_type = HTTPD_MSG_ANSWER;
1513 else if( !memcmp( cl->p_buffer, "GET ", 4 ) ||
1514 !memcmp( cl->p_buffer, "HEAD", 4 ) ||
1515 !memcmp( cl->p_buffer, "POST", 4 ) )
1517 cl->query.i_proto = HTTPD_PROTO_HTTP;
1518 cl->query.i_type = HTTPD_MSG_NONE;
1522 cl->query.i_proto = HTTPD_PROTO_RTSP;
1523 cl->query.i_type = HTTPD_MSG_NONE;
1527 else if( cl->query.i_body > 0 )
1529 /* we are reading the body of a request or a channel */
1530 i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
1531 cl->query.i_body - cl->i_buffer );
1534 cl->i_buffer += i_len;
1536 if( cl->i_buffer >= cl->query.i_body )
1538 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1543 /* we are reading a header -> char by char */
1546 i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
1553 if( cl->i_buffer + 1 >= cl->i_buffer_size )
1555 cl->i_buffer_size += 1024;
1556 cl->p_buffer = realloc( cl->p_buffer, cl->i_buffer_size );
1558 if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "\n\n", 2 ) )||
1559 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "\r\n\r\n", 4 ) ) )
1563 /* we have finished the header so parse it and set i_body */
1564 cl->p_buffer[cl->i_buffer] = '\0';
1566 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1569 * assume strlen( "HTTP/1.x" ) = 8
1571 cl->query.i_status =
1572 strtol( (char *)&cl->p_buffer[8],
1578 cl->query.psz_status = strdup( p );
1590 { "OPTIONS", HTTPD_MSG_OPTIONS, HTTPD_PROTO_RTSP },
1591 { "DESCRIBE", HTTPD_MSG_DESCRIBE, HTTPD_PROTO_RTSP },
1592 { "SETUP", HTTPD_MSG_SETUP, HTTPD_PROTO_RTSP },
1593 { "PLAY", HTTPD_MSG_PLAY, HTTPD_PROTO_RTSP },
1594 { "PAUSE", HTTPD_MSG_PAUSE, HTTPD_PROTO_RTSP },
1595 { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
1596 { "TEARDOWN", HTTPD_MSG_TEARDOWN, HTTPD_PROTO_RTSP },
1598 { "GET", HTTPD_MSG_GET, HTTPD_PROTO_HTTP },
1599 { "HEAD", HTTPD_MSG_HEAD, HTTPD_PROTO_HTTP },
1600 { "POST", HTTPD_MSG_POST, HTTPD_PROTO_HTTP },
1602 { NULL, HTTPD_MSG_NONE, HTTPD_PROTO_NONE }
1607 cl->query.i_type = HTTPD_MSG_NONE;
1609 /*fprintf( stderr, "received new request=%s\n", cl->p_buffer);*/
1611 for( i = 0; msg_type[i].name != NULL; i++ )
1613 if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
1614 strlen( msg_type[i].name ) ) )
1616 p = (char *)&cl->p_buffer[strlen((char *)msg_type[i].name) + 1 ];
1617 cl->query.i_type = msg_type[i].i_type;
1618 if( cl->query.i_proto != msg_type[i].i_proto )
1621 cl->query.i_proto = HTTPD_PROTO_NONE;
1622 cl->query.i_type = HTTPD_MSG_NONE;
1629 if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
1631 cl->query.i_proto = HTTPD_PROTO_HTTP;
1633 else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
1635 cl->query.i_proto = HTTPD_PROTO_RTSP;
1647 p2 = strchr( p, ' ' );
1652 if( !strncasecmp( p, "rtsp:", 5 ) )
1654 /* for rtsp url, you have rtsp://localhost:port/path */
1656 while( *p == '/' ) p++;
1657 while( *p && *p != '/' ) p++;
1659 cl->query.psz_url = strdup( p );
1660 if( ( p3 = strchr( cl->query.psz_url, '?' ) ) )
1663 cl->query.psz_args = (uint8_t *)strdup( p3 );
1671 if( !strncasecmp( p2, "HTTP/1.", 7 ) )
1673 cl->query.i_proto = HTTPD_PROTO_HTTP;
1674 cl->query.i_version = atoi( p2+7 );
1676 else if( !strncasecmp( p2, "RTSP/1.", 7 ) )
1678 cl->query.i_proto = HTTPD_PROTO_RTSP;
1679 cl->query.i_version = atoi( p2+7 );
1687 p = strchr( p, '\n' );
1691 while( *p == '\n' || *p == '\r' )
1695 while( p && *p != '\0' )
1698 char *eol = p = strchr( p, '\n' );
1701 while( eol && eol >= line && ( *eol == '\n' || *eol == '\r' ) )
1706 if( ( colon = strchr( line, ':' ) ) )
1712 while( *colon == ' ' )
1716 name = strdup( line );
1717 value = strdup( colon );
1719 TAB_APPEND( cl->query.i_name, cl->query.name, name );
1720 TAB_APPEND( cl->query.i_value,cl->query.value,value);
1722 if( !strcasecmp( name, "Content-Length" ) )
1724 cl->query.i_body = atol( value );
1731 while( *p == '\n' || *p == '\r' )
1738 if( cl->query.i_body > 0 )
1740 /* TODO Mhh, handle the case client will only send a request and close the connection
1741 * to mark and of body (probably only RTSP) */
1742 cl->query.p_body = malloc( cl->query.i_body );
1747 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1753 /* check if the client is to be set to dead */
1754 #if defined( WIN32 ) || defined( UNDER_CE )
1755 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1757 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1760 if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
1762 /* connection closed -> end of data */
1763 if( cl->query.i_body > 0 )
1765 cl->query.i_body = cl->i_buffer;
1767 cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
1771 cl->i_state = HTTPD_CLIENT_DEAD;
1774 cl->i_activity_date = mdate();
1776 /* XXX: for QT I have to disable timeout. Try to find why */
1777 if( cl->query.i_proto == HTTPD_PROTO_RTSP )
1778 cl->i_activity_timeout = 0;
1780 #if 0 /* Debugging only */
1781 if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
1785 fprintf( stderr, "received new request\n" );
1786 fprintf( stderr, " - proto=%s\n",
1787 cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
1788 fprintf( stderr, " - version=%d\n", cl->query.i_version );
1789 fprintf( stderr, " - msg=%d\n", cl->query.i_type );
1790 if( cl->query.i_type == HTTPD_MSG_ANSWER )
1792 fprintf( stderr, " - answer=%d '%s'\n", cl->query.i_status,
1793 cl->query.psz_status );
1795 else if( cl->query.i_type != HTTPD_MSG_NONE )
1797 fprintf( stderr, " - url=%s\n", cl->query.psz_url );
1799 for( i = 0; i < cl->query.i_name; i++ )
1801 fprintf( stderr, " - option name='%s' value='%s'\n",
1802 cl->query.name[i], cl->query.value[i] );
1808 static void httpd_ClientSend( httpd_client_t *cl )
1813 if( cl->i_buffer < 0 )
1815 /* We need to create the header */
1819 i_size = strlen( "HTTP/1.") + 10 + 10 +
1820 strlen( cl->answer.psz_status ? cl->answer.psz_status : "" ) + 5;
1821 for( i = 0; i < cl->answer.i_name; i++ )
1823 i_size += strlen( cl->answer.name[i] ) + 2 +
1824 strlen( cl->answer.value[i] ) + 2;
1827 if( cl->i_buffer_size < i_size )
1829 cl->i_buffer_size = i_size;
1830 free( cl->p_buffer );
1831 cl->p_buffer = malloc( i_size );
1833 p = (char *)cl->p_buffer;
1835 p += sprintf( p, "%s/1.%d %d %s\r\n",
1836 cl->answer.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP",
1837 cl->answer.i_version,
1838 cl->answer.i_status, cl->answer.psz_status );
1839 for( i = 0; i < cl->answer.i_name; i++ )
1841 p += sprintf( p, "%s: %s\r\n", cl->answer.name[i],
1842 cl->answer.value[i] );
1844 p += sprintf( p, "\r\n" );
1847 cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
1849 /*fprintf( stderr, "sending answer\n" );
1850 fprintf( stderr, "%s", cl->p_buffer );*/
1853 i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
1854 cl->i_buffer_size - cl->i_buffer );
1857 cl->i_activity_date = mdate();
1858 cl->i_buffer += i_len;
1860 if( cl->i_buffer >= cl->i_buffer_size )
1862 if( cl->answer.i_body == 0 && cl->answer.i_body_offset > 0 &&
1863 !cl->b_read_waiting )
1865 /* catch more body data */
1866 int i_msg = cl->query.i_type;
1867 int64_t i_offset = cl->answer.i_body_offset;
1869 httpd_MsgClean( &cl->answer );
1870 cl->answer.i_body_offset = i_offset;
1872 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
1873 &cl->answer, &cl->query );
1876 if( cl->answer.i_body > 0 )
1878 /* send the body data */
1879 free( cl->p_buffer );
1880 cl->p_buffer = cl->answer.p_body;
1881 cl->i_buffer_size = cl->answer.i_body;
1884 cl->answer.i_body = 0;
1885 cl->answer.p_body = NULL;
1890 cl->i_state = HTTPD_CLIENT_SEND_DONE;
1896 #if defined( WIN32 ) || defined( UNDER_CE )
1897 if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
1899 if( ( i_len < 0 && errno != EAGAIN && errno != EINTR ) || ( i_len == 0 ) )
1903 cl->i_state = HTTPD_CLIENT_DEAD;
1908 static void httpd_ClientTlsHsIn( httpd_client_t *cl )
1910 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1913 cl->i_state = HTTPD_CLIENT_RECEIVING;
1917 cl->i_state = HTTPD_CLIENT_DEAD;
1922 cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
1926 static void httpd_ClientTlsHsOut( httpd_client_t *cl )
1928 switch( tls_SessionContinueHandshake( cl->p_tls ) )
1931 cl->i_state = HTTPD_CLIENT_RECEIVING;
1935 cl->i_state = HTTPD_CLIENT_DEAD;
1940 cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
1945 static void httpd_HostThread( httpd_host_t *host )
1947 tls_session_t *p_tls = NULL;
1949 host->p_total_counter = stats_CounterCreate( host,
1950 VLC_VAR_INTEGER, STATS_COUNTER );
1951 host->p_active_counter = stats_CounterCreate( host,
1952 VLC_VAR_INTEGER, STATS_COUNTER );
1954 while( !host->b_die )
1956 struct timeval timeout;
1959 /* FIXME: (too) many int variables */
1961 int i_handle_max = 0;
1964 int b_low_delay = 0;
1966 if( host->i_url <= 0 )
1973 /* built a set of handle to select */
1974 FD_ZERO( &fds_read );
1975 FD_ZERO( &fds_write );
1979 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
1981 FD_SET( fd, &fds_read );
1982 if( fd > i_handle_max )
1986 /* prepare a new TLS session */
1987 if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
1988 p_tls = tls_ServerSessionPrepare( host->p_tls );
1990 /* add all socket that should be read/write and close dead connection */
1991 vlc_mutex_lock( &host->lock );
1992 for( i_client = 0; i_client < host->i_client; i_client++ )
1994 httpd_client_t *cl = host->client[i_client];
1996 if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
1997 ( cl->i_state == HTTPD_CLIENT_DEAD ||
1998 ( cl->i_activity_timeout > 0 &&
1999 cl->i_activity_date+cl->i_activity_timeout < mdate()) ) ) )
2001 httpd_ClientClean( cl );
2002 stats_UpdateInteger( host, host->p_active_counter, -1, NULL );
2003 TAB_REMOVE( host->i_client, host->client, cl );
2008 else if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
2009 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
2011 FD_SET( cl->fd, &fds_read );
2012 i_handle_max = __MAX( i_handle_max, cl->fd );
2014 else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
2015 || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
2017 FD_SET( cl->fd, &fds_write );
2018 i_handle_max = __MAX( i_handle_max, cl->fd );
2020 else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
2022 httpd_message_t *answer = &cl->answer;
2023 httpd_message_t *query = &cl->query;
2024 int i_msg = query->i_type;
2026 httpd_MsgInit( answer );
2028 /* Handle what we received */
2029 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
2030 (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
2032 /* we can only receive request from client when not
2035 cl->i_state = HTTPD_CLIENT_DEAD;
2037 else if( i_msg == HTTPD_MSG_ANSWER )
2039 /* We are in BIDIR mode, trigger the callback and then
2040 * check for new data */
2041 if( cl->url && cl->url->catch[i_msg].cb )
2043 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2046 cl->i_state = HTTPD_CLIENT_WAITING;
2048 else if( i_msg == HTTPD_MSG_CHANNEL )
2050 /* We are in BIDIR mode, trigger the callback and then
2051 * check for new data */
2052 if( cl->url && cl->url->catch[i_msg].cb )
2054 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
2057 cl->i_state = HTTPD_CLIENT_WAITING;
2059 else if( i_msg == HTTPD_MSG_OPTIONS )
2061 char *psz_cseq = NULL;
2065 answer->i_proto = query->i_proto ;
2066 answer->i_type = HTTPD_MSG_ANSWER;
2067 answer->i_version= 0;
2068 answer->i_status = 200;
2069 answer->psz_status = strdup( "Ok" );
2072 answer->p_body = NULL;
2074 psz_cseq = httpd_MsgGet( query, "Cseq" );
2076 i_cseq = atoi( psz_cseq );
2079 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
2080 httpd_MsgAdd( answer, "Server", "VLC Server" );
2081 httpd_MsgAdd( answer, "Public", "DESCRIBE, SETUP, "
2082 "TEARDOWN, PLAY, PAUSE" );
2083 httpd_MsgAdd( answer, "Content-Length", "%d",
2086 cl->i_buffer = -1; /* Force the creation of the answer in
2087 * httpd_ClientSend */
2088 cl->i_state = HTTPD_CLIENT_SENDING;
2090 else if( i_msg == HTTPD_MSG_NONE )
2092 if( query->i_proto == HTTPD_PROTO_NONE )
2095 cl->i_state = HTTPD_CLIENT_DEAD;
2102 answer->i_proto = query->i_proto ;
2103 answer->i_type = HTTPD_MSG_ANSWER;
2104 answer->i_version= 0;
2105 answer->i_status = 501;
2106 answer->psz_status = strdup( "Unimplemented" );
2108 p = answer->p_body = malloc( 1000 );
2110 p += sprintf( (char *)p,
2111 "<?xml version=\"1.0\" encoding=\"ascii\" ?>"
2112 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2113 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2116 "<title>Error 501</title>\n"
2119 "<h1>501 Unimplemented</h1>\n"
2121 "<a href=\"http://www.videolan.org\">VideoLAN</a>\n"
2125 answer->i_body = p - answer->p_body;
2126 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2128 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2129 cl->i_state = HTTPD_CLIENT_SENDING;
2134 vlc_bool_t b_auth_failed = VLC_FALSE;
2135 vlc_bool_t b_hosts_failed = VLC_FALSE;
2138 /* Search the url and trigger callbacks */
2139 for( i = 0; i < host->i_url; i++ )
2141 httpd_url_t *url = host->url[i];
2143 if( !strcmp( url->psz_url, query->psz_url ) )
2145 if( url->catch[i_msg].cb )
2147 if( answer && ( url->p_acl != NULL ) )
2149 char ip[NI_MAXNUMERICHOST];
2151 if( httpd_ClientIP( cl, ip ) != NULL )
2153 if( ACL_Check( url->p_acl, ip ) )
2155 b_hosts_failed = VLC_TRUE;
2160 b_hosts_failed = VLC_TRUE;
2163 if( answer && ( *url->psz_user || *url->psz_password ) )
2165 /* create the headers */
2166 char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
2170 asprintf( &id, "%s:%s", url->psz_user, url->psz_password );
2171 if( b64 ) auth = malloc( strlen(b64) + 1 );
2172 else auth = malloc( strlen("") + 1 );
2175 && !strncasecmp( b64, "BASIC", 5 ) )
2178 while( *b64 == ' ' )
2182 b64_decode( auth, b64 );
2189 if( strcmp( id, auth ) )
2191 httpd_MsgAdd( answer, "WWW-Authenticate", "Basic realm=\"%s\"", url->psz_user );
2192 /* We fail for all url */
2193 b_auth_failed = VLC_TRUE;
2203 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
2205 if( answer->i_proto == HTTPD_PROTO_NONE )
2207 /* Raw answer from a CGI */
2208 cl->i_buffer = cl->i_buffer_size;
2213 /* only one url can answer */
2215 if( cl->url == NULL )
2228 answer->i_proto = query->i_proto;
2229 answer->i_type = HTTPD_MSG_ANSWER;
2230 answer->i_version= 0;
2231 p = answer->p_body = malloc( 1000 + strlen(query->psz_url) );
2233 if( b_hosts_failed )
2235 answer->i_status = 403;
2236 answer->psz_status = strdup( "Forbidden" );
2238 /* FIXME: lots of code duplication */
2239 p += sprintf( (char *)p,
2240 "<?xml version=\"1.0\" encoding=\"us-ascii\" ?>"
2241 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2242 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2245 "<title>Error 403</title>\n"
2248 "<h1>403 Forbidden (%s)</h1>\n"
2250 "<p><a href=\"http://www.videolan.org\">VideoLAN</a></p>\n"
2252 "</html>\n", query->psz_url );
2254 else if( b_auth_failed )
2256 answer->i_status = 401;
2257 answer->psz_status = strdup( "Authorization Required" );
2259 p += sprintf( (char *)p,
2260 "<?xml version=\"1.0\" encoding=\"us-ascii\" ?>"
2261 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2262 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2265 "<title>Error 401</title>\n"
2268 "<h1>401 Authorization Required (%s)</h1>\n"
2270 "<p><a href=\"http://www.videolan.org\">VideoLAN</a></p>\n"
2272 "</html>\n", query->psz_url );
2276 /* no url registered */
2277 answer->i_status = 404;
2278 answer->psz_status = strdup( "Not found" );
2280 p += sprintf( (char *)p,
2281 "<?xml version=\"1.0\" encoding=\"us-ascii\" ?>"
2282 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
2283 "\"http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd\">\n"
2286 "<title>Error 404</title>\n"
2289 "<h1>404 Resource not found(%s)</h1>\n"
2291 "<p><a href=\"http://www.videolan.org\"> VideoLAN</a></p>\n"
2293 "</html>\n", query->psz_url );
2296 answer->i_body = p - answer->p_body;
2297 cl->i_buffer = -1; /* Force the creation of the answer in httpd_ClientSend */
2298 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
2299 httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
2302 cl->i_state = HTTPD_CLIENT_SENDING;
2305 else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
2307 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
2309 char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
2310 char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
2311 vlc_bool_t b_connection = VLC_FALSE;
2312 vlc_bool_t b_keepalive = VLC_FALSE;
2313 vlc_bool_t b_query = VLC_FALSE;
2316 if( psz_connection )
2318 b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
2319 b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
2324 b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
2327 if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
2328 ( ( cl->answer.i_version == 0 && b_keepalive ) ||
2329 ( cl->answer.i_version == 1 && !b_connection ) ) ) ||
2330 ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
2331 !b_query && !b_connection ) )
2333 httpd_MsgClean( &cl->query );
2334 httpd_MsgInit( &cl->query );
2337 cl->i_buffer_size = 1000;
2338 free( cl->p_buffer );
2339 cl->p_buffer = malloc( cl->i_buffer_size );
2340 cl->i_state = HTTPD_CLIENT_RECEIVING;
2344 cl->i_state = HTTPD_CLIENT_DEAD;
2346 httpd_MsgClean( &cl->answer );
2348 else if( cl->b_read_waiting )
2350 /* we have a message waiting for us to read it */
2351 httpd_MsgClean( &cl->answer );
2352 httpd_MsgClean( &cl->query );
2355 cl->i_buffer_size = 1000;
2356 free( cl->p_buffer );
2357 cl->p_buffer = malloc( cl->i_buffer_size );
2358 cl->i_state = HTTPD_CLIENT_RECEIVING;
2359 cl->b_read_waiting = VLC_FALSE;
2363 int64_t i_offset = cl->answer.i_body_offset;
2364 httpd_MsgClean( &cl->answer );
2366 cl->answer.i_body_offset = i_offset;
2367 free( cl->p_buffer );
2368 cl->p_buffer = NULL;
2370 cl->i_buffer_size = 0;
2372 cl->i_state = HTTPD_CLIENT_WAITING;
2375 else if( cl->i_state == HTTPD_CLIENT_WAITING )
2377 int64_t i_offset = cl->answer.i_body_offset;
2378 int i_msg = cl->query.i_type;
2380 httpd_MsgInit( &cl->answer );
2381 cl->answer.i_body_offset = i_offset;
2383 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
2384 &cl->answer, &cl->query );
2385 if( cl->answer.i_type != HTTPD_MSG_NONE )
2387 /* we have new data, so reenter send mode */
2389 cl->p_buffer = cl->answer.p_body;
2390 cl->i_buffer_size = cl->answer.i_body;
2391 cl->answer.p_body = NULL;
2392 cl->answer.i_body = 0;
2393 cl->i_state = HTTPD_CLIENT_SENDING;
2397 /* we shouldn't wait too long */
2398 b_low_delay = VLC_TRUE;
2402 /* Special for BIDIR mode we also check reading */
2403 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2404 cl->i_state == HTTPD_CLIENT_SENDING )
2406 FD_SET( cl->fd, &fds_read );
2407 i_handle_max = __MAX( i_handle_max, cl->fd );
2410 vlc_mutex_unlock( &host->lock );
2412 /* we will wait 100ms or 20ms (not too big 'cause HTTPD_CLIENT_WAITING) */
2414 timeout.tv_usec = b_low_delay ? 20000 : 100000;
2416 i_ret = select( i_handle_max + 1,
2417 &fds_read, &fds_write, NULL, &timeout );
2419 if( (i_ret == -1) && (errno != EINTR) )
2421 msg_Warn( host, "select error: %s", net_strerror( net_errno ) );
2425 else if( i_ret <= 0 )
2430 /* accept new connections */
2431 for( i_fd = 0; (fd = host->fd[i_fd]) != -1; i_fd++ )
2433 if( FD_ISSET( fd, &fds_read ) )
2435 socklen_t i_sock_size = sizeof( struct sockaddr_storage );
2436 struct sockaddr_storage sock;
2438 fd = accept( fd, (struct sockaddr *)&sock, &i_sock_size );
2444 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &i_state, sizeof (i_state));
2447 /* set this new socket non-block */
2448 #if defined( WIN32 ) || defined( UNDER_CE )
2450 unsigned long i_dummy = 1;
2451 ioctlsocket( fd, FIONBIO, &i_dummy );
2454 fcntl( fd, F_SETFD, FD_CLOEXEC );
2456 int i_val = fcntl( fd, F_GETFL );
2458 O_NONBLOCK | ((i_val != -1) ? i_val : 0) );
2461 if( fd >= FD_SETSIZE )
2470 switch ( tls_ServerSessionHandshake( p_tls, fd ) )
2473 msg_Err( host, "Rejecting TLS connection" );
2479 case 1: /* missing input - most likely */
2480 i_state = HTTPD_CLIENT_TLS_HS_IN;
2483 case 2: /* missing output */
2484 i_state = HTTPD_CLIENT_TLS_HS_OUT;
2492 char ip[NI_MAXNUMERICHOST];
2493 stats_UpdateInteger( host, host->p_total_counter,
2495 stats_UpdateInteger( host, host->p_active_counter,
2497 cl = httpd_ClientNew( fd, &sock, i_sock_size, p_tls );
2498 httpd_ClientIP( cl, ip );
2499 msg_Dbg( host, "Connection from %s", ip );
2501 vlc_mutex_lock( &host->lock );
2502 TAB_APPEND( host->i_client, host->client, cl );
2503 vlc_mutex_unlock( &host->lock );
2506 cl->i_state = i_state; // override state for TLS
2512 /* now try all others socket */
2513 vlc_mutex_lock( &host->lock );
2514 for( i_client = 0; i_client < host->i_client; i_client++ )
2516 httpd_client_t *cl = host->client[i_client];
2517 if( cl->i_state == HTTPD_CLIENT_RECEIVING )
2519 httpd_ClientRecv( cl );
2521 else if( cl->i_state == HTTPD_CLIENT_SENDING )
2523 httpd_ClientSend( cl );
2525 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
2527 httpd_ClientTlsHsIn( cl );
2529 else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
2531 httpd_ClientTlsHsOut( cl );
2534 if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
2535 cl->i_state == HTTPD_CLIENT_SENDING &&
2536 FD_ISSET( cl->fd, &fds_read ) )
2538 cl->b_read_waiting = VLC_TRUE;
2541 vlc_mutex_unlock( &host->lock );
2545 tls_ServerSessionClose( p_tls );
2548 #else /* ENABLE_HTTPD */
2550 /* We just define an empty wrapper */
2551 httpd_host_t *httpd_TLSHostNew( vlc_object_t *a, char *b, int c,
2554 msg_Err( a, "HTTP daemon support is disabled" );
2558 httpd_host_t *httpd_HostNew( vlc_object_t *a, char *b, int c )
2560 msg_Err( a, "HTTP daemon support is disabled" );
2564 void httpd_HostDelete( httpd_host_t *a )
2568 httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url,
2569 char *psz_user, char *psz_password,
2570 const vlc_acl_t *p_acl )
2575 httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url,
2576 char *psz_user, char *psz_password,
2577 const vlc_acl_t *p_acl )
2582 int httpd_UrlCatch( httpd_url_t *a, int b, httpd_callback_t c,
2583 httpd_callback_sys_t *d )
2588 void httpd_UrlDelete( httpd_url_t *a )
2592 char* httpd_ClientIP( httpd_client_t *cl, char *psz_ip )
2597 char* httpd_ServerIP( httpd_client_t *cl, char *psz_ip )
2602 void httpd_ClientModeStream( httpd_client_t *a )
2606 void httpd_ClientModeBidir( httpd_client_t *a )
2610 void httpd_FileDelete( httpd_file_t *a )
2614 httpd_file_t *httpd_FileNew( httpd_host_t *a, char *b, char *c, char *d,
2615 char *e, httpd_file_callback_t f,
2616 httpd_file_sys_t *g )
2621 httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
2622 const char *psz_user,
2623 const char *psz_password,
2624 const vlc_acl_t *p_acl,
2625 httpd_handler_callback_t pf_fill,
2626 httpd_handler_sys_t *p_sys )
2631 void httpd_HandlerDelete( httpd_handler_t *handler )
2635 void httpd_RedirectDelete( httpd_redirect_t *a )
2639 httpd_redirect_t *httpd_RedirectNew( httpd_host_t *a,
2645 void httpd_StreamDelete( httpd_stream_t *a )
2649 int httpd_StreamHeader( httpd_stream_t *a, uint8_t *b, int c )
2654 int httpd_StreamSend ( httpd_stream_t *a, uint8_t *b, int c )
2659 httpd_stream_t *httpd_StreamNew( httpd_host_t *a, char *b, char *c,
2665 void httpd_MsgInit ( httpd_message_t *a )
2669 void httpd_MsgAdd ( httpd_message_t *a, char *b, char *c, ... )
2673 char *httpd_MsgGet ( httpd_message_t *a, char *b )
2678 void httpd_MsgClean( httpd_message_t *a )
2682 #endif /* ENABLE_HTTPD */