httpd.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:78k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * httpd.c
  3.  *****************************************************************************
  4.  * Copyright (C) 2004-2006 the VideoLAN team
  5.  * Copyright © 2004-2007 Rémi Denis-Courmont
  6.  * $Id: c01666155ebfa2d1759a0a7d9fec88f10ca66786 $
  7.  *
  8.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  9.  *          Rémi Denis-Courmont <rem # videolan.org>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24.  *****************************************************************************/
  25. #ifdef HAVE_CONFIG_H
  26. # include "config.h"
  27. #endif
  28. #include <vlc_common.h>
  29. #include <vlc_httpd.h>
  30. #include <assert.h>
  31. #include <vlc_network.h>
  32. #include <vlc_tls.h>
  33. #include <vlc_acl.h>
  34. #include <vlc_strings.h>
  35. #include "../libvlc.h"
  36. #include <string.h>
  37. #include <errno.h>
  38. #ifdef HAVE_UNISTD_H
  39. #   include <unistd.h>
  40. #endif
  41. #ifdef HAVE_FCNTL_H
  42. #   include <fcntl.h>
  43. #endif
  44. #ifdef HAVE_POLL
  45. # include <poll.h>
  46. #endif
  47. #if defined( UNDER_CE )
  48. #   include <winsock.h>
  49. #elif defined( WIN32 )
  50. #   include <winsock2.h>
  51. #else
  52. #   include <sys/socket.h>
  53. #endif
  54. #if defined( WIN32 )
  55. /* We need HUGE buffer otherwise TCP throughput is very limited */
  56. #define HTTPD_CL_BUFSIZE 1000000
  57. #else
  58. #define HTTPD_CL_BUFSIZE 10000
  59. #endif
  60. static void httpd_ClientClean( httpd_client_t *cl );
  61. struct httpd_t
  62. {
  63.     VLC_COMMON_MEMBERS
  64.     int          i_host;
  65.     httpd_host_t **host;
  66. };
  67. /* each host run in his own thread */
  68. struct httpd_host_t
  69. {
  70.     VLC_COMMON_MEMBERS
  71.     httpd_t     *httpd;
  72.     /* ref count */
  73.     unsigned    i_ref;
  74.     /* address/port and socket for listening at connections */
  75.     char        *psz_hostname;
  76.     int         i_port;
  77.     int         *fds;
  78.     unsigned     nfd;
  79.     vlc_thread_t thread;
  80.     vlc_mutex_t lock;
  81.     vlc_cond_t  wait;
  82.     /* all registered url (becarefull that 2 httpd_url_t could point at the same url)
  83.      * This will slow down the url research but make my live easier
  84.      * All url will have their cb trigger, but only the first one can answer
  85.      * */
  86.     int         i_url;
  87.     httpd_url_t **url;
  88.     int            i_client;
  89.     httpd_client_t **client;
  90.     /* TLS data */
  91.     tls_server_t *p_tls;
  92. };
  93. struct httpd_url_t
  94. {
  95.     httpd_host_t *host;
  96.     vlc_mutex_t lock;
  97.     char      *psz_url;
  98.     char      *psz_user;
  99.     char      *psz_password;
  100.     vlc_acl_t *p_acl;
  101.     struct
  102.     {
  103.         httpd_callback_t     cb;
  104.         httpd_callback_sys_t *p_sys;
  105.     } catch[HTTPD_MSG_MAX];
  106. };
  107. /* status */
  108. enum
  109. {
  110.     HTTPD_CLIENT_RECEIVING,
  111.     HTTPD_CLIENT_RECEIVE_DONE,
  112.     HTTPD_CLIENT_SENDING,
  113.     HTTPD_CLIENT_SEND_DONE,
  114.     HTTPD_CLIENT_WAITING,
  115.     HTTPD_CLIENT_DEAD,
  116.     HTTPD_CLIENT_TLS_HS_IN,
  117.     HTTPD_CLIENT_TLS_HS_OUT
  118. };
  119. /* mode */
  120. enum
  121. {
  122.     HTTPD_CLIENT_FILE,      /* default */
  123.     HTTPD_CLIENT_STREAM,    /* regulary get data from cb */
  124.     HTTPD_CLIENT_BIDIR,     /* check for reading and get data from cb */
  125. };
  126. struct httpd_client_t
  127. {
  128.     httpd_url_t *url;
  129.     int     i_ref;
  130.     int     fd;
  131.     int     i_mode;
  132.     int     i_state;
  133.     int     b_read_waiting; /* stop as soon as possible sending */
  134.     mtime_t i_activity_date;
  135.     mtime_t i_activity_timeout;
  136.     /* buffer for reading header */
  137.     int     i_buffer_size;
  138.     int     i_buffer;
  139.     uint8_t *p_buffer;
  140.     /* */
  141.     httpd_message_t query;  /* client -> httpd */
  142.     httpd_message_t answer; /* httpd -> client */
  143.     /* TLS data */
  144.     tls_session_t *p_tls;
  145. };
  146. /*****************************************************************************
  147.  * Various functions
  148.  *****************************************************************************/
  149. static const struct
  150. {
  151.     const char psz_ext[8];
  152.     const char *psz_mime;
  153. } http_mime[] =
  154. {
  155.     { ".htm",   "text/html" },
  156.     { ".html",  "text/html" },
  157.     { ".txt",   "text/plain" },
  158.     { ".xml",   "text/xml" },
  159.     { ".dtd",   "text/dtd" },
  160.     { ".css",   "text/css" },
  161.     /* image mime */
  162.     { ".gif",   "image/gif" },
  163.     { ".jpe",   "image/jpeg" },
  164.     { ".jpg",   "image/jpeg" },
  165.     { ".jpeg",  "image/jpeg" },
  166.     { ".png",   "image/png" },
  167.     /* same as modules/mux/mpjpeg.c here: */
  168.     { ".mpjpeg","multipart/x-mixed-replace; boundary=7b3cc56e5f51db803f790dad720ed50a" },
  169.     /* media mime */
  170.     { ".avi",   "video/avi" },
  171.     { ".asf",   "video/x-ms-asf" },
  172.     { ".m1a",   "audio/mpeg" },
  173.     { ".m2a",   "audio/mpeg" },
  174.     { ".m1v",   "video/mpeg" },
  175.     { ".m2v",   "video/mpeg" },
  176.     { ".mp2",   "audio/mpeg" },
  177.     { ".mp3",   "audio/mpeg" },
  178.     { ".mpa",   "audio/mpeg" },
  179.     { ".mpg",   "video/mpeg" },
  180.     { ".mpeg",  "video/mpeg" },
  181.     { ".mpe",   "video/mpeg" },
  182.     { ".mov",   "video/quicktime" },
  183.     { ".moov",  "video/quicktime" },
  184.     { ".oga",   "audio/ogg" },
  185.     { ".ogg",   "application/ogg" },
  186.     { ".ogm",   "application/ogg" },
  187.     { ".ogv",   "video/ogg" },
  188.     { ".ogx",   "application/ogg" },
  189.     { ".spx",   "audio/ogg" },
  190.     { ".wav",   "audio/wav" },
  191.     { ".wma",   "audio/x-ms-wma" },
  192.     { ".wmv",   "video/x-ms-wmv" },
  193.     /* end */
  194.     { "",       "" }
  195. };
  196. static const char *httpd_MimeFromUrl( const char *psz_url )
  197. {
  198.     char *psz_ext;
  199.     psz_ext = strrchr( psz_url, '.' );
  200.     if( psz_ext )
  201.     {
  202.         int i;
  203.         for( i = 0; http_mime[i].psz_ext[0] ; i++ )
  204.         {
  205.             if( !strcasecmp( http_mime[i].psz_ext, psz_ext ) )
  206.             {
  207.                 return http_mime[i].psz_mime;
  208.             }
  209.         }
  210.     }
  211.     return "application/octet-stream";
  212. }
  213. typedef struct
  214. {
  215.     unsigned   i_code;
  216.     const char psz_reason[36];
  217. } http_status_info;
  218. static const http_status_info http_reason[] =
  219. {
  220.   /*{ 100, "Continue" },
  221.     { 101, "Switching Protocols" },*/
  222.     { 200, "OK" },
  223.   /*{ 201, "Created" },
  224.     { 202, "Accepted" },
  225.     { 203, "Non-authoritative information" },
  226.     { 204, "No content" },
  227.     { 205, "Reset content" },
  228.     { 206, "Partial content" },
  229.     { 250, "Low on storage space" },
  230.     { 300, "Multiple choices" },*/
  231.     { 301, "Moved permanently" },
  232.   /*{ 302, "Moved temporarily" },
  233.     { 303, "See other" },
  234.     { 304, "Not modified" },
  235.     { 305, "Use proxy" },
  236.     { 307, "Temporary redirect" },
  237.     { 400, "Bad request" },*/
  238.     { 401, "Unauthorized" },
  239.   /*{ 402, "Payment Required" },*/
  240.     { 403, "Forbidden" },
  241.     { 404, "Not found" },
  242.     { 405, "Method not allowed" },
  243.   /*{ 406, "Not acceptable" },
  244.     { 407, "Proxy authentication required" },
  245.     { 408, "Request time-out" },
  246.     { 409, "Conflict" },
  247.     { 410, "Gone" },
  248.     { 411, "Length required" },
  249.     { 412, "Precondition failed" },
  250.     { 413, "Request entity too large" },
  251.     { 414, "Request-URI too large" },
  252.     { 415, "Unsupported media Type" },
  253.     { 416, "Requested range not satisfiable" },
  254.     { 417, "Expectation failed" },
  255.     { 451, "Parameter not understood" },
  256.     { 452, "Conference not found" },
  257.     { 453, "Not enough bandwidth" },*/
  258.     { 454, "Session not found" },
  259.   /*{ 455, "Method not valid in this State" },*/
  260.     { 456, "Header field not valid for resource" },
  261.   /*{ 457, "Invalid range" },
  262.     { 458, "Read-only parameter" },*/
  263.     { 459, "Aggregate operation not allowed" },
  264.     { 460, "Non-aggregate operation not allowed" },
  265.     { 461, "Unsupported transport" },
  266.   /*{ 462, "Destination unreachable" },*/
  267.     { 500, "Internal server error" },
  268.     { 501, "Not implemented" },
  269.   /*{ 502, "Bad gateway" },*/
  270.     { 503, "Service unavailable" },
  271.   /*{ 504, "Gateway time-out" },*/
  272.     { 505, "Protocol version not supported" },
  273.     { 551, "Option not supported" },
  274.     { 999, "" }
  275. };
  276. static const char psz_fallback_reason[5][16] =
  277. { "Continue", "OK", "Found", "Client error", "Server error" };
  278. static const char *httpd_ReasonFromCode( unsigned i_code )
  279. {
  280.     const http_status_info *p;
  281.     assert( ( i_code >= 100 ) && ( i_code <= 599 ) );
  282.     for (p = http_reason; i_code > p->i_code; p++);
  283.     if( p->i_code == i_code )
  284.         return p->psz_reason;
  285.     return psz_fallback_reason[(i_code / 100) - 1];
  286. }
  287. static size_t httpd_HtmlError (char **body, int code, const char *url)
  288. {
  289.     const char *errname = httpd_ReasonFromCode (code);
  290.     assert (errname != NULL);
  291.     int res = asprintf (body,
  292.         "<?xml version="1.0" encoding="ascii" ?>n"
  293.         "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""
  294.         " "http://www.w3.org/TR/xhtml10/DTD/xhtml10strict.dtd">n"
  295.         "<html lang="en">n"
  296.         "<head>n"
  297.         "<title>%s</title>n"
  298.         "</head>n"
  299.         "<body>n"
  300.         "<h1>%d %s%s%s%s</h1>n"
  301.         "<hr />n"
  302.         "<a href="http://www.videolan.org">VideoLAN</a>n"
  303.         "</body>n"
  304.         "</html>n", errname, code, errname,
  305.         (url ? " (" : ""), (url ? url : ""), (url ? ")" : ""));
  306.     if (res == -1)
  307.     {
  308.         *body = NULL;
  309.         return 0;
  310.     }
  311.     return (size_t)res;
  312. }
  313. /*****************************************************************************
  314.  * High Level Functions: httpd_file_t
  315.  *****************************************************************************/
  316. struct httpd_file_t
  317. {
  318.     httpd_url_t *url;
  319.     char *psz_url;
  320.     char *psz_mime;
  321.     httpd_file_callback_t pf_fill;
  322.     httpd_file_sys_t      *p_sys;
  323. };
  324. static int
  325. httpd_FileCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
  326.                     httpd_message_t *answer, const httpd_message_t *query )
  327. {
  328.     httpd_file_t *file = (httpd_file_t*)p_sys;
  329.     uint8_t **pp_body, *p_body;
  330.     const char *psz_connection;
  331.     int *pi_body, i_body;
  332.     if( answer == NULL || query == NULL )
  333.     {
  334.         return VLC_SUCCESS;
  335.     }
  336.     answer->i_proto  = HTTPD_PROTO_HTTP;
  337.     answer->i_version= 1;
  338.     answer->i_type   = HTTPD_MSG_ANSWER;
  339.     answer->i_status = 200;
  340.     httpd_MsgAdd( answer, "Content-type",  "%s", file->psz_mime );
  341.     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
  342.     if( query->i_type != HTTPD_MSG_HEAD )
  343.     {
  344.         pp_body = &answer->p_body;
  345.         pi_body = &answer->i_body;
  346.     }
  347.     else
  348.     {
  349.         /* The file still needs to be executed. */
  350.         p_body = NULL;
  351.         i_body = 0;
  352.         pp_body = &p_body;
  353.         pi_body = &i_body;
  354.     }
  355.     if( query->i_type == HTTPD_MSG_POST )
  356.     {
  357.         /* msg_Warn not supported */
  358.     }
  359.     uint8_t *psz_args = query->psz_args;
  360.     file->pf_fill( file->p_sys, file, psz_args, pp_body, pi_body );
  361.     if( query->i_type == HTTPD_MSG_HEAD && p_body != NULL )
  362.     {
  363.         free( p_body );
  364.     }
  365.     /* We respect client request */
  366.     psz_connection = httpd_MsgGet( &cl->query, "Connection" );
  367.     if( psz_connection != NULL )
  368.     {
  369.         httpd_MsgAdd( answer, "Connection", "%s", psz_connection );
  370.     }
  371.     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
  372.     return VLC_SUCCESS;
  373. }
  374. httpd_file_t *httpd_FileNew( httpd_host_t *host,
  375.                              const char *psz_url, const char *psz_mime,
  376.                              const char *psz_user, const char *psz_password,
  377.                              const vlc_acl_t *p_acl, httpd_file_callback_t pf_fill,
  378.                              httpd_file_sys_t *p_sys )
  379. {
  380.     httpd_file_t *file = malloc( sizeof( httpd_file_t ) );
  381.     if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user,
  382.                                           psz_password, p_acl )
  383.         ) == NULL )
  384.     {
  385.         free( file );
  386.         return NULL;
  387.     }
  388.     file->psz_url  = strdup( psz_url );
  389.     if( psz_mime && *psz_mime )
  390.     {
  391.         file->psz_mime = strdup( psz_mime );
  392.     }
  393.     else
  394.     {
  395.         file->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
  396.     }
  397.     file->pf_fill = pf_fill;
  398.     file->p_sys   = p_sys;
  399.     httpd_UrlCatch( file->url, HTTPD_MSG_HEAD, httpd_FileCallBack,
  400.                     (httpd_callback_sys_t*)file );
  401.     httpd_UrlCatch( file->url, HTTPD_MSG_GET,  httpd_FileCallBack,
  402.                     (httpd_callback_sys_t*)file );
  403.     httpd_UrlCatch( file->url, HTTPD_MSG_POST, httpd_FileCallBack,
  404.                     (httpd_callback_sys_t*)file );
  405.     return file;
  406. }
  407. httpd_file_sys_t *httpd_FileDelete( httpd_file_t *file )
  408. {
  409.     httpd_file_sys_t *p_sys = file->p_sys;
  410.     httpd_UrlDelete( file->url );
  411.     free( file->psz_url );
  412.     free( file->psz_mime );
  413.     free( file );
  414.     return p_sys;
  415. }
  416. /*****************************************************************************
  417.  * High Level Functions: httpd_handler_t (for CGIs)
  418.  *****************************************************************************/
  419. struct httpd_handler_t
  420. {
  421.     httpd_url_t *url;
  422.     httpd_handler_callback_t pf_fill;
  423.     httpd_handler_sys_t      *p_sys;
  424. };
  425. static int
  426. httpd_HandlerCallBack( httpd_callback_sys_t *p_sys, httpd_client_t *cl,
  427.                        httpd_message_t *answer, const httpd_message_t *query )
  428. {
  429.     httpd_handler_t *handler = (httpd_handler_t*)p_sys;
  430.     char psz_remote_addr[NI_MAXNUMERICHOST];
  431.     if( answer == NULL || query == NULL )
  432.     {
  433.         return VLC_SUCCESS;
  434.     }
  435.     answer->i_proto  = HTTPD_PROTO_NONE;
  436.     answer->i_type   = HTTPD_MSG_ANSWER;
  437.     /* We do it ourselves, thanks */
  438.     answer->i_status = 0;
  439.     if( httpd_ClientIP( cl, psz_remote_addr ) == NULL )
  440.         *psz_remote_addr = '';
  441.     uint8_t *psz_args = query->psz_args;
  442.     handler->pf_fill( handler->p_sys, handler, query->psz_url, psz_args,
  443.                       query->i_type, query->p_body, query->i_body,
  444.                       psz_remote_addr, NULL,
  445.                       &answer->p_body, &answer->i_body );
  446.     if( query->i_type == HTTPD_MSG_HEAD )
  447.     {
  448.         char *p = (char *)answer->p_body;
  449.         /* Looks for end of header (i.e. one empty line) */
  450.         while ( (p = strchr( p, 'r' )) != NULL )
  451.         {
  452.             if( p[1] && p[1] == 'n' && p[2] && p[2] == 'r'
  453.                  && p[3] && p[3] == 'n' )
  454.             {
  455.                 break;
  456.             }
  457.         }
  458.         if( p != NULL )
  459.         {
  460.             p[4] = '';
  461.             answer->i_body = strlen((char*)answer->p_body) + 1;
  462.             answer->p_body = realloc( answer->p_body, answer->i_body );
  463.         }
  464.     }
  465.     if( strncmp( (char *)answer->p_body, "HTTP/1.", 7 ) )
  466.     {
  467.         int i_status, i_headers;
  468.         char *psz_headers, *psz_new;
  469.         const char *psz_status;
  470.         if( !strncmp( (char *)answer->p_body, "Status: ", 8 ) )
  471.         {
  472.             /* Apache-style */
  473.             i_status = strtol( (char *)&answer->p_body[8], &psz_headers, 0 );
  474.             if( *psz_headers ) psz_headers++;
  475.             if( *psz_headers ) psz_headers++;
  476.             i_headers = answer->i_body - (psz_headers - (char *)answer->p_body);
  477.         }
  478.         else
  479.         {
  480.             i_status = 200;
  481.             psz_headers = (char *)answer->p_body;
  482.             i_headers = answer->i_body;
  483.         }
  484.         psz_status = httpd_ReasonFromCode( i_status );
  485.         answer->i_body = sizeof("HTTP/1.0 xxx rn")
  486.                         + strlen(psz_status) + i_headers - 1;
  487.         psz_new = (char *)malloc( answer->i_body + 1);
  488.         sprintf( psz_new, "HTTP/1.0 %03d %srn", i_status, psz_status );
  489.         memcpy( &psz_new[strlen(psz_new)], psz_headers, i_headers );
  490.         free( answer->p_body );
  491.         answer->p_body = (uint8_t *)psz_new;
  492.     }
  493.     return VLC_SUCCESS;
  494. }
  495. httpd_handler_t *httpd_HandlerNew( httpd_host_t *host, const char *psz_url,
  496.                                    const char *psz_user,
  497.                                    const char *psz_password,
  498.                                    const vlc_acl_t *p_acl,
  499.                                    httpd_handler_callback_t pf_fill,
  500.                                    httpd_handler_sys_t *p_sys )
  501. {
  502.     httpd_handler_t *handler = malloc( sizeof( httpd_handler_t ) );
  503.     if( ( handler->url = httpd_UrlNewUnique( host, psz_url, psz_user,
  504.                                              psz_password, p_acl )
  505.         ) == NULL )
  506.     {
  507.         free( handler );
  508.         return NULL;
  509.     }
  510.     handler->pf_fill = pf_fill;
  511.     handler->p_sys   = p_sys;
  512.     httpd_UrlCatch( handler->url, HTTPD_MSG_HEAD, httpd_HandlerCallBack,
  513.                     (httpd_callback_sys_t*)handler );
  514.     httpd_UrlCatch( handler->url, HTTPD_MSG_GET,  httpd_HandlerCallBack,
  515.                     (httpd_callback_sys_t*)handler );
  516.     httpd_UrlCatch( handler->url, HTTPD_MSG_POST, httpd_HandlerCallBack,
  517.                     (httpd_callback_sys_t*)handler );
  518.     return handler;
  519. }
  520. httpd_handler_sys_t *httpd_HandlerDelete( httpd_handler_t *handler )
  521. {
  522.     httpd_handler_sys_t *p_sys = handler->p_sys;
  523.     httpd_UrlDelete( handler->url );
  524.     free( handler );
  525.     return p_sys;
  526. }
  527. /*****************************************************************************
  528.  * High Level Functions: httpd_redirect_t
  529.  *****************************************************************************/
  530. struct httpd_redirect_t
  531. {
  532.     httpd_url_t *url;
  533.     char        *psz_dst;
  534. };
  535. static int httpd_RedirectCallBack( httpd_callback_sys_t *p_sys,
  536.                                    httpd_client_t *cl, httpd_message_t *answer,
  537.                                    const httpd_message_t *query )
  538. {
  539.     httpd_redirect_t *rdir = (httpd_redirect_t*)p_sys;
  540.     char *p_body;
  541.     (void)cl;
  542.     if( answer == NULL || query == NULL )
  543.     {
  544.         return VLC_SUCCESS;
  545.     }
  546.     answer->i_proto  = HTTPD_PROTO_HTTP;
  547.     answer->i_version= 1;
  548.     answer->i_type   = HTTPD_MSG_ANSWER;
  549.     answer->i_status = 301;
  550.     answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
  551.     answer->p_body = (unsigned char *)p_body;
  552.     /* XXX check if it's ok or we need to set an absolute url */
  553.     httpd_MsgAdd( answer, "Location",  "%s", rdir->psz_dst );
  554.     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
  555.     return VLC_SUCCESS;
  556. }
  557. httpd_redirect_t *httpd_RedirectNew( httpd_host_t *host, const char *psz_url_dst,
  558.                                      const char *psz_url_src )
  559. {
  560.     httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) );
  561.     if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, NULL ) ) )
  562.     {
  563.         free( rdir );
  564.         return NULL;
  565.     }
  566.     rdir->psz_dst = strdup( psz_url_dst );
  567.     /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
  568.     httpd_UrlCatch( rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
  569.                     (httpd_callback_sys_t*)rdir );
  570.     httpd_UrlCatch( rdir->url, HTTPD_MSG_GET, httpd_RedirectCallBack,
  571.                     (httpd_callback_sys_t*)rdir );
  572.     httpd_UrlCatch( rdir->url, HTTPD_MSG_POST, httpd_RedirectCallBack,
  573.                     (httpd_callback_sys_t*)rdir );
  574.     httpd_UrlCatch( rdir->url, HTTPD_MSG_DESCRIBE, httpd_RedirectCallBack,
  575.                     (httpd_callback_sys_t*)rdir );
  576.     return rdir;
  577. }
  578. void httpd_RedirectDelete( httpd_redirect_t *rdir )
  579. {
  580.     httpd_UrlDelete( rdir->url );
  581.     free( rdir->psz_dst );
  582.     free( rdir );
  583. }
  584. /*****************************************************************************
  585.  * High Level Funtions: httpd_stream_t
  586.  *****************************************************************************/
  587. struct httpd_stream_t
  588. {
  589.     vlc_mutex_t lock;
  590.     httpd_url_t *url;
  591.     char    *psz_mime;
  592.     /* Header to send as first packet */
  593.     uint8_t *p_header;
  594.     int     i_header;
  595.     /* circular buffer */
  596.     int         i_buffer_size;      /* buffer size, can't be reallocated smaller */
  597.     uint8_t     *p_buffer;          /* buffer */
  598.     int64_t     i_buffer_pos;       /* absolute position from begining */
  599.     int64_t     i_buffer_last_pos;  /* a new connection will start with that */
  600. };
  601. static int httpd_StreamCallBack( httpd_callback_sys_t *p_sys,
  602.                                  httpd_client_t *cl, httpd_message_t *answer,
  603.                                  const httpd_message_t *query )
  604. {
  605.     httpd_stream_t *stream = (httpd_stream_t*)p_sys;
  606.     if( answer == NULL || query == NULL || cl == NULL )
  607.     {
  608.         return VLC_SUCCESS;
  609.     }
  610.     if( answer->i_body_offset > 0 )
  611.     {
  612.         int64_t i_write;
  613.         int     i_pos;
  614. #if 0
  615.         fprintf( stderr, "httpd_StreamCallBack i_body_offset=%lldn",
  616.                  answer->i_body_offset );
  617. #endif
  618.         if( answer->i_body_offset >= stream->i_buffer_pos )
  619.         {
  620.             /* fprintf( stderr, "httpd_StreamCallBack: no datan" ); */
  621.             return VLC_EGENERIC;    /* wait, no data available */
  622.         }
  623.         if( answer->i_body_offset + stream->i_buffer_size <
  624.             stream->i_buffer_pos )
  625.         {
  626.             /* this client isn't fast enough */
  627. #if 0
  628.             fprintf( stderr, "fixing i_body_offset (old=%lld new=%lld)n",
  629.                      answer->i_body_offset, stream->i_buffer_last_pos );
  630. #endif
  631.             answer->i_body_offset = stream->i_buffer_last_pos;
  632.         }
  633.         i_pos   = answer->i_body_offset % stream->i_buffer_size;
  634.         i_write = stream->i_buffer_pos - answer->i_body_offset;
  635.         if( i_write > HTTPD_CL_BUFSIZE )
  636.         {
  637.             i_write = HTTPD_CL_BUFSIZE;
  638.         }
  639.         else if( i_write <= 0 )
  640.         {
  641.             return VLC_EGENERIC;    /* wait, no data available */
  642.         }
  643.         /* Don't go past the end of the circular buffer */
  644.         i_write = __MIN( i_write, stream->i_buffer_size - i_pos );
  645.         /* using HTTPD_MSG_ANSWER -> data available */
  646.         answer->i_proto  = HTTPD_PROTO_HTTP;
  647.         answer->i_version= 0;
  648.         answer->i_type   = HTTPD_MSG_ANSWER;
  649.         answer->i_body = i_write;
  650.         answer->p_body = malloc( i_write );
  651.         memcpy( answer->p_body, &stream->p_buffer[i_pos], i_write );
  652.         answer->i_body_offset += i_write;
  653.         return VLC_SUCCESS;
  654.     }
  655.     else
  656.     {
  657.         answer->i_proto  = HTTPD_PROTO_HTTP;
  658.         answer->i_version= 0;
  659.         answer->i_type   = HTTPD_MSG_ANSWER;
  660.         answer->i_status = 200;
  661.         if( query->i_type != HTTPD_MSG_HEAD )
  662.         {
  663.             httpd_ClientModeStream( cl );
  664.             vlc_mutex_lock( &stream->lock );
  665.             /* Send the header */
  666.             if( stream->i_header > 0 )
  667.             {
  668.                 answer->i_body = stream->i_header;
  669.                 answer->p_body = malloc( stream->i_header );
  670.                 memcpy( answer->p_body, stream->p_header, stream->i_header );
  671.             }
  672.             answer->i_body_offset = stream->i_buffer_last_pos;
  673.             vlc_mutex_unlock( &stream->lock );
  674.         }
  675.         else
  676.         {
  677.             httpd_MsgAdd( answer, "Content-Length", "%d", 0 );
  678.             answer->i_body_offset = 0;
  679.         }
  680.         if( !strcmp( stream->psz_mime, "video/x-ms-asf-stream" ) )
  681.         {
  682.             bool b_xplaystream = false;
  683.             int i;
  684.             httpd_MsgAdd( answer, "Content-type", "%s",
  685.                           "application/octet-stream" );
  686.             httpd_MsgAdd( answer, "Server", "Cougar 4.1.0.3921" );
  687.             httpd_MsgAdd( answer, "Pragma", "no-cache" );
  688.             httpd_MsgAdd( answer, "Pragma", "client-id=%d", rand()&0x7fff );
  689.             httpd_MsgAdd( answer, "Pragma", "features="broadcast"" );
  690.             /* Check if there is a xPlayStrm=1 */
  691.             for( i = 0; i < query->i_name; i++ )
  692.             {
  693.                 if( !strcasecmp( query->name[i],  "Pragma" ) &&
  694.                     strstr( query->value[i], "xPlayStrm=1" ) )
  695.                 {
  696.                     b_xplaystream = true;
  697.                 }
  698.             }
  699.             if( !b_xplaystream )
  700.             {
  701.                 answer->i_body_offset = 0;
  702.             }
  703.         }
  704.         else
  705.         {
  706.             httpd_MsgAdd( answer, "Content-type",  "%s", stream->psz_mime );
  707.         }
  708.         httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
  709.         return VLC_SUCCESS;
  710.     }
  711. }
  712. httpd_stream_t *httpd_StreamNew( httpd_host_t *host,
  713.                                  const char *psz_url, const char *psz_mime,
  714.                                  const char *psz_user, const char *psz_password,
  715.                                  const vlc_acl_t *p_acl )
  716. {
  717.     httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) );
  718.     if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user,
  719.                                             psz_password, p_acl )
  720.         ) == NULL )
  721.     {
  722.         free( stream );
  723.         return NULL;
  724.     }
  725.     vlc_mutex_init( &stream->lock );
  726.     if( psz_mime && *psz_mime )
  727.     {
  728.         stream->psz_mime = strdup( psz_mime );
  729.     }
  730.     else
  731.     {
  732.         stream->psz_mime = strdup( httpd_MimeFromUrl( psz_url ) );
  733.     }
  734.     stream->i_header = 0;
  735.     stream->p_header = NULL;
  736.     stream->i_buffer_size = 5000000;    /* 5 Mo per stream */
  737.     stream->p_buffer = malloc( stream->i_buffer_size );
  738.     /* We set to 1 to make life simpler
  739.      * (this way i_body_offset can never be 0) */
  740.     stream->i_buffer_pos = 1;
  741.     stream->i_buffer_last_pos = 1;
  742.     httpd_UrlCatch( stream->url, HTTPD_MSG_HEAD, httpd_StreamCallBack,
  743.                     (httpd_callback_sys_t*)stream );
  744.     httpd_UrlCatch( stream->url, HTTPD_MSG_GET, httpd_StreamCallBack,
  745.                     (httpd_callback_sys_t*)stream );
  746.     httpd_UrlCatch( stream->url, HTTPD_MSG_POST, httpd_StreamCallBack,
  747.                     (httpd_callback_sys_t*)stream );
  748.     return stream;
  749. }
  750. int httpd_StreamHeader( httpd_stream_t *stream, uint8_t *p_data, int i_data )
  751. {
  752.     vlc_mutex_lock( &stream->lock );
  753.     free( stream->p_header );
  754.     stream->p_header = NULL;
  755.     stream->i_header = i_data;
  756.     if( i_data > 0 )
  757.     {
  758.         stream->p_header = malloc( i_data );
  759.         memcpy( stream->p_header, p_data, i_data );
  760.     }
  761.     vlc_mutex_unlock( &stream->lock );
  762.     return VLC_SUCCESS;
  763. }
  764. int httpd_StreamSend( httpd_stream_t *stream, uint8_t *p_data, int i_data )
  765. {
  766.     int i_count;
  767.     int i_pos;
  768.     if( i_data < 0 || p_data == NULL )
  769.     {
  770.         return VLC_SUCCESS;
  771.     }
  772.     vlc_mutex_lock( &stream->lock );
  773.     /* save this pointer (to be used by new connection) */
  774.     stream->i_buffer_last_pos = stream->i_buffer_pos;
  775.     i_pos = stream->i_buffer_pos % stream->i_buffer_size;
  776.     i_count = i_data;
  777.     while( i_count > 0)
  778.     {
  779.         int i_copy;
  780.         i_copy = __MIN( i_count, stream->i_buffer_size - i_pos );
  781.         /* Ok, we can't go past the end of our buffer */
  782.         memcpy( &stream->p_buffer[i_pos], p_data, i_copy );
  783.         i_pos = ( i_pos + i_copy ) % stream->i_buffer_size;
  784.         i_count -= i_copy;
  785.         p_data  += i_copy;
  786.     }
  787.     stream->i_buffer_pos += i_data;
  788.     vlc_mutex_unlock( &stream->lock );
  789.     return VLC_SUCCESS;
  790. }
  791. void httpd_StreamDelete( httpd_stream_t *stream )
  792. {
  793.     httpd_UrlDelete( stream->url );
  794.     vlc_mutex_destroy( &stream->lock );
  795.     free( stream->psz_mime );
  796.     free( stream->p_header );
  797.     free( stream->p_buffer );
  798.     free( stream );
  799. }
  800. /*****************************************************************************
  801.  * Low level
  802.  *****************************************************************************/
  803. static void* httpd_HostThread( void * );
  804. /* create a new host */
  805. httpd_host_t *httpd_HostNew( vlc_object_t *p_this, const char *psz_host,
  806.                              int i_port )
  807. {
  808.     return httpd_TLSHostNew( p_this, psz_host, i_port, NULL, NULL, NULL, NULL
  809.                            );
  810. }
  811. static const char psz_object_type[] = "http server";
  812. static vlc_mutex_t httpd_mutex = VLC_STATIC_MUTEX;
  813. httpd_host_t *httpd_TLSHostNew( vlc_object_t *p_this, const char *psz_hostname,
  814.                                 int i_port,
  815.                                 const char *psz_cert, const char *psz_key,
  816.                                 const char *psz_ca, const char *psz_crl )
  817. {
  818.     httpd_t      *httpd;
  819.     httpd_host_t *host;
  820.     tls_server_t *p_tls;
  821.     char *psz_host;
  822.     vlc_value_t  ptrval;
  823.     int i;
  824.     if( psz_hostname == NULL )
  825.         psz_hostname = "";
  826.     psz_host = strdup( psz_hostname );
  827.     if( psz_host == NULL )
  828.         return NULL;
  829.     /* to be sure to avoid multiple creation */
  830.     vlc_mutex_lock( &httpd_mutex );
  831.     httpd = libvlc_priv (p_this->p_libvlc)->p_httpd;
  832.     if( httpd == NULL )
  833.     {
  834.         msg_Info( p_this, "creating httpd" );
  835.         httpd = (httpd_t *)vlc_custom_create( p_this, sizeof (*httpd),
  836.                                               VLC_OBJECT_GENERIC,
  837.                                               psz_object_type );
  838.         if( httpd == NULL )
  839.         {
  840.             vlc_mutex_unlock( &httpd_mutex );
  841.             free( psz_host );
  842.             return NULL;
  843.         }
  844.         httpd->i_host = 0;
  845.         httpd->host   = NULL;
  846.         ptrval.p_address = httpd;
  847.         libvlc_priv (p_this->p_libvlc)->p_httpd = httpd;
  848.         vlc_object_attach( httpd, p_this->p_libvlc );
  849.     }
  850.     /* verify if it already exist */
  851.     for( i = httpd->i_host - 1; i >= 0; i-- )
  852.     {
  853.         host = httpd->host[i];
  854.         /* cannot mix TLS and non-TLS hosts */
  855.         if( ( ( httpd->host[i]->p_tls != NULL ) != ( psz_cert != NULL ) )
  856.          || ( host->i_port != i_port )
  857.          || strcmp( host->psz_hostname, psz_hostname ) )
  858.             continue;
  859.         /* Increase existing matching host reference count.
  860.          * The reference count is written under both the global httpd and the
  861.          * host lock. It is read with either or both locks held. The global
  862.          * lock is always acquired first. */
  863.         vlc_mutex_lock( &host->lock );
  864.         host->i_ref++;
  865.         vlc_mutex_unlock( &host->lock );
  866.         vlc_mutex_unlock( &httpd_mutex );
  867.         return host;
  868.     }
  869.     host = NULL;
  870.     /* determine TLS configuration */
  871.     if ( psz_cert != NULL )
  872.     {
  873.         p_tls = tls_ServerCreate( p_this, psz_cert, psz_key );
  874.         if ( p_tls == NULL )
  875.         {
  876.             msg_Err( p_this, "TLS initialization error" );
  877.             goto error;
  878.         }
  879.         if ( ( psz_ca != NULL) && tls_ServerAddCA( p_tls, psz_ca ) )
  880.         {
  881.             msg_Err( p_this, "TLS CA error" );
  882.             goto error;
  883.         }
  884.         if ( ( psz_crl != NULL) && tls_ServerAddCRL( p_tls, psz_crl ) )
  885.         {
  886.             msg_Err( p_this, "TLS CRL error" );
  887.             goto error;
  888.         }
  889.     }
  890.     else
  891.         p_tls = NULL;
  892.     /* create the new host */
  893.     host = (httpd_host_t *)vlc_custom_create( p_this, sizeof (*host),
  894.                                               VLC_OBJECT_GENERIC,
  895.                                               psz_object_type );
  896.     if (host == NULL)
  897.         goto error;
  898.     host->httpd = httpd;
  899.     vlc_mutex_init( &host->lock );
  900.     vlc_cond_init( &host->wait );
  901.     host->i_ref = 1;
  902.     host->fds = net_ListenTCP( p_this, psz_host, i_port );
  903.     if( host->fds == NULL )
  904.     {
  905.         msg_Err( p_this, "cannot create socket(s) for HTTP host" );
  906.         goto error;
  907.     }
  908.     for (host->nfd = 0; host->fds[host->nfd] != -1; host->nfd++);
  909.     if( vlc_object_waitpipe( VLC_OBJECT( host ) ) == -1 )
  910.     {
  911.         msg_Err( host, "signaling pipe error: %m" );
  912.         goto error;
  913.     }
  914.     host->i_port = i_port;
  915.     host->psz_hostname = psz_host;
  916.     host->i_url     = 0;
  917.     host->url       = NULL;
  918.     host->i_client  = 0;
  919.     host->client    = NULL;
  920.     host->p_tls = p_tls;
  921.     /* create the thread */
  922.     if( vlc_clone( &host->thread, httpd_HostThread, host,
  923.                    VLC_THREAD_PRIORITY_LOW ) )
  924.     {
  925.         msg_Err( p_this, "cannot spawn http host thread" );
  926.         goto error;
  927.     }
  928.     /* now add it to httpd */
  929.     TAB_APPEND( httpd->i_host, httpd->host, host );
  930.     vlc_mutex_unlock( &httpd_mutex );
  931.     return host;
  932. error:
  933.     free( psz_host );
  934.     if( httpd->i_host <= 0 )
  935.     {
  936.         libvlc_priv (httpd->p_libvlc)->p_httpd = NULL;
  937.         vlc_object_detach( httpd );
  938.         vlc_object_release( httpd );
  939.     }
  940.     vlc_mutex_unlock( &httpd_mutex );
  941.     if( host != NULL )
  942.     {
  943.         net_ListenClose( host->fds );
  944.         vlc_cond_destroy( &host->wait );
  945.         vlc_mutex_destroy( &host->lock );
  946.         vlc_object_release( host );
  947.     }
  948.     if( p_tls != NULL )
  949.         tls_ServerDelete( p_tls );
  950.     return NULL;
  951. }
  952. /* delete a host */
  953. void httpd_HostDelete( httpd_host_t *host )
  954. {
  955.     httpd_t *httpd = host->httpd;
  956.     int i;
  957.     bool delete = false;
  958.     vlc_mutex_lock( &httpd_mutex );
  959.     vlc_mutex_lock( &host->lock );
  960.     host->i_ref--;
  961.     if( host->i_ref == 0 )
  962.     {
  963.         vlc_cond_signal( &host->wait );
  964.         delete = true;
  965.     }
  966.     vlc_mutex_unlock( &host->lock );
  967.     if( !delete )
  968.     {
  969.         /* still used */
  970.         vlc_mutex_unlock( &httpd_mutex );
  971.         msg_Dbg( host, "httpd_HostDelete: host still in use" );
  972.         return;
  973.     }
  974.     TAB_REMOVE( httpd->i_host, httpd->host, host );
  975.     vlc_object_kill( host );
  976.     vlc_join( host->thread, NULL );
  977.     msg_Dbg( host, "HTTP host removed" );
  978.     for( i = 0; i < host->i_url; i++ )
  979.     {
  980.         msg_Err( host, "url still registered: %s", host->url[i]->psz_url );
  981.     }
  982.     for( i = 0; i < host->i_client; i++ )
  983.     {
  984.         httpd_client_t *cl = host->client[i];
  985.         msg_Warn( host, "client still connected" );
  986.         httpd_ClientClean( cl );
  987.         TAB_REMOVE( host->i_client, host->client, cl );
  988.         free( cl );
  989.         i--;
  990.         /* TODO */
  991.     }
  992.     if( host->p_tls != NULL)
  993.         tls_ServerDelete( host->p_tls );
  994.     net_ListenClose( host->fds );
  995.     free( host->psz_hostname );
  996.     vlc_cond_destroy( &host->wait );
  997.     vlc_mutex_destroy( &host->lock );
  998.     vlc_object_release( host );
  999.     if( httpd->i_host <= 0 )
  1000.     {
  1001.         msg_Dbg( httpd, "no hosts left, stopping httpd" );
  1002.         libvlc_priv (httpd->p_libvlc)->p_httpd = NULL;
  1003.         vlc_object_detach( httpd );
  1004.         vlc_object_release( httpd );
  1005.     }
  1006.     vlc_mutex_unlock( &httpd_mutex );
  1007. }
  1008. /* register a new url */
  1009. static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, const char *psz_url,
  1010.                                          const char *psz_user, const char *psz_password,
  1011.                                          const vlc_acl_t *p_acl, bool b_check )
  1012. {
  1013.     httpd_url_t *url;
  1014.     int         i;
  1015.     assert( psz_url != NULL );
  1016.     vlc_mutex_lock( &host->lock );
  1017.     if( b_check )
  1018.     {
  1019.         for( i = 0; i < host->i_url; i++ )
  1020.         {
  1021.             if( !strcmp( psz_url, host->url[i]->psz_url ) )
  1022.             {
  1023.                 msg_Warn( host->httpd,
  1024.                           "cannot add '%s' (url already defined)", psz_url );
  1025.                 vlc_mutex_unlock( &host->lock );
  1026.                 return NULL;
  1027.             }
  1028.         }
  1029.     }
  1030.     url = malloc( sizeof( httpd_url_t ) );
  1031.     url->host = host;
  1032.     vlc_mutex_init( &url->lock );
  1033.     url->psz_url = strdup( psz_url );
  1034.     url->psz_user = strdup( psz_user ? psz_user : "" );
  1035.     url->psz_password = strdup( psz_password ? psz_password : "" );
  1036.     url->p_acl = ACL_Duplicate( host, p_acl );
  1037.     for( i = 0; i < HTTPD_MSG_MAX; i++ )
  1038.     {
  1039.         url->catch[i].cb = NULL;
  1040.         url->catch[i].p_sys = NULL;
  1041.     }
  1042.     TAB_APPEND( host->i_url, host->url, url );
  1043.     vlc_cond_signal( &host->wait );
  1044.     vlc_mutex_unlock( &host->lock );
  1045.     return url;
  1046. }
  1047. httpd_url_t *httpd_UrlNew( httpd_host_t *host, const char *psz_url,
  1048.                            const char *psz_user, const char *psz_password,
  1049.                            const vlc_acl_t *p_acl )
  1050. {
  1051.     return httpd_UrlNewPrivate( host, psz_url, psz_user,
  1052.                                 psz_password, p_acl, false );
  1053. }
  1054. httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, const char *psz_url,
  1055.                                  const char *psz_user, const char *psz_password,
  1056.                                  const vlc_acl_t *p_acl )
  1057. {
  1058.     return httpd_UrlNewPrivate( host, psz_url, psz_user,
  1059.                                 psz_password, p_acl, true );
  1060. }
  1061. /* register callback on a url */
  1062. int httpd_UrlCatch( httpd_url_t *url, int i_msg, httpd_callback_t cb,
  1063.                     httpd_callback_sys_t *p_sys )
  1064. {
  1065.     vlc_mutex_lock( &url->lock );
  1066.     url->catch[i_msg].cb   = cb;
  1067.     url->catch[i_msg].p_sys= p_sys;
  1068.     vlc_mutex_unlock( &url->lock );
  1069.     return VLC_SUCCESS;
  1070. }
  1071. /* delete an url */
  1072. void httpd_UrlDelete( httpd_url_t *url )
  1073. {
  1074.     httpd_host_t *host = url->host;
  1075.     int          i;
  1076.     vlc_mutex_lock( &host->lock );
  1077.     TAB_REMOVE( host->i_url, host->url, url );
  1078.     vlc_mutex_destroy( &url->lock );
  1079.     free( url->psz_url );
  1080.     free( url->psz_user );
  1081.     free( url->psz_password );
  1082.     ACL_Destroy( url->p_acl );
  1083.     for( i = 0; i < host->i_client; i++ )
  1084.     {
  1085.         httpd_client_t *client = host->client[i];
  1086.         if( client->url == url )
  1087.         {
  1088.             /* TODO complete it */
  1089.             msg_Warn( host, "force closing connections" );
  1090.             httpd_ClientClean( client );
  1091.             TAB_REMOVE( host->i_client, host->client, client );
  1092.             free( client );
  1093.             i--;
  1094.         }
  1095.     }
  1096.     free( url );
  1097.     vlc_mutex_unlock( &host->lock );
  1098. }
  1099. static void httpd_MsgInit( httpd_message_t *msg )
  1100. {
  1101.     msg->cl         = NULL;
  1102.     msg->i_type     = HTTPD_MSG_NONE;
  1103.     msg->i_proto    = HTTPD_PROTO_NONE;
  1104.     msg->i_version  = -1; /* FIXME */
  1105.     msg->i_status   = 0;
  1106.     msg->psz_url    = NULL;
  1107.     msg->psz_args   = NULL;
  1108.     msg->i_channel  = -1;
  1109.     msg->i_name     = 0;
  1110.     msg->name       = NULL;
  1111.     msg->i_value    = 0;
  1112.     msg->value      = NULL;
  1113.     msg->i_body_offset = 0;
  1114.     msg->i_body        = 0;
  1115.     msg->p_body        = NULL;
  1116. }
  1117. static void httpd_MsgClean( httpd_message_t *msg )
  1118. {
  1119.     int i;
  1120.     free( msg->psz_url );
  1121.     free( msg->psz_args );
  1122.     for( i = 0; i < msg->i_name; i++ )
  1123.     {
  1124.         free( msg->name[i] );
  1125.         free( msg->value[i] );
  1126.     }
  1127.     free( msg->name );
  1128.     free( msg->value );
  1129.     free( msg->p_body );
  1130.     httpd_MsgInit( msg );
  1131. }
  1132. const char *httpd_MsgGet( const httpd_message_t *msg, const char *name )
  1133. {
  1134.     int i;
  1135.     for( i = 0; i < msg->i_name; i++ )
  1136.     {
  1137.         if( !strcasecmp( msg->name[i], name ))
  1138.         {
  1139.             return msg->value[i];
  1140.         }
  1141.     }
  1142.     return NULL;
  1143. }
  1144. void httpd_MsgAdd( httpd_message_t *msg, const char *name, const char *psz_value, ... )
  1145. {
  1146.     va_list args;
  1147.     char *value = NULL;
  1148.     va_start( args, psz_value );
  1149.     if( vasprintf( &value, psz_value, args ) == -1 )
  1150.         value = NULL;
  1151.     va_end( args );
  1152.     if( value == NULL )
  1153.         return;
  1154.     name = strdup( name );
  1155.     if( name == NULL )
  1156.     {
  1157.         free( value );
  1158.         return;
  1159.     }
  1160.     TAB_APPEND( msg->i_name,  msg->name,  (char*)name );
  1161.     TAB_APPEND( msg->i_value, msg->value, value );
  1162. }
  1163. static void httpd_ClientInit( httpd_client_t *cl, mtime_t now )
  1164. {
  1165.     cl->i_state = HTTPD_CLIENT_RECEIVING;
  1166.     cl->i_activity_date = now;
  1167.     cl->i_activity_timeout = INT64_C(10000000);
  1168.     cl->i_buffer_size = HTTPD_CL_BUFSIZE;
  1169.     cl->i_buffer = 0;
  1170.     cl->p_buffer = malloc( cl->i_buffer_size );
  1171.     cl->i_mode   = HTTPD_CLIENT_FILE;
  1172.     cl->b_read_waiting = false;
  1173.     httpd_MsgInit( &cl->query );
  1174.     httpd_MsgInit( &cl->answer );
  1175. }
  1176. void httpd_ClientModeStream( httpd_client_t *cl )
  1177. {
  1178.     cl->i_mode   = HTTPD_CLIENT_STREAM;
  1179. }
  1180. void httpd_ClientModeBidir( httpd_client_t *cl )
  1181. {
  1182.     cl->i_mode   = HTTPD_CLIENT_BIDIR;
  1183. }
  1184. char* httpd_ClientIP( const httpd_client_t *cl, char *psz_ip )
  1185. {
  1186.     return net_GetPeerAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
  1187. }
  1188. char* httpd_ServerIP( const httpd_client_t *cl, char *psz_ip )
  1189. {
  1190.     return net_GetSockAddress( cl->fd, psz_ip, NULL ) ? NULL : psz_ip;
  1191. }
  1192. static void httpd_ClientClean( httpd_client_t *cl )
  1193. {
  1194.     if( cl->fd >= 0 )
  1195.     {
  1196.         if( cl->p_tls != NULL )
  1197.             tls_ServerSessionClose( cl->p_tls );
  1198.         net_Close( cl->fd );
  1199.         cl->fd = -1;
  1200.     }
  1201.     httpd_MsgClean( &cl->answer );
  1202.     httpd_MsgClean( &cl->query );
  1203.     free( cl->p_buffer );
  1204.     cl->p_buffer = NULL;
  1205. }
  1206. static httpd_client_t *httpd_ClientNew( int fd, tls_session_t *p_tls, mtime_t now )
  1207. {
  1208.     httpd_client_t *cl = malloc( sizeof( httpd_client_t ) );
  1209.     if( !cl ) return NULL;
  1210.     cl->i_ref   = 0;
  1211.     cl->fd      = fd;
  1212.     cl->url     = NULL;
  1213.     cl->p_tls = p_tls;
  1214.     httpd_ClientInit( cl, now );
  1215.     return cl;
  1216. }
  1217. static
  1218. ssize_t httpd_NetRecv (httpd_client_t *cl, uint8_t *p, size_t i_len)
  1219. {
  1220.     tls_session_t *p_tls;
  1221.     ssize_t val;
  1222.     p_tls = cl->p_tls;
  1223.     do
  1224.         val = p_tls ? tls_Recv (p_tls, p, i_len)
  1225.                     : recv (cl->fd, p, i_len, 0);
  1226.     while (val == -1 && errno == EINTR);
  1227.     return val;
  1228. }
  1229. static
  1230. ssize_t httpd_NetSend (httpd_client_t *cl, const uint8_t *p, size_t i_len)
  1231. {
  1232.     tls_session_t *p_tls;
  1233.     ssize_t val;
  1234.     p_tls = cl->p_tls;
  1235.     do
  1236.         val = p_tls ? tls_Send( p_tls, p, i_len )
  1237.                     : send (cl->fd, p, i_len, 0);
  1238.     while (val == -1 && errno == EINTR);
  1239.     return val;
  1240. }
  1241. static const struct
  1242. {
  1243.     const char name[16];
  1244.     int  i_type;
  1245.     int  i_proto;
  1246. }
  1247. msg_type[] =
  1248. {
  1249.     { "OPTIONS",       HTTPD_MSG_OPTIONS,      HTTPD_PROTO_RTSP },
  1250.     { "DESCRIBE",      HTTPD_MSG_DESCRIBE,     HTTPD_PROTO_RTSP },
  1251.     { "SETUP",         HTTPD_MSG_SETUP,        HTTPD_PROTO_RTSP },
  1252.     { "PLAY",          HTTPD_MSG_PLAY,         HTTPD_PROTO_RTSP },
  1253.     { "PAUSE",         HTTPD_MSG_PAUSE,        HTTPD_PROTO_RTSP },
  1254.     { "GET_PARAMETER", HTTPD_MSG_GETPARAMETER, HTTPD_PROTO_RTSP },
  1255.     { "TEARDOWN",      HTTPD_MSG_TEARDOWN,     HTTPD_PROTO_RTSP },
  1256.     { "GET",           HTTPD_MSG_GET,          HTTPD_PROTO_HTTP },
  1257.     { "HEAD",          HTTPD_MSG_HEAD,         HTTPD_PROTO_HTTP },
  1258.     { "POST",          HTTPD_MSG_POST,         HTTPD_PROTO_HTTP },
  1259.     { "",              HTTPD_MSG_NONE,         HTTPD_PROTO_NONE }
  1260. };
  1261. static void httpd_ClientRecv( httpd_client_t *cl )
  1262. {
  1263.     int i_len;
  1264.     /* ignore leading whites */
  1265.     if( ( cl->query.i_proto == HTTPD_PROTO_NONE ) &&
  1266.         ( cl->i_buffer == 0 ) )
  1267.     {
  1268.         unsigned char c;
  1269.         i_len = httpd_NetRecv( cl, &c, 1 );
  1270.         if( ( i_len > 0 ) && ( strchr( "rnt ", c ) == NULL ) )
  1271.         {
  1272.             cl->p_buffer[0] = c;
  1273.             cl->i_buffer++;
  1274.         }
  1275.     }
  1276.     else
  1277.     if( cl->query.i_proto == HTTPD_PROTO_NONE )
  1278.     {
  1279.         /* enough to see if it's Interleaved RTP over RTSP or RTSP/HTTP */
  1280.         i_len = httpd_NetRecv( cl, &cl->p_buffer[cl->i_buffer],
  1281.                                7 - cl->i_buffer );
  1282.         if( i_len > 0 )
  1283.         {
  1284.             cl->i_buffer += i_len;
  1285.         }
  1286.         if( ( cl->i_buffer >= 4 ) && ( cl->p_buffer[0] == '$' ) )
  1287.         {
  1288.             /* Interleaved RTP over RTSP */
  1289.             cl->query.i_proto = HTTPD_PROTO_RTSP;
  1290.             cl->query.i_type  = HTTPD_MSG_CHANNEL;
  1291.             cl->query.i_channel = cl->p_buffer[1];
  1292.             cl->query.i_body  = (cl->p_buffer[2] << 8)|cl->p_buffer[3];
  1293.             cl->query.p_body  = malloc( cl->query.i_body );
  1294.             cl->i_buffer      -= 4;
  1295.             memcpy( cl->query.p_body, cl->p_buffer + 4, cl->i_buffer );
  1296.         }
  1297.         else
  1298.         /* The smallest legal request is 7 bytes ("GET /rn"),
  1299.          * this is the maximum we can ask at this point. */
  1300.         if( cl->i_buffer >= 7 )
  1301.         {
  1302.             if( !memcmp( cl->p_buffer, "HTTP/1.", 7 ) )
  1303.             {
  1304.                 cl->query.i_proto = HTTPD_PROTO_HTTP;
  1305.                 cl->query.i_type  = HTTPD_MSG_ANSWER;
  1306.             }
  1307.             else if( !memcmp( cl->p_buffer, "RTSP/1.", 7 ) )
  1308.             {
  1309.                 cl->query.i_proto = HTTPD_PROTO_RTSP;
  1310.                 cl->query.i_type  = HTTPD_MSG_ANSWER;
  1311.             }
  1312.             else
  1313.             {
  1314.                 /* We need the full request line to determine the protocol. */
  1315.                 cl->query.i_proto = HTTPD_PROTO_HTTP0;
  1316.                 cl->query.i_type  = HTTPD_MSG_NONE;
  1317.             }
  1318.         }
  1319.     }
  1320.     else if( cl->query.i_body > 0 )
  1321.     {
  1322.         /* we are reading the body of a request or a channel */
  1323.         i_len = httpd_NetRecv( cl, &cl->query.p_body[cl->i_buffer],
  1324.                                cl->query.i_body - cl->i_buffer );
  1325.         if( i_len > 0 )
  1326.         {
  1327.             cl->i_buffer += i_len;
  1328.         }
  1329.         if( cl->i_buffer >= cl->query.i_body )
  1330.         {
  1331.             cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
  1332.         }
  1333.     }
  1334.     else
  1335.     {
  1336.         /* we are reading a header -> char by char */
  1337.         for( ;; )
  1338.         {
  1339.             if( cl->i_buffer == cl->i_buffer_size )
  1340.             {
  1341.                 uint8_t *newbuf = realloc( cl->p_buffer, cl->i_buffer_size + 1024 );
  1342.                 if( newbuf == NULL )
  1343.                 {
  1344.                     i_len = 0;
  1345.                     break;
  1346.                 }
  1347.                 cl->p_buffer = newbuf;
  1348.                 cl->i_buffer_size += 1024;
  1349.             }
  1350.             i_len = httpd_NetRecv (cl, &cl->p_buffer[cl->i_buffer], 1 );
  1351.             if( i_len <= 0 )
  1352.             {
  1353.                 break;
  1354.             }
  1355.             cl->i_buffer++;
  1356.             if( ( cl->query.i_proto == HTTPD_PROTO_HTTP0 )
  1357.              && ( cl->p_buffer[cl->i_buffer - 1] == 'n' ) )
  1358.             {
  1359.                 /* Request line is now complete */
  1360.                 const char *p = memchr( cl->p_buffer, ' ', cl->i_buffer );
  1361.                 size_t len;
  1362.                 assert( cl->query.i_type == HTTPD_MSG_NONE );
  1363.                 if( p == NULL ) /* no URI: evil guy */
  1364.                 {
  1365.                     i_len = 0; /* drop connection */
  1366.                     break;
  1367.                 }
  1368.                 do
  1369.                     p++; /* skips extra spaces */
  1370.                 while( *p == ' ' );
  1371.                 p = memchr( p, ' ', ((char *)cl->p_buffer) + cl->i_buffer - p );
  1372.                 if( p == NULL ) /* no explicit protocol: HTTP/0.9 */
  1373.                 {
  1374.                     i_len = 0; /* not supported currently -> drop */
  1375.                     break;
  1376.                 }
  1377.                 do
  1378.                     p++; /* skips extra spaces ever again */
  1379.                 while( *p == ' ' );
  1380.                 len = ((char *)cl->p_buffer) + cl->i_buffer - p;
  1381.                 if( len < 7 ) /* foreign protocol */
  1382.                     i_len = 0; /* I don't understand -> drop */
  1383.                 else
  1384.                 if( memcmp( p, "HTTP/1.", 7 ) == 0 )
  1385.                 {
  1386.                     cl->query.i_proto = HTTPD_PROTO_HTTP;
  1387.                     cl->query.i_version = atoi( p + 7 );
  1388.                 }
  1389.                 else
  1390.                 if( memcmp( p, "RTSP/1.", 7 ) == 0 )
  1391.                 {
  1392.                     cl->query.i_proto = HTTPD_PROTO_RTSP;
  1393.                     cl->query.i_version = atoi( p + 7 );
  1394.                 }
  1395.                 else
  1396.                 if( memcmp( p, "HTTP/", 5 ) == 0 )
  1397.                 {
  1398.                     const uint8_t sorry[] =
  1399.                        "HTTP/1.1 505 Unknown HTTP versionrnrn";
  1400.                     httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
  1401.                     i_len = 0; /* drop */
  1402.                 }
  1403.                 else
  1404.                 if( memcmp( p, "RTSP/", 5 ) == 0 )
  1405.                 {
  1406.                     const uint8_t sorry[] =
  1407.                         "RTSP/1.0 505 Unknown RTSP versionrnrn";
  1408.                     httpd_NetSend( cl, sorry, sizeof( sorry ) - 1 );
  1409.                     i_len = 0; /* drop */
  1410.                 }
  1411.                 else /* yet another foreign protocol */
  1412.                     i_len = 0;
  1413.                 if( i_len == 0 )
  1414.                     break;
  1415.             }
  1416.             if( ( cl->i_buffer >= 2 && !memcmp( &cl->p_buffer[cl->i_buffer-2], "nn", 2 ) )||
  1417.                 ( cl->i_buffer >= 4 && !memcmp( &cl->p_buffer[cl->i_buffer-4], "rnrn", 4 ) ) )
  1418.             {
  1419.                 char *p;
  1420.                 /* we have finished the header so parse it and set i_body */
  1421.                 cl->p_buffer[cl->i_buffer] = '';
  1422.                 if( cl->query.i_type == HTTPD_MSG_ANSWER )
  1423.                 {
  1424.                     /* FIXME:
  1425.                      * assume strlen( "HTTP/1.x" ) = 8
  1426.                      */
  1427.                     cl->query.i_status =
  1428.                         strtol( (char *)&cl->p_buffer[8],
  1429.                                 &p, 0 );
  1430.                     while( *p == ' ' )
  1431.                         p++;
  1432.                 }
  1433.                 else
  1434.                 {
  1435.                     unsigned i;
  1436.                     p = NULL;
  1437.                     cl->query.i_type = HTTPD_MSG_NONE;
  1438.                     /*fprintf( stderr, "received new request=%sn", cl->p_buffer);*/
  1439.                     for( i = 0; msg_type[i].name[0]; i++ )
  1440.                     {
  1441.                         if( !strncmp( (char *)cl->p_buffer, msg_type[i].name,
  1442.                                       strlen( msg_type[i].name ) ) )
  1443.                         {
  1444.                             p = (char *)&cl->p_buffer[strlen(msg_type[i].name) + 1 ];
  1445.                             cl->query.i_type = msg_type[i].i_type;
  1446.                             if( cl->query.i_proto != msg_type[i].i_proto )
  1447.                             {
  1448.                                 p = NULL;
  1449.                                 cl->query.i_proto = HTTPD_PROTO_NONE;
  1450.                                 cl->query.i_type = HTTPD_MSG_NONE;
  1451.                             }
  1452.                             break;
  1453.                         }
  1454.                     }
  1455.                     if( p == NULL )
  1456.                     {
  1457.                         if( strstr( (char *)cl->p_buffer, "HTTP/1." ) )
  1458.                         {
  1459.                             cl->query.i_proto = HTTPD_PROTO_HTTP;
  1460.                         }
  1461.                         else if( strstr( (char *)cl->p_buffer, "RTSP/1." ) )
  1462.                         {
  1463.                             cl->query.i_proto = HTTPD_PROTO_RTSP;
  1464.                         }
  1465.                     }
  1466.                     else
  1467.                     {
  1468.                         char *p2;
  1469.                         char *p3;
  1470.                         while( *p == ' ' )
  1471.                         {
  1472.                             p++;
  1473.                         }
  1474.                         p2 = strchr( p, ' ' );
  1475.                         if( p2 )
  1476.                         {
  1477.                             *p2++ = '';
  1478.                         }
  1479.                         if( !strncasecmp( p, ( cl->query.i_proto
  1480.                                    == HTTPD_PROTO_HTTP ) ? "http" : "rtsp", 4 )
  1481.                          && p[4 + !!strchr( "sS", p[4] )] == ':' )
  1482.                         {   /* Skip hier-part of URL (if present) */
  1483.                             p = strchr( p, ':' ) + 1; /* skip URI scheme */
  1484.                             if( !strncmp( p, "//", 2 ) ) /* skip authority */
  1485.                             {   /* see RFC3986 §3.2 */
  1486.                                 p += 2;
  1487.                                 while( *p && !strchr( "/?#", *p ) ) p++;
  1488.                             }
  1489.                         }
  1490.                         cl->query.psz_url = strdup( p );
  1491.                         if( ( p3 = strchr( cl->query.psz_url, '?' ) )  )
  1492.                         {
  1493.                             *p3++ = '';
  1494.                             cl->query.psz_args = (uint8_t *)strdup( p3 );
  1495.                         }
  1496.                         p = p2;
  1497.                     }
  1498.                 }
  1499.                 if( p )
  1500.                 {
  1501.                     p = strchr( p, 'n' );
  1502.                 }
  1503.                 if( p )
  1504.                 {
  1505.                     while( *p == 'n' || *p == 'r' )
  1506.                     {
  1507.                         p++;
  1508.                     }
  1509.                     while( p && *p != '' )
  1510.                     {
  1511.                         char *line = p;
  1512.                         char *eol = p = strchr( p, 'n' );
  1513.                         char *colon;
  1514.                         while( eol && eol >= line && ( *eol == 'n' || *eol == 'r' ) )
  1515.                         {
  1516.                             *eol-- = '';
  1517.                         }
  1518.                         if( ( colon = strchr( line, ':' ) ) )
  1519.                         {
  1520.                             char *name;
  1521.                             char *value;
  1522.                             *colon++ = '';
  1523.                             while( *colon == ' ' )
  1524.                             {
  1525.                                 colon++;
  1526.                             }
  1527.                             name = strdup( line );
  1528.                             value = strdup( colon );
  1529.                             TAB_APPEND( cl->query.i_name, cl->query.name, name );
  1530.                             TAB_APPEND( cl->query.i_value,cl->query.value,value);
  1531.                             if( !strcasecmp( name, "Content-Length" ) )
  1532.                             {
  1533.                                 cl->query.i_body = atol( value );
  1534.                             }
  1535.                         }
  1536.                         if( p )
  1537.                         {
  1538.                             p++;
  1539.                             while( *p == 'n' || *p == 'r' )
  1540.                             {
  1541.                                 p++;
  1542.                             }
  1543.                         }
  1544.                     }
  1545.                 }
  1546.                 if( cl->query.i_body > 0 )
  1547.                 {
  1548.                     /* TODO Mhh, handle the case client will only send a
  1549.                      * request and close the connection
  1550.                      * to mark and of body (probably only RTSP) */
  1551.                     cl->query.p_body = malloc( cl->query.i_body );
  1552.                     cl->i_buffer = 0;
  1553.                 }
  1554.                 else
  1555.                 {
  1556.                     cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
  1557.                 }
  1558.             }
  1559.         }
  1560.     }
  1561.     /* check if the client is to be set to dead */
  1562. #if defined( WIN32 ) || defined( UNDER_CE )
  1563.     if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
  1564. #else
  1565.     if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
  1566. #endif
  1567.     {
  1568.         if( cl->query.i_proto != HTTPD_PROTO_NONE && cl->query.i_type != HTTPD_MSG_NONE )
  1569.         {
  1570.             /* connection closed -> end of data */
  1571.             if( cl->query.i_body > 0 )
  1572.             {
  1573.                 cl->query.i_body = cl->i_buffer;
  1574.             }
  1575.             cl->i_state = HTTPD_CLIENT_RECEIVE_DONE;
  1576.         }
  1577.         else
  1578.         {
  1579.             cl->i_state = HTTPD_CLIENT_DEAD;
  1580.         }
  1581.     }
  1582.     /* XXX: for QT I have to disable timeout. Try to find why */
  1583.     if( cl->query.i_proto == HTTPD_PROTO_RTSP )
  1584.         cl->i_activity_timeout = 0;
  1585. #if 0 /* Debugging only */
  1586.     if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
  1587.     {
  1588.         int i;
  1589.         fprintf( stderr, "received new requestn" );
  1590.         fprintf( stderr, "  - proto=%sn",
  1591.                  cl->query.i_proto == HTTPD_PROTO_HTTP ? "HTTP" : "RTSP" );
  1592.         fprintf( stderr, "  - version=%dn", cl->query.i_version );
  1593.         fprintf( stderr, "  - msg=%dn", cl->query.i_type );
  1594.         if( cl->query.i_type == HTTPD_MSG_ANSWER )
  1595.         {
  1596.             fprintf( stderr, "  - answer=%d '%s'n", cl->query.i_status,
  1597.                      cl->query.psz_status );
  1598.         }
  1599.         else if( cl->query.i_type != HTTPD_MSG_NONE )
  1600.         {
  1601.             fprintf( stderr, "  - url=%sn", cl->query.psz_url );
  1602.         }
  1603.         for( i = 0; i < cl->query.i_name; i++ )
  1604.         {
  1605.             fprintf( stderr, "  - option name='%s' value='%s'n",
  1606.                      cl->query.name[i], cl->query.value[i] );
  1607.         }
  1608.     }
  1609. #endif
  1610. }
  1611. static void httpd_ClientSend( httpd_client_t *cl )
  1612. {
  1613.     int i;
  1614.     int i_len;
  1615.     if( cl->i_buffer < 0 )
  1616.     {
  1617.         /* We need to create the header */
  1618.         int i_size = 0;
  1619.         char *p;
  1620.         const char *psz_status = httpd_ReasonFromCode( cl->answer.i_status );
  1621.         i_size = strlen( "HTTP/1.") + 10 + 10 + strlen( psz_status ) + 5;
  1622.         for( i = 0; i < cl->answer.i_name; i++ )
  1623.         {
  1624.             i_size += strlen( cl->answer.name[i] ) + 2 +
  1625.                       strlen( cl->answer.value[i] ) + 2;
  1626.         }
  1627.         if( cl->i_buffer_size < i_size )
  1628.         {
  1629.             cl->i_buffer_size = i_size;
  1630.             free( cl->p_buffer );
  1631.             cl->p_buffer = malloc( i_size );
  1632.         }
  1633.         p = (char *)cl->p_buffer;
  1634.         p += sprintf( p, "%s.%u %d %srn",
  1635.                       cl->answer.i_proto ==  HTTPD_PROTO_HTTP ? "HTTP/1" : "RTSP/1",
  1636.                       cl->answer.i_version,
  1637.                       cl->answer.i_status, psz_status );
  1638.         for( i = 0; i < cl->answer.i_name; i++ )
  1639.         {
  1640.             p += sprintf( p, "%s: %srn", cl->answer.name[i],
  1641.                           cl->answer.value[i] );
  1642.         }
  1643.         p += sprintf( p, "rn" );
  1644.         cl->i_buffer = 0;
  1645.         cl->i_buffer_size = (uint8_t*)p - cl->p_buffer;
  1646.         /*fprintf( stderr, "sending answern" );
  1647.         fprintf( stderr, "%s",  cl->p_buffer );*/
  1648.     }
  1649.     i_len = httpd_NetSend( cl, &cl->p_buffer[cl->i_buffer],
  1650.                            cl->i_buffer_size - cl->i_buffer );
  1651.     if( i_len >= 0 )
  1652.     {
  1653.         cl->i_buffer += i_len;
  1654.         if( cl->i_buffer >= cl->i_buffer_size )
  1655.         {
  1656.             if( cl->answer.i_body == 0  && cl->answer.i_body_offset > 0 &&
  1657.                 !cl->b_read_waiting )
  1658.             {
  1659.                 /* catch more body data */
  1660.                 int     i_msg = cl->query.i_type;
  1661.                 int64_t i_offset = cl->answer.i_body_offset;
  1662.                 httpd_MsgClean( &cl->answer );
  1663.                 cl->answer.i_body_offset = i_offset;
  1664.                 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
  1665.                                           &cl->answer, &cl->query );
  1666.             }
  1667.             if( cl->answer.i_body > 0 )
  1668.             {
  1669.                 /* send the body data */
  1670.                 free( cl->p_buffer );
  1671.                 cl->p_buffer = cl->answer.p_body;
  1672.                 cl->i_buffer_size = cl->answer.i_body;
  1673.                 cl->i_buffer = 0;
  1674.                 cl->answer.i_body = 0;
  1675.                 cl->answer.p_body = NULL;
  1676.             }
  1677.             else
  1678.             {
  1679.                 /* send finished */
  1680.                 cl->i_state = HTTPD_CLIENT_SEND_DONE;
  1681.             }
  1682.         }
  1683.     }
  1684.     else
  1685.     {
  1686. #if defined( WIN32 ) || defined( UNDER_CE )
  1687.         if( ( i_len < 0 && WSAGetLastError() != WSAEWOULDBLOCK ) || ( i_len == 0 ) )
  1688. #else
  1689.         if( ( i_len < 0 && errno != EAGAIN ) || ( i_len == 0 ) )
  1690. #endif
  1691.         {
  1692.             /* error */
  1693.             cl->i_state = HTTPD_CLIENT_DEAD;
  1694.         }
  1695.     }
  1696. }
  1697. static void httpd_ClientTlsHsIn( httpd_client_t *cl )
  1698. {
  1699.     switch( tls_SessionContinueHandshake( cl->p_tls ) )
  1700.     {
  1701.         case 0:
  1702.             cl->i_state = HTTPD_CLIENT_RECEIVING;
  1703.             break;
  1704.         case -1:
  1705.             cl->i_state = HTTPD_CLIENT_DEAD;
  1706.             cl->p_tls = NULL;
  1707.             break;
  1708.         case 2:
  1709.             cl->i_state = HTTPD_CLIENT_TLS_HS_OUT;
  1710.     }
  1711. }
  1712. static void httpd_ClientTlsHsOut( httpd_client_t *cl )
  1713. {
  1714.     switch( tls_SessionContinueHandshake( cl->p_tls ) )
  1715.     {
  1716.         case 0:
  1717.             cl->i_state = HTTPD_CLIENT_RECEIVING;
  1718.             break;
  1719.         case -1:
  1720.             cl->i_state = HTTPD_CLIENT_DEAD;
  1721.             cl->p_tls = NULL;
  1722.             break;
  1723.         case 1:
  1724.             cl->i_state = HTTPD_CLIENT_TLS_HS_IN;
  1725.             break;
  1726.     }
  1727. }
  1728. static void* httpd_HostThread( void *data )
  1729. {
  1730.     httpd_host_t *host = data;
  1731.     tls_session_t *p_tls = NULL;
  1732.     counter_t *p_total_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
  1733.     counter_t *p_active_counter = stats_CounterCreate( host, VLC_VAR_INTEGER, STATS_COUNTER );
  1734.     int evfd = vlc_object_waitpipe( VLC_OBJECT( host ) );
  1735.     for( ;; )
  1736.     {
  1737.         /* prepare a new TLS session */
  1738.         if( ( p_tls == NULL ) && ( host->p_tls != NULL ) )
  1739.             p_tls = tls_ServerSessionPrepare( host->p_tls );
  1740.         struct pollfd ufd[host->nfd + host->i_client + 1];
  1741.         unsigned nfd;
  1742.         for( nfd = 0; nfd < host->nfd; nfd++ )
  1743.         {
  1744.             ufd[nfd].fd = host->fds[nfd];
  1745.             ufd[nfd].events = POLLIN;
  1746.             ufd[nfd].revents = 0;
  1747.         }
  1748.         /* add all socket that should be read/write and close dead connection */
  1749.         vlc_mutex_lock( &host->lock );
  1750.         while( host->i_url <= 0 && host->i_ref > 0 )
  1751.             vlc_cond_wait( &host->wait, &host->lock );
  1752.         mtime_t now = mdate();
  1753.         bool b_low_delay = false;
  1754.         for(int i_client = 0; i_client < host->i_client; i_client++ )
  1755.         {
  1756.             httpd_client_t *cl = host->client[i_client];
  1757.             if( cl->i_ref < 0 || ( cl->i_ref == 0 &&
  1758.                 ( cl->i_state == HTTPD_CLIENT_DEAD ||
  1759.                   ( cl->i_activity_timeout > 0 &&
  1760.                     cl->i_activity_date+cl->i_activity_timeout < now) ) ) )
  1761.             {
  1762.                 httpd_ClientClean( cl );
  1763.                 stats_UpdateInteger( host, p_active_counter, -1, NULL );
  1764.                 TAB_REMOVE( host->i_client, host->client, cl );
  1765.                 free( cl );
  1766.                 i_client--;
  1767.                 continue;
  1768.             }
  1769.             struct pollfd *pufd = ufd + nfd;
  1770.             assert (pufd < ufd + (sizeof (ufd) / sizeof (ufd[0])));
  1771.             pufd->fd = cl->fd;
  1772.             pufd->events = pufd->revents = 0;
  1773.             if( ( cl->i_state == HTTPD_CLIENT_RECEIVING )
  1774.                   || ( cl->i_state == HTTPD_CLIENT_TLS_HS_IN ) )
  1775.             {
  1776.                 pufd->events = POLLIN;
  1777.             }
  1778.             else if( ( cl->i_state == HTTPD_CLIENT_SENDING )
  1779.                   || ( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT ) )
  1780.             {
  1781.                 pufd->events = POLLOUT;
  1782.             }
  1783.             else if( cl->i_state == HTTPD_CLIENT_RECEIVE_DONE )
  1784.             {
  1785.                 httpd_message_t *answer = &cl->answer;
  1786.                 httpd_message_t *query  = &cl->query;
  1787.                 int i_msg = query->i_type;
  1788.                 httpd_MsgInit( answer );
  1789.                 /* Handle what we received */
  1790.                 if( (cl->i_mode != HTTPD_CLIENT_BIDIR) &&
  1791.                     (i_msg == HTTPD_MSG_ANSWER || i_msg == HTTPD_MSG_CHANNEL) )
  1792.                 {
  1793.                     /* we can only receive request from client when not
  1794.                      * in BIDIR mode */
  1795.                     cl->url     = NULL;
  1796.                     cl->i_state = HTTPD_CLIENT_DEAD;
  1797.                 }
  1798.                 else if( i_msg == HTTPD_MSG_ANSWER )
  1799.                 {
  1800.                     /* We are in BIDIR mode, trigger the callback and then
  1801.                      * check for new data */
  1802.                     if( cl->url && cl->url->catch[i_msg].cb )
  1803.                     {
  1804.                         cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
  1805.                                                   cl, NULL, query );
  1806.                     }
  1807.                     cl->i_state = HTTPD_CLIENT_WAITING;
  1808.                 }
  1809.                 else if( i_msg == HTTPD_MSG_CHANNEL )
  1810.                 {
  1811.                     /* We are in BIDIR mode, trigger the callback and then
  1812.                      * check for new data */
  1813.                     if( cl->url && cl->url->catch[i_msg].cb )
  1814.                     {
  1815.                         cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys,
  1816.                                                   cl, NULL, query );
  1817.                     }
  1818.                     cl->i_state = HTTPD_CLIENT_WAITING;
  1819.                 }
  1820.                 else if( i_msg == HTTPD_MSG_OPTIONS )
  1821.                 {
  1822.                     answer->i_type   = HTTPD_MSG_ANSWER;
  1823.                     answer->i_proto  = query->i_proto;
  1824.                     answer->i_status = 200;
  1825.                     answer->i_body = 0;
  1826.                     answer->p_body = NULL;
  1827.                     httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
  1828.                     httpd_MsgAdd( answer, "Content-Length", "0" );
  1829.                     switch( query->i_proto )
  1830.                     {
  1831.                         case HTTPD_PROTO_HTTP:
  1832.                             answer->i_version = 1;
  1833.                             httpd_MsgAdd( answer, "Allow",
  1834.                                           "GET,HEAD,POST,OPTIONS" );
  1835.                             break;
  1836.                         case HTTPD_PROTO_RTSP:
  1837.                         {
  1838.                             const char *p;
  1839.                             answer->i_version = 0;
  1840.                             p = httpd_MsgGet( query, "Cseq" );
  1841.                             if( p != NULL )
  1842.                                 httpd_MsgAdd( answer, "Cseq", "%s", p );
  1843.                             p = httpd_MsgGet( query, "Timestamp" );
  1844.                             if( p != NULL )
  1845.                                 httpd_MsgAdd( answer, "Timestamp", "%s", p );
  1846.                             p = httpd_MsgGet( query, "Require" );
  1847.                             if( p != NULL )
  1848.                             {
  1849.                                 answer->i_status = 551;
  1850.                                 httpd_MsgAdd( query, "Unsupported", "%s", p );
  1851.                             }
  1852.                             httpd_MsgAdd( answer, "Public", "DESCRIBE,SETUP,"
  1853.                                           "TEARDOWN,PLAY,PAUSE,GET_PARAMETER" );
  1854.                             break;
  1855.                         }
  1856.                     }
  1857.                     cl->i_buffer = -1;  /* Force the creation of the answer in
  1858.                                          * httpd_ClientSend */
  1859.                     cl->i_state = HTTPD_CLIENT_SENDING;
  1860.                 }
  1861.                 else if( i_msg == HTTPD_MSG_NONE )
  1862.                 {
  1863.                     if( query->i_proto == HTTPD_PROTO_NONE )
  1864.                     {
  1865.                         cl->url = NULL;
  1866.                         cl->i_state = HTTPD_CLIENT_DEAD;
  1867.                     }
  1868.                     else
  1869.                     {
  1870.                         char *p;
  1871.                         /* unimplemented */
  1872.                         answer->i_proto  = query->i_proto ;
  1873.                         answer->i_type   = HTTPD_MSG_ANSWER;
  1874.                         answer->i_version= 0;
  1875.                         answer->i_status = 501;
  1876.                         answer->i_body = httpd_HtmlError (&p, 501, NULL);
  1877.                         answer->p_body = (uint8_t *)p;
  1878.                         httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
  1879.                         cl->i_buffer = -1;  /* Force the creation of the answer in httpd_ClientSend */
  1880.                         cl->i_state = HTTPD_CLIENT_SENDING;
  1881.                     }
  1882.                 }
  1883.                 else
  1884.                 {
  1885.                     bool b_auth_failed = false;
  1886.                     bool b_hosts_failed = false;
  1887.                     /* Search the url and trigger callbacks */
  1888.                     for(int i = 0; i < host->i_url; i++ )
  1889.                     {
  1890.                         httpd_url_t *url = host->url[i];
  1891.                         if( !strcmp( url->psz_url, query->psz_url ) )
  1892.                         {
  1893.                             if( url->catch[i_msg].cb )
  1894.                             {
  1895.                                 if( answer && ( url->p_acl != NULL ) )
  1896.                                 {
  1897.                                     char ip[NI_MAXNUMERICHOST];
  1898.                                     if( ( httpd_ClientIP( cl, ip ) == NULL )
  1899.                                      || ACL_Check( url->p_acl, ip ) )
  1900.                                     {
  1901.                                         b_hosts_failed = true;
  1902.                                         break;
  1903.                                     }
  1904.                                 }
  1905.                                 if( answer && ( *url->psz_user || *url->psz_password ) )
  1906.                                 {
  1907.                                     /* create the headers */
  1908.                                     const char *b64 = httpd_MsgGet( query, "Authorization" ); /* BASIC id */
  1909.                                     char *user = NULL, *pass = NULL;
  1910.                                     if( b64 != NULL
  1911.                                      && !strncasecmp( b64, "BASIC", 5 ) )
  1912.                                     {
  1913.                                         b64 += 5;
  1914.                                         while( *b64 == ' ' )
  1915.                                             b64++;
  1916.                                         user = vlc_b64_decode( b64 );
  1917.                                         if (user != NULL)
  1918.                                         {
  1919.                                             pass = strchr (user, ':');
  1920.                                             if (pass != NULL)
  1921.                                                 *pass++ = '';
  1922.                                         }
  1923.                                     }
  1924.                                     if ((user == NULL) || (pass == NULL)
  1925.                                      || strcmp (user, url->psz_user)
  1926.                                      || strcmp (pass, url->psz_password))
  1927.                                     {
  1928.                                         httpd_MsgAdd( answer,
  1929.                                                       "WWW-Authenticate",
  1930.                                                       "Basic realm="VLC stream"" );
  1931.                                         /* We fail for all url */
  1932.                                         b_auth_failed = true;
  1933.                                         free( user );
  1934.                                         break;
  1935.                                     }
  1936.                                     free( user );
  1937.                                 }
  1938.                                 if( !url->catch[i_msg].cb( url->catch[i_msg].p_sys, cl, answer, query ) )
  1939.                                 {
  1940.                                     if( answer->i_proto == HTTPD_PROTO_NONE )
  1941.                                     {
  1942.                                         /* Raw answer from a CGI */
  1943.                                         cl->i_buffer = cl->i_buffer_size;
  1944.                                     }
  1945.                                     else
  1946.                                         cl->i_buffer = -1;
  1947.                                     /* only one url can answer */
  1948.                                     answer = NULL;
  1949.                                     if( cl->url == NULL )
  1950.                                     {
  1951.                                         cl->url = url;
  1952.                                     }
  1953.                                 }
  1954.                             }
  1955.                         }
  1956.                     }
  1957.                     if( answer )
  1958.                     {
  1959.                         char *p;
  1960.                         answer->i_proto  = query->i_proto;
  1961.                         answer->i_type   = HTTPD_MSG_ANSWER;
  1962.                         answer->i_version= 0;
  1963.                         if( b_hosts_failed )
  1964.                         {
  1965.                             answer->i_status = 403;
  1966.                         }
  1967.                         else if( b_auth_failed )
  1968.                         {
  1969.                             answer->i_status = 401;
  1970.                         }
  1971.                         else
  1972.                         {
  1973.                             /* no url registered */
  1974.                             answer->i_status = 404;
  1975.                         }
  1976.                         answer->i_body = httpd_HtmlError (&p,
  1977.                                                           answer->i_status,
  1978.                                                           query->psz_url);
  1979.                         answer->p_body = (uint8_t *)p;
  1980.                         cl->i_buffer = -1;  /* Force the creation of the answer in httpd_ClientSend */
  1981.                         httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
  1982.                         httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
  1983.                     }
  1984.                     cl->i_state = HTTPD_CLIENT_SENDING;
  1985.                 }
  1986.             }
  1987.             else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
  1988.             {
  1989.                 if( cl->i_mode == HTTPD_CLIENT_FILE || cl->answer.i_body_offset == 0 )
  1990.                 {
  1991.                     const char *psz_connection = httpd_MsgGet( &cl->answer, "Connection" );
  1992.                     const char *psz_query = httpd_MsgGet( &cl->query, "Connection" );
  1993.                     bool b_connection = false;
  1994.                     bool b_keepalive = false;
  1995.                     bool b_query = false;
  1996.                     cl->url = NULL;
  1997.                     if( psz_connection )
  1998.                     {
  1999.                         b_connection = ( strcasecmp( psz_connection, "Close" ) == 0 );
  2000.                         b_keepalive = ( strcasecmp( psz_connection, "Keep-Alive" ) == 0 );
  2001.                     }
  2002.                     if( psz_query )
  2003.                     {
  2004.                         b_query = ( strcasecmp( psz_query, "Close" ) == 0 );
  2005.                     }
  2006.                     if( ( ( cl->query.i_proto == HTTPD_PROTO_HTTP ) &&
  2007.                           ( ( cl->query.i_version == 0 && b_keepalive ) ||
  2008.                             ( cl->query.i_version == 1 && !b_connection ) ) ) ||
  2009.                         ( ( cl->query.i_proto == HTTPD_PROTO_RTSP ) &&
  2010.                           !b_query && !b_connection ) )
  2011.                     {
  2012.                         httpd_MsgClean( &cl->query );
  2013.                         httpd_MsgInit( &cl->query );
  2014.                         cl->i_buffer = 0;
  2015.                         cl->i_buffer_size = 1000;
  2016.                         free( cl->p_buffer );
  2017.                         cl->p_buffer = malloc( cl->i_buffer_size );
  2018.                         cl->i_state = HTTPD_CLIENT_RECEIVING;
  2019.                     }
  2020.                     else
  2021.                     {
  2022.                         cl->i_state = HTTPD_CLIENT_DEAD;
  2023.                     }
  2024.                     httpd_MsgClean( &cl->answer );
  2025.                 }
  2026.                 else if( cl->b_read_waiting )
  2027.                 {
  2028.                     /* we have a message waiting for us to read it */
  2029.                     httpd_MsgClean( &cl->answer );
  2030.                     httpd_MsgClean( &cl->query );
  2031.                     cl->i_buffer = 0;
  2032.                     cl->i_buffer_size = 1000;
  2033.                     free( cl->p_buffer );
  2034.                     cl->p_buffer = malloc( cl->i_buffer_size );
  2035.                     cl->i_state = HTTPD_CLIENT_RECEIVING;
  2036.                     cl->b_read_waiting = false;
  2037.                 }
  2038.                 else
  2039.                 {
  2040.                     int64_t i_offset = cl->answer.i_body_offset;
  2041.                     httpd_MsgClean( &cl->answer );
  2042.                     cl->answer.i_body_offset = i_offset;
  2043.                     free( cl->p_buffer );
  2044.                     cl->p_buffer = NULL;
  2045.                     cl->i_buffer = 0;
  2046.                     cl->i_buffer_size = 0;
  2047.                     cl->i_state = HTTPD_CLIENT_WAITING;
  2048.                 }
  2049.             }
  2050.             else if( cl->i_state == HTTPD_CLIENT_WAITING )
  2051.             {
  2052.                 int64_t i_offset = cl->answer.i_body_offset;
  2053.                 int     i_msg = cl->query.i_type;
  2054.                 httpd_MsgInit( &cl->answer );
  2055.                 cl->answer.i_body_offset = i_offset;
  2056.                 cl->url->catch[i_msg].cb( cl->url->catch[i_msg].p_sys, cl,
  2057.                                           &cl->answer, &cl->query );
  2058.                 if( cl->answer.i_type != HTTPD_MSG_NONE )
  2059.                 {
  2060.                     /* we have new data, so re-enter send mode */
  2061.                     cl->i_buffer      = 0;
  2062.                     cl->p_buffer      = cl->answer.p_body;
  2063.                     cl->i_buffer_size = cl->answer.i_body;
  2064.                     cl->answer.p_body = NULL;
  2065.                     cl->answer.i_body = 0;
  2066.                     cl->i_state = HTTPD_CLIENT_SENDING;
  2067.                 }
  2068.             }
  2069.             /* Special for BIDIR mode we also check reading */
  2070.             if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
  2071.                 cl->i_state == HTTPD_CLIENT_SENDING )
  2072.             {
  2073.                 pufd->events |= POLLIN;
  2074.             }
  2075.             if (pufd->events != 0)
  2076.                 nfd++;
  2077.             else
  2078.                 b_low_delay = true;
  2079.         }
  2080.         vlc_mutex_unlock( &host->lock );
  2081.         ufd[nfd].fd = evfd;
  2082.         ufd[nfd].events = POLLIN;
  2083.         ufd[nfd].revents = 0;
  2084.         nfd++;
  2085.         /* we will wait 20ms (not too big) if HTTPD_CLIENT_WAITING */
  2086.         switch( poll( ufd, nfd, b_low_delay ? 20 : -1) )
  2087.         {
  2088.             case -1:
  2089.                 if (errno != EINTR)
  2090.                 {
  2091.                     /* Kernel on low memory or a bug: pace */
  2092.                     msg_Err( host, "polling error: %m" );
  2093.                     msleep( 100000 );
  2094.                 }
  2095.             case 0:
  2096.                 continue;
  2097.         }
  2098.         if( ufd[nfd - 1].revents )
  2099.             break;
  2100.         /* Handle client sockets */
  2101.         vlc_mutex_lock( &host->lock );
  2102.         now = mdate();
  2103.         nfd = host->nfd;
  2104.         for( int i_client = 0; i_client < host->i_client; i_client++ )
  2105.         {
  2106.             httpd_client_t *cl = host->client[i_client];
  2107.             const struct pollfd *pufd = &ufd[nfd];
  2108.             assert( pufd < &ufd[sizeof(ufd) / sizeof(ufd[0])] );
  2109.             if( cl->fd != pufd->fd )
  2110.                 continue; // we were not waiting for this client
  2111.             ++nfd;
  2112.             if( pufd->revents == 0 )
  2113.                 continue; // no event received
  2114.             cl->i_activity_date = now;
  2115.             if( cl->i_state == HTTPD_CLIENT_RECEIVING )
  2116.             {
  2117.                 httpd_ClientRecv( cl );
  2118.             }
  2119.             else if( cl->i_state == HTTPD_CLIENT_SENDING )
  2120.             {
  2121.                 httpd_ClientSend( cl );
  2122.             }
  2123.             else if( cl->i_state == HTTPD_CLIENT_TLS_HS_IN )
  2124.             {
  2125.                 httpd_ClientTlsHsIn( cl );
  2126.             }
  2127.             else if( cl->i_state == HTTPD_CLIENT_TLS_HS_OUT )
  2128.             {
  2129.                 httpd_ClientTlsHsOut( cl );
  2130.             }
  2131.             if( cl->i_mode == HTTPD_CLIENT_BIDIR &&
  2132.                 cl->i_state == HTTPD_CLIENT_SENDING &&
  2133.                 (pufd->revents & POLLIN) )
  2134.             {
  2135.                 cl->b_read_waiting = true;
  2136.             }
  2137.         }
  2138.         vlc_mutex_unlock( &host->lock );
  2139.         /* Handle server sockets (accept new connections) */
  2140.         for( nfd = 0; nfd < host->nfd; nfd++ )
  2141.         {
  2142.             httpd_client_t *cl;
  2143.             int i_state = -1;
  2144.             int fd = ufd[nfd].fd;
  2145.             assert (fd == host->fds[nfd]);
  2146.             if( ufd[nfd].revents == 0 )
  2147.                 continue;
  2148.             /* */
  2149.             fd = accept (fd, NULL, NULL);
  2150.             if (fd == -1)
  2151.                 continue;
  2152.             net_SetupSocket (fd);
  2153.             if( p_tls != NULL )
  2154.             {
  2155.                 switch( tls_ServerSessionHandshake( p_tls, fd ) )
  2156.                 {
  2157.                     case -1:
  2158.                         msg_Err( host, "Rejecting TLS connection" );
  2159.                         net_Close( fd );
  2160.                         fd = -1;
  2161.                         p_tls = NULL;
  2162.                         break;
  2163.                     case 1: /* missing input - most likely */
  2164.                         i_state = HTTPD_CLIENT_TLS_HS_IN;
  2165.                         break;
  2166.                     case 2: /* missing output */
  2167.                         i_state = HTTPD_CLIENT_TLS_HS_OUT;
  2168.                         break;
  2169.                 }
  2170.                 if( (p_tls == NULL) != (host->p_tls == NULL) )
  2171.                     break; // wasted TLS session, cannot accept() anymore
  2172.             }
  2173.             stats_UpdateInteger( host, p_total_counter, 1, NULL );
  2174.             stats_UpdateInteger( host, p_active_counter, 1, NULL );
  2175.             cl = httpd_ClientNew( fd, p_tls, now );
  2176.             p_tls = NULL;
  2177.             vlc_mutex_lock( &host->lock );
  2178.             TAB_APPEND( host->i_client, host->client, cl );
  2179.             vlc_mutex_unlock( &host->lock );
  2180.             if( i_state != -1 )
  2181.                 cl->i_state = i_state; // override state for TLS
  2182.             if (host->p_tls != NULL)
  2183.                 break; // cannot accept further without new TLS session
  2184.         }
  2185.     }
  2186.     if( p_tls != NULL )
  2187.         tls_ServerSessionClose( p_tls );
  2188.     if( p_total_counter )
  2189.         stats_CounterClean( p_total_counter );
  2190.     if( p_active_counter )
  2191.         stats_CounterClean( p_active_counter );
  2192.     return NULL;
  2193. }