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

📄 ngx_http_fastcgi_module.c

📁 nginx 反向代理0.7.1版本 用于实现反向代理
💻 C
📖 第 1 页 / 共 5 页
字号:
    u->create_request = ngx_http_fastcgi_create_request;    u->reinit_request = ngx_http_fastcgi_reinit_request;    u->process_header = ngx_http_fastcgi_process_header;    u->abort_request = ngx_http_fastcgi_abort_request;    u->finalize_request = ngx_http_fastcgi_finalize_request;    u->buffering = 1;    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_http_fastcgi_input_filter;    u->pipe->input_ctx = r;    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_fastcgi_create_request(ngx_http_request_t *r){    off_t                         file_pos;    u_char                        ch, *pos;    size_t                        size, len, key_len, val_len, padding;    ngx_uint_t                    i, n, next;    ngx_buf_t                    *b;    ngx_chain_t                  *cl, *body;    ngx_list_part_t              *part;    ngx_table_elt_t              *header;    ngx_http_script_code_pt       code;    ngx_http_script_engine_t      e, le;    ngx_http_fastcgi_header_t    *h;    ngx_http_fastcgi_loc_conf_t  *flcf;    ngx_http_script_len_code_pt   lcode;    len = 0;    flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);    if (flcf->params_len) {        ngx_memzero(&le, sizeof(ngx_http_script_engine_t));        ngx_http_script_flush_no_cacheable_variables(r, flcf->flushes);        le.flushed = 1;        le.ip = flcf->params_len->elts;        le.request = r;        while (*(uintptr_t *) le.ip) {            lcode = *(ngx_http_script_len_code_pt *) le.ip;            key_len = lcode(&le);            for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {                lcode = *(ngx_http_script_len_code_pt *) le.ip;            }            le.ip += sizeof(uintptr_t);            len += 1 + key_len + ((val_len > 127) ? 4 : 1) + val_len;        }    }    if (flcf->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;            }            len += ((sizeof("HTTP_") - 1 + header[i].key.len > 127) ? 4 : 1)                + ((header[i].value.len > 127) ? 4 : 1)                + sizeof("HTTP_") - 1 + header[i].key.len + header[i].value.len;        }    }    if (len > 65535) {        ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,                      "fastcgi request record is too big: %uz", len);        return NGX_ERROR;    }    padding = 8 - len % 8;    padding = (padding == 8) ? 0 : padding;    size = sizeof(ngx_http_fastcgi_header_t)           + sizeof(ngx_http_fastcgi_begin_request_t)           + sizeof(ngx_http_fastcgi_header_t)  /* NGX_HTTP_FASTCGI_PARAMS */           + len + padding           + sizeof(ngx_http_fastcgi_header_t)  /* NGX_HTTP_FASTCGI_PARAMS */           + sizeof(ngx_http_fastcgi_header_t); /* NGX_HTTP_FASTCGI_STDIN */    b = ngx_create_temp_buf(r->pool, size);    if (b == NULL) {        return NGX_ERROR;    }    cl = ngx_alloc_chain_link(r->pool);    if (cl == NULL) {        return NGX_ERROR;    }    cl->buf = b;    ngx_memcpy(b->pos, &ngx_http_fastcgi_request_start,               sizeof(ngx_http_fastcgi_request_start_t));    h = (ngx_http_fastcgi_header_t *)             (b->pos + sizeof(ngx_http_fastcgi_header_t)                     + sizeof(ngx_http_fastcgi_begin_request_t));    h->content_length_hi = (u_char) ((len >> 8) & 0xff);    h->content_length_lo = (u_char) (len & 0xff);    h->padding_length = (u_char) padding;    h->reserved = 0;    b->last = b->pos + sizeof(ngx_http_fastcgi_header_t)                     + sizeof(ngx_http_fastcgi_begin_request_t)                     + sizeof(ngx_http_fastcgi_header_t);    if (flcf->params_len) {        ngx_memzero(&e, sizeof(ngx_http_script_engine_t));        e.ip = flcf->params->elts;        e.pos = b->last;        e.request = r;        e.flushed = 1;        le.ip = flcf->params_len->elts;        while (*(uintptr_t *) le.ip) {            lcode = *(ngx_http_script_len_code_pt *) le.ip;            key_len = (u_char) lcode(&le);            for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {                lcode = *(ngx_http_script_len_code_pt *) le.ip;            }            le.ip += sizeof(uintptr_t);            *e.pos++ = (u_char) key_len;            if (val_len > 127) {                *e.pos++ = (u_char) (((val_len >> 24) & 0x7f) | 0x80);                *e.pos++ = (u_char) ((val_len >> 16) & 0xff);                *e.pos++ = (u_char) ((val_len >> 8) & 0xff);                *e.pos++ = (u_char) (val_len & 0xff);            } else {                *e.pos++ = (u_char) val_len;            }            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);            ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                           "fastcgi param: \"%*s: %*s\"",                           key_len, e.pos - (key_len + val_len),                           val_len, e.pos - val_len);        }        b->last = e.pos;    }    if (flcf->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;            }            len = sizeof("HTTP_") - 1 + header[i].key.len;            if (len > 127) {                *b->last++ = (u_char) (((len >> 24) & 0x7f) | 0x80);                *b->last++ = (u_char) ((len >> 16) & 0xff);                *b->last++ = (u_char) ((len >> 8) & 0xff);                *b->last++ = (u_char) (len & 0xff);            } else {                *b->last++ = (u_char) len;            }            len = header[i].value.len;            if (len > 127) {                *b->last++ = (u_char) (((len >> 24) & 0x7f) | 0x80);                *b->last++ = (u_char) ((len >> 16) & 0xff);                *b->last++ = (u_char) ((len >> 8) & 0xff);                *b->last++ = (u_char) (len & 0xff);            } else {                *b->last++ = (u_char) len;            }            b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1);            for (n = 0; n < header[i].key.len; n++) {                ch = header[i].key.data[n];                if (ch >= 'a' && ch <= 'z') {                    ch &= ~0x20;                } else if (ch == '-') {                    ch = '_';                }                *b->last++ = ch;            }            b->last = ngx_copy(b->last, header[i].value.data,                               header[i].value.len);        }    }    if (padding) {        ngx_memzero(b->last, padding);        b->last += padding;    }    h = (ngx_http_fastcgi_header_t *) b->last;    b->last += sizeof(ngx_http_fastcgi_header_t);    h->version = 1;    h->type = NGX_HTTP_FASTCGI_PARAMS;    h->request_id_hi = 0;    h->request_id_lo = 1;    h->content_length_hi = 0;    h->content_length_lo = 0;    h->padding_length = 0;    h->reserved = 0;    h = (ngx_http_fastcgi_header_t *) b->last;    b->last += sizeof(ngx_http_fastcgi_header_t);    if (flcf->upstream.pass_request_body) {        body = r->upstream->request_bufs;        r->upstream->request_bufs = cl;#if (NGX_SUPPRESS_WARN)        file_pos = 0;        pos = NULL;#endif        while (body) {            if (body->buf->in_file) {                file_pos = body->buf->file_pos;            } else {                pos = body->buf->pos;            }            next = 0;            do {                b = ngx_alloc_buf(r->pool);                if (b == NULL) {                    return NGX_ERROR;                }                ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));                if (body->buf->in_file) {                    b->file_pos = file_pos;                    file_pos += 32 * 1024;                    if (file_pos >= body->buf->file_last) {                        file_pos = body->buf->file_last;                        next = 1;                    }                    b->file_last = file_pos;                    len = (ngx_uint_t) (file_pos - b->file_pos);                } else {                    b->pos = pos;                    pos += 32 * 1024;                    if (pos >= body->buf->last) {                        pos = body->buf->last;                        next = 1;                    }                    b->last = pos;                    len = (ngx_uint_t) (pos - b->pos);                }                padding = 8 - len % 8;                padding = (padding == 8) ? 0 : padding;                h->version = 1;                h->type = NGX_HTTP_FASTCGI_STDIN;                h->request_id_hi = 0;                h->request_id_lo = 1;                h->content_length_hi = (u_char) ((len >> 8) & 0xff);                h->content_length_lo = (u_char) (len & 0xff);                h->padding_length = (u_char) padding;                h->reserved = 0;                cl->next = ngx_alloc_chain_link(r->pool);                if (cl->next == NULL) {                    return NGX_ERROR;                }                cl = cl->next;                cl->buf = b;                b = ngx_create_temp_buf(r->pool,                                        sizeof(ngx_http_fastcgi_header_t)                                        + padding);                if (b == NULL) {                    return NGX_ERROR;                }                if (padding) {                    ngx_memzero(b->last, padding);                    b->last += padding;                }                h = (ngx_http_fastcgi_header_t *) b->last;                b->last += sizeof(ngx_http_fastcgi_header_t);                cl->next = ngx_alloc_chain_link(r->pool);                if (cl->next == NULL) {                    return NGX_ERROR;                }                cl = cl->next;                cl->buf = b;            } while (!next);            body = body->next;        }    } else {        r->upstream->request_bufs = cl;    }    h->version = 1;    h->type = NGX_HTTP_FASTCGI_STDIN;    h->request_id_hi = 0;    h->request_id_lo = 1;    h->content_length_hi = 0;    h->content_length_lo = 0;    h->padding_length = 0;    h->reserved = 0;    cl->next = NULL;    return NGX_OK;}static ngx_int_tngx_http_fastcgi_reinit_request(ngx_http_request_t *r){    ngx_http_fastcgi_ctx_t  *f;    f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);    if (f == NULL) {        return NGX_OK;    }    f->state = ngx_http_fastcgi_st_version;    f->fastcgi_stdout = 0;    return NGX_OK;}static ngx_int_tngx_http_fastcgi_process_header(ngx_http_request_t *r){    u_char                         *p, *start, *last, *part_start;    size_t                          size;    ngx_str_t                      *status_line, line, *pattern;    ngx_int_t                       rc, status;    ngx_buf_t                       buf;    ngx_uint_t                      i;    ngx_table_elt_t                *h;    ngx_http_upstream_t            *u;    ngx_http_fastcgi_ctx_t         *f;    ngx_http_upstream_header_t     *hh;    ngx_http_fastcgi_loc_conf_t    *flcf;    ngx_http_fastcgi_split_part_t  *part;    ngx_http_upstream_main_conf_t  *umcf;    f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);    umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);

⌨️ 快捷键说明

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