Klaus Demo nginx / c9491d1
*) refactor ngx_parse_inet_url() *) refactor ngx_parse_unix_domain_url() *) delete unused ngx_url_t fields Igor Sysoev 13 years ago
5 changed file(s) with 137 addition(s) and 165 deletion(s). Raw diff Collapse all Expand all
178178 ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
179179 {
180180 #if (NGX_HAVE_UNIX_DOMAIN)
181 u_char *p;
181 u_char *path, *uri, *last;
182182 size_t len;
183 ngx_uint_t i;
184183 struct sockaddr_un *saun;
185184
186185 len = u->url.len;
187 p = u->url.data;
188
189 p += 5;
186 path = u->url.data;
187
188 path += 5;
190189 len -= 5;
191190
192 u->uri.len = len;
193 u->uri.data = p;
194
195191 if (u->uri_part) {
196 for (i = 0; i < len; i++) {
197
198 if (p[i] == ':') {
199 len = i;
200
201 u->uri.len -= len + 1;
202 u->uri.data += len + 1;
203
204 break;
205 }
192
193 last = path + len;
194 uri = ngx_strlchr(path, last, ':');
195
196 if (uri) {
197 len = uri - path;
198 uri++;
199 u->uri.len = last - uri;
200 u->uri.data = uri;
206201 }
207202 }
208203
211206 return NGX_ERROR;
212207 }
213208
214 if (len + 1 > sizeof(saun->sun_path)) {
209 u->host.len = len++;
210 u->host.data = path;
211 u->family = AF_UNIX;
212
213 if (len > sizeof(saun->sun_path)) {
215214 u->err = "too long path in the unix domain socket";
216215 return NGX_ERROR;
217216 }
229228 u->naddrs = 1;
230229
231230 saun->sun_family = AF_UNIX;
232 (void) ngx_cpystrn((u_char *) saun->sun_path, p, len + 1);
231 (void) ngx_cpystrn((u_char *) saun->sun_path, path, len);
233232
234233 u->addrs[0].sockaddr = (struct sockaddr *) saun;
235234 u->addrs[0].socklen = sizeof(struct sockaddr_un);
236 u->addrs[0].name.len = len + 5;
235 u->addrs[0].name.len = len + 4;
237236 u->addrs[0].name.data = u->url.data;
238
239 u->host.len = len;
240 u->host.data = p;
241
242 u->unix_socket = 1;
243237
244238 return NGX_OK;
245239
256250 static ngx_int_t
257251 ngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u)
258252 {
259 u_char *p, *host, *port_start;
260 size_t len, port_len;
261 ngx_int_t port;
262 ngx_uint_t i;
253 u_char *p, *host, *port, *last, *uri;
254 size_t len;
255 ngx_int_t n;
263256 struct hostent *h;
264257
265 len = u->url.len;
266 p = u->url.data;
267
268 u->host.data = p;
269
270 port_start = NULL;
271 port_len = 0;
272
273 for (i = 0; i < len; i++) {
274
275 if (p[i] == ':') {
276 port_start = &p[i + 1];
277 u->host.len = i;
278
279 if (!u->uri_part) {
280 port_len = len - (i + 1);
281 break;
258 host = u->url.data;
259
260 last = host + u->url.len;
261
262 port = ngx_strlchr(host, last, ':');
263
264 uri = ngx_strlchr(port ? port : host, last, '/');
265
266 if (uri) {
267 if (u->listen || !u->uri_part) {
268 u->err = "invalid host";
269 return NGX_ERROR;
270 }
271
272 u->uri.len = last - uri;
273 u->uri.data = uri;
274
275 last = uri;
276 }
277
278 if (port) {
279 port++;
280
281 if (last - port == 0) {
282 u->err = "invalid port";
283 return NGX_ERROR;
284 }
285
286 u->port_text.len = last - port;
287 u->port_text.data = port;
288
289 last = port - 1;
290
291 } else {
292 if (uri == NULL) {
293
294 if (u->listen) {
295
296 /* test value as port only */
297
298 n = ngx_atoi(host, last - host);
299
300 if (n != NGX_ERROR) {
301
302 if (n < 1 || n > 65536) {
303 u->err = "invalid port";
304 return NGX_ERROR;
305 }
306
307 u->port = (in_port_t) n;
308
309 u->port_text.len = last - host;
310 u->port_text.data = host;
311
312 return NGX_OK;
313 }
282314 }
283315 }
284316
285 if (p[i] == '/') {
286 u->uri.len = len - i;
287 u->uri.data = &p[i];
288
289 if (u->host.len == 0) {
290 u->host.len = i;
291 }
292
293 if (port_start == NULL) {
294 u->no_port = 1;
295 goto no_port;
296 }
297
298 port_len = &p[i] - port_start;
299
300 if (port_len == 0) {
301 u->err = "invalid port";
317 u->no_port = 1;
318 }
319
320 len = last - host;
321
322 if (len == 0) {
323 u->err = "no host";
324 return NGX_ERROR;
325 }
326
327 if (len == 1 && *host == '*') {
328 len = 0;
329 }
330
331 u->host.len = len;
332 u->host.data = host;
333
334 if (len++) {
335
336 p = ngx_alloc(len, pool->log);
337 if (p == NULL) {
338 return NGX_ERROR;
339 }
340
341 (void) ngx_cpystrn(p, host, len);
342
343 u->addr.in_addr = inet_addr((const char *) p);
344
345 if (u->addr.in_addr == INADDR_NONE) {
346 h = gethostbyname((const char *) p);
347
348 if (h == NULL || h->h_addr_list[0] == NULL) {
349 ngx_free(p);
350 u->err = "host not found";
302351 return NGX_ERROR;
303352 }
304353
305 break;
306 }
307 }
308
309 if (port_start) {
310
311 if (port_len == 0) {
312 port_len = &p[i] - port_start;
313
314 if (port_len == 0) {
315 u->err = "invalid port";
316 return NGX_ERROR;
317 }
318 }
319
320 port = ngx_atoi(port_start, port_len);
321
322 if (port == NGX_ERROR || port < 1 || port > 65536) {
354 u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
355 }
356
357 ngx_free(p);
358
359 } else {
360 u->addr.in_addr = INADDR_ANY;
361 }
362
363 if (u->port_text.len) {
364
365 n = ngx_atoi(u->port_text.data, u->port_text.len);
366
367 if (n < 1 || n > 65536) {
323368 u->err = "invalid port";
324369 return NGX_ERROR;
325370 }
326371
327 u->port_text.len = port_len;
328 u->port_text.data = port_start;
329
330 } else {
331 port = ngx_atoi(p, len);
332
333 if (port == NGX_ERROR) {
334 u->host.len = len;
335 u->no_port = 1;
336
337 goto no_port;
338 }
339
340 u->wildcard = 1;
341 }
342
343 u->port = (in_port_t) port;
344
345 no_port:
346
347 if (u->listen) {
348
349 if (u->port == 0) {
350 if (u->default_port == 0) {
351 u->err = "no port";
352 return NGX_ERROR;
353 }
354
355 u->port = u->default_port;
356 }
357
358 if (u->host.len == 1 && u->host.data[0] == '*') {
359 u->host.len = 0;
360 }
361
362 /* AF_INET only */
363
364 if (u->host.len) {
365
366 host = ngx_alloc(u->host.len + 1, pool->log);
367 if (host == NULL) {
368 return NGX_ERROR;
369 }
370
371 (void) ngx_cpystrn(host, u->host.data, u->host.len + 1);
372
373 u->addr.in_addr = inet_addr((const char *) host);
374
375 if (u->addr.in_addr == INADDR_NONE) {
376 h = gethostbyname((const char *) host);
377
378 if (h == NULL || h->h_addr_list[0] == NULL) {
379 ngx_free(host);
380 u->err = "host not found";
381 return NGX_ERROR;
382 }
383
384 u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
385 }
386
387 ngx_free(host);
388
389 } else {
390 u->addr.in_addr = INADDR_ANY;
391 }
392
393 return NGX_OK;
394 }
395
396 if (u->host.len == 0) {
397 u->err = "no host";
398 return NGX_ERROR;
399 }
372 u->port = (in_port_t) n;
373 }
374
375 u->family = AF_INET;
400376
401377 if (u->no_resolve) {
402378 return NGX_OK;
406382 u->port = u->default_port;
407383 }
408384
409 if (u->port == 0) {
410 u->err = "no port";
411 return NGX_ERROR;
385 if (u->listen) {
386 return NGX_OK;
412387 }
413388
414389 if (ngx_inet_resolve_host(pool, u) != NGX_OK) {
3333
3434
3535 typedef struct {
36 ngx_int_t type;
37
3836 ngx_str_t url;
3937 ngx_str_t host;
4038 ngx_str_t port_text;
4240
4341 in_port_t port;
4442 in_port_t default_port;
43 int family;
4544
4645 unsigned listen:1;
4746 unsigned uri_part:1;
4847 unsigned no_resolve:1;
4948 unsigned one_addr:1;
5049
51 unsigned wildcard:1;
5250 unsigned no_port:1;
53 unsigned unix_socket:1;
5451
5552 ngx_url_addr_t addr;
5653
25452545 ngx_http_proxy_set_vars(ngx_pool_t *pool, ngx_url_t *u,
25462546 ngx_http_proxy_vars_t *v)
25472547 {
2548 if (!u->unix_socket) {
2548 if (u->family != AF_UNIX) {
25492549 if (u->no_port || u->port == u->default_port) {
25502550 v->host_header = u->host;
25512551
29612961
29622962 ngx_memzero(ls, sizeof(ngx_http_listen_t));
29632963
2964 ls->family = AF_INET;
2964 ls->family = u.family;
29652965 ls->addr = u.addr.in_addr;
29662966 ls->port = u.port;
29672967 ls->file_name = cf->conf_file->file.name.data;
328328
329329 imls->addr = u.addr.in_addr;
330330 imls->port = u.port;
331 imls->family = AF_INET;
331 imls->family = u.family;
332332 imls->ctx = cf->ctx;
333333
334334 for (m = 0; ngx_modules[m]; m++) {