httpd.c 13.1 KB
Newer Older
1
2
3
/*****************************************************************************
 * httpd.c: HTTPd wrapper
 *****************************************************************************
4
 * Copyright (C) 2007-2008 the VideoLAN team
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea at videolan tod org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifndef  _GNU_SOURCE
#   define  _GNU_SOURCE
#endif

31
32
33
34
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

35
#include <vlc_common.h>
36
37
38
39
40
41
#include <vlc_httpd.h>

#include <lua.h>        /* Low level lua C API */
#include <lauxlib.h>    /* Higher level C API */
#include <lualib.h>     /* Lua libs */

42
43
#include "../vlc.h"
#include "../libs.h"
44
45
46
47
48
49

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static uint8_t *vlclua_todata( lua_State *L, int narg, int *i_data );

50
51
52
53
54
55
56
57
static int vlclua_httpd_host_delete( lua_State * );
static int vlclua_httpd_handler_new( lua_State * );
static int vlclua_httpd_handler_delete( lua_State * );
static int vlclua_httpd_file_new( lua_State * );
static int vlclua_httpd_file_delete( lua_State * );
static int vlclua_httpd_redirect_new( lua_State * );
static int vlclua_httpd_redirect_delete( lua_State * );

58
59
60
/*****************************************************************************
 * HTTPD Host
 *****************************************************************************/
61
62
63
64
65
66
static const luaL_Reg vlclua_httpd_reg[] = {
    { "handler", vlclua_httpd_handler_new },
    { "file", vlclua_httpd_file_new },
    { "redirect", vlclua_httpd_redirect_new },
    { NULL, NULL }
};
67

68
static int vlclua_httpd_tls_host_new( lua_State *L )
69
70
71
72
73
74
75
76
{
    vlc_object_t *p_this = vlclua_get_this( L );
    const char *psz_host = luaL_checkstring( L, 1 );
    int i_port = luaL_checkint( L, 2 );
    const char *psz_cert = luaL_optstring( L, 3, NULL );
    const char *psz_key = luaL_optstring( L, 4, NULL );
    const char *psz_ca = luaL_optstring( L, 5, NULL );
    const char *psz_crl = luaL_optstring( L, 6, NULL );
77
    httpd_host_t *p_host = httpd_TLSHostNew( p_this, psz_host, i_port,
78
79
                                                   psz_cert, psz_key,
                                                   psz_ca, psz_crl );
80
    if( !p_host )
81
82
83
84
        return luaL_error( L, "Failed to create HTTP TLS host \"%s:%d\" "
                           "(cert: \"%s\", key: \"%s\", ca: \"%s\", "
                           "crl: \"%s\").", psz_host, i_port,
                           psz_cert, psz_key, psz_ca, psz_crl );
85
86
87
88
89
90
91
92
93
94
95
96
97
98

    httpd_host_t **pp_host = lua_newuserdata( L, sizeof( httpd_host_t * ) );
    *pp_host = p_host;

    if( luaL_newmetatable( L, "httpd_host" ) )
    {
        lua_newtable( L );
        luaL_register( L, NULL, vlclua_httpd_reg );
        lua_setfield( L, -2, "__index" );
        lua_pushcfunction( L, vlclua_httpd_host_delete );
        lua_setfield( L, -2, "__gc" );
    }

    lua_setmetatable( L, -2 );
99
100
101
    return 1;
}

102
static int vlclua_httpd_host_delete( lua_State *L )
103
{
104
105
    httpd_host_t **pp_host = (httpd_host_t **)luaL_checkudata( L, 1, "httpd_host" );
    httpd_HostDelete( *pp_host );
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    return 0;
}

/*****************************************************************************
 * HTTPd Handler
 *****************************************************************************/
struct httpd_handler_sys_t
{
    lua_State *L;
    int ref;
};

static int vlclua_httpd_handler_callback(
     httpd_handler_sys_t *p_sys, httpd_handler_t *p_handler, char *psz_url,
     uint8_t *psz_request, int i_type, uint8_t *p_in, int i_in,
     char *psz_remote_addr, char *psz_remote_host,
     uint8_t **pp_data, int *pi_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
124
    VLC_UNUSED(p_handler);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    lua_State *L = p_sys->L;

    /* function data */
    lua_pushvalue( L, 1 );
    lua_pushvalue( L, 2 );
    /* function data function data */
    lua_pushstring( L, psz_url );
    /* function data function data url */
    lua_pushstring( L, (const char *)psz_request );
    /* function data function data url request */
    lua_pushinteger( L, i_type ); /* Q: what does i_type stand for? */
    /* function data function data url request type */
    lua_pushlstring( L, (const char *)p_in, i_in ); /* Q: what do p_in contain? */
    /* function data function data url request type in */
    lua_pushstring( L, psz_remote_addr );
    /* function data function data url request type in addr */
    lua_pushstring( L, psz_remote_host );
    /* function data function data url request type in addr host */
    if( lua_pcall( L, 7, 1, 0 ) )
    {
        /* function data err */
        vlc_object_t *p_this = vlclua_get_this( L );
        const char *psz_err = lua_tostring( L, -1 );
Pierre Ynard's avatar
Pierre Ynard committed
148
        msg_Err( p_this, "Error while running the lua HTTPd handler "
149
150
151
152
153
154
155
156
157
158
159
160
                 "callback: %s", psz_err );
        lua_settop( L, 2 );
        /* function data */
        return VLC_EGENERIC;
    }
    /* function data outdata */
    *pp_data = vlclua_todata( L, -1, pi_data );
    lua_pop( L, 1 );
    /* function data */
    return VLC_SUCCESS;
}

