3
// TODO : generalize this into a generic list module, with weight
6
ngx_array_t *ndk_upstreams;
12
} ndk_upstream_list_parse_t;
17
ndk_upstream_list_parse_weight (ndk_upstream_list_parse_t *ulp)
24
for (i=0; i<s->len; i++) {
26
if (s->data[i] < '0' || s->data[i] > '9')
36
ngx_conf_log_error (NGX_LOG_EMERG, ulp->cf, 0,
37
"upstream list just consists of number \"%V\"", s);
42
if (s->data[i] != ':') {
43
ngx_conf_log_error (NGX_LOG_EMERG, ulp->cf, 0,
44
"upstream list not correct format \"%V\"", s);
50
ulp->weight = ngx_atoi (s->data, i);
61
ndk_upstream_list (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
63
// TODO : change this for getting upstream pointer if available
65
ngx_uint_t buckets, count, i, j;
66
ngx_str_t *value, **bucket, *us;
68
ndk_upstream_list_t *ul, *ule;
69
ndk_upstream_list_parse_t ulp;
74
// create array of upstream lists it doesn't already exist
78
ula = ngx_array_create (cf->pool, 4, sizeof (ndk_upstream_list_t));
80
return NGX_CONF_ERROR;
86
// check to see if the list already exists
88
value = cf->args->elts;
92
ule = ul + ula->nelts;
94
for ( ; ul<ule; ul++) {
96
if (ul->name.len == value->len &&
97
ngx_strncasecmp (ul->name.data, value->data, value->len) == 0) {
99
ngx_conf_log_error (NGX_LOG_EMERG, cf, 0,
100
"duplicate upstream list name \"%V\"", value);
102
return NGX_CONF_ERROR;
109
ul = ngx_array_push (ula);
111
return NGX_CONF_ERROR;
117
// copy all the upstream names
119
count = cf->args->nelts - 2;
121
us = ngx_palloc (cf->pool, count * sizeof (ngx_str_t));
123
return NGX_CONF_ERROR;
125
ngx_memcpy (us, value + 1, count * sizeof (ngx_str_t));
128
// calculate the total number of buckets
134
for (i=0; i<count; i++, us++) {
138
if (ndk_upstream_list_parse_weight (&ulp) != NGX_OK)
139
return NGX_CONF_ERROR;
141
buckets += ulp.weight;
145
// allocate space for all buckets
147
bucket = ngx_palloc (cf->pool, buckets * sizeof (ngx_str_t **));
149
return NGX_CONF_ERROR;
155
// set values for each bucket
159
for (i=0; i<count; i++, us++) {
163
if (ndk_upstream_list_parse_weight (&ulp) != NGX_OK)
164
return NGX_CONF_ERROR;
166
us->data = ulp.s.data;
169
// TODO : check format of upstream
170
// TODO : add automatic adding of http:// in upstreams?
172
for (j=0; j<ulp.weight; j++, bucket++) {