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

📄 ngx_http_userid_filter_module.c

📁 Nginx是一个高性能的HTTP和反向代理服务器
💻 C
📖 第 1 页 / 共 2 页
字号:
    }    if (conf->domain.len) {        len += conf->domain.len;    }    cookie = ngx_palloc(r->pool, len);    if (cookie == NULL) {        return NGX_ERROR;    }    p = ngx_copy(cookie, conf->name.data, conf->name.len);    *p++ = '=';    if (ctx->uid_got[3] == 0) {        src.len = 16;        src.data = (u_char *) ctx->uid_set;        dst.data = p;        ngx_encode_base64(&dst, &src);        p += dst.len;        if (conf->mark) {            *(p - 2) = conf->mark;        }    } else {        p = ngx_cpymem(p, ctx->cookie.data, 22);        *p++ = conf->mark;        *p++ = '=';    }    if (conf->expires == NGX_HTTP_USERID_MAX_EXPIRES) {        p = ngx_cpymem(p, expires, sizeof(expires) - 1);    } else if (conf->expires) {        p = ngx_cpymem(p, expires, sizeof("; expires=") - 1);        p = ngx_http_cookie_time(p, ngx_time() + conf->expires);    }    p = ngx_copy(p, conf->domain.data, conf->domain.len);    p = ngx_copy(p, conf->path.data, conf->path.len);    set_cookie = ngx_list_push(&r->headers_out.headers);    if (set_cookie == NULL) {        return NGX_ERROR;    }    set_cookie->hash = 1;    set_cookie->key.len = sizeof("Set-Cookie") - 1;    set_cookie->key.data = (u_char *) "Set-Cookie";    set_cookie->value.len = p - cookie;    set_cookie->value.data = cookie;    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "uid cookie: \"%V\"", &set_cookie->value);    if (conf->p3p.len == 0) {        return NGX_OK;    }    p3p = ngx_list_push(&r->headers_out.headers);    if (p3p == NULL) {        return NGX_ERROR;    }    p3p->hash = 1;    p3p->key.len = sizeof("P3P") - 1;    p3p->key.data = (u_char *) "P3P";    p3p->value = conf->p3p;    return NGX_OK;}static ngx_int_tngx_http_userid_add_variables(ngx_conf_t *cf){    ngx_http_variable_t  *var;    var = ngx_http_add_variable(cf, &ngx_http_userid_got, NGX_HTTP_VAR_NOHASH);    if (var == NULL) {        return NGX_ERROR;    }    var->get_handler = ngx_http_userid_variable;    var->data = offsetof(ngx_http_userid_ctx_t, uid_got);    var = ngx_http_add_variable(cf, &ngx_http_userid_set, NGX_HTTP_VAR_NOHASH);    if (var == NULL) {        return NGX_ERROR;    }    var->get_handler = ngx_http_userid_variable;    var->data = offsetof(ngx_http_userid_ctx_t, uid_set);    return NGX_OK;}static ngx_int_tngx_http_userid_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,    uintptr_t data){    uint32_t                *uid;    ngx_http_userid_ctx_t   *ctx;    ngx_http_userid_conf_t  *conf;    ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module);    uid = (uint32_t *) ((char *) ctx + data);    if (ctx == NULL || uid[3] == 0) {        v->not_found = 1;        return NGX_OK;    }    conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);    v->len = conf->name.len + sizeof("=00001111222233334444555566667777") - 1;    v->data = ngx_palloc(r->pool, v->len);    if (v->data == NULL) {        return NGX_ERROR;    }    v->valid = 1;    v->no_cacheable = 0;    v->not_found = 0;    ngx_sprintf(v->data, "%V=%08XD%08XD%08XD%08XD",                &conf->name, uid[0], uid[1], uid[2], uid[3]);    return NGX_OK;}static void *ngx_http_userid_create_conf(ngx_conf_t *cf){    ngx_http_userid_conf_t  *conf;    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_userid_conf_t));    if (conf == NULL) {        return NGX_CONF_ERROR;    }    /*     * set by ngx_pcalloc():     *     *     conf->name.len = 0;     *     conf->name.date = NULL;     *     conf->domain.len = 0;     *     conf->domain.date = NULL;     *     conf->path.len = 0;     *     conf->path.date = NULL;     *     conf->p3p.len = 0;     *     conf->p3p.date = NULL;     */    conf->enable = NGX_CONF_UNSET_UINT;    conf->service = NGX_CONF_UNSET;    conf->expires = NGX_CONF_UNSET;    conf->mark = (u_char) '\xFF';    return conf;}static char *ngx_http_userid_merge_conf(ngx_conf_t *cf, void *parent, void *child){    ngx_http_userid_conf_t *prev = parent;    ngx_http_userid_conf_t *conf = child;    ngx_conf_merge_uint_value(conf->enable, prev->enable,                              NGX_HTTP_USERID_OFF);    ngx_conf_merge_str_value(conf->name, prev->name, "uid");    ngx_conf_merge_str_value(conf->domain, prev->domain, "");    ngx_conf_merge_str_value(conf->path, prev->path, "; path=/");    ngx_conf_merge_str_value(conf->p3p, prev->p3p, "");    ngx_conf_merge_value(conf->service, prev->service, NGX_CONF_UNSET);    ngx_conf_merge_sec_value(conf->expires, prev->expires, 0);    if (conf->mark == (u_char) '\xFF') {        if (prev->mark == (u_char) '\xFF') {            conf->mark = '\0';        } else {            conf->mark = prev->mark;        }    }    return NGX_CONF_OK;}static ngx_int_tngx_http_userid_init(ngx_conf_t *cf){    ngx_http_next_header_filter = ngx_http_top_header_filter;    ngx_http_top_header_filter = ngx_http_userid_filter;    return NGX_OK;}static char *ngx_http_userid_domain(ngx_conf_t *cf, void *post, void *data){    ngx_str_t  *domain = data;    u_char  *p, *new;    if (ngx_strcmp(domain->data, "none") == 0) {        domain->len = 0;        domain->data = (u_char *) "";        return NGX_CONF_OK;    }    new = ngx_palloc(cf->pool, sizeof("; domain=") - 1 + domain->len);    if (new == NULL) {        return NGX_CONF_ERROR;    }    p = ngx_cpymem(new, "; domain=", sizeof("; domain=") - 1);    ngx_memcpy(p, domain->data, domain->len);    domain->len += sizeof("; domain=") - 1;    domain->data = new;    return NGX_CONF_OK;}static char *ngx_http_userid_path(ngx_conf_t *cf, void *post, void *data){    ngx_str_t  *path = data;    u_char  *p, *new;    new = ngx_palloc(cf->pool, sizeof("; path=") - 1 + path->len);    if (new == NULL) {        return NGX_CONF_ERROR;    }    p = ngx_cpymem(new, "; path=", sizeof("; path=") - 1);    ngx_memcpy(p, path->data, path->len);    path->len += sizeof("; path=") - 1;    path->data = new;    return NGX_CONF_OK;}static char *ngx_http_userid_expires(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    ngx_http_userid_conf_t *ucf = conf;    ngx_str_t  *value;    if (ucf->expires != NGX_CONF_UNSET) {        return "is duplicate";    }    value = cf->args->elts;    if (ngx_strcmp(value[1].data, "max") == 0) {        ucf->expires = NGX_HTTP_USERID_MAX_EXPIRES;        return NGX_CONF_OK;    }    if (ngx_strcmp(value[1].data, "off") == 0) {        ucf->expires = 0;        return NGX_CONF_OK;    }    ucf->expires = ngx_parse_time(&value[1], 1);    if (ucf->expires == NGX_ERROR) {        return "invalid value";    }    if (ucf->expires == NGX_PARSE_LARGE_TIME) {        return "value must be less than 68 years";    }    return NGX_CONF_OK;}static char *ngx_http_userid_p3p(ngx_conf_t *cf, void *post, void *data){    ngx_str_t  *p3p = data;    if (ngx_strcmp(p3p->data, "none") == 0) {        p3p->len = 0;        p3p->data = (u_char *) "";    }    return NGX_CONF_OK;}static char *ngx_http_userid_mark(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    ngx_http_userid_conf_t *ucf = conf;    ngx_str_t  *value;    if (ucf->mark != (u_char) '\xFF') {        return "is duplicate";    }    value = cf->args->elts;    if (ngx_strcmp(value[1].data, "off") == 0) {        ucf->mark = '\0';        return NGX_CONF_OK;    }    if (value[1].len != 1        || !((value[1].data[0] >= '0' && value[1].data[0] <= '9')              || (value[1].data[0] >= 'A' && value[1].data[0] <= 'Z')              || (value[1].data[0] >= 'a' && value[1].data[0] <= 'z')              || value[1].data[0] == '='))    {        return "value must be \"off\" or a single letter, digit or \"=\"";    }    ucf->mark = value[1].data[0];    return NGX_CONF_OK;}static ngx_int_tngx_http_userid_init_worker(ngx_cycle_t *cycle){    struct timeval  tp;    ngx_gettimeofday(&tp);    /* use the most significant usec part that fits to 16 bits */    start_value = ((tp.tv_usec / 20) << 16) | ngx_pid;    return NGX_OK;}

⌨️ 快捷键说明

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