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

📄 ngx_http_proxy_module.c

📁 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器
💻 C
📖 第 1 页 / 共 5 页
字号:
    plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);    u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));    if (u == NULL) {        return NGX_HTTP_INTERNAL_SERVER_ERROR;    }    u->peer.log = r->connection->log;    u->peer.log_error = NGX_ERROR_ERR;#if (NGX_THREADS)    u->peer.lock = &r->connection->lock;#endif    u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module;    u->conf = &plcf->upstream;    u->create_request = ngx_http_proxy_create_request;    u->reinit_request = ngx_http_proxy_reinit_request;    u->process_header = ngx_http_proxy_process_status_line;    u->abort_request = ngx_http_proxy_abort_request;    u->finalize_request = ngx_http_proxy_finalize_request;    if (plcf->redirects) {        u->rewrite_redirect = ngx_http_proxy_rewrite_redirect;    }    u->buffering = plcf->upstream.buffering;    u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));    if (u->pipe == NULL) {        return NGX_HTTP_INTERNAL_SERVER_ERROR;    }    u->pipe->input_filter = ngx_event_pipe_copy_input_filter;    u->accel = 1;    r->upstream = u;    rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);    if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {        return rc;    }    return NGX_DONE;}static ngx_int_tngx_http_proxy_create_request(ngx_http_request_t *r){    size_t                        len, loc_len, body_len;    uintptr_t                     escape;    ngx_buf_t                    *b;    ngx_str_t                     method;    ngx_uint_t                    i, unparsed_uri;    ngx_chain_t                  *cl, *body;    ngx_list_part_t              *part;    ngx_table_elt_t              *header;    ngx_http_upstream_t          *u;    ngx_http_proxy_ctx_t         *p;    ngx_http_script_code_pt       code;    ngx_http_script_engine_t      e, le;    ngx_http_proxy_loc_conf_t    *plcf;    ngx_http_script_len_code_pt   lcode;    u = r->upstream;    plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);    p = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));    if (p == NULL) {        return NGX_HTTP_INTERNAL_SERVER_ERROR;    }    ngx_http_set_ctx(r, p, ngx_http_proxy_module);    len = sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1;    if (u->method.len) {        /* HEAD was changed to GET to cache response */        method = u->method;        method.len++;    } else if (plcf->method.len) {        method = plcf->method;    } else {        method = r->method_name;        method.len++;    }    len += method.len + u->conf->uri.len;    escape = 0;    loc_len = (r->valid_location && u->conf->uri.len) ? u->conf->location.len:                                                        0;    if (u->conf->uri.len == 0 && r->valid_unparsed_uri && r == r->main) {        unparsed_uri = 1;        len += r->unparsed_uri.len;    } else {        unparsed_uri = 0;        if (r->quoted_uri || r->internal) {            escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,                                        r->uri.len - loc_len, NGX_ESCAPE_URI);        }        len += r->uri.len - loc_len + escape + sizeof("?") - 1 + r->args.len;    }    ngx_http_script_flush_no_cacheable_variables(r, plcf->flushes);    if (plcf->body_set_len) {        le.ip = plcf->body_set_len->elts;        le.request = r;        le.flushed = 1;        body_len = 0;        while (*(uintptr_t *) le.ip) {            lcode = *(ngx_http_script_len_code_pt *) le.ip;            body_len += lcode(&le);        }        p->internal_body_length = body_len;        len += body_len;    }    le.ip = plcf->headers_set_len->elts;    le.request = r;    le.flushed = 1;    while (*(uintptr_t *) le.ip) {        while (*(uintptr_t *) le.ip) {            lcode = *(ngx_http_script_len_code_pt *) le.ip;            len += lcode(&le);        }        le.ip += sizeof(uintptr_t);    }    if (plcf->upstream.pass_request_headers) {        part = &r->headers_in.headers.part;        header = part->elts;        for (i = 0; /* void */; i++) {            if (i >= part->nelts) {                if (part->next == NULL) {                    break;                }                part = part->next;                header = part->elts;                i = 0;            }            if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,                              header[i].lowcase_key, header[i].key.len))            {                continue;            }            len += header[i].key.len + sizeof(": ") - 1                + header[i].value.len + sizeof(CRLF) - 1;        }    }    b = ngx_create_temp_buf(r->pool, len);    if (b == NULL) {        return NGX_ERROR;    }    cl = ngx_alloc_chain_link(r->pool);    if (cl == NULL) {        return NGX_ERROR;    }    cl->buf = b;    /* the request line */    b->last = ngx_copy(b->last, method.data, method.len);    u->uri.data = b->last;    if (unparsed_uri) {        b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len);    } else {        if (r->valid_location) {            b->last = ngx_copy(b->last, u->conf->uri.data, u->conf->uri.len);        }        if (escape) {            ngx_escape_uri(b->last, r->uri.data + loc_len,                           r->uri.len - loc_len, NGX_ESCAPE_URI);            b->last += r->uri.len - loc_len + escape;        } else {            b->last = ngx_copy(b->last, r->uri.data + loc_len,                               r->uri.len - loc_len);        }        if (r->args.len > 0) {            *b->last++ = '?';            b->last = ngx_copy(b->last, r->args.data, r->args.len);        }    }    u->uri.len = b->last - u->uri.data;    b->last = ngx_cpymem(b->last, ngx_http_proxy_version,                         sizeof(ngx_http_proxy_version) - 1);    ngx_memzero(&e, sizeof(ngx_http_script_engine_t));    e.ip = plcf->headers_set->elts;    e.pos = b->last;    e.request = r;    e.flushed = 1;    le.ip = plcf->headers_set_len->elts;    while (*(uintptr_t *) le.ip) {        lcode = *(ngx_http_script_len_code_pt *) le.ip;        /* skip the header line name length */        (void) lcode(&le);        if (*(ngx_http_script_len_code_pt *) le.ip) {            for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {                lcode = *(ngx_http_script_len_code_pt *) le.ip;            }            e.skip = (len == sizeof(CRLF) - 1) ? 1 : 0;        } else {            e.skip = 0;        }        le.ip += sizeof(uintptr_t);        while (*(uintptr_t *) e.ip) {            code = *(ngx_http_script_code_pt *) e.ip;            code((ngx_http_script_engine_t *) &e);        }        e.ip += sizeof(uintptr_t);    }    b->last = e.pos;    if (plcf->upstream.pass_request_headers) {        part = &r->headers_in.headers.part;        header = part->elts;        for (i = 0; /* void */; i++) {            if (i >= part->nelts) {                if (part->next == NULL) {                    break;                }                part = part->next;                header = part->elts;                i = 0;            }            if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,                              header[i].lowcase_key, header[i].key.len))            {                continue;            }            b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);            *b->last++ = ':'; *b->last++ = ' ';            b->last = ngx_copy(b->last, header[i].value.data,                               header[i].value.len);            *b->last++ = CR; *b->last++ = LF;            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                           "http proxy header: \"%V: %V\"",                           &header[i].key, &header[i].value);        }    }    /* add "\r\n" at the header end */    *b->last++ = CR; *b->last++ = LF;    if (plcf->body_set) {        e.ip = plcf->body_set->elts;        e.pos = b->last;        while (*(uintptr_t *) e.ip) {            code = *(ngx_http_script_code_pt *) e.ip;            code((ngx_http_script_engine_t *) &e);        }        b->last = e.pos;    }#if (NGX_DEBUG)    {    ngx_str_t  s;    s.len = b->last - b->pos;    s.data = b->pos;    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "http proxy header:\n\"%V\"", &s);    }#endif    if (plcf->body_set == NULL && plcf->upstream.pass_request_body) {        body = u->request_bufs;        u->request_bufs = cl;        while (body) {            b = ngx_alloc_buf(r->pool);            if (b == NULL) {                return NGX_ERROR;            }            ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));            cl->next = ngx_alloc_chain_link(r->pool);            if (cl->next == NULL) {                return NGX_ERROR;            }            cl = cl->next;            cl->buf = b;            body = body->next;        }        b->flush = 1;    } else {        u->request_bufs = cl;    }    cl->next = NULL;    return NGX_OK;}static ngx_int_tngx_http_proxy_reinit_request(ngx_http_request_t *r){    ngx_http_proxy_ctx_t  *p;    p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);    if (p == NULL) {        return NGX_OK;    }    p->status = 0;    p->status_count = 0;    p->status_start = NULL;    p->status_end = NULL;    r->upstream->process_header = ngx_http_proxy_process_status_line;    return NGX_OK;}static ngx_int_tngx_http_proxy_process_status_line(ngx_http_request_t *r){    ngx_int_t              rc;    ngx_http_upstream_t   *u;    ngx_http_proxy_ctx_t  *p;    p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);    if (p == NULL) {        return NGX_HTTP_INTERNAL_SERVER_ERROR;    }    rc = ngx_http_proxy_parse_status_line(r, p);    if (rc == NGX_AGAIN) {        return rc;    }    u = r->upstream;    if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) {        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                      "upstream sent no valid HTTP/1.0 header");#if 0        if (u->accel) {            return NGX_HTTP_UPSTREAM_INVALID_HEADER;        }#endif        r->http_version = NGX_HTTP_VERSION_9;        u->headers_in.status_n = NGX_HTTP_OK;        u->state->status = NGX_HTTP_OK;        return NGX_OK;    }    u->headers_in.status_n = p->status;    u->state->status = p->status;    u->headers_in.status_line.len = p->status_end - p->status_start;    u->headers_in.status_line.data = ngx_palloc(r->pool,                                                u->headers_in.status_line.len);    if (u->headers_in.status_line.data == NULL) {        return NGX_HTTP_INTERNAL_SERVER_ERROR;    }    ngx_memcpy(u->headers_in.status_line.data, p->status_start,               u->headers_in.status_line.len);    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "http proxy status %ui \"%V\"",                   u->headers_in.status_n, &u->headers_in.status_line);    u->process_header = ngx_http_proxy_process_header;    return ngx_http_proxy_process_header(r);}static ngx_int_tngx_http_proxy_parse_status_line(ngx_http_request_t *r, ngx_http_proxy_ctx_t *p){    u_char                ch;    u_char               *pos;

⌨️ 快捷键说明

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