1 
2 
3 /* NOTE : you will find other conf_set functions in the following files :
4  *
5  * complex_value.c
6  * encoding.c
7  * path.c
8  *
9  */
10 
11 
12 char *
ndk_conf_set_true_slot(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)13 ndk_conf_set_true_slot (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
14 {
15     char  *p = conf;
16 
17     ngx_flag_t       *fp;
18     ngx_conf_post_t  *post;
19 
20     fp = (ngx_flag_t*) (p + cmd->offset);
21 
22     if (*fp != NGX_CONF_UNSET) {
23         return  "is duplicate";
24     }
25 
26     *fp = 1;
27 
28     if (cmd->post) {
29         post = cmd->post;
30         return  post->post_handler (cf, post, fp);
31     }
32 
33     return  NGX_CONF_OK;
34 }
35 
36 
37 
38 char *
ndk_conf_set_false_slot(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)39 ndk_conf_set_false_slot (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
40 {
41     char  *p = conf;
42 
43     ngx_flag_t       *fp;
44     ngx_conf_post_t  *post;
45 
46     fp = (ngx_flag_t*) (p + cmd->offset);
47 
48     if (*fp != NGX_CONF_UNSET) {
49         return  "is duplicate";
50     }
51 
52     *fp = 0;
53 
54     if (cmd->post) {
55         post = cmd->post;
56         return  post->post_handler (cf, post, fp);
57     }
58 
59     return  NGX_CONF_OK;
60 }
61 
62 
63 
64 
65 char *
ndk_conf_set_ptr_slot(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)66 ndk_conf_set_ptr_slot (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
67 {
68     char  *p = conf;
69 
70     void  **ptr;
71 
72     ptr = (void**) (p + cmd->offset);
73 
74     if (*ptr != NGX_CONF_UNSET_PTR) {
75         return  "is duplicate";
76     }
77 
78     *ptr = cmd->post;
79 
80     return  NGX_CONF_OK;
81 }
82 
83 
84 
85 char *
ndk_conf_set_null_slot(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)86 ndk_conf_set_null_slot (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
87 {
88     char  *p = conf;
89 
90     void            **pp;
91     ngx_conf_post_t  *post;
92 
93     pp = (void **) (p + cmd->offset);
94 
95     if (*pp != NGX_CONF_UNSET_PTR) {
96         return  "is duplicate";
97     }
98 
99     *pp = NULL;
100 
101     if (cmd->post) {
102         post = cmd->post;
103         return  post->post_handler (cf, post, pp);
104     }
105 
106     return  NGX_CONF_OK;
107 }
108 
109 
110 char *
ndk_conf_set_num64_slot(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)111 ndk_conf_set_num64_slot (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
112 {
113     char  *p = conf;
114 
115     int64_t          *np;
116     ngx_str_t        *value;
117     ngx_conf_post_t  *post;
118 
119 
120     np = (int64_t *) (p + cmd->offset);
121 
122     if (*np != NGX_CONF_UNSET) {
123         return "is duplicate";
124     }
125 
126     value = cf->args->elts;
127     *np = ndk_atoi64 (value[1].data, value[1].len);
128     if (*np == NGX_ERROR) {
129         return "invalid number";
130     }
131 
132     if (cmd->post) {
133         post = cmd->post;
134         return post->post_handler(cf, post, np);
135     }
136 
137     return NGX_CONF_OK;
138 }
139 
140 
141 char *
ndk_conf_set_str_array_multi_slot(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)142 ndk_conf_set_str_array_multi_slot (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
143 {
144     char  *p = conf;
145 
146     ngx_str_t         *value, *s;
147     ngx_array_t      **a;
148     ngx_conf_post_t   *post;
149     ngx_uint_t         i;
150 
151     a = (ngx_array_t **) (p + cmd->offset);
152 
153     if (*a == NGX_CONF_UNSET_PTR) {
154         *a = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));
155         if (*a == NULL) {
156             return NGX_CONF_ERROR;
157         }
158     }
159 
160     s = NULL;
161 
162     for (i=cf->args->nelts-1; i; i--) {
163 
164         s = ngx_array_push(*a);
165         if (s == NULL) {
166             return NGX_CONF_ERROR;
167         }
168 
169         value = cf->args->elts;
170 
171         *s = value[i];
172     }
173 
174     if (cmd->post) {
175         post = cmd->post;
176         return  post->post_handler(cf, post, s);
177     }
178 
179     return NGX_CONF_OK;
180 }
181 
182 
183 
184 char *
ndk_conf_set_keyval1_slot(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)185 ndk_conf_set_keyval1_slot (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
186 {
187     char  *p = conf;
188 
189     ngx_str_t           *value;
190     ngx_keyval_t        *kv;
191     ngx_conf_post_t     *post;
192 
193     kv = (ngx_keyval_t *) (p + cmd->offset);
194 
195     if (kv->key.data)
196         return  "is duplicate";
197 
198     value = cf->args->elts;
199 
200     kv->key = value[1];
201     kv->value = value[2];
202 
203     if (cmd->post) {
204         post = cmd->post;
205         return  post->post_handler (cf, post, kv);
206     }
207 
208     return  NGX_CONF_OK;
209 }
210 
211 
212 
213 char *
ndk_conf_set_num_flag_slot(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)214 ndk_conf_set_num_flag_slot (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
215 {
216     char  *p = conf;
217 
218     ngx_int_t        *np;
219     ngx_str_t        *value;
220     ngx_conf_post_t  *post;
221 
222     np = (ngx_int_t *) (p + cmd->offset);
223 
224     if (*np != NGX_CONF_UNSET) {
225         return  "is duplicate";
226     }
227 
228     value = cf->args->elts;
229 
230     if (ngx_strcasecmp (value[1].data, (u_char *) "on") == 0) {
231         *np = NDK_CONF_SET_TRUE;
232 
233     } else if (ngx_strcasecmp (value[1].data, (u_char *) "off") == 0) {
234         *np = NDK_CONF_SET_FALSE;
235 
236     } else {
237         *np = ngx_atoi (value[1].data, value[1].len);
238         if (*np == NGX_ERROR) {
239             return  "invalid number and not 'on'/'off'";
240         }
241     }
242 
243     if (cmd->post) {
244         post = cmd->post;
245         return  post->post_handler (cf, post, np);
246     }
247 
248     return  NGX_CONF_OK;
249 }
250 
251 
252 
253 char *
ndk_conf_set_sec_flag_slot(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)254 ndk_conf_set_sec_flag_slot (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
255 {
256     char  *p = conf;
257 
258     time_t              *tp;
259     ngx_str_t           *value;
260     ngx_conf_post_t     *post;
261 
262     tp = (time_t *) (p + cmd->offset);
263 
264     if (*tp != NGX_CONF_UNSET) {
265         return  "is duplicate";
266     }
267 
268     value = cf->args->elts;
269 
270     if (ngx_strcasecmp (value[1].data, (u_char *) "on") == 0) {
271         *tp = NDK_CONF_SET_TRUE;
272 
273     } else if (ngx_strcasecmp (value[1].data, (u_char *) "off") == 0) {
274         *tp = NDK_CONF_SET_FALSE;
275 
276     } else {
277         *tp = ngx_parse_time (&value[1], 1);
278         if (*tp == NGX_ERROR) {
279             return  "has an invalid time and not 'on'/'off'";
280         }
281     }
282 
283     if (cmd->post) {
284         post = cmd->post;
285         return  post->post_handler (cf, post, tp);
286     }
287 
288     return  NGX_CONF_OK;
289 }
290 
291 
292 
293 ngx_http_conf_ctx_t *
ndk_conf_create_http_location(ngx_conf_t * cf)294 ndk_conf_create_http_location (ngx_conf_t *cf)
295 {
296     ngx_http_conf_ctx_t          *ctx, *pctx;
297     void                         *mconf;
298     ngx_http_core_loc_conf_t     *clcf, *pclcf;
299     ngx_uint_t                    i;
300     ngx_http_module_t            *module;
301 
302     ndk_pcallocp_rce (ctx, cf->pool);
303 
304     pctx = cf->ctx;
305     ctx->main_conf = pctx->main_conf;
306     ctx->srv_conf = pctx->srv_conf;
307 
308     ndk_pcalloc_rce (ctx->loc_conf, cf->pool, sizeof(void *) * ngx_http_max_module);
309 
310 
311     for (i = 0; ngx_modules[i]; i++) {
312         if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
313             continue;
314         }
315 
316         module = ngx_modules[i]->ctx;
317 
318         if (module->create_loc_conf) {
319 
320             mconf = module->create_loc_conf(cf);
321             if (mconf == NULL) {
322                  return NGX_CONF_ERROR;
323             }
324 
325             ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
326         }
327     }
328 
329     pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
330 
331     clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
332     clcf->loc_conf = ctx->loc_conf;
333     clcf->name = pclcf->name;
334     clcf->noname = 1;
335 
336     if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
337         return NGX_CONF_ERROR;
338     }
339 
340     return  ctx;
341 }
342 
343 
344 ngx_http_conf_ctx_t *
ngx_conf_create_http_named_location(ngx_conf_t * cf,ngx_str_t * name)345 ngx_conf_create_http_named_location (ngx_conf_t *cf, ngx_str_t *name)
346 {
347     ngx_http_conf_ctx_t          *ctx;
348     ngx_http_core_loc_conf_t     *clcf;
349 
350     ctx = ndk_conf_create_http_location (cf);
351     if (ctx == NGX_CONF_ERROR)
352         return  NGX_CONF_ERROR;
353 
354     clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
355 
356     /* in case the developer forgets to add '@' at the beginning of the named location */
357 
358     if (name->data[0] != '@' && ndk_catstrf (cf->pool, name, "sS", "@", name) == NULL)
359         return  NGX_CONF_ERROR;
360 
361     clcf->name = *name;     /* TODO : copy? */
362     clcf->noname = 0;
363     clcf->named = 1;
364 
365     return  ctx;
366 }
367 
368 
369 ngx_int_t
ndk_replace_command(ngx_command_t * new_cmd,ngx_uint_t module_type)370 ndk_replace_command (ngx_command_t *new_cmd, ngx_uint_t module_type)
371 {
372     ngx_uint_t       i;
373     ngx_command_t   *cmd;
374 
375     for (i = 0; ngx_modules[i]; i++) {
376 
377         if (ngx_modules[i]->type != module_type)
378             continue;
379 
380         cmd = ngx_modules[i]->commands;
381         if (cmd == NULL) {
382             continue;
383         }
384 
385         for ( /* void */ ; cmd->name.len; cmd++) {
386 
387             if (ndk_cmpstr (&new_cmd->name, &cmd->name) == 0) {
388 
389                 ndk_memcpyp (cmd, new_cmd);
390                 return  NGX_OK;
391             }
392         }
393     }
394 
395     return  NGX_DECLINED;
396 }
397