1/**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "lua.h"
19#include "lauxlib.h"
20#include "lualib.h"
21
22#include "httpd.h"
23
24#include "apr_thread_rwlock.h"
25#include "apr_strings.h"
26#include "apr_tables.h"
27#include "apr_hash.h"
28#include "apr_buckets.h"
29#include "apr_file_info.h"
30#include "apr_time.h"
31#include "apr_pools.h"
32#include "apr_reslist.h"
33
34
35#ifndef VMPREP_H
36#define VMPREP_H
37
38#define AP_LUA_SCOPE_UNSET         0
39#define AP_LUA_SCOPE_ONCE          1
40#define AP_LUA_SCOPE_REQUEST       2
41#define AP_LUA_SCOPE_CONN          3
42#define AP_LUA_SCOPE_THREAD        4
43#define AP_LUA_SCOPE_SERVER        5
44
45#define AP_LUA_CACHE_UNSET         0
46#define AP_LUA_CACHE_NEVER         1
47#define AP_LUA_CACHE_STAT          2
48#define AP_LUA_CACHE_FOREVER       3
49
50#define AP_LUA_FILTER_INPUT        1
51#define AP_LUA_FILTER_OUTPUT       2
52
53typedef void (*ap_lua_state_open_callback) (lua_State *L, apr_pool_t *p,
54                                             void *ctx);
55/**
56 * Specification for a lua virtual machine
57 */
58typedef struct
59{
60    /* NEED TO ADD ADDITIONAL PACKAGE PATHS AS PART OF SPEC INSTEAD OF DIR CONFIG */
61    apr_array_header_t *package_paths;
62    apr_array_header_t *package_cpaths;
63
64    /* name of base file to load in the vm */
65    const char *file;
66
67    /* APL_SCOPE_ONCE | APL_SCOPE_REQUEST | APL_SCOPE_CONN | APL_SCOPE_THREAD | APL_SCOPE_SERVER */
68    int scope;
69    unsigned int vm_min;
70    unsigned int vm_max;
71
72    ap_lua_state_open_callback cb;
73    void* cb_arg;
74
75    /* pool to use for lifecycle if APL_SCOPE_ONCE is set, otherwise unused */
76    apr_pool_t *pool;
77
78    /* Pre-compiled Lua Byte code to load directly.  If bytecode_len is >0,
79     * the file part of this structure is ignored for loading purposes, but
80     * it is used for error messages.
81     */
82    const char *bytecode;
83    apr_size_t bytecode_len;
84
85    int codecache;
86} ap_lua_vm_spec;
87
88typedef struct
89{
90    const char *function_name;
91    const char *file_name;
92    int scope;
93    ap_regex_t *uri_pattern;
94    const char *bytecode;
95    apr_size_t bytecode_len;
96    int codecache;
97} ap_lua_mapped_handler_spec;
98
99typedef struct
100{
101    const char *function_name;
102    const char *file_name;
103    const char* filter_name;
104    int         direction; /* AP_LUA_FILTER_INPUT | AP_LUA_FILTER_OUTPUT */
105} ap_lua_filter_handler_spec;
106
107typedef struct {
108    apr_size_t runs;
109    apr_time_t modified;
110    apr_off_t  size;
111} ap_lua_finfo;
112
113typedef struct {
114    lua_State* L;
115    ap_lua_finfo* finfo;
116} ap_lua_server_spec;
117
118/**
119 * Fake out addition of the "apache2" module
120 */
121void ap_lua_load_apache2_lmodule(lua_State *L);
122
123/*
124 * alternate means of getting lua_State (preferred eventually)
125 * Obtain a lua_State which has loaded file and is associated with lifecycle_pool
126 * If one exists, will return extant one, otherwise will create, attach, and return
127 * This does no locking around the lua_State, so if the pool is shared between
128 * threads, locking is up the client.
129 *
130 * @lifecycle_pool -> pool whose lifeycle controls the lua_State
131 * @file file to be opened, also used as a key for uniquing lua_States
132 * @cb callback for vm initialization called *before* the file is opened
133 * @ctx a baton passed to cb
134 */
135lua_State *ap_lua_get_lua_state(apr_pool_t *lifecycle_pool,
136                                                ap_lua_vm_spec *spec, request_rec* r);
137
138#if APR_HAS_THREADS || defined(DOXYGEN)
139/*
140 * Initialize mod_lua mutex.
141 * @pool pool for mutex
142 * @s server_rec for logging
143 */
144void ap_lua_init_mutex(apr_pool_t *pool, server_rec *s);
145#endif
146
147#endif
148