66
77
88static ngx_int_t ngx_http_wasm_init (ngx_conf_t * cf );
9+ static void * ngx_http_wasm_create_main_conf (ngx_conf_t * cf );
10+ static char * ngx_http_wasm_init_main_conf (ngx_conf_t * cf , void * conf );
11+ static char * ngx_http_wasm_vm (ngx_conf_t * cf , ngx_command_t * cmd , void * conf );
912
1013
1114static bool ngx_http_wasm_vm_inited = false;
@@ -18,6 +21,11 @@ static ngx_str_t proxy_on_done = ngx_string("proxy_on_done");
1821static ngx_str_t proxy_on_delete = ngx_string ("proxy_on_delete" );
1922
2023
24+ typedef struct {
25+ ngx_str_t vm ;
26+ } ngx_http_wasm_main_conf_t ;
27+
28+
2129typedef struct {
2230 void * plugin ;
2331 uint32_t cur_ctx_id ;
@@ -36,12 +44,24 @@ typedef struct {
3644} ngx_http_wasm_plugin_ctx_t ;
3745
3846
47+ static ngx_command_t ngx_http_wasm_cmds [] = {
48+ { ngx_string ("wasm_vm" ),
49+ NGX_HTTP_MAIN_CONF |NGX_CONF_TAKE1 ,
50+ ngx_http_wasm_vm ,
51+ NGX_HTTP_MAIN_CONF_OFFSET ,
52+ 0 ,
53+ NULL },
54+
55+ ngx_null_command
56+ };
57+
58+
3959static ngx_http_module_t ngx_http_wasm_module_ctx = {
4060 NULL , /* preconfiguration */
4161 ngx_http_wasm_init , /* postconfiguration */
4262
43- NULL , /* create main configuration */
44- NULL , /* init main configuration */
63+ ngx_http_wasm_create_main_conf , /* create main configuration */
64+ ngx_http_wasm_init_main_conf , /* init main configuration */
4565
4666 NULL , /* create server configuration */
4767 NULL , /* merge server configuration */
@@ -54,7 +74,7 @@ static ngx_http_module_t ngx_http_wasm_module_ctx = {
5474ngx_module_t ngx_http_wasm_module = {
5575 NGX_MODULE_V1 ,
5676 & ngx_http_wasm_module_ctx , /* module context */
57- NULL , /* module directives */
77+ ngx_http_wasm_cmds , /* module directives */
5878 NGX_HTTP_MODULE , /* module type */
5979 NULL , /* init master */
6080 NULL , /* init module */
@@ -67,16 +87,71 @@ ngx_module_t ngx_http_wasm_module = {
6787};
6888
6989
90+ static void *
91+ ngx_http_wasm_create_main_conf (ngx_conf_t * cf )
92+ {
93+ ngx_http_wasm_main_conf_t * wmcf ;
94+
95+ wmcf = ngx_pcalloc (cf -> pool , sizeof (ngx_http_wasm_main_conf_t ));
96+ if (wmcf == NULL ) {
97+ return NULL ;
98+ }
99+
100+ /* set by ngx_pcalloc:
101+ * wmcf->vm = { 0, NULL };
102+ */
103+
104+ return wmcf ;
105+ }
106+
107+
108+ static char *
109+ ngx_http_wasm_init_main_conf (ngx_conf_t * cf , void * conf )
110+ {
111+ return NGX_CONF_OK ;
112+ }
113+
114+
115+ static char *
116+ ngx_http_wasm_vm (ngx_conf_t * cf , ngx_command_t * cmd , void * conf )
117+ {
118+ ngx_http_wasm_main_conf_t * wmcf = conf ;
119+ ngx_str_t * value ;
120+
121+ if (wmcf -> vm .len != 0 ) {
122+ return "is duplicate" ;
123+ }
124+
125+ value = cf -> args -> elts ;
126+
127+ if (ngx_strcmp (value [1 ].data , "wasmtime" ) != 0 ) {
128+ ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "unsupported wasm vm %V" , & value [1 ]);
129+ return NGX_CONF_ERROR ;
130+ }
131+
132+ wmcf -> vm .len = value [1 ].len ;
133+ wmcf -> vm .data = value [1 ].data ;
134+
135+ return NGX_CONF_OK ;
136+ }
137+
138+
70139static ngx_int_t
71140ngx_http_wasm_init (ngx_conf_t * cf )
72141{
73142 ngx_int_t rc ;
74143 ngx_pool_cleanup_t * cln ;
144+ ngx_http_wasm_main_conf_t * wmcf ;
75145
76146 if (ngx_process == NGX_PROCESS_SIGNALLER || ngx_test_config || ngx_http_wasm_vm_inited ) {
77147 return NGX_OK ;
78148 }
79149
150+ wmcf = ngx_http_conf_get_module_main_conf (cf , ngx_http_wasm_module );
151+ if (wmcf -> vm .len == 0 ) {
152+ return NGX_OK ;
153+ }
154+
80155 cln = ngx_pool_cleanup_add (cf -> pool , 0 );
81156 if (cln == NULL ) {
82157 return NGX_ERROR ;
@@ -105,6 +180,11 @@ ngx_http_wasm_load_plugin(const char *bytecode, size_t size)
105180 ngx_int_t rc ;
106181 ngx_http_wasm_plugin_t * hw_plugin ;
107182
183+ if (!ngx_http_wasm_vm_inited ) {
184+ ngx_log_error (NGX_LOG_ERR , ngx_cycle -> log , 0 , "miss wasm_vm configuration" );
185+ return NULL ;
186+ }
187+
108188 plugin = ngx_wasm_vm .load (bytecode , size );
109189
110190 if (plugin == NULL ) {
@@ -217,6 +297,11 @@ ngx_http_wasm_on_configure(ngx_http_wasm_plugin_t *hw_plugin, const char *conf,
217297
218298 log = ngx_cycle -> log ;
219299
300+ if (!ngx_http_wasm_vm_inited ) {
301+ ngx_log_error (NGX_LOG_ERR , log , 0 , "miss wasm_vm configuration" );
302+ return NULL ;
303+ }
304+
220305 if (!ngx_queue_empty (& hw_plugin -> free )) {
221306 ngx_queue_t * q ;
222307
0 commit comments