/
ndk_upstream_list.c
205 lines (127 loc) · 4.02 KB
/
ndk_upstream_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* TODO : generalize this into a generic list module, with weight */
typedef struct {
ngx_uint_t weight;
ngx_str_t s;
ngx_conf_t *cf;
} ndk_upstream_list_parse_t;
static ngx_int_t
ndk_upstream_list_parse_weight (ndk_upstream_list_parse_t *ulp)
{
size_t i;
ngx_str_t *s;
s = &ulp->s;
for (i=0; i<s->len; i++) {
if (s->data[i] < '0' || s->data[i] > '9')
break;
}
if (!i) {
ulp->weight = 1;
return NGX_OK;
}
if (i == s->len) {
ngx_conf_log_error (NGX_LOG_EMERG, ulp->cf, 0,
"upstream list just consists of number \"%V\"", s);
return NGX_ERROR;
}
if (s->data[i] != ':') {
ngx_conf_log_error (NGX_LOG_EMERG, ulp->cf, 0,
"upstream list not correct format \"%V\"", s);
return NGX_ERROR;
}
ulp->weight = ngx_atoi (s->data, i);
s->data += (i + 1);
s->len -= (i + 1);
return NGX_OK;
}
static char *
ndk_upstream_list (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
/* TODO : change this for getting upstream pointer if available */
ngx_uint_t buckets, count, i, j;
ngx_str_t *value, **bucket, *us;
ngx_array_t *ula;
ndk_upstream_list_t *ul, *ule;
ndk_upstream_list_parse_t ulp;
ndk_http_main_conf_t *mcf;
mcf = ngx_http_conf_get_module_main_conf (cf, ndk_http_module);
ula = mcf->upstreams;
/* create array of upstream lists it doesn't already exist */
if (ula == NULL) {
ula = ngx_array_create (cf->pool, 4, sizeof (ndk_upstream_list_t));
if (ula == NULL)
return NGX_CONF_ERROR;
mcf->upstreams = ula;
}
/* check to see if the list already exists */
value = cf->args->elts;
value++;
ul = ula->elts;
ule = ul + ula->nelts;
for ( ; ul<ule; ul++) {
if (ul->name.len == value->len &&
ngx_strncasecmp (ul->name.data, value->data, value->len) == 0) {
ngx_conf_log_error (NGX_LOG_EMERG, cf, 0,
"duplicate upstream list name \"%V\"", value);
return NGX_CONF_ERROR;
}
}
/* create a new list */
ul = ngx_array_push (ula);
if (ul == NULL)
return NGX_CONF_ERROR;
ul->name = *value;
/* copy all the upstream names */
count = cf->args->nelts - 2;
us = ngx_palloc (cf->pool, count * sizeof (ngx_str_t));
if (us == NULL)
return NGX_CONF_ERROR;
ngx_memcpy (us, value + 1, count * sizeof (ngx_str_t));
/* calculate the total number of buckets */
buckets = 0;
ulp.cf = cf;
for (i=0; i<count; i++, us++) {
ulp.s = *us;
if (ndk_upstream_list_parse_weight (&ulp) != NGX_OK)
return NGX_CONF_ERROR;
buckets += ulp.weight;
}
/* allocate space for all buckets */
bucket = ngx_palloc (cf->pool, buckets * sizeof (ngx_str_t **));
if (bucket == NULL)
return NGX_CONF_ERROR;
ul->elts = bucket;
ul->nelts = buckets;
/* set values for each bucket */
us -= count;
for (i=0; i<count; i++, us++) {
ulp.s = *us;
if (ndk_upstream_list_parse_weight (&ulp) != NGX_OK)
return NGX_CONF_ERROR;
us->data = ulp.s.data;
us->len = ulp.s.len;
/* TODO : check format of upstream */
/* TODO : add automatic adding of http:// in upstreams? */
for (j=0; j<ulp.weight; j++, bucket++) {
*bucket = us;
}
}
return NGX_CONF_OK;
}
ndk_upstream_list_t *
ndk_get_upstream_list (ndk_http_main_conf_t *mcf, u_char *data, size_t len)
{
ndk_upstream_list_t *ul, *ule;
ngx_array_t *ua = mcf->upstreams;
if (ua == NULL) {
return NULL;
}
ul = ua->elts;
ule = ul + ua->nelts;
for (; ul < ule; ul++) {
if (ul->name.len == len && ngx_strncasecmp(ul->name.data, data, len) == 0)
{
return ul;
}
}
return NULL;
}