2002-09-11 23:18:33 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
|
2002-12-19 15:08:55 +08:00
|
|
|
#include <ngx_core.h>
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-19 15:08:55 +08:00
|
|
|
#include <ngx_config_file.h>
|
2002-12-03 00:09:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
static int argument_number[] = {
|
|
|
|
NGX_CONF_NOARGS,
|
|
|
|
NGX_CONF_TAKE1,
|
2002-12-19 15:08:55 +08:00
|
|
|
NGX_CONF_TAKE2
|
2002-12-03 00:09:40 +08:00
|
|
|
};
|
|
|
|
|
2002-12-26 15:24:21 +08:00
|
|
|
static int ngx_conf_read_token(ngx_conf_t *cf);
|
|
|
|
static ngx_command_t *ngx_conf_find_token(ngx_conf_t *cf,
|
|
|
|
ngx_http_module_t **modules);
|
|
|
|
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
int ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
|
2002-12-03 00:09:40 +08:00
|
|
|
{
|
2002-12-26 15:24:21 +08:00
|
|
|
int rc;
|
|
|
|
char *error;
|
|
|
|
ngx_fd_t fd;
|
2002-12-19 15:08:55 +08:00
|
|
|
ngx_conf_file_t *prev;
|
2002-12-26 15:24:21 +08:00
|
|
|
ngx_command_t *cmd;
|
2002-12-03 00:09:40 +08:00
|
|
|
|
|
|
|
if (filename) {
|
2002-12-19 15:08:55 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
fd = ngx_open_file(filename->data, NGX_FILE_RDONLY);
|
2002-12-19 15:08:55 +08:00
|
|
|
if (fd == NGX_INVALID_FILE) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
|
2002-12-23 14:29:22 +08:00
|
|
|
"ngx_conf_file: "
|
|
|
|
ngx_open_file_n " %s failed", filename->data);
|
2002-12-19 15:08:55 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
prev = cf->conf_file;
|
|
|
|
ngx_test_null(cf->conf_file,
|
|
|
|
ngx_palloc(cf->pool, sizeof(ngx_conf_file_t)),
|
2002-12-19 15:08:55 +08:00
|
|
|
NGX_ERROR);
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
if (ngx_stat_fd(fd, &cf->conf_file->file.info) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
|
|
|
|
"ngx_conf_file: "
|
|
|
|
ngx_stat_fd_n " %s failed", filename->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_test_null(cf->conf_file->hunk,
|
|
|
|
ngx_create_temp_hunk(cf->pool, 1024, 0, 0),
|
|
|
|
NGX_ERROR);
|
|
|
|
|
|
|
|
cf->conf_file->file.fd = fd;
|
|
|
|
cf->conf_file->file.name.len = filename->len;
|
|
|
|
cf->conf_file->file.name.data = filename->data;
|
|
|
|
cf->conf_file->file.log = cf->log;;
|
|
|
|
cf->conf_file->line = 1;
|
2002-12-03 00:09:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
rc = ngx_conf_read_token(cf);
|
|
|
|
|
2002-12-26 15:24:21 +08:00
|
|
|
/* NGX_OK, NGX_ERROR, NGX_CONF_FILE_DONE, NGX_CONF_BLOCK_DONE */
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-26 15:24:21 +08:00
|
|
|
if (rc == NGX_ERROR || rc == NGX_CONF_FILE_DONE) {
|
2002-12-03 00:09:40 +08:00
|
|
|
return rc;
|
2002-12-19 15:08:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cf->handler) {
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
if ((*cf->handler)(cf) == NGX_ERROR) {
|
2002-12-19 15:08:55 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-26 15:24:21 +08:00
|
|
|
cmd = ngx_conf_find_token(cf);
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
#if 0
|
2002-12-03 00:09:40 +08:00
|
|
|
cmd = ngx_conf_find_token(cf);
|
|
|
|
if (cmd == NULL) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"unknown directive \"%s\" in %s:%d",
|
|
|
|
cf->name, cf->file->name, cf->file->line);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
if (cmd->type & argument_number[cf->args->nelts - 1]) {
|
2002-12-19 15:08:55 +08:00
|
|
|
error = cmd->set(cf, cmd->offset, cf->args);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"%s in directive \"%s\" in %s:%d",
|
|
|
|
error, cf->name, cf->file->name, cf->file->line);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2002-12-23 14:29:22 +08:00
|
|
|
#endif
|
2002-12-19 15:08:55 +08:00
|
|
|
|
|
|
|
#if 0
|
2002-12-03 00:09:40 +08:00
|
|
|
if (cmd->type == NGX_CONF_CONTAINER) {
|
|
|
|
ngx_conf_parse(cf, cmd->container, NULL);
|
|
|
|
|
|
|
|
} else if (cmd->type == NGX_CONF_FLAG) {
|
|
|
|
|
|
|
|
if (cf->args->nelts != 1) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"invalid number of arguments "
|
|
|
|
"in directive \"%s\" in %s:%d",
|
|
|
|
cf->name, cf->file->name, cf->file->line);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strcasecmp(cf->args->elts[0], "on") == 0) {
|
|
|
|
flag = 1;
|
|
|
|
|
|
|
|
} else if (ngx_strcasecmp(cf->args->elts[0], "off") == 0) {
|
|
|
|
flag = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"invalid flag in directive \"%s\" in %s:%d",
|
|
|
|
cf->name, cf->file->name, cf->file->line);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = cmd->set(cf, cmd->offset, flag);
|
|
|
|
if (rv) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"%s in directive \"%s\" in %s:%d",
|
|
|
|
rv, cf->name, cf->file->name, cf->file->line);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (cmd->type & argument_number[args->nelts]) {
|
|
|
|
rv = cmd->set(cf, cmd->offset, cf->args);
|
|
|
|
if (rv) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"%s in directive \"%s\" in %s:%d",
|
|
|
|
rv, cf->name, cf->file->name, cf->file->line);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"invalid number of arguments "
|
|
|
|
"in directive \"%s\" in %s:%d",
|
|
|
|
cf->name, cf->file->name, cf->file->line);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2002-12-19 15:08:55 +08:00
|
|
|
#endif
|
2002-12-23 14:29:22 +08:00
|
|
|
}
|
2002-12-03 00:09:40 +08:00
|
|
|
|
|
|
|
if (filename) {
|
2002-12-23 14:29:22 +08:00
|
|
|
cf->conf_file = prev;
|
2002-12-19 15:08:55 +08:00
|
|
|
|
|
|
|
if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ERR, cf->log, ngx_errno,
|
2002-12-23 14:29:22 +08:00
|
|
|
ngx_close_file_n " %s failed",
|
|
|
|
cf->conf_file->file.name.data);
|
2002-12-19 15:08:55 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2002-12-03 00:09:40 +08:00
|
|
|
}
|
2002-12-19 15:08:55 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
2002-12-23 14:29:22 +08:00
|
|
|
}
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-19 15:08:55 +08:00
|
|
|
|
2002-12-26 15:24:21 +08:00
|
|
|
static int ngx_conf_read_token(ngx_conf_t *cf)
|
2002-12-03 00:09:40 +08:00
|
|
|
{
|
2002-12-19 15:08:55 +08:00
|
|
|
char *start, ch, *src, *dst;
|
2002-12-26 15:24:21 +08:00
|
|
|
int len;
|
|
|
|
int found, need_space, last_space, sharp_comment;
|
|
|
|
int quoted, s_quoted, d_quoted;
|
2002-12-23 14:29:22 +08:00
|
|
|
ssize_t n;
|
|
|
|
ngx_str_t *word;
|
2002-12-19 15:08:55 +08:00
|
|
|
ngx_hunk_t *h;
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
found = 0;
|
2002-12-03 00:09:40 +08:00
|
|
|
need_space = 0;
|
|
|
|
last_space = 1;
|
2002-12-26 15:24:21 +08:00
|
|
|
sharp_comment = 0;
|
2002-12-03 00:09:40 +08:00
|
|
|
quoted = s_quoted = d_quoted = 0;
|
|
|
|
|
|
|
|
cf->args->nelts = 0;
|
2002-12-19 15:08:55 +08:00
|
|
|
h = cf->conf_file->hunk;
|
2002-12-24 02:22:18 +08:00
|
|
|
start = h->pos.mem;
|
2002-12-19 15:08:55 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
ngx_log_debug(cf->log, "TOKEN START");
|
|
|
|
|
2002-12-24 02:22:18 +08:00
|
|
|
for ( ;; ) {
|
2002-12-19 15:08:55 +08:00
|
|
|
|
|
|
|
if (h->pos.mem >= h->last.mem) {
|
2002-12-23 14:29:22 +08:00
|
|
|
if (cf->conf_file->file.offset
|
|
|
|
>= ngx_file_size(cf->conf_file->file.info)) {
|
2002-12-26 15:24:21 +08:00
|
|
|
return NGX_CONF_FILE_DONE;
|
2002-12-23 14:29:22 +08:00
|
|
|
}
|
|
|
|
|
2002-12-19 15:08:55 +08:00
|
|
|
if (h->pos.mem - start) {
|
|
|
|
ngx_memcpy(h->start, start, h->pos.mem - start);
|
|
|
|
}
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
n = ngx_read_file(&cf->conf_file->file,
|
2002-12-19 15:08:55 +08:00
|
|
|
h->start + (h->pos.mem - start),
|
|
|
|
h->end - (h->start + (h->pos.mem - start)),
|
|
|
|
cf->conf_file->file.offset);
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-19 15:08:55 +08:00
|
|
|
if (n == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-19 15:08:55 +08:00
|
|
|
h->pos.mem = h->start + (h->pos.mem - start);
|
|
|
|
start = h->start;
|
|
|
|
h->last.mem = h->pos.mem + n;
|
|
|
|
}
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
ch = *h->pos.mem++;
|
|
|
|
|
2002-12-24 02:22:18 +08:00
|
|
|
#if 0
|
2002-12-23 14:29:22 +08:00
|
|
|
ngx_log_debug(cf->log, "%d:%d:%d:%d:%d '%c'" _
|
|
|
|
last_space _ need_space _
|
|
|
|
quoted _ s_quoted _ d_quoted _ ch);
|
2002-12-24 02:22:18 +08:00
|
|
|
#endif
|
2002-12-19 15:08:55 +08:00
|
|
|
|
|
|
|
if (ch == LF) {
|
|
|
|
cf->conf_file->line++;
|
2002-12-26 15:24:21 +08:00
|
|
|
|
|
|
|
if (sharp_comment) {
|
|
|
|
sharp_comment = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sharp_comment) {
|
|
|
|
continue;
|
2002-12-19 15:08:55 +08:00
|
|
|
}
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-24 02:22:18 +08:00
|
|
|
if (quoted) {
|
|
|
|
quoted = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
if (need_space) {
|
|
|
|
if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
|
|
|
|
last_space = 1;
|
|
|
|
need_space = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == ';' || ch == '{') {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-12-19 15:08:55 +08:00
|
|
|
if (last_space) {
|
|
|
|
if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
start = h->pos.mem - 1;
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
switch (ch) {
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-24 02:22:18 +08:00
|
|
|
case ';':
|
|
|
|
case '{':
|
2002-12-26 15:24:21 +08:00
|
|
|
if (cf->args->nelts == 0) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"unexpected '%c' in %s:%d",
|
|
|
|
ch, cf->conf_file->file.name.data,
|
|
|
|
cf->conf_file->line);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-12-24 02:22:18 +08:00
|
|
|
return NGX_OK;
|
|
|
|
|
2002-12-26 15:24:21 +08:00
|
|
|
case '}':
|
|
|
|
if (cf->args->nelts > 0) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"unexpected '}' in %s:%d",
|
|
|
|
cf->conf_file->file.name.data,
|
|
|
|
cf->conf_file->line);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONF_BLOCK_DONE;
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
sharp_comment = 1;
|
|
|
|
continue;
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
case '\\':
|
|
|
|
quoted = 1;
|
|
|
|
last_space = 0;
|
2002-12-03 00:09:40 +08:00
|
|
|
continue;
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
case '"':
|
|
|
|
start++;
|
|
|
|
d_quoted = 1;
|
|
|
|
last_space = 0;
|
|
|
|
continue;
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
case '\'':
|
|
|
|
start++;
|
|
|
|
s_quoted = 1;
|
|
|
|
last_space = 0;
|
2002-12-03 00:09:40 +08:00
|
|
|
continue;
|
2002-12-23 14:29:22 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
last_space = 0;
|
2002-12-03 00:09:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2002-12-23 14:29:22 +08:00
|
|
|
if (ch == '\\') {
|
|
|
|
quoted = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
if (d_quoted) {
|
|
|
|
if (ch == '"') {
|
|
|
|
d_quoted = 0;
|
|
|
|
need_space = 1;
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (s_quoted) {
|
|
|
|
if (ch == '\'') {
|
|
|
|
s_quoted = 0;
|
|
|
|
need_space = 1;
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (ch == ' ' || ch == '\t' || ch == CR || ch == LF
|
|
|
|
|| ch == ';' || ch == '{') {
|
|
|
|
last_space = 1;
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
2002-12-19 15:08:55 +08:00
|
|
|
ngx_test_null(word, ngx_push_array(cf->args), NGX_ERROR);
|
2002-12-24 02:22:18 +08:00
|
|
|
ngx_test_null(word->data,
|
|
|
|
ngx_palloc(cf->pool, h->pos.mem - start + 1),
|
2002-12-03 00:09:40 +08:00
|
|
|
NGX_ERROR);
|
|
|
|
|
2002-12-24 02:22:18 +08:00
|
|
|
for (dst = word->data, src = start, len = 0;
|
2002-12-23 14:29:22 +08:00
|
|
|
src < h->pos.mem - 1;
|
2002-12-24 02:22:18 +08:00
|
|
|
len++)
|
2002-12-23 14:29:22 +08:00
|
|
|
{
|
|
|
|
if (*src == '\\') {
|
2002-12-03 00:09:40 +08:00
|
|
|
src++;
|
2002-12-23 14:29:22 +08:00
|
|
|
}
|
2002-12-03 00:09:40 +08:00
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
2002-12-24 02:22:18 +08:00
|
|
|
word->len = len;
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
ngx_log_debug(cf->log, "FOUND %d:'%s'" _ word->len _ word->data);
|
2002-12-03 00:09:40 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
if (ch == ';' || ch == '{') {
|
|
|
|
return NGX_OK;
|
2002-12-03 00:09:40 +08:00
|
|
|
}
|
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
found = 0;
|
2002-12-03 00:09:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-26 15:24:21 +08:00
|
|
|
static ngx_command_t *ngx_conf_find_token(ngx_conf_t *cf)
|
2002-09-11 23:18:33 +08:00
|
|
|
{
|
2002-12-26 15:24:21 +08:00
|
|
|
int i;
|
|
|
|
ngx_command_t *cmd;
|
2002-09-11 23:18:33 +08:00
|
|
|
|
2002-12-26 15:24:21 +08:00
|
|
|
for (i = 0; cf->modules[i]; i++) {
|
|
|
|
cmd = cf->modules[i]->commands;
|
|
|
|
if (cmd == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (cmd->name) {
|
|
|
|
|
|
|
|
ngx_log_debug(cf->log, "command '%s'" _ cmd->name);
|
|
|
|
|
|
|
|
cmd++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *ngx_conf_set_size_slot(ngx_conf_t *cf, char *conf)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
ngx_str_t *value;
|
|
|
|
|
|
|
|
value = (ngx_str_t *) cf->args->elts;
|
|
|
|
|
|
|
|
size = atoi(value.data);
|
|
|
|
if (size < 0) {
|
2002-09-11 23:18:33 +08:00
|
|
|
return "value must be greater or equal to zero";
|
2002-12-26 15:24:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*(int *) (conf + cf->offset) = size;
|
2002-09-11 23:18:33 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-09-16 23:01:44 +08:00
|
|
|
|
2002-12-26 15:24:21 +08:00
|
|
|
|
|
|
|
char *ngx_conf_set_time_slot(ngx_conf_t *cf, char *conf)
|
2002-09-16 23:01:44 +08:00
|
|
|
{
|
2002-12-26 15:24:21 +08:00
|
|
|
int size;
|
|
|
|
ngx_str_t *value;
|
|
|
|
|
|
|
|
value = (ngx_str_t *) cf->args->elts;
|
2002-09-16 23:01:44 +08:00
|
|
|
|
2002-12-26 15:24:21 +08:00
|
|
|
size = atoi(value.data);
|
|
|
|
if (size < 0) {
|
2002-09-16 23:01:44 +08:00
|
|
|
return "value must be greater or equal to zero";
|
2002-12-26 15:24:21 +08:00
|
|
|
}
|
2002-09-16 23:01:44 +08:00
|
|
|
|
|
|
|
*(int *) (conf + offset) = size;
|
2002-12-26 15:24:21 +08:00
|
|
|
|
2002-09-16 23:01:44 +08:00
|
|
|
return NULL;
|
|
|
|
}
|