Klaus Demo nginx / bf3aaac
rewritten upstream Igor Sysoev 15 years ago
12 changed file(s) with 130 addition(s) and 108 deletion(s). Raw diff Collapse all Expand all
220220 ngx_int_t
221221 ngx_parse_url(ngx_conf_t *cf, ngx_url_t *u)
222222 {
223 u_char *p, *host;
224 size_t len;
223 u_char *p, *host, *port_start;
224 size_t len, port_len;
225225 ngx_int_t port;
226226 ngx_uint_t i;
227227 struct hostent *h;
289289 u->host.len = len;
290290 u->host.data = p;
291291
292 u->host_header.len = sizeof("localhost") - 1;
293 u->host_header.data = (u_char *) "localhost";
292 u->unix_socket = 1;
294293
295294 return NGX_OK;
296295
308307 }
309308
310309 u->host.data = p;
311 u->host_header.len = len;
312 u->host_header.data = p;
310
311 port_start = NULL;
312 port_len = 0;
313313
314314 for (i = 0; i < len; i++) {
315315
316316 if (p[i] == ':') {
317 u->port.data = &p[i + 1];
317 port_start = &p[i + 1];
318318 u->host.len = i;
319319
320320 if (!u->uri_part) {
321 u->port.len = len - (i + 1);
321 port_len = len - (i + 1);
322322 break;
323323 }
324324 }
326326 if (p[i] == '/') {
327327 u->uri.len = len - i;
328328 u->uri.data = &p[i];
329 u->host_header.len = i;
330329
331330 if (u->host.len == 0) {
332331 u->host.len = i;
333332 }
334333
335 if (u->port.data == NULL) {
334 if (port_start == NULL) {
336335 u->no_port = 1;
337336 goto no_port;
338337 }
339338
340 u->port.len = &p[i] - u->port.data;
341
342 if (u->port.len == 0) {
339 port_len = &p[i] - port_start;
340
341 if (port_len == 0) {
343342 u->err = "invalid port";
344343 return NGX_ERROR;
345344 }
348347 }
349348 }
350349
351 if (u->port.data) {
352
353 if (u->port.len == 0) {
354 u->port.len = &p[i] - u->port.data;
355
356 if (u->port.len == 0) {
350 if (port_start) {
351
352 if (port_len == 0) {
353 port_len = &p[i] - port_start;
354
355 if (port_len == 0) {
357356 u->err = "invalid port";
358357 return NGX_ERROR;
359358 }
360359 }
361360
362 port = ngx_atoi(u->port.data, u->port.len);
361 port = ngx_atoi(port_start, port_len);
363362
364363 if (port == NGX_ERROR || port < 1 || port > 65536) {
365364 u->err = "invalid port";
376375 goto no_port;
377376 }
378377
379 u->port.len = len;
380 u->port.data = p;
381378 u->wildcard = 1;
382379 }
383380
384 u->portn = (in_port_t) port;
381 u->port = (in_port_t) port;
385382
386383 no_port:
387384
388385 if (u->listen) {
389386
390 if (u->portn == 0) {
391 if (u->default_portn == 0) {
387 if (u->port == 0) {
388 if (u->default_port == 0) {
392389 u->err = "no port";
393390 return NGX_ERROR;
394391 }
395392
396 u->portn = u->default_portn;
393 u->port = u->default_port;
397394 }
398395
399396 if (u->host.len == 1 && u->host.data[0] == '*') {
421418 return NGX_ERROR;
422419 }
423420
424 u->addr.in_addr = *(in_addr_t *)(h->h_addr_list[0]);
421 u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
425422 }
426423
427424 } else {
431428 return NGX_OK;
432429 }
433430
434 if (u->no_port) {
435
436 if (u->default_portn == 0 && !u->upstream) {
437 u->err = "no port";
438 return NGX_ERROR;
439 }
440
441 u->portn = u->default_portn;
442
443 u->port.data = ngx_palloc(cf->pool, sizeof("65536") - 1);
444 if (u->port.data == NULL) {
445 return NGX_ERROR;
446 }
447
448 u->port.len = ngx_sprintf(u->port.data, "%d", u->portn) - u->port.data;
449
450 } else if (u->portn == 0) {
451
452 u->err = "no port";
453 return NGX_ERROR;
454 }
455
456431 if (u->host.len == 0) {
457432 u->err = "no host";
458433 return NGX_ERROR;
460435
461436 if (u->no_resolve) {
462437 return NGX_OK;
438 }
439
440 if (u->no_port) {
441 u->port = u->default_port;
442 }
443
444 if (u->port == 0) {
445 u->err = "no port";
446 return NGX_ERROR;
463447 }
464448
465449 if (ngx_inet_resolve_host(cf, u) != NGX_OK) {
473457 ngx_int_t
474458 ngx_inet_resolve_host(ngx_conf_t *cf, ngx_url_t *u)
475459 {
476 u_char *host;
460 u_char *p, *host;
477461 size_t len;
478462 in_addr_t in_addr;
479463 ngx_uint_t i;
523507 }
524508
525509 sin->sin_family = AF_INET;
526 sin->sin_port = htons(u->portn);
510 sin->sin_port = htons(u->port);
527511 sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]);
528512
529513 u->addrs[i].sockaddr = (struct sockaddr *) sin;
531515
532516 len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1;
533517
534 u->addrs[i].name.data = ngx_palloc(cf->pool, len);
535 if (u->addrs[i].name.data == NULL) {
518 p = ngx_palloc(cf->pool, len);
519 if (p == NULL) {
536520 return NGX_ERROR;
537521 }
538522
539 len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin,
540 u->addrs[i].name.data, len);
541
542 u->addrs[i].name.len = ngx_sprintf(&u->addrs[i].name.data[len],
543 ":%d", u->portn)
544 - u->addrs[i].name.data;
523 len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin, p, len);
524
525 u->addrs[i].name.len = ngx_sprintf(&p[len], ":%d", u->port) - p;
526 u->addrs[i].name.data = p;
545527 }
546528
547529 } else {
561543 u->naddrs = 1;
562544
563545 sin->sin_family = AF_INET;
564 sin->sin_port = htons(u->portn);
546 sin->sin_port = htons(u->port);
565547 sin->sin_addr.s_addr = in_addr;
566548
567549 u->addrs[0].sockaddr = (struct sockaddr *) sin;
568550 u->addrs[0].socklen = sizeof(struct sockaddr_in);
569551
570 u->addrs[0].name.data = ngx_palloc(cf->pool,
571 u->host.len + sizeof(":65536") - 1);
572 if (u->addrs[0].name.data == NULL) {
573 return NGX_ERROR;
574 }
575
576 u->addrs[0].name.len = ngx_sprintf(u->addrs[0].name.data, "%V:%d",
577 &u->host, u->portn)
578 - u->addrs[0].name.data;
552 p = ngx_palloc(cf->pool, u->host.len + sizeof(":65536") - 1);
553 if (p == NULL) {
554 return NGX_ERROR;
555 }
556
557 u->addrs[0].name.len = ngx_sprintf(p, "%V:%d", &u->host, u->port) - p;
558 u->addrs[0].name.data = p;
579559 }
580560
581561 return NGX_OK;
3434
3535 ngx_str_t url;
3636 ngx_str_t host;
37 ngx_str_t host_header;
38 ngx_str_t port;
3937 ngx_str_t uri;
4038
41 in_port_t portn;
42 in_port_t default_portn;
39 in_port_t port;
40 in_port_t default_port;
4341
4442 unsigned listen:1;
4543 unsigned uri_part:1;
46 unsigned upstream:1;
4744 unsigned no_resolve:1;
4845 unsigned one_addr:1;
4946
5047 unsigned wildcard:1;
5148 unsigned no_port:1;
49 unsigned unix_socket:1;
5250
5351 ngx_url_addr_t addr;
5452
20092009 ngx_memzero(&u, sizeof(ngx_url_t));
20102010
20112011 u.url = value[1];
2012 u.upstream = 1;
20132012 u.no_resolve = 1;
20142013
20152014 lcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
582582 ngx_memzero(&u, sizeof(ngx_url_t));
583583
584584 u.url = value[1];
585 u.upstream = 1;
586585 u.no_resolve = 1;
587586 /* u.uri_part = 1; may be used as namespace */
588587
5050
5151 ngx_str_t method;
5252 ngx_str_t host_header;
53 ngx_str_t port_text;
53 ngx_str_t port;
5454
5555 ngx_flag_t redirect;
5656 } ngx_http_proxy_loc_conf_t;
12311231
12321232 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
12331233
1234 v->len = plcf->port_text.len;
1234 v->len = plcf->port.len;
12351235 v->valid = 1;
12361236 v->no_cachable = 0;
12371237 v->not_found = 0;
1238 v->data = plcf->port_text.data;
1238 v->data = plcf->port.data;
12391239
12401240 return NGX_OK;
12411241 }
18161816 conf->upstream.upstream = prev->upstream.upstream;
18171817
18181818 conf->host_header = prev->host_header;
1819 conf->port_text = prev->port_text;
1819 conf->port = prev->port;
18201820 conf->upstream.schema = prev->upstream.schema;
18211821 }
18221822
20922092 {
20932093 ngx_http_proxy_loc_conf_t *plcf = conf;
20942094
2095 u_char *p;
20952096 size_t add;
20962097 u_short port;
20972098 ngx_str_t *value, *url;
21572158
21582159 u.url.len = url->len - add;
21592160 u.url.data = url->data + add;
2160 u.default_portn = port;
2161 u.upstream = 1;
2161 u.default_port = port;
2162 u.uri_part = 1;
21622163 u.no_resolve = 1;
2163 u.uri_part = 1;
21642164
21652165 plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
21662166 if (plcf->upstream.upstream == NULL) {
21672167 return NGX_CONF_ERROR;
21682168 }
21692169
2170 plcf->host_header = u.host_header;
2171 plcf->port_text = u.port;
2170 if (!u.unix_socket) {
2171 if (u.no_port || u.port == port) {
2172 plcf->host_header = u.host;
2173
2174 if (port == 80) {
2175 plcf->port.len = sizeof("80") - 1;
2176 plcf->port.data = (u_char *) "80";
2177 } else {
2178 plcf->port.len = sizeof("443") - 1;
2179 plcf->port.data = (u_char *) "443";
2180 }
2181
2182 } else {
2183 p = ngx_palloc(cf->pool, u.host.len + sizeof(":65536") - 1);
2184 if (p == NULL) {
2185 return NGX_CONF_ERROR;
2186 }
2187
2188 plcf->host_header.len = ngx_sprintf(p, "%V:%d", &u.host, u.port)
2189 - p;
2190 plcf->host_header.data = p;
2191
2192 plcf->port.len = plcf->host_header.len - u.host.len - 1;
2193 plcf->port.data = p + u.host.len + 1;
2194 }
2195
2196
2197 } else {
2198 plcf->host_header.len = sizeof("localhost") - 1;
2199 plcf->host_header.data = (u_char *) "localhost";
2200 plcf->port.len = 0;
2201 plcf->port.data = (u_char *) "";
2202 }
2203
21722204 plcf->upstream.uri = u.uri;
21732205
21742206 plcf->upstream.schema.len = add;
23892389
23902390 u.url = value[1];
23912391 u.listen = 1;
2392 u.default_portn = 80;
2392 u.default_port = 80;
23932393
23942394 if (ngx_parse_url(cf, &u) != NGX_OK) {
23952395 if (u.err) {
24102410
24112411 ls->family = AF_INET;
24122412 ls->addr = u.addr.in_addr;
2413 ls->port = u.portn;
2413 ls->port = u.port;
24142414 ls->file_name = cf->conf_file->file.name;
24152415 ls->line = cf->conf_file->line;
24162416 ls->conf.backlog = -1;
25972597
25982598 value = cf->args->elts;
25992599 u.host = value[1];
2600 u.upstream = 1;
26012600 u.no_resolve = 1;
26022601
26032602 uscf = ngx_http_upstream_add(cf, &u, NGX_HTTP_UPSTREAM_CREATE
27212720 ngx_memzero(&u, sizeof(ngx_url_t));
27222721
27232722 u.url = value[1];
2724 u.default_portn = 80;
2723 u.default_port = 80;
27252724
27262725 if (ngx_parse_url(cf, &u) != NGX_OK) {
27272726 if (u.err) {
28422841 uscfp = umcf->upstreams.elts;
28432842
28442843 for (i = 0; i < umcf->upstreams.nelts; i++) {
2845 if ((uscfp[i]->port && uscfp[i]->port != u->portn)
2846 || uscfp[i]->host.len != u->host.len
2844
2845 if (uscfp[i]->host.len != u->host.len
28472846 || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len)
28482847 != 0)
28492848 {
28582857 return NULL;
28592858 }
28602859
2861 if (uscfp[i]->port == 0 && u->portn && !u->no_port) {
2860 if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && u->port) {
28622861 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
2863 "upstream \"%V\" port %d is ignored",
2864 &u->host, u->portn);
2862 "upstream \"%V\" may not have port %d",
2863 &u->host, u->port);
2864 return NULL;
2865 }
2866
2867 if ((flags & NGX_HTTP_UPSTREAM_CREATE) && uscfp[i]->port) {
2868 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
2869 "upstream \"%V\" may not have port %d in %s:%ui",
2870 &u->host, uscfp[i]->port,
2871 uscfp[i]->file_name.data, uscfp[i]->line);
2872 return NULL;
2873 }
2874
2875 if (uscfp[i]->port != u->port) {
2876 continue;
28652877 }
28662878
28672879 return uscfp[i];
28762888 uscf->host = u->host;
28772889 uscf->file_name = cf->conf_file->file.name;
28782890 uscf->line = cf->conf_file->line;
2879 uscf->port = u->portn;
2891 uscf->port = u->port;
2892 uscf->default_port = u->default_port;
28802893
28812894 if (u->naddrs == 1) {
28822895 uscf->servers = ngx_array_create(cf->pool, 1,
9393 ngx_str_t file_name;
9494 ngx_uint_t line;
9595 in_port_t port;
96 in_port_t default_port;
9697 };
9798
9899
6060
6161 /* an upstream implicitly defined by proxy_pass, etc. */
6262
63 if (us->port == 0 && us->default_port == 0) {
64 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
65 "no port in upstream \"%V\" in %s:%ui",
66 &us->host, us->file_name.data, us->line);
67 return NGX_ERROR;
68 }
69
6370 ngx_memzero(&u, sizeof(ngx_url_t));
6471
6572 u.host = us->host;
66 u.portn = us->port;
73 u.port = us->port ? us->port : us->default_port;
6774
6875 if (ngx_inet_resolve_host(cf, &u) != NGX_OK) {
6976 if (u.err) {
7279 u.err, &us->host, us->file_name.data, us->line);
7380 }
7481
75 return NGX_ERROR;
76 }
77
78 if (us->port == 0) {
79 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
80 "no port in upstream \"%V\" in %s:%ui",
81 &us->host, us->file_name.data, us->line);
8282 return NGX_ERROR;
8383 }
8484
12901290 ngx_memzero(&u, sizeof(ngx_url_t));
12911291
12921292 u.url = value[1];
1293 u.default_portn = 80;
1293 u.default_port = 80;
12941294 u.uri_part = 1;
12951295 u.one_addr = 1;
12961296
13031303
13041304 ahcf->peer = u.addrs;
13051305
1306 ahcf->host_header = u.host_header;
1306 ahcf->host_header = u.host;
13071307 ahcf->uri = u.uri;
13081308
13091309 if (ahcf->uri.len == 0) {
540540
541541 for (i = 0; i < cmcf->listen.nelts; i++) {
542542
543 if (imls[i].addr != u.addr.in_addr || imls[i].port != u.portn) {
543 if (imls[i].addr != u.addr.in_addr || imls[i].port != u.port) {
544544 continue;
545545 }
546546
557557 ngx_memzero(imls, sizeof(ngx_imap_listen_t));
558558
559559 imls->addr = u.addr.in_addr;
560 imls->port = u.portn;
560 imls->port = u.port;
561561 imls->family = AF_INET;
562562 imls->ctx = cf->ctx;
563563
184184 ngx_memzero(&u, sizeof(ngx_url_t));
185185
186186 u.url = value[1];
187 u.default_portn = 3306;
187 u.default_port = 3306;
188188
189189 if (ngx_parse_url(cf, &u) != NGX_OK) {
190190 if (u.err) {