⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ngx_http_variables.c

📁 Nginx是一个高性能的HTTP和反向代理服务器
💻 C
📖 第 1 页 / 共 3 页
字号:
    v->valid = 1;    v->no_cacheable = 0;    v->not_found = 0;    v->data = path.data;    return NGX_OK;}static ngx_int_tngx_http_variable_server_name(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    ngx_http_core_srv_conf_t  *cscf;    cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);    v->len = cscf->server_name.len;    v->valid = 1;    v->no_cacheable = 0;    v->not_found = 0;    v->data = cscf->server_name.data;    return NGX_OK;}static ngx_int_tngx_http_variable_request_method(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    if (r->main->method_name.data) {        v->len = r->main->method_name.len;        v->valid = 1;        v->no_cacheable = 0;        v->not_found = 0;        v->data = r->main->method_name.data;    } else {        v->not_found = 1;    }    return NGX_OK;}static ngx_int_tngx_http_variable_remote_user(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    ngx_int_t  rc;    rc = ngx_http_auth_basic_user(r);    if (rc == NGX_DECLINED) {        v->not_found = 1;        return NGX_OK;    }    if (rc == NGX_ERROR) {        return NGX_ERROR;    }    v->len = r->headers_in.user.len;    v->valid = 1;    v->no_cacheable = 0;    v->not_found = 0;    v->data = r->headers_in.user.data;    return NGX_OK;}static ngx_int_tngx_http_variable_body_bytes_sent(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    off_t    sent;    u_char  *p;    sent = r->connection->sent - r->header_size;    if (sent < 0) {        sent = 0;    }    p = ngx_palloc(r->pool, NGX_OFF_T_LEN);    if (p == NULL) {        return NGX_ERROR;    }    v->len = ngx_sprintf(p, "%O", sent) - p;    v->valid = 1;    v->no_cacheable = 0;    v->not_found = 0;    v->data = p;    return NGX_OK;}static ngx_int_tngx_http_variable_sent_content_type(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    if (r->headers_out.content_type.len) {        v->len = r->headers_out.content_type.len;        v->valid = 1;        v->no_cacheable = 0;        v->not_found = 0;        v->data = r->headers_out.content_type.data;    } else {        v->not_found = 1;    }    return NGX_OK;}static ngx_int_tngx_http_variable_sent_content_length(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    u_char  *p;    if (r->headers_out.content_length) {        v->len = r->headers_out.content_length->value.len;        v->valid = 1;        v->no_cacheable = 0;        v->not_found = 0;        v->data = r->headers_out.content_length->value.data;        return NGX_OK;    }    if (r->headers_out.content_length_n >= 0) {        p = ngx_palloc(r->pool, NGX_OFF_T_LEN);        if (p == NULL) {            return NGX_ERROR;        }        v->len = ngx_sprintf(p, "%O", r->headers_out.content_length_n) - p;        v->valid = 1;        v->no_cacheable = 0;        v->not_found = 0;        v->data = p;        return NGX_OK;    }    v->not_found = 1;    return NGX_OK;}static ngx_int_tngx_http_variable_sent_last_modified(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    u_char  *p;    if (r->headers_out.last_modified) {        v->len = r->headers_out.last_modified->value.len;        v->valid = 1;        v->no_cacheable = 0;        v->not_found = 0;        v->data = r->headers_out.last_modified->value.data;        return NGX_OK;    }    if (r->headers_out.last_modified_time >= 0) {        p = ngx_palloc(r->pool,                   sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT") - 1);        if (p == NULL) {            return NGX_ERROR;        }        v->len = ngx_http_time(p, r->headers_out.last_modified_time) - p;        v->valid = 1;        v->no_cacheable = 0;        v->not_found = 0;        v->data = p;        return NGX_OK;    }    v->not_found = 1;    return NGX_OK;}static ngx_int_tngx_http_variable_sent_connection(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    size_t   len;    char    *p;    if (r->keepalive) {        len = sizeof("keep-alive") - 1;        p = "keep-alive";    } else {        len = sizeof("close") - 1;        p = "close";    }    v->len = len;    v->valid = 1;    v->no_cacheable = 0;    v->not_found = 0;    v->data = (u_char *) p;    return NGX_OK;}static ngx_int_tngx_http_variable_sent_keep_alive(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    u_char                    *p;    ngx_http_core_loc_conf_t  *clcf;    if (r->keepalive) {        clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);        if (clcf->keepalive_header) {            p = ngx_palloc(r->pool, sizeof("timeout=") - 1 + NGX_TIME_T_LEN);            if (p == NULL) {                return NGX_ERROR;            }            v->len = ngx_sprintf(p, "timeout=%T", clcf->keepalive_header) - p;            v->valid = 1;            v->no_cacheable = 0;            v->not_found = 0;            v->data = p;            return NGX_OK;        }    }    v->not_found = 1;    return NGX_OK;}static ngx_int_tngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    if (r->chunked) {        v->len = sizeof("chunked") - 1;        v->valid = 1;        v->no_cacheable = 0;        v->not_found = 0;        v->data = (u_char *) "chunked";    } else {        v->not_found = 1;    }    return NGX_OK;}static ngx_int_tngx_http_variable_request_completion(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    if (r->request_complete) {        v->len = 2;        v->valid = 1;        v->no_cacheable = 0;        v->not_found = 0;        v->data = (u_char *) "OK";        return NGX_OK;    }    v->len = 0;    v->valid = 1;    v->no_cacheable = 0;    v->not_found = 0;    v->data = (u_char *) "";    return NGX_OK;}static ngx_int_tngx_http_variable_request_body_file(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    if (r->request_body == NULL || r->request_body->temp_file == NULL) {        v->not_found = 1;        return NGX_OK;    }    v->len = r->request_body->temp_file->file.name.len;    v->valid = 1;    v->no_cacheable = 0;    v->not_found = 0;    v->data = r->request_body->temp_file->file.name.data;    return NGX_OK;}static ngx_int_tngx_http_variable_nginx_version(ngx_http_request_t *r,    ngx_http_variable_value_t *v, uintptr_t data){    v->len = sizeof(NGINX_VERSION) - 1;    v->valid = 1;    v->no_cacheable = 0;    v->not_found = 0;    v->data = (u_char *) NGINX_VERSION;    return NGX_OK;}ngx_int_tngx_http_variables_add_core_vars(ngx_conf_t *cf){    ngx_int_t                   rc;    ngx_http_variable_t        *v;    ngx_http_core_main_conf_t  *cmcf;    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);    cmcf->variables_keys = ngx_pcalloc(cf->temp_pool,                                       sizeof(ngx_hash_keys_arrays_t));    if (cmcf->variables_keys == NULL) {        return NGX_ERROR;    }    cmcf->variables_keys->pool = cf->pool;    cmcf->variables_keys->temp_pool = cf->pool;    if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL)        != NGX_OK)    {        return NGX_ERROR;    }    for (v = ngx_http_core_variables; v->name.len; v++) {        rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v,                              NGX_HASH_READONLY_KEY);        if (rc == NGX_OK) {            continue;        }        if (rc == NGX_BUSY) {            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                               "conflicting variable name \"%V\"", &v->name);        }        return NGX_ERROR;    }    return NGX_OK;}ngx_int_tngx_http_variables_init_vars(ngx_conf_t *cf){    ngx_uint_t                  i, n;    ngx_hash_key_t             *key;    ngx_hash_init_t             hash;    ngx_http_variable_t        *v, *av;    ngx_http_core_main_conf_t  *cmcf;    /* set the handlers for the indexed http variables */    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);    v = cmcf->variables.elts;    key = cmcf->variables_keys->keys.elts;    for (i = 0; i < cmcf->variables.nelts; i++) {        for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {            av = key[n].value;            if (av->get_handler                && v[i].name.len == key[n].key.len                && ngx_strncmp(v[i].name.data, key[n].key.data, v[i].name.len)                   == 0)            {                v[i].get_handler = av->get_handler;                v[i].data = av->data;                av->flags |= NGX_HTTP_VAR_INDEXED;                v[i].flags = av->flags;                av->index = i;                goto next;            }        }        if (ngx_strncmp(v[i].name.data, "http_", 5) == 0) {            v[i].get_handler = ngx_http_variable_unknown_header_in;            v[i].data = (uintptr_t) &v[i].name;            continue;        }        if (ngx_strncmp(v[i].name.data, "sent_http_", 10) == 0) {            v[i].get_handler = ngx_http_variable_unknown_header_out;            v[i].data = (uintptr_t) &v[i].name;            continue;        }        if (ngx_strncmp(v[i].name.data, "upstream_http_", 14) == 0) {            v[i].get_handler = ngx_http_upstream_header_variable;            v[i].data = (uintptr_t) &v[i].name;            v[i].flags = NGX_HTTP_VAR_NOCACHEABLE;            continue;        }        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,                      "unknown \"%V\" variable", &v[i].name);        return NGX_ERROR;    next:        continue;    }    for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {        av = key[n].value;        if (av->flags & NGX_HTTP_VAR_NOHASH) {            key[n].key.data = NULL;        }    }    hash.hash = &cmcf->variables_hash;    hash.key = ngx_hash_key;    hash.max_size = cmcf->variables_hash_max_size;    hash.bucket_size = cmcf->variables_hash_bucket_size;    hash.name = "variables_hash";    hash.pool = cf->pool;    hash.temp_pool = NULL;    if (ngx_hash_init(&hash, cmcf->variables_keys->keys.elts,                      cmcf->variables_keys->keys.nelts)        != NGX_OK)    {        return NGX_ERROR;    }    cmcf->variables_keys = NULL;    return NGX_OK;}

⌨️ 快捷键说明

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