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

📄 ngx_http_core_module.c

📁 Nginx是一个高性能的HTTP和反向代理服务器
💻 C
📖 第 1 页 / 共 5 页
字号:
    encoded.len -= sizeof("Basic ") - 1;    encoded.data += sizeof("Basic ") - 1;    while (encoded.len && encoded.data[0] == ' ') {        encoded.len--;        encoded.data++;    }    if (encoded.len == 0) {        r->headers_in.user.data = (u_char *) "";        return NGX_DECLINED;    }    auth.len = ngx_base64_decoded_length(encoded.len);    auth.data = ngx_palloc(r->pool, auth.len + 1);    if (auth.data == NULL) {        return NGX_ERROR;    }    if (ngx_decode_base64(&auth, &encoded) != NGX_OK) {        r->headers_in.user.data = (u_char *) "";        return NGX_DECLINED;    }    auth.data[auth.len] = '\0';    for (len = 0; len < auth.len; len++) {        if (auth.data[len] == ':') {            break;        }    }    if (len == 0 || len == auth.len) {        r->headers_in.user.data = (u_char *) "";        return NGX_DECLINED;    }    r->headers_in.user.len = len;    r->headers_in.user.data = auth.data;    r->headers_in.passwd.len = auth.len - len - 1;    r->headers_in.passwd.data = &auth.data[len + 1];    return NGX_OK;}ngx_int_tngx_http_server_addr(ngx_http_request_t *r, ngx_str_t *s){    socklen_t            len;    ngx_connection_t    *c;    struct sockaddr_in   sin;    /* AF_INET only */    c = r->connection;    if (r->in_addr == 0) {        len = sizeof(struct sockaddr_in);        if (getsockname(c->fd, (struct sockaddr *) &sin, &len) == -1) {            ngx_connection_error(c, ngx_socket_errno, "getsockname() failed");            return NGX_ERROR;        }        r->in_addr = sin.sin_addr.s_addr;    }    if (s == NULL) {        return NGX_OK;    }    s->len = ngx_inet_ntop(c->listening->family, &r->in_addr,                           s->data, INET_ADDRSTRLEN);    return NGX_OK;}#if (NGX_HTTP_GZIP)ngx_int_tngx_http_gzip_ok(ngx_http_request_t *r){    time_t                     date, expires;    ngx_uint_t                 p;    ngx_array_t               *cc;    ngx_table_elt_t           *e, *d;    ngx_http_core_loc_conf_t  *clcf;    if (r->gzip == 1) {        return NGX_OK;    }    if (r->gzip == 2) {        return NGX_DECLINED;    }    r->gzip = 2;    if (r != r->main        || r->headers_in.accept_encoding == NULL        || ngx_strcasestrn(r->headers_in.accept_encoding->value.data,                           "gzip", 4 - 1)           == NULL        /*         * if the URL (without the "http://" prefix) is longer than 253 bytes,         * then MSIE 4.x can not handle the compressed stream - it waits         * too long, hangs up or crashes         */        || (r->headers_in.msie4 && r->unparsed_uri.len > 200))    {        return NGX_DECLINED;    }    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);    if (r->http_version < clcf->gzip_http_version) {        return NGX_DECLINED;    }    if (r->headers_in.via == NULL) {        goto ok;    }    p = clcf->gzip_proxied;    if (p & NGX_HTTP_GZIP_PROXIED_OFF) {        return NGX_DECLINED;    }    if (p & NGX_HTTP_GZIP_PROXIED_ANY) {        goto ok;    }    if (r->headers_in.authorization && (p & NGX_HTTP_GZIP_PROXIED_AUTH)) {        goto ok;    }    e = r->headers_out.expires;    if (e) {        if (!(p & NGX_HTTP_GZIP_PROXIED_EXPIRED)) {            return NGX_DECLINED;        }        expires = ngx_http_parse_time(e->value.data, e->value.len);        if (expires == NGX_ERROR) {            return NGX_DECLINED;        }        d = r->headers_out.date;        if (d) {            date = ngx_http_parse_time(d->value.data, d->value.len);            if (date == NGX_ERROR) {                return NGX_DECLINED;            }        } else {            date = ngx_time();        }        if (expires < date) {            goto ok;        }        return NGX_DECLINED;    }    cc = &r->headers_out.cache_control;    if (cc->elts) {        if ((p & NGX_HTTP_GZIP_PROXIED_NO_CACHE)            && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_cache,                                                 NULL)               >= 0)        {            goto ok;        }        if ((p & NGX_HTTP_GZIP_PROXIED_NO_STORE)            && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_store,                                                 NULL)               >= 0)        {            goto ok;        }        if ((p & NGX_HTTP_GZIP_PROXIED_PRIVATE)            && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_private,                                                 NULL)               >= 0)        {            goto ok;        }        return NGX_DECLINED;    }    if ((p & NGX_HTTP_GZIP_PROXIED_NO_LM) && r->headers_out.last_modified) {        return NGX_DECLINED;    }    if ((p & NGX_HTTP_GZIP_PROXIED_NO_ETAG) && r->headers_out.etag) {        return NGX_DECLINED;    }ok:#if (NGX_PCRE)    if (clcf->gzip_disable && r->headers_in.user_agent) {        if (ngx_regex_exec_array(clcf->gzip_disable,                                 &r->headers_in.user_agent->value,                                 r->connection->log)            != NGX_DECLINED)        {            return NGX_DECLINED;        }    }#endif    r->gzip = 1;    return NGX_OK;}#endifngx_int_tngx_http_subrequest(ngx_http_request_t *r,    ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **psr,    ngx_http_post_subrequest_t *ps, ngx_uint_t flags){    ngx_connection_t              *c;    ngx_http_request_t            *sr;    ngx_http_log_ctx_t            *ctx;    ngx_http_core_srv_conf_t      *cscf;    ngx_http_postponed_request_t  *pr, *p;    r->main->subrequests--;    if (r->main->subrequests == 0) {        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                      "subrequests cycle while processing \"%V\"", uri);        r->main->subrequests = 1;        return NGX_ERROR;    }    sr = ngx_pcalloc(r->pool, sizeof(ngx_http_request_t));    if (sr == NULL) {        return NGX_ERROR;    }    sr->signature = NGX_HTTP_MODULE;    c = r->connection;    sr->connection = c;    sr->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);    if (sr->ctx == NULL) {        return NGX_ERROR;    }    if (ngx_list_init(&sr->headers_out.headers, r->pool, 20,                      sizeof(ngx_table_elt_t))        == NGX_ERROR)    {        return NGX_ERROR;    }    cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);    sr->main_conf = cscf->ctx->main_conf;    sr->srv_conf = cscf->ctx->srv_conf;    sr->loc_conf = cscf->ctx->loc_conf;    sr->pool = r->pool;    sr->headers_in = r->headers_in;    ngx_http_clear_content_length(sr);    ngx_http_clear_accept_ranges(sr);    ngx_http_clear_last_modified(sr);    sr->request_body = r->request_body;    sr->method = NGX_HTTP_GET;    sr->http_version = r->http_version;    sr->request_line = r->request_line;    sr->uri = *uri;    if (args) {        sr->args = *args;    }    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,                   "http subrequest \"%V?%V\"", uri, &sr->args);    sr->zero_in_uri = (flags & NGX_HTTP_ZERO_IN_URI) != 0;    sr->subrequest_in_memory = (flags & NGX_HTTP_SUBREQUEST_IN_MEMORY) != 0;    sr->unparsed_uri = r->unparsed_uri;    sr->method_name = ngx_http_core_get_method;    sr->http_protocol = r->http_protocol;    if (ngx_http_set_exten(sr) != NGX_OK) {        return NGX_ERROR;    }    sr->main = r->main;    sr->parent = r;    sr->post_subrequest = ps;    sr->read_event_handler = ngx_http_request_empty_handler;    sr->write_event_handler = ngx_http_request_empty_handler;    if (c->data == r) {        c->data = sr;    }    sr->in_addr = r->in_addr;    sr->port = r->port;    sr->port_text = r->port_text;    sr->variables = r->variables;    sr->log_handler = r->log_handler;    pr = ngx_palloc(r->pool, sizeof(ngx_http_postponed_request_t));    if (pr == NULL) {        return NGX_ERROR;    }    pr->request = sr;    pr->out = NULL;    pr->next = NULL;    if (r->postponed) {        for (p = r->postponed; p->next; p = p->next) { /* void */ }        p->next = pr;    } else {        r->postponed = pr;    }    ctx = c->log->data;    ctx->current_request = sr;    sr->internal = 1;    sr->fast_subrequest = 1;    sr->discard_body = r->discard_body;    sr->main_filter_need_in_memory = r->main_filter_need_in_memory;    sr->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;    ngx_http_handler(sr);    if (!c->destroyed) {        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,                       "http subrequest done \"%V?%V\"", uri, &sr->args);        r->main->subrequests++;        *psr = sr;        if (sr->fast_subrequest) {            sr->fast_subrequest = 0;            if (sr->done) {                return NGX_OK;            }        }        return NGX_AGAIN;    }    return NGX_DONE;}ngx_int_tngx_http_internal_redirect(ngx_http_request_t *r,    ngx_str_t *uri, ngx_str_t *args){    ngx_http_core_srv_conf_t  *cscf;    r->uri_changes--;    if (r->uri_changes == 0) {        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                      "rewrite or internal redirection cycle "                      "while internal redirect to \"%V\"", uri);        ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);        return NGX_DONE;    }    r->uri = *uri;    if (args) {        r->args = *args;    } else {        r->args.len = 0;        r->args.data = NULL;    }    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "internal redirect: \"%V?%V\"", uri, &r->args);    if (ngx_http_set_exten(r) != NGX_OK) {        ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);        return NGX_DONE;    }    /* clear the modules contexts */    ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);    cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);    r->loc_conf = cscf->ctx->loc_conf;    ngx_http_update_location_config(r);    r->internal = 1;    ngx_http_handler(r);    return NGX_DONE;}ngx_int_tngx_http_named_location(ngx_http_request_t *r, ngx_str_t *name){    ngx_uint_t                   i;    ngx_http_core_srv_conf_t    *cscf;    ngx_http_core_loc_conf_t   **clcfp;    ngx_http_core_main_conf_t   *cmcf;    cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);    clcfp = cscf->locations.elts;    for (i = cscf->named_start; i < cscf->locations.nelts; i++) {        if (name->len != clcfp[i]->name.len            || ngx_strncmp(name->data, clcfp[i]->name.data, name->len) != 0)        {            continue;        }        ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                       "named location: %V \"%V?%V\"", name, &r->uri, &r->args);        r->internal = 1;        r->content_handler = NULL;        r->loc_conf = clcfp[i]->loc_conf;        ngx_http_update_location_config(r);        cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);        r->phase_handler = cmcf->phase_engine.location_rewrite_index;        ngx_http_core_run_phases(r);        return NGX_DONE;    }    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                  "could not find named location \"%V\"", name);    ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);    return NGX_DONE;}

⌨️ 快捷键说明

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