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

📄 ngx_http_charset_filter_module.c

📁 nginx 反向代理0.7.1版本 用于实现反向代理
💻 C
📖 第 1 页 / 共 3 页
字号:
            b->shadow = NULL;            return cl;        }    }    cl = ngx_alloc_chain_link(pool);    if (cl == NULL) {        return NULL;    }    cl->buf = ngx_create_temp_buf(pool, size);    if (cl->buf == NULL) {        return NULL;    }    cl->next = NULL;    cl->buf->temporary = 1;    cl->buf->tag = (ngx_buf_tag_t) &ngx_http_charset_filter_module;    return cl;}static char *ngx_http_charset_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    ngx_http_charset_main_conf_t  *mcf = conf;    char                         *rv;    u_char                       *p, *dst2src, **pp;    ngx_int_t                     src, dst;    ngx_uint_t                    i, n;    ngx_str_t                    *value;    ngx_conf_t                    pvcf;    ngx_http_charset_t           *charset;    ngx_http_charset_tables_t    *table;    ngx_http_charset_conf_ctx_t   ctx;    value = cf->args->elts;    src = ngx_http_add_charset(&mcf->charsets, &value[1]);    if (src == NGX_ERROR) {        return NGX_CONF_ERROR;    }    dst = ngx_http_add_charset(&mcf->charsets, &value[2]);    if (dst == NGX_ERROR) {        return NGX_CONF_ERROR;    }    if (src == dst) {        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                           "\"charset_map\" between the same charsets "                           "\"%V\" and \"%V\"", &value[1], &value[2]);        return NGX_CONF_ERROR;    }    table = mcf->tables.elts;    for (i = 0; i < mcf->tables.nelts; i++) {        if ((src == table->src && dst == table->dst)             || (src == table->dst && dst == table->src))        {            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                               "duplicate \"charset_map\" between "                               "\"%V\" and \"%V\"", &value[1], &value[2]);            return NGX_CONF_ERROR;        }    }    table = ngx_array_push(&mcf->tables);    if (table == NULL) {        return NGX_CONF_ERROR;    }    table->src = src;    table->dst = dst;    if (ngx_strcasecmp(value[2].data, (u_char *) "utf-8") == 0) {        table->src2dst = ngx_pcalloc(cf->pool, 256 * NGX_UTF_LEN);        if (table->src2dst == NULL) {            return NGX_CONF_ERROR;        }        table->dst2src = ngx_pcalloc(cf->pool, 256 * sizeof(void *));        if (table->dst2src == NULL) {            return NGX_CONF_ERROR;        }        dst2src = ngx_pcalloc(cf->pool, 256);        if (dst2src == NULL) {            return NGX_CONF_ERROR;        }        pp = (u_char **) &table->dst2src[0];        pp[0] = dst2src;        for (i = 0; i < 128; i++) {            p = &table->src2dst[i * NGX_UTF_LEN];            p[0] = '\1';            p[1] = (u_char) i;            dst2src[i] = (u_char) i;        }        for (/* void */; i < 256; i++) {            p = &table->src2dst[i * NGX_UTF_LEN];            p[0] = '\1';            p[1] = '?';        }    } else {        table->src2dst = ngx_palloc(cf->pool, 256);        if (table->src2dst == NULL) {            return NGX_CONF_ERROR;        }        table->dst2src = ngx_palloc(cf->pool, 256);        if (table->dst2src == NULL) {            return NGX_CONF_ERROR;        }        for (i = 0; i < 128; i++) {            table->src2dst[i] = (u_char) i;            table->dst2src[i] = (u_char) i;        }        for (/* void */; i < 256; i++) {            table->src2dst[i] = '?';            table->dst2src[i] = '?';        }    }    charset = mcf->charsets.elts;    ctx.table = table;    ctx.charset = &charset[dst];    ctx.characters = 0;    pvcf = *cf;    cf->ctx = &ctx;    cf->handler = ngx_http_charset_map;    cf->handler_conf = conf;    rv = ngx_conf_parse(cf, NULL);    *cf = pvcf;    if (ctx.characters) {        n = ctx.charset->length;        ctx.charset->length /= ctx.characters;        if (((n * 10) / ctx.characters) % 10 > 4) {            ctx.charset->length++;        }    }    return rv;}static char *ngx_http_charset_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf){    u_char                       *p, *dst2src, **pp;    uint32_t                      n;    ngx_int_t                     src, dst;    ngx_str_t                    *value;    ngx_uint_t                    i;    ngx_http_charset_tables_t    *table;    ngx_http_charset_conf_ctx_t  *ctx;    if (cf->args->nelts != 2) {        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameters number");        return NGX_CONF_ERROR;    }    value = cf->args->elts;    src = ngx_hextoi(value[0].data, value[0].len);    if (src == NGX_ERROR || src > 255) {        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                           "invalid value \"%V\"", &value[0]);        return NGX_CONF_ERROR;    }    ctx = cf->ctx;    table = ctx->table;    if (ctx->charset->utf8) {        p = &table->src2dst[src * NGX_UTF_LEN];        *p++ = (u_char) (value[1].len / 2);        for (i = 0; i < value[1].len; i += 2) {            dst = ngx_hextoi(&value[1].data[i], 2);            if (dst == NGX_ERROR || dst > 255) {                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                                   "invalid value \"%V\"", &value[1]);                return NGX_CONF_ERROR;            }            *p++ = (u_char) dst;        }        i /= 2;        ctx->charset->length += i;        ctx->characters++;        p = &table->src2dst[src * NGX_UTF_LEN] + 1;        n = ngx_utf_decode(&p, i);        if (n > 0xffff) {            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                               "invalid value \"%V\"", &value[1]);            return NGX_CONF_ERROR;        }        pp = (u_char **) &table->dst2src[0];        dst2src = pp[n >> 8];        if (dst2src == NULL) {            dst2src = ngx_pcalloc(cf->pool, 256);            if (dst2src == NULL) {                return NGX_CONF_ERROR;            }            pp[n >> 8] = dst2src;        }        dst2src[n & 0xff] = (u_char) src;    } else {        dst = ngx_hextoi(value[1].data, value[1].len);        if (dst == NGX_ERROR || dst > 255) {            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                               "invalid value \"%V\"", &value[1]);            return NGX_CONF_ERROR;        }        table->src2dst[src] = (u_char) dst;        table->dst2src[dst] = (u_char) src;    }    return NGX_CONF_OK;}static char *ngx_http_set_charset_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char  *p = conf;    ngx_int_t                     *cp;    ngx_str_t                     *value, var;    ngx_http_charset_main_conf_t  *mcf;    cp = (ngx_int_t *) (p + cmd->offset);    if (*cp != NGX_CONF_UNSET) {        return "is duplicate";    }    value = cf->args->elts;    if (cmd->offset == offsetof(ngx_http_charset_loc_conf_t, charset)        && ngx_strcmp(value[1].data, "off") == 0)    {        *cp = NGX_HTTP_NO_CHARSET;        return NGX_CONF_OK;    }    if (value[1].data[0] == '$') {        var.len = value[1].len - 1;        var.data = value[1].data + 1;        *cp = ngx_http_get_variable_index(cf, &var);        if (*cp == NGX_ERROR) {            return NGX_CONF_ERROR;        }        *cp += NGX_HTTP_CHARSET_VAR;        return NGX_CONF_OK;    }    mcf = ngx_http_conf_get_module_main_conf(cf,                                             ngx_http_charset_filter_module);    *cp = ngx_http_add_charset(&mcf->charsets, &value[1]);    if (*cp == NGX_ERROR) {        return NGX_CONF_ERROR;    }    return NGX_CONF_OK;}static ngx_int_tngx_http_add_charset(ngx_array_t *charsets, ngx_str_t *name){    ngx_uint_t           i;    ngx_http_charset_t  *c;    c = charsets->elts;    for (i = 0; i < charsets->nelts; i++) {        if (name->len != c[i].name.len) {            continue;        }        if (ngx_strcasecmp(name->data, c[i].name.data) == 0) {            break;        }    }    if (i < charsets->nelts) {        return i;    }    c = ngx_array_push(charsets);    if (c == NULL) {        return NGX_ERROR;    }    c->tables = NULL;    c->name = *name;    c->length = 0;    if (ngx_strcasecmp(name->data, (u_char *) "utf-8") == 0) {        c->utf8 = 1;    } else {        c->utf8 = 0;    }    return i;}static void *ngx_http_charset_create_main_conf(ngx_conf_t *cf){    ngx_http_charset_main_conf_t  *mcf;    mcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_charset_main_conf_t));    if (mcf == NULL) {        return NGX_CONF_ERROR;    }    if (ngx_array_init(&mcf->charsets, cf->pool, 2, sizeof(ngx_http_charset_t))        == NGX_ERROR)    {        return NGX_CONF_ERROR;    }    if (ngx_array_init(&mcf->tables, cf->pool, 1,                       sizeof(ngx_http_charset_tables_t)) == NGX_ERROR)    {        return NGX_CONF_ERROR;    }    if (ngx_array_init(&mcf->recodes, cf->pool, 2,                       sizeof(ngx_http_charset_recode_t)) == NGX_ERROR)    {        return NGX_CONF_ERROR;    }    return mcf;}static void *ngx_http_charset_create_loc_conf(ngx_conf_t *cf){    ngx_http_charset_loc_conf_t  *lcf;    lcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_charset_loc_conf_t));    if (lcf == NULL) {        return NGX_CONF_ERROR;    }    lcf->charset = NGX_CONF_UNSET;    lcf->source_charset = NGX_CONF_UNSET;    lcf->override_charset = NGX_CONF_UNSET;    return lcf;}static char *ngx_http_charset_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child){    ngx_http_charset_loc_conf_t *prev = parent;    ngx_http_charset_loc_conf_t *conf = child;    ngx_uint_t                     i;    ngx_http_charset_recode_t     *recode;    ngx_http_charset_main_conf_t  *mcf;    ngx_conf_merge_value(conf->override_charset, prev->override_charset, 0);    ngx_conf_merge_value(conf->charset, prev->charset, NGX_HTTP_NO_CHARSET);    if (conf->source_charset == NGX_CONF_UNSET) {        conf->source_charset = prev->source_charset;    }    if (conf->charset == NGX_HTTP_NO_CHARSET        || conf->source_charset == NGX_CONF_UNSET        || conf->charset == conf->source_charset)    {        return NGX_CONF_OK;    }    if (conf->source_charset >= NGX_HTTP_CHARSET_VAR        || conf->charset >= NGX_HTTP_CHARSET_VAR)    {        return NGX_CONF_OK;    }    mcf = ngx_http_conf_get_module_main_conf(cf,                                             ngx_http_charset_filter_module);    recode = mcf->recodes.elts;    for (i = 0; i < mcf->recodes.nelts; i++) {        if (conf->source_charset == recode[i].src            && conf->charset == recode[i].dst)        {            return NGX_CONF_OK;        }    }    recode = ngx_array_push(&mcf->recodes);    if (recode == NULL) {        return NGX_CONF_ERROR;    }    recode->src = conf->source_charset;    recode->dst = conf->charset;    return NGX_CONF_OK;}static ngx_int_tngx_http_charset_postconfiguration(ngx_conf_t *cf){    u_char                       **src, **dst;    ngx_int_t                      c;    ngx_uint_t                     i, t;    ngx_http_charset_t            *charset;    ngx_http_charset_recode_t     *recode;    ngx_http_charset_tables_t     *tables;    ngx_http_charset_main_conf_t  *mcf;    mcf = ngx_http_conf_get_module_main_conf(cf,                                             ngx_http_charset_filter_module);    recode = mcf->recodes.elts;    tables = mcf->tables.elts;    charset = mcf->charsets.elts;    for (i = 0; i < mcf->recodes.nelts; i++) {        c = recode[i].src;        for (t = 0; t < mcf->tables.nelts; t++) {            if (c == tables[t].src && recode[i].dst == tables[t].dst) {                goto next;            }            if (c == tables[t].dst && recode[i].dst == tables[t].src) {                goto next;            }        }        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,                   "no \"charset_map\" between the charsets \"%V\" and \"%V\"",                   &charset[c].name, &charset[recode[i].dst].name);        return NGX_ERROR;    next:        continue;    }    for (t = 0; t < mcf->tables.nelts; t++) {        src = charset[tables[t].src].tables;        if (src == NULL) {            src = ngx_pcalloc(cf->pool, sizeof(u_char *) * mcf->charsets.nelts);            if (src == NULL) {                return NGX_ERROR;            }            charset[tables[t].src].tables = src;        }        dst = charset[tables[t].dst].tables;        if (dst == NULL) {            dst = ngx_pcalloc(cf->pool, sizeof(u_char *) * mcf->charsets.nelts);            if (dst == NULL) {                return NGX_ERROR;            }            charset[tables[t].dst].tables = dst;        }        src[tables[t].dst] = tables[t].src2dst;        dst[tables[t].src] = tables[t].dst2src;    }    ngx_http_next_header_filter = ngx_http_top_header_filter;    ngx_http_top_header_filter = ngx_http_charset_header_filter;    ngx_http_next_body_filter = ngx_http_top_body_filter;    ngx_http_top_body_filter = ngx_http_charset_body_filter;    return NGX_OK;}

⌨️ 快捷键说明

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