161
static int vlclua_httpd_handler_new( lua_State * L )
162
{
163
    httpd_host_t **pp_host = (httpd_host_t **)luaL_checkudata( L, 1, "httpd_host" );
164
165
166
    const char *psz_url = luaL_checkstring( L, 2 );
    const char *psz_user = luaL_nilorcheckstring( L, 3 );
    const char *psz_password = luaL_nilorcheckstring( L, 4 );
167
    const vlc_acl_t **pp_acl = lua_isnil( L, 5 ) ? NULL : luaL_checkudata( L, 5, "acl" );
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    /* Stack item 6 is the callback function */
    luaL_argcheck( L, lua_isfunction( L, 6 ), 6, "Should be a function" );
    /* Stack item 7 is the callback data */
    lua_settop( L, 7 );
    httpd_handler_sys_t *p_sys = (httpd_handler_sys_t*)
                                 malloc( sizeof( httpd_handler_sys_t ) );
    if( !p_sys )
        return luaL_error( L, "Failed to allocate private buffer." );
    p_sys->L = lua_newthread( L );
    p_sys->ref = luaL_ref( L, LUA_REGISTRYINDEX ); /* pops the object too */
    /* use lua_xmove to move the lua callback function and data to
     * the callback's stack. */
    lua_xmove( L, p_sys->L, 2 );
    httpd_handler_t *p_handler = httpd_HandlerNew(
182
                            *pp_host, psz_url, psz_user, psz_password,
183
184
                            pp_acl?*pp_acl:NULL,
                            vlclua_httpd_handler_callback, p_sys );
185
    if( !p_handler )
186
187
    {
        free( p_sys );
188
        return luaL_error( L, "Failed to create HTTPd handler." );
189
190
191
192
193
194
195
196
197
198
199
200
    }

    httpd_handler_t **pp_handler = lua_newuserdata( L, sizeof( httpd_handler_t * ) );
    *pp_handler = p_handler;

    if( luaL_newmetatable( L, "httpd_handler" ) )
    {
        lua_pushcfunction( L, vlclua_httpd_handler_delete );
        lua_setfield( L, -2, "__gc" );
    }

    lua_setmetatable( L, -2 );
201
202
203
    return 1;
}

204
static int vlclua_httpd_handler_delete( lua_State *L )
205
{
206
207
    httpd_handler_t **pp_handler = (httpd_handler_t**)luaL_checkudata( L, 1, "httpd_handler" );
    httpd_handler_sys_t *p_sys = httpd_HandlerDelete( *pp_handler );
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    luaL_unref( p_sys->L, LUA_REGISTRYINDEX, p_sys->ref );
    free( p_sys );
    return 0;
}

/*****************************************************************************
 * HTTPd File
 *****************************************************************************/
struct httpd_file_sys_t
{
    lua_State *L;
    int ref;
};

static int vlclua_httpd_file_callback(
    httpd_file_sys_t *p_sys, httpd_file_t *p_file, uint8_t *psz_request,
    uint8_t **pp_data, int *pi_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
226
    VLC_UNUSED(p_file);
227
228
229
230
231
232
233
234
235
236
237
238
239
    lua_State *L = p_sys->L;

    /* function data */
    lua_pushvalue( L, 1 );
    lua_pushvalue( L, 2 );
    /* function data function data */
    lua_pushstring( L, (const char *)psz_request );
    /* function data function data request */
    if( lua_pcall( L, 2, 1, 0 ) )
    {
        /* function data err */
        vlc_object_t *p_this = vlclua_get_this( L );
        const char *psz_err = lua_tostring( L, -1 );
Pierre Ynard's avatar
Pierre Ynard committed
240
        msg_Err( p_this, "Error while running the lua HTTPd file callback: %s",
241
242
243
244
245
246
247
248
249
250
251
252
                 psz_err );
        lua_settop( L, 2 );
        /* function data */
        return VLC_EGENERIC;
    }
    /* function data outdata */
    *pp_data = vlclua_todata( L, -1, pi_data );
    lua_pop( L, 1 );
    /* function data */
    return VLC_SUCCESS;
}

