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

📄 ngx_http_core_module.c

📁 Nginx是一个高性能的HTTP和反向代理服务器
💻 C
📖 第 1 页 / 共 5 页
字号:
    ngx_http_finalize_request(r, rc);    return NGX_OK;}ngx_int_tngx_http_core_post_access_phase(ngx_http_request_t *r,    ngx_http_phase_handler_t *ph){    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "post access phase: %ui", r->phase_handler);    if (r->access_code) {        if (r->access_code == NGX_HTTP_FORBIDDEN) {            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                          "access forbidden by rule");        }        ngx_http_finalize_request(r, r->access_code);        return NGX_OK;    }    r->phase_handler++;    return NGX_AGAIN;}ngx_int_tngx_http_core_content_phase(ngx_http_request_t *r,    ngx_http_phase_handler_t *ph){    size_t     root;    ngx_int_t  rc;    ngx_str_t  path;    if (r->content_handler) {        r->write_event_handler = ngx_http_request_empty_handler;        ngx_http_finalize_request(r, r->content_handler(r));        return NGX_OK;    }    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "content phase: %ui", r->phase_handler);    rc = ph->handler(r);    if (rc == NGX_DONE) {        return NGX_OK;    }    if (rc != NGX_DECLINED) {        ngx_http_finalize_request(r, rc);        return NGX_OK;    }    /* rc == NGX_DECLINED */    ph++;    if (ph->checker) {        r->phase_handler++;        return NGX_AGAIN;    }    /* no content handler was found */    if (r->uri.data[r->uri.len - 1] == '/' && !r->zero_in_uri) {        if (ngx_http_map_uri_to_path(r, &path, &root, 0) != NULL) {            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,                          "directory index of \"%s\" is forbidden", path.data);        }        ngx_http_finalize_request(r, NGX_HTTP_FORBIDDEN);        return NGX_OK;    }    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no handler found");    ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);    return NGX_OK;}voidngx_http_update_location_config(ngx_http_request_t *r){    ngx_http_core_loc_conf_t  *clcf;    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);    if (r->method & clcf->limit_except) {        r->loc_conf = clcf->limit_except_loc_conf;        clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);    }    if (r == r->main) {        r->connection->log->file = clcf->err_log->file;        if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {            r->connection->log->log_level = clcf->err_log->log_level;        }    }    if ((ngx_io.flags & NGX_IO_SENDFILE) && clcf->sendfile) {        r->connection->sendfile = 1;    } else {        r->connection->sendfile = 0;    }    if (clcf->client_body_in_file_only) {        r->request_body_in_file_only = 1;        r->request_body_in_persistent_file = 1;        r->request_body_in_clean_file =            clcf->client_body_in_file_only == NGX_HTTP_REQUEST_BODY_FILE_CLEAN;        r->request_body_file_log_level = NGX_LOG_NOTICE;    } else {        r->request_body_file_log_level = NGX_LOG_WARN;    }    if (r->keepalive && clcf->keepalive_timeout == 0) {        r->keepalive = 0;    }    if (!clcf->tcp_nopush) {        /* disable TCP_NOPUSH/TCP_CORK use */        r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;    }    if (r->limit_rate == 0) {        r->limit_rate = clcf->limit_rate;    }    if (clcf->handler) {        r->content_handler = clcf->handler;    }}static ngx_int_tngx_http_core_find_location(ngx_http_request_t *r,    ngx_array_t *locations, ngx_uint_t regex_start, size_t len){    ngx_int_t                  n, rc;    ngx_uint_t                 i, found;    ngx_http_core_loc_conf_t  *clcf, **clcfp;#if (NGX_PCRE)    ngx_uint_t                 noregex;#endif    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "find location for \"%V\"", &r->uri);    found = 0;#if (NGX_PCRE)    noregex = 0;#endif    clcfp = locations->elts;    for (i = 0; i < locations->nelts; i++) {        if (clcfp[i]->noname#if (NGX_PCRE)            || clcfp[i]->regex#endif            || clcfp[i]->named)        {            break;        }        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                       "find location: %s\"%V\"",                       clcfp[i]->exact_match ? "= " : "", &clcfp[i]->name);        if (clcfp[i]->auto_redirect            && r->uri.len == clcfp[i]->name.len - 1            && ngx_strncmp(r->uri.data, clcfp[i]->name.data,                           clcfp[i]->name.len - 1)                == 0)        {            /* the locations are lexicographically sorted */            r->loc_conf = clcfp[i]->loc_conf;            return NGX_HTTP_LOCATION_AUTO_REDIRECT;        }        if (r->uri.len < clcfp[i]->name.len) {            continue;        }        n = ngx_strncmp(r->uri.data, clcfp[i]->name.data, clcfp[i]->name.len);        if (n < 0) {            /* the locations are lexicographically sorted */            break;        }        if (n == 0) {            if (clcfp[i]->exact_match) {                if (r->uri.len == clcfp[i]->name.len) {                    r->loc_conf = clcfp[i]->loc_conf;                    return NGX_HTTP_LOCATION_EXACT;                }                continue;            }            if (len > clcfp[i]->name.len) {                /* the previous match is longer */                break;            }            found = 1;            r->loc_conf = clcfp[i]->loc_conf;#if (NGX_PCRE)            noregex = clcfp[i]->noregex;#endif        }    }    if (found) {        clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);        if (clcf->locations) {            rc = ngx_http_core_find_location(r, clcf->locations,                                             clcf->regex_start, len);            if (rc != NGX_OK) {                return rc;            }        }    }#if (NGX_PCRE)    if (noregex) {        return NGX_HTTP_LOCATION_NOREGEX;    }    /* regex matches */    for (i = regex_start; i < locations->nelts; i++) {        if (!clcfp[i]->regex) {            break;        }        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                       "find location: ~ \"%V\"", &clcfp[i]->name);        n = ngx_regex_exec(clcfp[i]->regex, &r->uri, NULL, 0);        if (n == NGX_REGEX_NO_MATCHED) {            continue;        }        if (n < 0) {            ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,                          ngx_regex_exec_n                          " failed: %d on \"%V\" using \"%V\"",                          n, &r->uri, &clcfp[i]->name);            return NGX_HTTP_INTERNAL_SERVER_ERROR;        }        /* match */        r->loc_conf = clcfp[i]->loc_conf;        return NGX_HTTP_LOCATION_REGEX;    }#endif /* NGX_PCRE */    return NGX_OK;}ngx_int_tngx_http_set_content_type(ngx_http_request_t *r){    u_char                     c, *p, *exten;    ngx_str_t                 *type;    ngx_uint_t                 i, hash;    ngx_http_core_loc_conf_t  *clcf;    if (r->headers_out.content_type.len) {        return NGX_OK;    }    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);    if (r->exten.len) {        hash = 0;        for (i = 0; i < r->exten.len; i++) {            c = r->exten.data[i];            if (c >= 'A' && c <= 'Z') {                p = ngx_palloc(r->pool, r->exten.len);                if (p == NULL) {                    return NGX_HTTP_INTERNAL_SERVER_ERROR;                }                hash = 0;                exten = p;                for (i = 0; i < r->exten.len; i++) {                    c = ngx_tolower(r->exten.data[i]);                    hash = ngx_hash(hash, c);                    *p++ = c;                }                r->exten.data = exten;                break;            }            hash = ngx_hash(hash, c);        }        type = ngx_hash_find(&clcf->types_hash, hash,                             r->exten.data, r->exten.len);        if (type) {            r->headers_out.content_type_len = type->len;            r->headers_out.content_type = *type;            return NGX_OK;        }    }    r->headers_out.content_type_len = clcf->default_type.len;    r->headers_out.content_type = clcf->default_type;    return NGX_OK;}ngx_int_tngx_http_set_exten(ngx_http_request_t *r){    ngx_int_t  i;    r->exten.len = 0;    r->exten.data = NULL;    for (i = r->uri.len - 1; i > 1; i--) {        if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {            r->exten.len = r->uri.len - i - 1;            r->exten.data = &r->uri.data[i + 1];            break;        } else if (r->uri.data[i] == '/') {            break;        }    }    return NGX_OK;}ngx_int_tngx_http_send_header(ngx_http_request_t *r){    if (r->err_status) {        r->headers_out.status = r->err_status;        r->headers_out.status_line.len = 0;    }    return ngx_http_top_header_filter(r);}ngx_int_tngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in){    ngx_int_t  rc;    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "http output filter \"%V?%V\"", &r->uri, &r->args);    rc = ngx_http_top_body_filter(r, in);    if (rc == NGX_ERROR) {        /* NGX_ERROR may be returned by any filter */        r->connection->error = 1;    }    return rc;}u_char *ngx_http_map_uri_to_path(ngx_http_request_t *r, ngx_str_t *path,    size_t *root_length, size_t reserved){    u_char                    *last;    size_t                     alias;    ngx_http_core_loc_conf_t  *clcf;    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);    alias = clcf->alias ? clcf->name.len : 0;    if (alias && !r->valid_location) {        ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,                      "\"alias\" could not be used in location \"%V\" "                      "where URI was rewritten", &clcf->name);        return NULL;    }    reserved += r->uri.len - alias + 1;    if (clcf->root_lengths == NULL) {        *root_length = clcf->root.len;        path->len = clcf->root.len + reserved;        path->data = ngx_palloc(r->pool, path->len);        if (path->data == NULL) {            return NULL;        }        last = ngx_copy(path->data, clcf->root.data, clcf->root.len);    } else {        if (ngx_http_script_run(r, path, clcf->root_lengths->elts, reserved,                                clcf->root_values->elts)            == NULL)        {            return NULL;        }        if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, path, 0)== NGX_ERROR)        {            return NULL;        }        *root_length = path->len - reserved;        last = path->data + *root_length;    }    last = ngx_cpystrn(last, r->uri.data + alias, r->uri.len - alias + 1);    return last;}ngx_int_tngx_http_auth_basic_user(ngx_http_request_t *r){    ngx_str_t   auth, encoded;    ngx_uint_t  len;    if (r->headers_in.user.len == 0 && r->headers_in.user.data != NULL) {        return NGX_DECLINED;    }    if (r->headers_in.authorization == NULL) {        r->headers_in.user.data = (u_char *) "";        return NGX_DECLINED;    }    encoded = r->headers_in.authorization->value;    if (encoded.len < sizeof("Basic ") - 1        || ngx_strncasecmp(encoded.data, (u_char *) "Basic ",                           sizeof("Basic ") - 1)           != 0)    {        r->headers_in.user.data = (u_char *) "";        return NGX_DECLINED;    }

⌨️ 快捷键说明

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