Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.11 ]​[ nginx-1.14.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_http.h>
0011 
0012 
0013 typedef struct {
0014     uint32_t                    percent;
0015     ngx_http_variable_value_t   value;
0016 } ngx_http_split_clients_part_t;
0017 
0018 
0019 typedef struct {
0020     ngx_http_complex_value_t    value;
0021     ngx_array_t                 parts;
0022 } ngx_http_split_clients_ctx_t;
0023 
0024 
0025 static char *ngx_conf_split_clients_block(ngx_conf_t *cf, ngx_command_t *cmd,
0026     void *conf);
0027 static char *ngx_http_split_clients(ngx_conf_t *cf, ngx_command_t *dummy,
0028     void *conf);
0029 
0030 static ngx_command_t  ngx_http_split_clients_commands[] = {
0031 
0032     { ngx_string("split_clients"),
0033       NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2,
0034       ngx_conf_split_clients_block,
0035       NGX_HTTP_MAIN_CONF_OFFSET,
0036       0,
0037       NULL },
0038 
0039       ngx_null_command
0040 };
0041 
0042 
0043 static ngx_http_module_t  ngx_http_split_clients_module_ctx = {
0044     NULL,                                  /* preconfiguration */
0045     NULL,                                  /* postconfiguration */
0046 
0047     NULL,                                  /* create main configuration */
0048     NULL,                                  /* init main configuration */
0049 
0050     NULL,                                  /* create server configuration */
0051     NULL,                                  /* merge server configuration */
0052 
0053     NULL,                                  /* create location configuration */
0054     NULL                                   /* merge location configuration */
0055 };
0056 
0057 
0058 ngx_module_t  ngx_http_split_clients_module = {
0059     NGX_MODULE_V1,
0060     &ngx_http_split_clients_module_ctx,    /* module context */
0061     ngx_http_split_clients_commands,       /* module directives */
0062     NGX_HTTP_MODULE,                       /* module type */
0063     NULL,                                  /* init master */
0064     NULL,                                  /* init module */
0065     NULL,                                  /* init process */
0066     NULL,                                  /* init thread */
0067     NULL,                                  /* exit thread */
0068     NULL,                                  /* exit process */
0069     NULL,                                  /* exit master */
0070     NGX_MODULE_V1_PADDING
0071 };
0072 
0073 
0074 static ngx_int_t
0075 ngx_http_split_clients_variable(ngx_http_request_t *r,
0076     ngx_http_variable_value_t *v, uintptr_t data)
0077 {
0078     ngx_http_split_clients_ctx_t *ctx = (ngx_http_split_clients_ctx_t *) data;
0079 
0080     uint32_t                        hash;
0081     ngx_str_t                       val;
0082     ngx_uint_t                      i;
0083     ngx_http_split_clients_part_t  *part;
0084 
0085     *v = ngx_http_variable_null_value;
0086 
0087     if (ngx_http_complex_value(r, &ctx->value, &val) != NGX_OK) {
0088         return NGX_OK;
0089     }
0090 
0091     hash = ngx_murmur_hash2(val.data, val.len);
0092 
0093     part = ctx->parts.elts;
0094 
0095     for (i = 0; i < ctx->parts.nelts; i++) {
0096 
0097         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0098                        "http split: %uD %uD", hash, part[i].percent);
0099 
0100         if (hash < part[i].percent || part[i].percent == 0) {
0101             *v = part[i].value;
0102             return NGX_OK;
0103         }
0104     }
0105 
0106     return NGX_OK;
0107 }
0108 
0109 
0110 static char *
0111 ngx_conf_split_clients_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0112 {
0113     char                                *rv;
0114     uint32_t                             sum, last;
0115     ngx_str_t                           *value, name;
0116     ngx_uint_t                           i;
0117     ngx_conf_t                           save;
0118     ngx_http_variable_t                 *var;
0119     ngx_http_split_clients_ctx_t        *ctx;
0120     ngx_http_split_clients_part_t       *part;
0121     ngx_http_compile_complex_value_t     ccv;
0122 
0123     ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_split_clients_ctx_t));
0124     if (ctx == NULL) {
0125         return NGX_CONF_ERROR;
0126     }
0127 
0128     value = cf->args->elts;
0129 
0130     ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
0131 
0132     ccv.cf = cf;
0133     ccv.value = &value[1];
0134     ccv.complex_value = &ctx->value;
0135 
0136     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
0137         return NGX_CONF_ERROR;
0138     }
0139 
0140     name = value[2];
0141 
0142     if (name.data[0] != '$') {
0143         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0144                            "invalid variable name \"%V\"", &name);
0145         return NGX_CONF_ERROR;
0146     }
0147 
0148     name.len--;
0149     name.data++;
0150 
0151     var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
0152     if (var == NULL) {
0153         return NGX_CONF_ERROR;
0154     }
0155 
0156     var->get_handler = ngx_http_split_clients_variable;
0157     var->data = (uintptr_t) ctx;
0158 
0159     if (ngx_array_init(&ctx->parts, cf->pool, 2,
0160                        sizeof(ngx_http_split_clients_part_t))
0161         != NGX_OK)
0162     {
0163         return NGX_CONF_ERROR;
0164     }
0165 
0166     save = *cf;
0167     cf->ctx = ctx;
0168     cf->handler = ngx_http_split_clients;
0169     cf->handler_conf = conf;
0170 
0171     rv = ngx_conf_parse(cf, NULL);
0172 
0173     *cf = save;
0174 
0175     if (rv != NGX_CONF_OK) {
0176         return rv;
0177     }
0178 
0179     sum = 0;
0180     last = 0;
0181     part = ctx->parts.elts;
0182 
0183     for (i = 0; i < ctx->parts.nelts; i++) {
0184         sum = part[i].percent ? sum + part[i].percent : 10000;
0185         if (sum > 10000) {
0186             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0187                                "percent total is greater than 100%%");
0188             return NGX_CONF_ERROR;
0189         }
0190 
0191         if (part[i].percent) {
0192             last += part[i].percent * (uint64_t) 0xffffffff / 10000;
0193             part[i].percent = last;
0194         }
0195     }
0196 
0197     return rv;
0198 }
0199 
0200 
0201 static char *
0202 ngx_http_split_clients(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
0203 {
0204     ngx_int_t                       n;
0205     ngx_str_t                      *value;
0206     ngx_http_split_clients_ctx_t   *ctx;
0207     ngx_http_split_clients_part_t  *part;
0208 
0209     ctx = cf->ctx;
0210     value = cf->args->elts;
0211 
0212     part = ngx_array_push(&ctx->parts);
0213     if (part == NULL) {
0214         return NGX_CONF_ERROR;
0215     }
0216 
0217     if (value[0].len == 1 && value[0].data[0] == '*') {
0218         part->percent = 0;
0219 
0220     } else {
0221         if (value[0].len == 0 || value[0].data[value[0].len - 1] != '%') {
0222             goto invalid;
0223         }
0224 
0225         n = ngx_atofp(value[0].data, value[0].len - 1, 2);
0226         if (n == NGX_ERROR || n == 0) {
0227             goto invalid;
0228         }
0229 
0230         part->percent = (uint32_t) n;
0231     }
0232 
0233     part->value.len = value[1].len;
0234     part->value.valid = 1;
0235     part->value.no_cacheable = 0;
0236     part->value.not_found = 0;
0237     part->value.data = value[1].data;
0238 
0239     return NGX_CONF_OK;
0240 
0241 invalid:
0242 
0243     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0244                        "invalid percent value \"%V\"", &value[0]);
0245     return NGX_CONF_ERROR;
0246 }