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

📄 ngx_http_proxy_module.c

📁 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器
💻 C
📖 第 1 页 / 共 5 页
字号:
        if (conf->upstream.pass_headers == NULL) {            conf->upstream.pass_headers = prev->upstream.pass_headers;        }    }    if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t))        != NGX_OK)    {        return NGX_CONF_ERROR;    }    for (header = ngx_http_proxy_hide_headers; header->len; header++) {        hk = ngx_array_push(&hide_headers);        if (hk == NULL) {            return NGX_CONF_ERROR;        }        hk->key = *header;        hk->key_hash = ngx_hash_key_lc(header->data, header->len);        hk->value = (void *) 1;    }    if (conf->upstream.hide_headers) {        header = conf->upstream.hide_headers->elts;        for (i = 0; i < conf->upstream.hide_headers->nelts; i++) {            hk = hide_headers.elts;            for (j = 0; j < hide_headers.nelts; j++) {                if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) {                    goto exist;                }            }            hk = ngx_array_push(&hide_headers);            if (hk == NULL) {                return NGX_CONF_ERROR;            }            hk->key = header[i];            hk->key_hash = ngx_hash_key_lc(header[i].data, header[i].len);            hk->value = (void *) 1;        exist:            continue;        }    }    if (conf->upstream.pass_headers) {        hk = hide_headers.elts;        header = conf->upstream.pass_headers->elts;        for (i = 0; i < conf->upstream.pass_headers->nelts; i++) {            for (j = 0; j < hide_headers.nelts; j++) {                if (hk[j].key.data == NULL) {                    continue;                }                if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) {                    hk[j].key.data = NULL;                    break;                }            }        }    }    hash.hash = &conf->upstream.hide_headers_hash;    hash.key = ngx_hash_key_lc;    hash.max_size = conf->headers_hash_max_size;    hash.bucket_size = conf->headers_hash_bucket_size;    hash.name = "proxy_headers_hash";    hash.pool = cf->pool;    hash.temp_pool = NULL;    if (ngx_hash_init(&hash, hide_headers.elts, hide_headers.nelts) != NGX_OK) {        return NGX_CONF_ERROR;    }peers:    if (conf->upstream.upstream == NULL) {        conf->upstream.upstream = prev->upstream.upstream;        conf->host_header = prev->host_header;        conf->port = prev->port;        conf->upstream.schema = prev->upstream.schema;    }    if (conf->body_source.data == NULL) {        conf->body_source = prev->body_source;        conf->body_set_len = prev->body_set_len;        conf->body_set = prev->body_set;    }    if (conf->body_source.data && conf->body_set_len == NULL) {        ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));        sc.cf = cf;        sc.source = &conf->body_source;        sc.flushes = &conf->flushes;        sc.lengths = &conf->body_set_len;        sc.values = &conf->body_set;        sc.complete_lengths = 1;        sc.complete_values = 1;        if (ngx_http_script_compile(&sc) != NGX_OK) {            return NGX_CONF_ERROR;        }        if (conf->headers_source == NULL) {            conf->headers_source = ngx_array_create(cf->pool, 4,                                                    sizeof(ngx_keyval_t));            if (conf->headers_source == NULL) {                return NGX_CONF_ERROR;            }        }        s = ngx_array_push(conf->headers_source);        if (s == NULL) {            return NGX_CONF_ERROR;        }        s->key.len = sizeof("Content-Length") - 1;        s->key.data = (u_char *) "Content-Length";        s->value.len = sizeof("$proxy_internal_body_length") - 1;        s->value.data = (u_char *) "$proxy_internal_body_length";    }    if (conf->headers_source == NULL) {        conf->flushes = prev->flushes;        conf->headers_set_len = prev->headers_set_len;        conf->headers_set = prev->headers_set;        conf->headers_set_hash = prev->headers_set_hash;        conf->headers_source = prev->headers_source;    }    if (conf->headers_set_hash.buckets) {        return NGX_CONF_OK;    }    conf->headers_names = ngx_array_create(cf->pool, 4, sizeof(ngx_hash_key_t));    if (conf->headers_names == NULL) {        return NGX_CONF_ERROR;    }    if (conf->headers_source == NULL) {        conf->headers_source = ngx_array_create(cf->pool, 4,                                                sizeof(ngx_keyval_t));        if (conf->headers_source == NULL) {            return NGX_CONF_ERROR;        }    }    conf->headers_set_len = ngx_array_create(cf->pool, 64, 1);    if (conf->headers_set_len == NULL) {        return NGX_CONF_ERROR;    }    conf->headers_set = ngx_array_create(cf->pool, 512, 1);    if (conf->headers_set == NULL) {        return NGX_CONF_ERROR;    }    src = conf->headers_source->elts;    for (h = ngx_http_proxy_headers; h->key.len; h++) {        for (i = 0; i < conf->headers_source->nelts; i++) {            if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {                goto next;            }        }        s = ngx_array_push(conf->headers_source);        if (s == NULL) {            return NGX_CONF_ERROR;        }        *s = *h;        src = conf->headers_source->elts;    next:        continue;    }    src = conf->headers_source->elts;    for (i = 0; i < conf->headers_source->nelts; i++) {        hk = ngx_array_push(conf->headers_names);        if (hk == NULL) {            return NGX_CONF_ERROR;        }        hk->key = src[i].key;        hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);        hk->value = (void *) 1;        if (src[i].value.len == 0) {            continue;        }        if (ngx_http_script_variables_count(&src[i].value) == 0) {            copy = ngx_array_push_n(conf->headers_set_len,                                    sizeof(ngx_http_script_copy_code_t));            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = (ngx_http_script_code_pt)                                                 ngx_http_script_copy_len_code;            copy->len = src[i].key.len + sizeof(": ") - 1                        + src[i].value.len + sizeof(CRLF) - 1;            size = (sizeof(ngx_http_script_copy_code_t)                       + src[i].key.len + sizeof(": ") - 1                       + src[i].value.len + sizeof(CRLF) - 1                       + sizeof(uintptr_t) - 1)                    & ~(sizeof(uintptr_t) - 1);            copy = ngx_array_push_n(conf->headers_set, size);            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = ngx_http_script_copy_code;            copy->len = src[i].key.len + sizeof(": ") - 1                        + src[i].value.len + sizeof(CRLF) - 1;            p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);            p = ngx_cpymem(p, src[i].key.data, src[i].key.len);            *p++ = ':'; *p++ = ' ';            p = ngx_cpymem(p, src[i].value.data, src[i].value.len);            *p++ = CR; *p = LF;        } else {            copy = ngx_array_push_n(conf->headers_set_len,                                    sizeof(ngx_http_script_copy_code_t));            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = (ngx_http_script_code_pt)                                                 ngx_http_script_copy_len_code;            copy->len = src[i].key.len + sizeof(": ") - 1;            size = (sizeof(ngx_http_script_copy_code_t)                    + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1)                    & ~(sizeof(uintptr_t) - 1);            copy = ngx_array_push_n(conf->headers_set, size);            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = ngx_http_script_copy_code;            copy->len = src[i].key.len + sizeof(": ") - 1;            p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);            p = ngx_cpymem(p, src[i].key.data, src[i].key.len);            *p++ = ':'; *p = ' ';            ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));            sc.cf = cf;            sc.source = &src[i].value;            sc.flushes = &conf->flushes;            sc.lengths = &conf->headers_set_len;            sc.values = &conf->headers_set;            if (ngx_http_script_compile(&sc) != NGX_OK) {                return NGX_CONF_ERROR;            }            copy = ngx_array_push_n(conf->headers_set_len,                                    sizeof(ngx_http_script_copy_code_t));            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = (ngx_http_script_code_pt)                                                 ngx_http_script_copy_len_code;            copy->len = sizeof(CRLF) - 1;            size = (sizeof(ngx_http_script_copy_code_t)                    + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)                    & ~(sizeof(uintptr_t) - 1);            copy = ngx_array_push_n(conf->headers_set, size);            if (copy == NULL) {                return NGX_CONF_ERROR;            }            copy->code = ngx_http_script_copy_code;            copy->len = sizeof(CRLF) - 1;            p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);            *p++ = CR; *p = LF;        }        code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));        if (code == NULL) {            return NGX_CONF_ERROR;        }        *code = (uintptr_t) NULL;        code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t));        if (code == NULL) {            return NGX_CONF_ERROR;        }        *code = (uintptr_t) NULL;    }    code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));    if (code == NULL) {        return NGX_CONF_ERROR;    }    *code = (uintptr_t) NULL;    hash.hash = &conf->headers_set_hash;    hash.key = ngx_hash_key_lc;    hash.max_size = conf->headers_hash_max_size;    hash.bucket_size = conf->headers_hash_bucket_size;    hash.name = "proxy_headers_hash";    hash.pool = cf->pool;    hash.temp_pool = NULL;    if (ngx_hash_init(&hash, conf->headers_names->elts,                      conf->headers_names->nelts)        != NGX_OK)    {        return NGX_CONF_ERROR;    }    return NGX_CONF_OK;}static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    ngx_http_proxy_loc_conf_t *plcf = conf;    u_char                    *p;    size_t                     add;    u_short                    port;    ngx_str_t                 *value, *url;    ngx_url_t                  u;    ngx_http_core_loc_conf_t  *clcf;#if (NGX_HTTP_SSL)    ngx_pool_cleanup_t        *cln;#endif    if (plcf->upstream.schema.len) {        return "is duplicate";    }    value = cf->args->elts;    url = &value[1];    if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {        add = 7;        port = 80;    } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {#if (NGX_HTTP_SSL)        add = 8;        port = 443;        plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));        if (plcf->upstream.ssl == NULL) {            return NGX_CONF_ERROR;        }        plcf->upstream.ssl->log = cf->log;        if (ngx_ssl_create(plcf->upstream.ssl,                           NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1, NULL)            != NGX_OK)        {            return NGX_CONF_ERROR;        }        cln = ngx_pool_cleanup_add(cf->pool, 0);        if (cln == NULL) {            return NGX_CONF_ERROR;        }        cln->handler = ngx_ssl_cleanup_ctx;        cln->data = plcf->upstream.ssl;#else        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                           "https protocol requires SSL support");        return NGX_CONF_ERROR;#endif    } else {        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");        return NGX_CONF_ERROR;    }    ngx_memzero(&u, sizeof(ngx_url_t));    u.url.len = url->len - add;    u.url.data = url->data + add;    u.default_port = port;    u.uri_part = 1;    u.no_resolve = 1;    plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);    if (plcf->upstream.upstream == NULL) {        return NGX_CONF_ERROR;    }    if (!u.unix_socket) {        if (u.no_port || u.port == port) {            plcf->host_header = u.host;            if (port == 80) {             

⌨️ 快捷键说明

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