]> git.sesse.net Git - vlc/blob - modules/access/dvb/http.c
Add ranges to font sizes
[vlc] / modules / access / dvb / http.c
1 /*****************************************************************************
2  * http.c: HTTP interface
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * Authors: Christophe Massiot <massiot@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef ENABLE_HTTPD
32 #include <vlc_common.h>
33 #include <vlc_access.h>
34 #include <vlc_httpd.h>
35
36 #include <sys/types.h>
37
38 /* Include dvbpsi headers */
39 # include <dvbpsi/dvbpsi.h>
40 # include <dvbpsi/descriptor.h>
41 # include <dvbpsi/pat.h>
42 # include <dvbpsi/pmt.h>
43 # include <dvbpsi/dr.h>
44 # include <dvbpsi/psi.h>
45 # include <dvbpsi/demux.h>
46 # include <dvbpsi/sdt.h>
47
48 #include "dvb.h"
49
50 struct httpd_file_sys_t
51 {
52     access_t         *p_access;
53     httpd_file_t     *p_file;
54 };
55
56 static int HttpCallback( httpd_file_sys_t *p_args,
57                          httpd_file_t *p_file,
58                          uint8_t *_p_request,
59                          uint8_t **_pp_data, int *pi_data );
60
61 /*****************************************************************************
62  * HTTPOpen: Start the internal HTTP server
63  *****************************************************************************/
64 int HTTPOpen( access_t *p_access )
65 {
66     access_sys_t *p_sys = p_access->p_sys;
67     char          *psz_user = NULL, *psz_password = NULL;
68     char          psz_tmp[10];
69     httpd_file_sys_t *f;
70
71     vlc_mutex_init( &p_sys->httpd_mutex );
72     vlc_cond_init( &p_sys->httpd_cond );
73     p_sys->b_request_frontend_info = p_sys->b_request_mmi_info = false;
74     p_sys->i_httpd_timeout = 0;
75
76     p_sys->p_httpd_host = vlc_http_HostNew( VLC_OBJECT(p_access) );
77     if ( p_sys->p_httpd_host == NULL )
78     {
79         return VLC_EGENERIC;
80     }
81     free( psz_address );
82
83     psz_user = var_GetNonEmptyString( p_access, "dvb-http-user" );
84     psz_password = var_GetNonEmptyString( p_access, "dvb-http-password" );
85
86     /* Declare an index.html file. */
87     f = malloc( sizeof(httpd_file_sys_t) );
88     f->p_access = p_access;
89     f->p_file = httpd_FileNew( p_sys->p_httpd_host, "/index.html",
90                                "text/html; charset=UTF-8",
91                                psz_user, psz_password, NULL,
92                                HttpCallback, f );
93
94     free( psz_user );
95     free( psz_password );
96
97     if ( f->p_file == NULL )
98     {
99         free( f );
100         p_sys->p_httpd_file = NULL;
101         return VLC_EGENERIC;
102     }
103
104     p_sys->p_httpd_file = f;
105     p_sys->p_httpd_redir = httpd_RedirectNew( p_sys->p_httpd_host,
106                                               "/index.html", "/" );
107
108     return VLC_SUCCESS;
109 }
110
111 /*****************************************************************************
112  * HTTPClose: Stop the internal HTTP server
113  *****************************************************************************/
114 void HTTPClose( access_t *p_access )
115 {
116     access_sys_t *p_sys = p_access->p_sys;
117
118     if ( p_sys->p_httpd_host != NULL )
119     {
120         if ( p_sys->p_httpd_file != NULL )
121         {
122             /* Unlock the thread if it is stuck in HttpCallback */
123             vlc_mutex_lock( &p_sys->httpd_mutex );
124             if ( p_sys->b_request_frontend_info )
125             {
126                 p_sys->b_request_frontend_info = false;
127                 p_sys->psz_frontend_info = strdup("");
128             }
129             if ( p_sys->b_request_mmi_info )
130             {
131                 p_sys->b_request_mmi_info = false;
132                 p_sys->psz_mmi_info = strdup("");
133             }
134             vlc_cond_signal( &p_sys->httpd_cond );
135             vlc_mutex_unlock( &p_sys->httpd_mutex );
136
137             httpd_FileDelete( p_sys->p_httpd_file->p_file );
138             httpd_RedirectDelete( p_sys->p_httpd_redir );
139         }
140
141         httpd_HostDelete( p_sys->p_httpd_host );
142     }
143
144     vlc_mutex_destroy( &p_sys->httpd_mutex );
145     vlc_cond_destroy( &p_sys->httpd_cond );
146 }
147
148
149 static const char *psz_constant_header =
150     "<html>\n"
151     "<head><title>VLC DVB monitoring interface</title></head>\n"
152     "<body><a href=\"index.html\">Reload this page</a>\n"
153     "<h1>CAM info</h1>\n";
154
155 static const char *psz_constant_middle =
156     "<hr><h1>Frontend Info</h1>\n";
157
158 static const char *psz_constant_footer =
159     "</body></html>\n";
160
161 /****************************************************************************
162  * HttpCallback: Return the index.html file
163  ****************************************************************************/
164 static int HttpCallback( httpd_file_sys_t *p_args,
165                          httpd_file_t *p_file,
166                          uint8_t *_psz_request,
167                          uint8_t **_pp_data, int *pi_data )
168 {
169     VLC_UNUSED(p_file);
170     access_sys_t *p_sys = p_args->p_access->p_sys;
171     char *psz_request = (char *)_psz_request;
172     char **pp_data = (char **)_pp_data;
173
174     vlc_mutex_lock( &p_sys->httpd_mutex );
175
176     p_sys->i_httpd_timeout = mdate() + INT64_C(3000000); /* 3 s */
177     p_sys->psz_request = psz_request;
178     p_sys->b_request_frontend_info = true;
179     if ( p_sys->p_cam != NULL )
180     {
181         p_sys->b_request_mmi_info = true;
182     }
183     else
184     {
185         p_sys->psz_mmi_info = strdup( "No available CAM interface\n" );
186     }
187
188     do
189     {
190         vlc_cond_wait( &p_sys->httpd_cond, &p_sys->httpd_mutex );
191     }
192     while ( p_sys->b_request_frontend_info || p_sys->b_request_mmi_info );
193
194     p_sys->i_httpd_timeout = 0;
195     vlc_mutex_unlock( &p_sys->httpd_mutex );
196
197     *pi_data = strlen( psz_constant_header )
198                 + strlen( p_sys->psz_mmi_info )
199                 + strlen( psz_constant_middle )
200                 + strlen( p_sys->psz_frontend_info )
201                 + strlen( psz_constant_footer ) + 1;
202     *pp_data = malloc( *pi_data );
203
204     sprintf( *pp_data, "%s%s%s%s%s", psz_constant_header,
205              p_sys->psz_mmi_info, psz_constant_middle,
206              p_sys->psz_frontend_info, psz_constant_footer );
207     free( p_sys->psz_frontend_info );
208     free( p_sys->psz_mmi_info );
209
210     return VLC_SUCCESS;
211 }
212
213 /****************************************************************************
214  * HTTPExtractValue: Extract a GET variable from psz_request
215  ****************************************************************************/
216 const char *HTTPExtractValue( const char *psz_uri, const char *psz_name,
217                         char *psz_value, int i_value_max )
218 {
219     const char *p = psz_uri;
220
221     while( (p = strstr( p, psz_name )) )
222     {
223         /* Verify that we are dealing with a post/get argument */
224         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
225               && p[strlen(psz_name)] == '=' )
226             break;
227         p++;
228     }
229
230     if( p )
231     {
232         int i_len;
233
234         p += strlen( psz_name );
235         if( *p == '=' ) p++;
236
237         if( strchr( p, '&' ) )
238         {
239             i_len = strchr( p, '&' ) - p;
240         }
241         else
242         {
243             /* for POST method */
244             if( strchr( p, '\n' ) )
245             {
246                 i_len = strchr( p, '\n' ) - p;
247                 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
248             }
249             else
250             {
251                 i_len = strlen( p );
252             }
253         }
254         i_len = __MIN( i_value_max - 1, i_len );
255         if( i_len > 0 )
256         {
257             strncpy( psz_value, p, i_len );
258             psz_value[i_len] = '\0';
259         }
260         else
261         {
262             strncpy( psz_value, "", i_value_max );
263         }
264         p += i_len;
265     }
266     else
267     {
268         strncpy( psz_value, "", i_value_max );
269     }
270
271     return p;
272 }
273
274 #endif /* ENABLE_HTTPD */