int main(){ //数字对齐方法ngx_align用到了/os/unix/ngx_alloc.c中ngx_cacheline_size的值 //如果不启动nginx则该值为0,这时ngx_align就一直返回为0,所以我们这给他一个默认值 ngx_cacheline_size = 32; //用于构造带通配符的散列表 ngx_hash_keys_arrays_t ha; ngx_pool_t *pool = ngx_create_pool(2024, NULL); ha.temp_pool = pool; ha.pool = pool; if(ngx_hash_keys_array_init(&ha,NGX_HASH_LARGE) != NGX_OK){ printf("初始化失败\n"); return 0; } //添加元素 *.jd.com ngx_str_t *key = ngx_pcalloc(pool, sizeof(ngx_str_t)); key->len = ngx_strlen("*.com"); key->data = ngx_pcalloc(pool, ngx_strlen("*.com")); ngx_memcpy(key->data, "*.com", ngx_strlen("*.com")); int *value = ngx_pcalloc(pool, sizeof(int)); *value = 1234; ngx_int_t rc; rc = ngx_hash_add_key(&ha, key, value, NGX_HASH_WILDCARD_KEY); if(rc != NGX_OK){ printf("添加key错误\n"); } //构造通配符散列表 ngx_hash_init_t hash; hash.hash = NULL; hash.key = ngx_hash_key; hash.max_size = 10; hash.bucket_size = 100; hash.pool = pool; hash.temp_pool = pool; //初始化前置通配符散列表 rc = ngx_hash_wildcard_init(&hash, ha.dns_wc_head.elts, ha.dns_wc_head.nelts); if(rc != NGX_OK){ printf("初始化通配符散列表错误\n"); } // *.com和 as.dd.com a.b.c.com都匹配 void *result = ngx_hash_find_wc_head((ngx_hash_wildcard_t *)hash.hash, "as.dd.com",ngx_strlen("as.dd.com")); int *bb = (int *)result; printf("%d\n",*bb); }
ngx_int_t ngx_http_variables_add_core_vars(ngx_conf_t *cf) {//ngx_http_core_preconfiguration函数在程序解析配置之前就调用这里 //将ngx_http_core_variables 的变量全部导入到variables_keys里面,其里面会有各个handler来帮助存取变量值。 ngx_int_t rc; ngx_http_variable_t *v; ngx_http_core_main_conf_t *cmcf; cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); cmcf->variables_keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t)); if (cmcf->variables_keys == NULL) { return NGX_ERROR; } cmcf->variables_keys->pool = cf->pool; cmcf->variables_keys->temp_pool = cf->pool; if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL) != NGX_OK) { return NGX_ERROR; } //循环将这些变量加入到哈希表中。用户自定义的是不会存在这里面的,因为太多了。 for (v = ngx_http_core_variables; v->name.len; v++) { //将著名的头部字段加入variables_keys,注意没有加入variables。为了效率。 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, NGX_HASH_READONLY_KEY); if (rc == NGX_OK) { continue; } if (rc == NGX_BUSY) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "conflicting variable name \"%V\"", &v->name); } return NGX_ERROR; } return NGX_OK; }
void* add_urls_to_array(ngx_pool_t *pool, ngx_hash_keys_arrays_t *ha, ngx_array_t *url, ngx_array_t *value) { ngx_uint_t loop; ngx_int_t rc; ngx_str_t *strUrl, *strValue; memset(ha, 0, sizeof(ngx_hash_keys_arrays_t)); ha->temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, &ngx_log); ha->pool = pool; if (ngx_hash_keys_array_init(ha, NGX_HASH_LARGE) != NGX_OK) { goto failed; } strUrl = url->elts; strValue = value->elts; for (loop = 0; loop < url->nelts; loop++) { rc = ngx_hash_add_key(ha, &strUrl[loop], &strValue[loop], NGX_HASH_WILDCARD_KEY); if (rc == NGX_ERROR) { goto failed; } } return ha; failed: ngx_destroy_pool(ha->temp_pool); return NULL; }
ngx_int_t ngx_stream_variables_add_core_vars(ngx_conf_t *cf) { ngx_int_t rc; ngx_stream_variable_t *cv, *v; ngx_stream_core_main_conf_t *cmcf; cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module); cmcf->variables_keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t)); if (cmcf->variables_keys == NULL) { return NGX_ERROR; } cmcf->variables_keys->pool = cf->pool; cmcf->variables_keys->temp_pool = cf->pool; if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL) != NGX_OK) { return NGX_ERROR; } for (cv = ngx_stream_core_variables; cv->name.len; cv++) { v = ngx_palloc(cf->pool, sizeof(ngx_stream_variable_t)); if (v == NULL) { return NGX_ERROR; } *v = *cv; rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, NGX_HASH_READONLY_KEY); if (rc == NGX_OK) { continue; } if (rc == NGX_BUSY) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "conflicting variable name \"%V\"", &v->name); } return NGX_ERROR; } return NGX_OK; }
ngx_int_t ngx_stream_variables_add_core_vars(ngx_conf_t *cf) { ngx_stream_variable_t *cv, *v; ngx_stream_core_main_conf_t *cmcf; cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module); cmcf->variables_keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t)); if (cmcf->variables_keys == NULL) { return NGX_ERROR; } cmcf->variables_keys->pool = cf->pool; cmcf->variables_keys->temp_pool = cf->pool; if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL) != NGX_OK) { return NGX_ERROR; } if (ngx_array_init(&cmcf->prefix_variables, cf->pool, 8, sizeof(ngx_stream_variable_t)) != NGX_OK) { return NGX_ERROR; } for (cv = ngx_stream_core_variables; cv->name.len; cv++) { v = ngx_stream_add_variable(cf, &cv->name, cv->flags); if (v == NULL) { return NGX_ERROR; } *v = *cv; } return NGX_OK; }
static void *ngx_http_small_light_create_srv_conf(ngx_conf_t *cf) { ngx_http_small_light_conf_t *srv_conf; ngx_pool_t *pool, *temp_pool; srv_conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_small_light_conf_t)); if (srv_conf == NULL) { return NGX_CONF_ERROR; } pool = ngx_create_pool(16384, cf->log); if (pool == NULL) { return NGX_CONF_ERROR; } temp_pool = ngx_create_pool(16384, cf->log); if (temp_pool == NULL) { return NGX_CONF_ERROR; } srv_conf->patterns.keys.pool = pool; srv_conf->patterns.temp_pool = temp_pool; if (ngx_hash_keys_array_init(&srv_conf->patterns, NGX_HASH_SMALL) != NGX_OK) { return NGX_CONF_ERROR; } return srv_conf; }
static char * ngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_map_conf_t *mcf = conf; char *rv; ngx_str_t *value, name; ngx_conf_t save; ngx_pool_t *pool; ngx_hash_init_t hash; ngx_http_map_ctx_t *map; ngx_http_variable_t *var; ngx_http_map_conf_ctx_t ctx; if (mcf->hash_max_size == NGX_CONF_UNSET_UINT) { mcf->hash_max_size = 2048; } if (mcf->hash_bucket_size == NGX_CONF_UNSET_UINT) { mcf->hash_bucket_size = ngx_cacheline_size; } else { mcf->hash_bucket_size = ngx_align(mcf->hash_bucket_size, ngx_cacheline_size); } map = ngx_pcalloc(cf->pool, sizeof(ngx_http_map_ctx_t)); if (map == NULL) { return NGX_CONF_ERROR; } value = cf->args->elts; name = value[1]; name.len--; name.data++; map->index = ngx_http_get_variable_index(cf, &name); if (map->index == NGX_ERROR) { return NGX_CONF_ERROR; } name = value[2]; name.len--; name.data++; var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE); if (var == NULL) { return NGX_CONF_ERROR; } var->get_handler = ngx_http_map_variable; var->data = (uintptr_t) map; pool = ngx_create_pool(16384, cf->log); if (pool == NULL) { return NGX_CONF_ERROR; } ctx.keys.pool = cf->pool; ctx.keys.temp_pool = pool; if (ngx_hash_keys_array_init(&ctx.keys, NGX_HASH_LARGE) != NGX_OK) { ngx_destroy_pool(pool); return NGX_CONF_ERROR; } ctx.values_hash = ngx_pcalloc(pool, sizeof(ngx_array_t) * ctx.keys.hsize); if (ctx.values_hash == NULL) { ngx_destroy_pool(pool); return NGX_CONF_ERROR; } ctx.default_value = NULL; ctx.hostnames = 0; save = *cf; cf->pool = pool; cf->ctx = &ctx; cf->handler = ngx_http_map; cf->handler_conf = conf; rv = ngx_conf_parse(cf, NULL); *cf = save; if (rv != NGX_CONF_OK) { ngx_destroy_pool(pool); return rv; } map->default_value = ctx.default_value ? ctx.default_value: &ngx_http_variable_null_value; hash.key = ngx_hash_key_lc; hash.max_size = mcf->hash_max_size; hash.bucket_size = mcf->hash_bucket_size; hash.name = "map_hash"; hash.pool = cf->pool; if (ctx.keys.keys.nelts) { hash.hash = &map->hash.hash; hash.temp_pool = NULL; if (ngx_hash_init(&hash, ctx.keys.keys.elts, ctx.keys.keys.nelts) != NGX_OK) { ngx_destroy_pool(pool); return NGX_CONF_ERROR; } } if (ctx.keys.dns_wc_head.nelts) { ngx_qsort(ctx.keys.dns_wc_head.elts, (size_t) ctx.keys.dns_wc_head.nelts, sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards); hash.hash = NULL; hash.temp_pool = pool; if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_head.elts, ctx.keys.dns_wc_head.nelts) != NGX_OK) { ngx_destroy_pool(pool); return NGX_CONF_ERROR; } map->hash.wc_head = (ngx_hash_wildcard_t *) hash.hash; } if (ctx.keys.dns_wc_tail.nelts) { ngx_qsort(ctx.keys.dns_wc_tail.elts, (size_t) ctx.keys.dns_wc_tail.nelts, sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards); hash.hash = NULL; hash.temp_pool = pool; if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_tail.elts, ctx.keys.dns_wc_tail.nelts) != NGX_OK) { ngx_destroy_pool(pool); return NGX_CONF_ERROR; } map->hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash; } ngx_destroy_pool(pool); return rv; }
static ngx_int_t ngx_http_server_names(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf, ngx_http_conf_addr_t *addr) { ngx_int_t rc; ngx_uint_t n, s; ngx_hash_init_t hash; ngx_hash_keys_arrays_t ha; ngx_http_server_name_t *name; ngx_http_core_srv_conf_t **cscfp; #if (NGX_PCRE) ngx_uint_t regex, i; regex = 0; #endif ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t)); ha.temp_pool = ngx_create_pool(16384, cf->log); if (ha.temp_pool == NULL) { return NGX_ERROR; } ha.pool = cf->pool; if (ngx_hash_keys_array_init(&ha, NGX_HASH_LARGE) != NGX_OK) { goto failed; } cscfp = addr->servers.elts; for (s = 0; s < addr->servers.nelts; s++) { name = cscfp[s]->server_names.elts; for (n = 0; n < cscfp[s]->server_names.nelts; n++) { #if (NGX_PCRE) if (name[n].regex) { regex++; continue; } #endif rc = ngx_hash_add_key(&ha, &name[n].name, name[n].server, NGX_HASH_WILDCARD_KEY); if (rc == NGX_ERROR) { return NGX_ERROR; } if (rc == NGX_DECLINED) { ngx_log_error(NGX_LOG_EMERG, cf->log, 0, "invalid server name or wildcard \"%V\" on %s", &name[n].name, addr->opt.addr); return NGX_ERROR; } if (rc == NGX_BUSY) { ngx_log_error(NGX_LOG_WARN, cf->log, 0, "conflicting server name \"%V\" on %s, ignored", &name[n].name, addr->opt.addr); } } } hash.key = ngx_hash_key_lc; hash.max_size = cmcf->server_names_hash_max_size; hash.bucket_size = cmcf->server_names_hash_bucket_size; hash.name = "server_names_hash"; hash.pool = cf->pool; if (ha.keys.nelts) { hash.hash = &addr->hash; hash.temp_pool = NULL; if (ngx_hash_init(&hash, ha.keys.elts, ha.keys.nelts) != NGX_OK) { goto failed; } } if (ha.dns_wc_head.nelts) { ngx_qsort(ha.dns_wc_head.elts, (size_t) ha.dns_wc_head.nelts, sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards); hash.hash = NULL; hash.temp_pool = ha.temp_pool; if (ngx_hash_wildcard_init(&hash, ha.dns_wc_head.elts, ha.dns_wc_head.nelts) != NGX_OK) { goto failed; } addr->wc_head = (ngx_hash_wildcard_t *) hash.hash; } if (ha.dns_wc_tail.nelts) { ngx_qsort(ha.dns_wc_tail.elts, (size_t) ha.dns_wc_tail.nelts, sizeof(ngx_hash_key_t), ngx_http_cmp_dns_wildcards); hash.hash = NULL; hash.temp_pool = ha.temp_pool; if (ngx_hash_wildcard_init(&hash, ha.dns_wc_tail.elts, ha.dns_wc_tail.nelts) != NGX_OK) { goto failed; } addr->wc_tail = (ngx_hash_wildcard_t *) hash.hash; } ngx_destroy_pool(ha.temp_pool); #if (NGX_PCRE) if (regex == 0) { return NGX_OK; } addr->nregex = regex; addr->regex = ngx_palloc(cf->pool, regex * sizeof(ngx_http_server_name_t)); if (addr->regex == NULL) { return NGX_ERROR; } i = 0; for (s = 0; s < addr->servers.nelts; s++) { name = cscfp[s]->server_names.elts; for (n = 0; n < cscfp[s]->server_names.nelts; n++) { if (name[n].regex) { addr->regex[i++] = name[n]; } } } #endif return NGX_OK; failed: ngx_destroy_pool(ha.temp_pool); return NGX_ERROR; }
static ngx_int_t ngx_http_small_light_header_filter(ngx_http_request_t *r) { ngx_http_small_light_conf_t *srv_conf; ngx_http_small_light_conf_t *loc_conf; ngx_http_small_light_ctx_t *ctx; ngx_hash_init_t hash_init; ngx_str_t define_pattern; char *converter; if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED || r->headers_out.content_length_n == 0) { return ngx_http_next_header_filter(r); } srv_conf = ngx_http_get_module_srv_conf(r, ngx_http_small_light_module); loc_conf = ngx_http_get_module_loc_conf(r, ngx_http_small_light_module); if (!loc_conf->enable) { return ngx_http_next_header_filter(r); } if (!loc_conf->enable_getparam_mode) { if(ngx_http_small_light_parse_define_pattern(r, &r->unparsed_uri, &define_pattern) != NGX_OK) { return ngx_http_next_header_filter(r); } } ctx = ngx_http_get_module_ctx(r, ngx_http_small_light_module); if (ctx) { ngx_http_set_ctx(r, NULL, ngx_http_small_light_module); return ngx_http_next_header_filter(r); } if ((ctx = ngx_pcalloc(r->pool, sizeof(*ctx))) == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to allocate memory from r->pool %s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } ctx->params.keys.pool = r->pool; ctx->params.temp_pool = r->pool; if (ngx_hash_keys_array_init(&ctx->params, NGX_HASH_SMALL) != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to init hash keys for parameters %s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } if (loc_conf->enable_getparam_mode) { if (ngx_http_small_light_init_getparams(r, ctx, srv_conf) != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to analyze parameters:%V %s:%d", &define_pattern, __FUNCTION__, __LINE__); return NGX_ERROR; } } else { if (ngx_http_small_light_init_params(r, ctx, &define_pattern, srv_conf) != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to analyze parameters:%V %s:%d", &define_pattern, __FUNCTION__, __LINE__); return NGX_ERROR; } } hash_init.hash = &ctx->hash; hash_init.key = ngx_hash_key_lc; hash_init.max_size = 128; hash_init.bucket_size = ngx_cacheline_size; hash_init.name = "small_light_init_params"; hash_init.pool = ctx->params.keys.pool; hash_init.temp_pool = NULL; if (ngx_hash_init(&hash_init, ctx->params.keys.elts, ctx->params.keys.nelts) != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to init hash table for parameters %s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } ctx->inf = (char *)r->headers_out.content_type.data; ctx->material_dir = &srv_conf->material_dir; ctx->imlib2_temp_dir = loc_conf->imlib2_temp_dir; if (r->headers_out.content_length_n < 0) { ctx->content_length = loc_conf->buffer_size; } else { ctx->content_length = r->headers_out.content_length_n; } converter = NGX_HTTP_SMALL_LIGHT_PARAM_GET_LIT(&ctx->hash, "e"); if (ngx_strcmp(converter, NGX_HTTP_SMALL_LIGHT_CONVERTER_IMAGEMAGICK) == 0) { ctx->converter.init = ngx_http_small_light_imagemagick_init; ctx->converter.term = ngx_http_small_light_imagemagick_term; ctx->converter.process = ngx_http_small_light_imagemagick_process; ctx->ictx = ngx_pcalloc(r->pool, sizeof(ngx_http_small_light_imagemagick_ctx_t)); #ifdef NGX_HTTP_SMALL_LIGHT_IMLIB2_ENABLED } else if (ngx_strcmp(converter, NGX_HTTP_SMALL_LIGHT_CONVERTER_IMLIB2) == 0) { ctx->converter.init = ngx_http_small_light_imlib2_init; ctx->converter.term = ngx_http_small_light_imlib2_term; ctx->converter.process = ngx_http_small_light_imlib2_process; ctx->ictx = ngx_pcalloc(r->pool, sizeof(ngx_http_small_light_imlib2_ctx_t)); #endif #ifdef NGX_HTTP_SMALL_LIGHT_GD_ENABLED } else if (ngx_strcmp(converter, NGX_HTTP_SMALL_LIGHT_CONVERTER_GD) == 0) { ctx->converter.init = ngx_http_small_light_gd_init; ctx->converter.term = ngx_http_small_light_gd_term; ctx->converter.process = ngx_http_small_light_gd_process; ctx->ictx = ngx_pcalloc(r->pool, sizeof(ngx_http_small_light_gd_ctx_t)); #endif } else { ctx->converter.init = ngx_http_small_light_imagemagick_init; ctx->converter.term = ngx_http_small_light_imagemagick_term; ctx->converter.process = ngx_http_small_light_imagemagick_process; ctx->ictx = ngx_pcalloc(r->pool, sizeof(ngx_http_small_light_imagemagick_ctx_t)); } if (ctx->ictx == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "failed to allocate memory from r->pool %s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } ngx_http_set_ctx(r, ctx, ngx_http_small_light_module); if (r->headers_out.refresh) { r->headers_out.refresh->hash = 0; } r->main_filter_need_in_memory = 1; r->allow_ranges = 0; return NGX_OK; }
static ngx_int_t ngx_http_small_light_header_filter(ngx_http_request_t *r) { ngx_http_small_light_conf_t *srv_conf; ngx_http_small_light_conf_t *loc_conf; ngx_http_small_light_ctx_t *ctx; ngx_hash_init_t hash; ngx_str_t define_pattern; if (r->headers_out.status == NGX_HTTP_NOT_MODIFIED) { return ngx_http_next_header_filter(r); } srv_conf = ngx_http_get_module_srv_conf(r, ngx_http_small_light_module); loc_conf = ngx_http_get_module_loc_conf(r, ngx_http_small_light_module); if (!loc_conf->enable) { return ngx_http_next_header_filter(r); } if(ngx_http_small_light_parse_define_pattern(r, &r->unparsed_uri, &define_pattern) != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s:%d", __FUNCTION__, __LINE__); return ngx_http_next_header_filter(r); } ctx = ngx_http_get_module_ctx(r, ngx_http_small_light_module); if (ctx) { ngx_http_set_ctx(r, NULL, ngx_http_small_light_module); return ngx_http_next_header_filter(r); } if ((ctx = ngx_pcalloc(r->pool, sizeof(*ctx))) == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } ngx_memzero(ctx, sizeof(ngx_http_small_light_ctx_t)); ctx->params.keys.pool = r->pool; ctx->params.temp_pool = r->pool; if (ngx_hash_keys_array_init(&ctx->params, NGX_HASH_SMALL) != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } if (ngx_http_small_light_init_params(r, ctx, &define_pattern, srv_conf) != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } hash.hash = &ctx->hash; hash.key = ngx_hash_key_lc; hash.max_size = 128; hash.bucket_size = ngx_cacheline_size; hash.name = "small_light_init_params"; hash.pool = ctx->params.keys.pool; hash.temp_pool = NULL; if (ngx_hash_init(&hash, ctx->params.keys.elts, ctx->params.keys.nelts) != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s:%d", __FUNCTION__, __LINE__); return NGX_ERROR; } ctx->inf = r->headers_out.content_type.data; ctx->content_length = r->headers_out.content_length_n; ngx_http_set_ctx(r, ctx, ngx_http_small_light_module); if (r->headers_out.refresh) { r->headers_out.refresh->hash = 0; } r->main_filter_need_in_memory = 1; r->allow_ranges = 0; return NGX_OK; }
static char * ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_referer_conf_t *rlcf = conf; u_char *p; ngx_str_t *value, uri, name; ngx_uint_t i, n; ngx_http_variable_t *var; ngx_http_server_name_t *sn; ngx_http_core_srv_conf_t *cscf; ngx_str_set(&name, "invalid_referer"); var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOHASH); if (var == NULL) { return NGX_CONF_ERROR; } var->get_handler = ngx_http_referer_variable; if (rlcf->keys == NULL) { rlcf->keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t)); if (rlcf->keys == NULL) { return NGX_CONF_ERROR; } rlcf->keys->pool = cf->pool; rlcf->keys->temp_pool = cf->pool; if (ngx_hash_keys_array_init(rlcf->keys, NGX_HASH_SMALL) != NGX_OK) { return NGX_CONF_ERROR; } } value = cf->args->elts; for (i = 1; i < cf->args->nelts; i++) { if (value[i].len == 0) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid referer \"%V\"", &value[i]); return NGX_CONF_ERROR; } if (ngx_strcmp(value[i].data, "none") == 0) { rlcf->no_referer = 1; continue; } if (ngx_strcmp(value[i].data, "blocked") == 0) { rlcf->blocked_referer = 1; continue; } ngx_str_null(&uri); if (ngx_strcmp(value[i].data, "server_names") == 0) { cscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module); sn = cscf->server_names.elts; for (n = 0; n < cscf->server_names.nelts; n++) { #if (NGX_PCRE) if (sn[n].regex) { if (ngx_http_add_regex_referer(cf, rlcf, &sn[n].name, sn[n].regex->regex) != NGX_OK) { return NGX_CONF_ERROR; } continue; } #endif if (ngx_http_add_referer(cf, rlcf->keys, &sn[n].name, &uri) != NGX_OK) { return NGX_CONF_ERROR; } } continue; } if (value[i].data[0] == '~') { if (ngx_http_add_regex_referer(cf, rlcf, &value[i], NULL) != NGX_OK) { return NGX_CONF_ERROR; } continue; } p = (u_char *) ngx_strchr(value[i].data, '/'); if (p) { uri.len = (value[i].data + value[i].len) - p; uri.data = p; value[i].len = p - value[i].data; } if (ngx_http_add_referer(cf, rlcf->keys, &value[i], &uri) != NGX_OK) { return NGX_CONF_ERROR; } } return NGX_CONF_OK; }
static char * ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_referer_conf_t *rlcf = conf; u_char *p; ngx_str_t *value, uri; ngx_uint_t i; if (rlcf->keys == NULL) { rlcf->keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t)); if (rlcf->keys == NULL) { return NGX_CONF_ERROR; } rlcf->keys->pool = cf->pool; rlcf->keys->temp_pool = cf->pool; if (ngx_hash_keys_array_init(rlcf->keys, NGX_HASH_SMALL) != NGX_OK) { return NGX_CONF_ERROR; } } value = cf->args->elts; for (i = 1; i < cf->args->nelts; i++) { if (value[i].len == 0) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid referer \"%V\"", &value[i]); return NGX_CONF_ERROR; } if (ngx_strcmp(value[i].data, "none") == 0) { rlcf->no_referer = 1; continue; } if (ngx_strcmp(value[i].data, "blocked") == 0) { rlcf->blocked_referer = 1; continue; } if (ngx_strcmp(value[i].data, "server_names") == 0) { rlcf->server_names = 1; continue; } if (value[i].data[0] == '~') { if (ngx_http_add_regex_referer(cf, rlcf, &value[i]) != NGX_OK) { return NGX_CONF_ERROR; } continue; } ngx_str_null(&uri); p = (u_char *) ngx_strchr(value[i].data, '/'); if (p) { uri.len = (value[i].data + value[i].len) - p; uri.data = p; value[i].len = p - value[i].data; } if (ngx_http_add_referer(cf, rlcf->keys, &value[i], &uri) != NGX_OK) { return NGX_CONF_ERROR; } } return NGX_CONF_OK; }
static char * ngx_http_map_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_map_conf_t *mcf = conf; char *rv; ngx_str_t *value, name; ngx_conf_t save; ngx_pool_t *pool; ngx_hash_init_t hash; ngx_http_map_ctx_t *map; ngx_http_variable_t *var; ngx_http_map_conf_ctx_t ctx; ngx_http_compile_complex_value_t ccv; if (mcf->hash_max_size == NGX_CONF_UNSET_UINT) { mcf->hash_max_size = 2048; } if (mcf->hash_bucket_size == NGX_CONF_UNSET_UINT) { mcf->hash_bucket_size = ngx_cacheline_size; } else { mcf->hash_bucket_size = ngx_align(mcf->hash_bucket_size, ngx_cacheline_size); } map = ngx_pcalloc(cf->pool, sizeof(ngx_http_map_ctx_t)); if (map == NULL) { return NGX_CONF_ERROR; } value = cf->args->elts; ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); ccv.cf = cf; ccv.value = &value[1]; ccv.complex_value = &map->value; if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { return NGX_CONF_ERROR; } name = value[2]; if (name.data[0] != '$') { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid variable name \"%V\"", &name); return NGX_CONF_ERROR; } name.len--; name.data++; var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE); if (var == NULL) { return NGX_CONF_ERROR; } var->get_handler = ngx_http_map_variable; var->data = (uintptr_t) map; pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, cf->log); if (pool == NULL) { return NGX_CONF_ERROR; } ctx.keys.pool = cf->pool; ctx.keys.temp_pool = pool; if (ngx_hash_keys_array_init(&ctx.keys, NGX_HASH_LARGE) != NGX_OK) { ngx_destroy_pool(pool); return NGX_CONF_ERROR; } ctx.values_hash = ngx_pcalloc(pool, sizeof(ngx_array_t) * ctx.keys.hsize); if (ctx.values_hash == NULL) { ngx_destroy_pool(pool); return NGX_CONF_ERROR; } #if (NGX_PCRE) if (ngx_array_init(&ctx.regexes, cf->pool, 2, sizeof(ngx_http_map_regex_t)) != NGX_OK) { ngx_destroy_pool(pool); return NGX_CONF_ERROR; } #endif ctx.default_value = NULL; ctx.cf = &save; ctx.hostnames = 0; ctx.no_cacheable = 0; save = *cf; cf->pool = pool; cf->ctx = &ctx; cf->handler = ngx_http_map; cf->handler_conf = conf; rv = ngx_conf_parse(cf, NULL); *cf = save; if (rv != NGX_CONF_OK) { ngx_destroy_pool(pool); return rv; } if (ctx.no_cacheable) { var->flags |= NGX_HTTP_VAR_NOCACHEABLE; } map->default_value = ctx.default_value ? ctx.default_value: &ngx_http_variable_null_value; map->hostnames = ctx.hostnames; hash.key = ngx_hash_key_lc; hash.max_size = mcf->hash_max_size; hash.bucket_size = mcf->hash_bucket_size; hash.name = "map_hash"; hash.pool = cf->pool; if (ctx.keys.keys.nelts) { hash.hash = &map->map.hash.hash; hash.temp_pool = NULL; if (ngx_hash_init(&hash, ctx.keys.keys.elts, ctx.keys.keys.nelts) != NGX_OK) { ngx_destroy_pool(pool); return NGX_CONF_ERROR; } } if (ctx.keys.dns_wc_head.nelts) { ngx_qsort(ctx.keys.dns_wc_head.elts, (size_t) ctx.keys.dns_wc_head.nelts, sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards); hash.hash = NULL; hash.temp_pool = pool; if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_head.elts, ctx.keys.dns_wc_head.nelts) != NGX_OK) { ngx_destroy_pool(pool); return NGX_CONF_ERROR; } map->map.hash.wc_head = (ngx_hash_wildcard_t *) hash.hash; } if (ctx.keys.dns_wc_tail.nelts) { ngx_qsort(ctx.keys.dns_wc_tail.elts, (size_t) ctx.keys.dns_wc_tail.nelts, sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards); hash.hash = NULL; hash.temp_pool = pool; if (ngx_hash_wildcard_init(&hash, ctx.keys.dns_wc_tail.elts, ctx.keys.dns_wc_tail.nelts) != NGX_OK) { ngx_destroy_pool(pool); return NGX_CONF_ERROR; } map->map.hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash; } #if (NGX_PCRE) if (ctx.regexes.nelts) { map->map.regex = ctx.regexes.elts; map->map.nregex = ctx.regexes.nelts; } #endif ngx_destroy_pool(pool); return rv; }