253
static int vlclua_httpd_file_new( lua_State *L )
254
{
255
    httpd_host_t **pp_host = (httpd_host_t **)luaL_checkudata( L, 1, "httpd_host" );
256
257
258
259
    const char *psz_url = luaL_checkstring( L, 2 );
    const char *psz_mime = luaL_nilorcheckstring( L, 3 );
    const char *psz_user = luaL_nilorcheckstring( L, 4 );
    const char *psz_password = luaL_nilorcheckstring( L, 5 );
260
    const vlc_acl_t **pp_acl = lua_isnil( L, 6 ) ? NULL : luaL_checkudata( L, 6, "acl" );
261
262
263
264
265
266
267
268
269
270
    /* Stack item 7 is the callback function */
    luaL_argcheck( L, lua_isfunction( L, 7 ), 7, "Should be a function" );
    /* Stack item 8 is the callback data */
    httpd_file_sys_t *p_sys = (httpd_file_sys_t *)
                              malloc( sizeof( httpd_file_sys_t ) );
    if( !p_sys )
        return luaL_error( L, "Failed to allocate private buffer." );
    p_sys->L = lua_newthread( L );
    p_sys->ref = luaL_ref( L, LUA_REGISTRYINDEX ); /* pops the object too */
    lua_xmove( L, p_sys->L, 2 );
271
    httpd_file_t *p_file = httpd_FileNew( *pp_host, psz_url, psz_mime,
272
273
                                          psz_user, psz_password,
                                          pp_acl?*pp_acl:NULL,
274
275
                                          vlclua_httpd_file_callback, p_sys );
    if( !p_file )
276
277
    {
        free( p_sys );
278
        return luaL_error( L, "Failed to create HTTPd file." );
279
280
281
282
283
284
285
286
287
288
289
290
    }

    httpd_file_t **pp_file = lua_newuserdata( L, sizeof( httpd_file_t * ) );
    *pp_file = p_file;

    if( luaL_newmetatable( L, "httpd_file" ) )
    {
        lua_pushcfunction( L, vlclua_httpd_file_delete );
        lua_setfield( L, -2, "__gc" );
    }

    lua_setmetatable( L, -2 );
291
292
293
    return 1;
}

294
static int vlclua_httpd_file_delete( lua_State *L )
295
{
296
297
    httpd_file_t **pp_file = (httpd_file_t**)luaL_checkudata( L, 1, "httpd_file" );
    httpd_file_sys_t *p_sys = httpd_FileDelete( *pp_file );
298
299
300
301
302
303
304
305
    luaL_unref( p_sys->L, LUA_REGISTRYINDEX, p_sys->ref );
    free( p_sys );
    return 0;
}

/*****************************************************************************
 * HTTPd Redirect
 *****************************************************************************/
306
static int vlclua_httpd_redirect_new( lua_State *L )
307
{
308
    httpd_host_t **pp_host = (httpd_host_t **)luaL_checkudata( L, 1, "httpd_host" );
309
310
    const char *psz_url_dst = luaL_checkstring( L, 2 );
    const char *psz_url_src = luaL_checkstring( L, 3 );
311
    httpd_redirect_t *p_redirect = httpd_RedirectNew( *pp_host,
312
313
314
315
                                                      psz_url_dst,
                                                      psz_url_src );
    if( !p_redirect )
        return luaL_error( L, "Failed to create HTTPd redirect." );
316
317
318
319
320
321
322
323
324
325
326

    httpd_redirect_t **pp_redirect = lua_newuserdata( L, sizeof( httpd_redirect_t * ) );
    *pp_redirect = p_redirect;

    if( luaL_newmetatable( L, "httpd_redirect" ) )
    {
        lua_pushcfunction( L, vlclua_httpd_redirect_delete );
        lua_setfield( L, -2, "__gc" );
    }

    lua_setmetatable( L, -2 );
327
328
329
    return 1;
}

330
static int vlclua_httpd_redirect_delete( lua_State *L )
331
{
332
333
    httpd_redirect_t **pp_redirect = (httpd_redirect_t**)luaL_checkudata( L, 1, "httpd_redirect" );
    httpd_RedirectDelete( *pp_redirect );
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
    return 0;
}

/*****************************************************************************
 * Utils
 *****************************************************************************/
static uint8_t *vlclua_todata( lua_State *L, int narg, int *pi_data )
{
    size_t i_data;
    const char *psz_data = lua_tolstring( L, narg, &i_data );
    uint8_t *p_data = (uint8_t*)malloc( i_data * sizeof(uint8_t) );
    *pi_data = (int)i_data;
    if( !p_data )
    {
        luaL_error( L, "Error while allocating buffer." );
        return NULL; /* To please gcc even though luaL_error longjmp-ed out of here */
    }
    memcpy( p_data, psz_data, i_data );
    return p_data;
}
354
355
356
357
358
359
360
361
362

/*****************************************************************************
 *
 *****************************************************************************/
void luaopen_httpd( lua_State *L )
{
    lua_pushcfunction( L, vlclua_httpd_tls_host_new );
    lua_setfield( L, -2, "httpd" );
}