ngx_conf_file.c

来自「Nginx是一个高性能的HTTP和反向代理服务器」· C语言 代码 · 共 1,335 行 · 第 1/2 页

C
1,335
字号
    for ( ;; ) {        n = ngx_read_glob(&gl, &name);        if (n != NGX_OK) {            break;        }        file.len = name.len++;        file.data = ngx_pstrdup(cf->pool, &name);        ngx_log_debug1(NGX_LOG_DEBUG_CORE, cf->log, 0, "include %s", file.data);        rv = ngx_conf_parse(cf, &file);        if (rv != NGX_CONF_OK) {            break;        }    }    ngx_close_glob(&gl);    return rv;}ngx_int_tngx_conf_full_name(ngx_cycle_t *cycle, ngx_str_t *name, ngx_uint_t conf_prefix){    size_t      len;    u_char     *p, *prefix;    ngx_str_t   old;    if (name->data[0] == '/') {        return NGX_OK;    }#if (NGX_WIN32)    if (name->len > 2        && name->data[1] == ':'        && ((name->data[0] >= 'a' && name->data[0] <= 'z')             || (name->data[0] >= 'A' && name->data[0] <= 'Z')))    {        return NGX_OK;    }#endif    old = *name;    if (conf_prefix) {        len = sizeof(NGX_CONF_PREFIX) - 1;        prefix = (u_char *) NGX_CONF_PREFIX;    } else {        len = cycle->root.len;        prefix = cycle->root.data;    }    name->len = len + old.len;    name->data = ngx_palloc(cycle->pool, name->len + 1);    if (name->data == NULL) {        return  NGX_ERROR;    }    p = ngx_cpymem(name->data, prefix, len);    ngx_cpystrn(p, old.data, old.len + 1);    return NGX_OK;}ngx_open_file_t *ngx_conf_open_file(ngx_cycle_t *cycle, ngx_str_t *name){    ngx_str_t         full;    ngx_uint_t        i;    ngx_list_part_t  *part;    ngx_open_file_t  *file;#if (NGX_SUPPRESS_WARN)    full.len = 0;    full.data = NULL;#endif    if (name) {        full = *name;        if (ngx_conf_full_name(cycle, &full, 0) == NGX_ERROR) {            return NULL;        }        part = &cycle->open_files.part;        file = part->elts;        for (i = 0; /* void */ ; i++) {            if (i >= part->nelts) {                if (part->next == NULL) {                    break;                }                part = part->next;                file = part->elts;                i = 0;            }            if (full.len != file[i].name.len) {                continue;            }            if (ngx_strcmp(full.data, file[i].name.data) == 0) {                return &file[i];            }        }    }    file = ngx_list_push(&cycle->open_files);    if (file == NULL) {        return NULL;    }    if (name) {        file->fd = NGX_INVALID_FILE;        file->name = full;    } else {        file->fd = ngx_stderr_fileno;        file->name.len = 0;        file->name.data = NULL;    }    file->buffer = NULL;    return file;}static voidngx_conf_flush_files(ngx_cycle_t *cycle){    ngx_uint_t        i;    ngx_list_part_t  *part;    ngx_open_file_t  *file;    ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0, "flush files");    part = &cycle->open_files.part;    file = part->elts;    for (i = 0; /* void */ ; i++) {        if (i >= part->nelts) {            if (part->next == NULL) {                break;            }            part = part->next;            file = part->elts;            i = 0;        }        if (file[i].buffer == NULL || file[i].pos - file[i].buffer == 0) {            continue;        }        ngx_write_fd(file[i].fd, file[i].buffer, file[i].pos - file[i].buffer);    }}void ngx_cdeclngx_conf_log_error(ngx_uint_t level, ngx_conf_t *cf, ngx_err_t err,    char *fmt, ...){    u_char   errstr[NGX_MAX_CONF_ERRSTR], *buf, *last;    va_list  args;    last = errstr + NGX_MAX_CONF_ERRSTR;    va_start(args, fmt);    buf = ngx_vsnprintf(errstr, last - errstr, fmt, args);    va_end(args);    *buf = '\0';    if (err) {        buf = ngx_snprintf(buf, last - buf - 1, " (%d: ", err);        buf = ngx_strerror_r(err, buf, last - buf - 1);        *buf++ = ')';        *buf = '\0';    }    if (cf->conf_file == NULL) {        ngx_log_error(level, cf->log, 0, "%s", errstr);        return;    }    ngx_log_error(level, cf->log, 0, "%s in %s:%ui",                  errstr, cf->conf_file->file.name.data, cf->conf_file->line);}char *ngx_conf_set_flag_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    ngx_str_t        *value;    ngx_flag_t       *fp;    ngx_conf_post_t  *post;    fp = (ngx_flag_t *) (p + cmd->offset);    if (*fp != NGX_CONF_UNSET) {        return "is duplicate";    }    value = cf->args->elts;    if (ngx_strcasecmp(value[1].data, (u_char *) "on") == 0) {        *fp = 1;    } else if (ngx_strcasecmp(value[1].data, (u_char *) "off") == 0) {        *fp = 0;    } else {        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                     "invalid value \"%s\" in \"%s\" directive, "                     "it must be \"on\" or \"off\"",                     value[1].data, cmd->name.data);        return NGX_CONF_ERROR;    }    if (cmd->post) {        post = cmd->post;        return post->post_handler(cf, post, fp);    }    return NGX_CONF_OK;}char *ngx_conf_set_str_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    ngx_str_t        *field, *value;    ngx_conf_post_t  *post;    field = (ngx_str_t *) (p + cmd->offset);    if (field->data) {        return "is duplicate";    }    value = cf->args->elts;    *field = value[1];    if (cmd->post) {        post = cmd->post;        return post->post_handler(cf, post, field);    }    return NGX_CONF_OK;}char *ngx_conf_set_str_array_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    ngx_str_t         *value, *s;    ngx_array_t      **a;    ngx_conf_post_t   *post;    a = (ngx_array_t **) (p + cmd->offset);    if (*a == NGX_CONF_UNSET_PTR) {        *a = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));        if (*a == NULL) {            return NGX_CONF_ERROR;        }    }    s = ngx_array_push(*a);    if (s == NULL) {        return NGX_CONF_ERROR;    }    value = cf->args->elts;    *s = value[1];    if (cmd->post) {        post = cmd->post;        return post->post_handler(cf, post, s);    }    return NGX_CONF_OK;}char *ngx_conf_set_keyval_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    ngx_str_t         *value;    ngx_array_t      **a;    ngx_keyval_t      *kv;    ngx_conf_post_t   *post;    a = (ngx_array_t **) (p + cmd->offset);    if (*a == NULL) {        *a = ngx_array_create(cf->pool, 4, sizeof(ngx_keyval_t));        if (*a == NULL) {            return NGX_CONF_ERROR;        }    }    kv = ngx_array_push(*a);    if (kv == NULL) {        return NGX_CONF_ERROR;    }    value = cf->args->elts;    kv->key = value[1];    kv->value = value[2];    if (cmd->post) {        post = cmd->post;        return post->post_handler(cf, post, kv);    }    return NGX_CONF_OK;}char *ngx_conf_set_num_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    ngx_int_t        *np;    ngx_str_t        *value;    ngx_conf_post_t  *post;    np = (ngx_int_t *) (p + cmd->offset);    if (*np != NGX_CONF_UNSET) {        return "is duplicate";    }    value = cf->args->elts;    *np = ngx_atoi(value[1].data, value[1].len);    if (*np == NGX_ERROR) {        return "invalid number";    }    if (cmd->post) {        post = cmd->post;        return post->post_handler(cf, post, np);    }    return NGX_CONF_OK;}char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    size_t           *sp;    ngx_str_t        *value;    ngx_conf_post_t  *post;    sp = (size_t *) (p + cmd->offset);    if (*sp != NGX_CONF_UNSET_SIZE) {        return "is duplicate";    }    value = cf->args->elts;    *sp = ngx_parse_size(&value[1]);    if (*sp == (size_t) NGX_ERROR) {        return "invalid value";    }    if (cmd->post) {        post = cmd->post;        return post->post_handler(cf, post, sp);    }    return NGX_CONF_OK;}char *ngx_conf_set_off_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    off_t            *op;    ngx_str_t        *value;    ngx_conf_post_t  *post;    op = (off_t *) (p + cmd->offset);    if (*op != NGX_CONF_UNSET) {        return "is duplicate";    }    value = cf->args->elts;    *op = ngx_parse_offset(&value[1]);    if (*op == (off_t) NGX_ERROR) {        return "invalid value";    }    if (cmd->post) {        post = cmd->post;        return post->post_handler(cf, post, op);    }    return NGX_CONF_OK;}char *ngx_conf_set_msec_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    ngx_msec_t       *msp;    ngx_str_t        *value;    ngx_conf_post_t  *post;    msp = (ngx_msec_t *) (p + cmd->offset);    if (*msp != NGX_CONF_UNSET_MSEC) {        return "is duplicate";    }    value = cf->args->elts;    *msp = ngx_parse_time(&value[1], 0);    if (*msp == (ngx_msec_t) NGX_ERROR) {        return "invalid value";    }    if (*msp == (ngx_msec_t) NGX_PARSE_LARGE_TIME) {        return "value must be less than 597 hours";    }    if (cmd->post) {        post = cmd->post;        return post->post_handler(cf, post, msp);    }    return NGX_CONF_OK;}char *ngx_conf_set_sec_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    time_t           *sp;    ngx_str_t        *value;    ngx_conf_post_t  *post;    sp = (time_t *) (p + cmd->offset);    if (*sp != NGX_CONF_UNSET) {        return "is duplicate";    }    value = cf->args->elts;    *sp = ngx_parse_time(&value[1], 1);    if (*sp == NGX_ERROR) {        return "invalid value";    }    if (*sp == NGX_PARSE_LARGE_TIME) {        return "value must be less than 68 years";    }    if (cmd->post) {        post = cmd->post;        return post->post_handler(cf, post, sp);    }    return NGX_CONF_OK;}char *ngx_conf_set_bufs_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char *p = conf;    ngx_str_t   *value;    ngx_bufs_t  *bufs;    bufs = (ngx_bufs_t *) (p + cmd->offset);    if (bufs->num) {        return "is duplicate";    }    value = cf->args->elts;    bufs->num = ngx_atoi(value[1].data, value[1].len);    if (bufs->num == NGX_ERROR || bufs->num == 0) {        return "invalid value";    }    bufs->size = ngx_parse_size(&value[2]);    if (bufs->size == (size_t) NGX_ERROR || bufs->size == 0) {        return "invalid value";    }    return NGX_CONF_OK;}char *ngx_conf_set_enum_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    ngx_uint_t       *np, i;    ngx_str_t        *value;    ngx_conf_enum_t  *e;    np = (ngx_uint_t *) (p + cmd->offset);    if (*np != NGX_CONF_UNSET_UINT) {        return "is duplicate";    }    value = cf->args->elts;    e = cmd->post;    for (i = 0; e[i].name.len != 0; i++) {        if (e[i].name.len != value[1].len            || ngx_strcasecmp(e[i].name.data, value[1].data) != 0)        {            continue;        }        *np = e[i].value;        return NGX_CONF_OK;    }    ngx_conf_log_error(NGX_LOG_WARN, cf, 0,                       "invalid value \"%s\"", value[1].data);    return NGX_CONF_ERROR;}char *ngx_conf_set_bitmask_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    ngx_uint_t          *np, i, m;    ngx_str_t           *value;    ngx_conf_bitmask_t  *mask;    np = (ngx_uint_t *) (p + cmd->offset);    value = cf->args->elts;    mask = cmd->post;    for (i = 1; i < cf->args->nelts; i++) {        for (m = 0; mask[m].name.len != 0; m++) {            if (mask[m].name.len != value[i].len                || ngx_strcasecmp(mask[m].name.data, value[i].data) != 0)            {                continue;            }            if (*np & mask[m].mask) {                ngx_conf_log_error(NGX_LOG_WARN, cf, 0,                                   "duplicate value \"%s\"", value[i].data);            } else {                *np |= mask[m].mask;            }            break;        }        if (mask[m].name.len == 0) {            ngx_conf_log_error(NGX_LOG_WARN, cf, 0,                               "invalid value \"%s\"", value[i].data);            return NGX_CONF_ERROR;        }    }    return NGX_CONF_OK;}char *ngx_conf_unsupported(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    return "unsupported on this platform";}char *ngx_conf_deprecated(ngx_conf_t *cf, void *post, void *data){    ngx_conf_deprecated_t  *d = post;    ngx_conf_log_error(NGX_LOG_WARN, cf, 0,                       "the \"%s\" directive is deprecated, "                       "use the \"%s\" directive instead",                       d->old_name, d->new_name);    return NGX_CONF_OK;}char *ngx_conf_check_num_bounds(ngx_conf_t *cf, void *post, void *data){    ngx_conf_num_bounds_t  *bounds = post;    ngx_int_t  *np = data;    if (bounds->high == -1) {        if (*np >= bounds->low) {            return NGX_CONF_OK;        }        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                           "value must be equal or more than %i", bounds->low);        return NGX_CONF_ERROR;    }    if (*np >= bounds->low && *np <= bounds->high) {        return NGX_CONF_OK;    }    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                       "value must be between %i and %i",                       bounds->low, bounds->high);    return NGX_CONF_ERROR;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?