Mercurial > hg > nginx
comparison src/stream/ngx_stream_proxy_module.c @ 6529:cb8177ca0990
Stream: prepared proxy_bind to accept parameters.
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Wed, 13 Apr 2016 15:42:47 +0300 |
parents | a01e315b3a78 |
children | 1d0e03db9f8e |
comparison
equal
deleted
inserted
replaced
6528:88f012eee7d8 | 6529:cb8177ca0990 |
---|---|
6 | 6 |
7 | 7 |
8 #include <ngx_config.h> | 8 #include <ngx_config.h> |
9 #include <ngx_core.h> | 9 #include <ngx_core.h> |
10 #include <ngx_stream.h> | 10 #include <ngx_stream.h> |
11 | |
12 | |
13 typedef struct { | |
14 ngx_addr_t *addr; | |
15 } ngx_stream_upstream_local_t; | |
11 | 16 |
12 | 17 |
13 typedef struct { | 18 typedef struct { |
14 ngx_msec_t connect_timeout; | 19 ngx_msec_t connect_timeout; |
15 ngx_msec_t timeout; | 20 ngx_msec_t timeout; |
19 size_t download_rate; | 24 size_t download_rate; |
20 ngx_uint_t responses; | 25 ngx_uint_t responses; |
21 ngx_uint_t next_upstream_tries; | 26 ngx_uint_t next_upstream_tries; |
22 ngx_flag_t next_upstream; | 27 ngx_flag_t next_upstream; |
23 ngx_flag_t proxy_protocol; | 28 ngx_flag_t proxy_protocol; |
24 ngx_addr_t *local; | 29 ngx_stream_upstream_local_t *local; |
25 | 30 |
26 #if (NGX_STREAM_SSL) | 31 #if (NGX_STREAM_SSL) |
27 ngx_flag_t ssl_enable; | 32 ngx_flag_t ssl_enable; |
28 ngx_flag_t ssl_session_reuse; | 33 ngx_flag_t ssl_session_reuse; |
29 ngx_uint_t ssl_protocols; | 34 ngx_uint_t ssl_protocols; |
45 ngx_stream_upstream_srv_conf_t *upstream; | 50 ngx_stream_upstream_srv_conf_t *upstream; |
46 } ngx_stream_proxy_srv_conf_t; | 51 } ngx_stream_proxy_srv_conf_t; |
47 | 52 |
48 | 53 |
49 static void ngx_stream_proxy_handler(ngx_stream_session_t *s); | 54 static void ngx_stream_proxy_handler(ngx_stream_session_t *s); |
55 static ngx_int_t ngx_stream_proxy_set_local(ngx_stream_session_t *s, | |
56 ngx_stream_upstream_t *u, ngx_stream_upstream_local_t *local); | |
50 static void ngx_stream_proxy_connect(ngx_stream_session_t *s); | 57 static void ngx_stream_proxy_connect(ngx_stream_session_t *s); |
51 static void ngx_stream_proxy_init_upstream(ngx_stream_session_t *s); | 58 static void ngx_stream_proxy_init_upstream(ngx_stream_session_t *s); |
52 static void ngx_stream_proxy_upstream_handler(ngx_event_t *ev); | 59 static void ngx_stream_proxy_upstream_handler(ngx_event_t *ev); |
53 static void ngx_stream_proxy_downstream_handler(ngx_event_t *ev); | 60 static void ngx_stream_proxy_downstream_handler(ngx_event_t *ev); |
54 static void ngx_stream_proxy_process_connection(ngx_event_t *ev, | 61 static void ngx_stream_proxy_process_connection(ngx_event_t *ev, |
356 s->log_handler = ngx_stream_proxy_log_error; | 363 s->log_handler = ngx_stream_proxy_log_error; |
357 | 364 |
358 u->peer.log = c->log; | 365 u->peer.log = c->log; |
359 u->peer.log_error = NGX_ERROR_ERR; | 366 u->peer.log_error = NGX_ERROR_ERR; |
360 | 367 |
361 u->peer.local = pscf->local; | 368 if (ngx_stream_proxy_set_local(s, u, pscf->local) != NGX_OK) { |
369 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
370 return; | |
371 } | |
372 | |
362 u->peer.type = c->type; | 373 u->peer.type = c->type; |
363 | 374 |
364 uscf = pscf->upstream; | 375 uscf = pscf->upstream; |
365 | 376 |
366 if (uscf->peer.init(s, uscf) != NGX_OK) { | 377 if (uscf->peer.init(s, uscf) != NGX_OK) { |
423 if (c->read->ready) { | 434 if (c->read->ready) { |
424 ngx_post_event(c->read, &ngx_posted_events); | 435 ngx_post_event(c->read, &ngx_posted_events); |
425 } | 436 } |
426 | 437 |
427 ngx_stream_proxy_connect(s); | 438 ngx_stream_proxy_connect(s); |
439 } | |
440 | |
441 | |
442 static ngx_int_t | |
443 ngx_stream_proxy_set_local(ngx_stream_session_t *s, ngx_stream_upstream_t *u, | |
444 ngx_stream_upstream_local_t *local) | |
445 { | |
446 if (local == NULL) { | |
447 u->peer.local = NULL; | |
448 return NGX_OK; | |
449 } | |
450 | |
451 if (local->addr) { | |
452 u->peer.local = local->addr; | |
453 return NGX_OK; | |
454 } | |
455 | |
456 return NGX_OK; | |
428 } | 457 } |
429 | 458 |
430 | 459 |
431 static void | 460 static void |
432 ngx_stream_proxy_connect(ngx_stream_session_t *s) | 461 ngx_stream_proxy_connect(ngx_stream_session_t *s) |
1635 static char * | 1664 static char * |
1636 ngx_stream_proxy_bind(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | 1665 ngx_stream_proxy_bind(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
1637 { | 1666 { |
1638 ngx_stream_proxy_srv_conf_t *pscf = conf; | 1667 ngx_stream_proxy_srv_conf_t *pscf = conf; |
1639 | 1668 |
1640 ngx_int_t rc; | 1669 ngx_int_t rc; |
1641 ngx_str_t *value; | 1670 ngx_str_t *value; |
1671 ngx_stream_upstream_local_t *local; | |
1642 | 1672 |
1643 if (pscf->local != NGX_CONF_UNSET_PTR) { | 1673 if (pscf->local != NGX_CONF_UNSET_PTR) { |
1644 return "is duplicate"; | 1674 return "is duplicate"; |
1645 } | 1675 } |
1646 | 1676 |
1649 if (ngx_strcmp(value[1].data, "off") == 0) { | 1679 if (ngx_strcmp(value[1].data, "off") == 0) { |
1650 pscf->local = NULL; | 1680 pscf->local = NULL; |
1651 return NGX_CONF_OK; | 1681 return NGX_CONF_OK; |
1652 } | 1682 } |
1653 | 1683 |
1654 pscf->local = ngx_palloc(cf->pool, sizeof(ngx_addr_t)); | 1684 local = ngx_palloc(cf->pool, sizeof(ngx_stream_upstream_local_t)); |
1655 if (pscf->local == NULL) { | 1685 if (local == NULL) { |
1656 return NGX_CONF_ERROR; | 1686 return NGX_CONF_ERROR; |
1657 } | 1687 } |
1658 | 1688 |
1659 rc = ngx_parse_addr(cf->pool, pscf->local, value[1].data, value[1].len); | 1689 pscf->local = local; |
1690 | |
1691 local->addr = ngx_palloc(cf->pool, sizeof(ngx_addr_t)); | |
1692 if (local->addr == NULL) { | |
1693 return NGX_CONF_ERROR; | |
1694 } | |
1695 | |
1696 rc = ngx_parse_addr(cf->pool, local->addr, value[1].data, value[1].len); | |
1660 | 1697 |
1661 switch (rc) { | 1698 switch (rc) { |
1662 case NGX_OK: | 1699 case NGX_OK: |
1663 pscf->local->name = value[1]; | 1700 local->addr->name = value[1]; |
1664 return NGX_CONF_OK; | 1701 break; |
1665 | 1702 |
1666 case NGX_DECLINED: | 1703 case NGX_DECLINED: |
1667 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | 1704 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
1668 "invalid address \"%V\"", &value[1]); | 1705 "invalid address \"%V\"", &value[1]); |
1669 /* fall through */ | 1706 /* fall through */ |
1670 | 1707 |
1671 default: | 1708 default: |
1672 return NGX_CONF_ERROR; | 1709 return NGX_CONF_ERROR; |
1673 } | 1710 } |
1674 } | 1711 |
1712 return NGX_CONF_OK; | |
1713 } |