summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_router.c
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2019-02-27 16:41:11 +0300
committerIgor Sysoev <igor@sysoev.ru>2019-02-27 16:41:11 +0300
commitd4ccaae900f78b13923a9bd9ee7bbaa33c99b18b (patch)
tree6755a201d8b49f6945a213d76bf40d2b6ea7666d /src/nxt_router.c
parent95c9bba33bcb1d0e8e9e7c64d2591187ce11bab1 (diff)
downloadunit-d4ccaae900f78b13923a9bd9ee7bbaa33c99b18b.tar.gz
unit-d4ccaae900f78b13923a9bd9ee7bbaa33c99b18b.tar.bz2
Initial routing implementation.
Diffstat (limited to 'src/nxt_router.c')
-rw-r--r--src/nxt_router.c74
1 files changed, 45 insertions, 29 deletions
diff --git a/src/nxt_router.c b/src/nxt_router.c
index 5621abec..e81a2f96 100644
--- a/src/nxt_router.c
+++ b/src/nxt_router.c
@@ -31,7 +31,8 @@ typedef struct {
typedef struct {
- nxt_str_t application;
+ nxt_str_t pass;
+ nxt_str_t application;
} nxt_router_listener_conf_t;
@@ -163,8 +164,6 @@ static void nxt_router_conf_send(nxt_task_t *task,
static nxt_int_t nxt_router_conf_create(nxt_task_t *task,
nxt_router_temp_conf_t *tmcf, u_char *start, u_char *end);
static nxt_app_t *nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name);
-static nxt_app_t *nxt_router_listener_application(nxt_router_temp_conf_t *tmcf,
- nxt_str_t *name);
static void nxt_router_listen_socket_rpc_create(nxt_task_t *task,
nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf);
static void nxt_router_listen_socket_ready(nxt_task_t *task,
@@ -1174,11 +1173,14 @@ nxt_router_conf_error(nxt_task_t *task, nxt_router_temp_conf_t *tmcf)
nxt_queue_add(&new_socket_confs, &tmcf->pending);
nxt_queue_add(&new_socket_confs, &tmcf->creating);
+ rtcf = tmcf->router_conf;
+
+ nxt_http_routes_cleanup(task, rtcf->routes);
+
nxt_queue_each(skcf, &new_socket_confs, nxt_socket_conf_t, link) {
- if (skcf->application != NULL) {
- nxt_router_app_use(task, skcf->application, -1);
- skcf->application = NULL;
+ if (skcf->pass != NULL) {
+ nxt_http_pass_cleanup(task, skcf->pass);
}
} nxt_queue_loop;
@@ -1189,7 +1191,6 @@ nxt_router_conf_error(nxt_task_t *task, nxt_router_temp_conf_t *tmcf)
} nxt_queue_loop;
- rtcf = tmcf->router_conf;
router = rtcf->router;
nxt_queue_add(&router->sockets, &tmcf->keeping);
@@ -1299,8 +1300,14 @@ static nxt_conf_map_t nxt_router_app_processes_conf[] = {
static nxt_conf_map_t nxt_router_listener_conf[] = {
{
+ nxt_string("pass"),
+ NXT_CONF_MAP_STR_COPY,
+ offsetof(nxt_router_listener_conf_t, pass),
+ },
+
+ {
nxt_string("application"),
- NXT_CONF_MAP_STR,
+ NXT_CONF_MAP_STR_COPY,
offsetof(nxt_router_listener_conf_t, application),
},
};
@@ -1379,7 +1386,9 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
nxt_conf_value_t *conf, *http, *value;
nxt_conf_value_t *applications, *application;
nxt_conf_value_t *listeners, *listener;
+ nxt_conf_value_t *routes_conf;
nxt_socket_conf_t *skcf;
+ nxt_http_routes_t *routes;
nxt_event_engine_t *engine;
nxt_app_lang_module_t *lang;
nxt_router_app_conf_t apcf;
@@ -1392,6 +1401,7 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
static nxt_str_t http_path = nxt_string("/settings/http");
static nxt_str_t applications_path = nxt_string("/applications");
static nxt_str_t listeners_path = nxt_string("/listeners");
+ static nxt_str_t routes_path = nxt_string("/routes");
static nxt_str_t access_log_path = nxt_string("/access_log");
#if (NXT_TLS)
static nxt_str_t certificate_path = nxt_string("/tls/certificate");
@@ -1589,6 +1599,15 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
app_joint->free_app_work.obj = app_joint;
}
+ routes_conf = nxt_conf_get_path(conf, &routes_path);
+ if (nxt_fast_path(routes_conf != NULL)) {
+ routes = nxt_http_routes_create(task, tmcf, routes_conf);
+ if (nxt_slow_path(routes == NULL)) {
+ return NXT_ERROR;
+ }
+ tmcf->router_conf->routes = routes;
+ }
+
http = nxt_conf_get_path(conf, &http_path);
#if 0
if (http == NULL) {
@@ -1671,10 +1690,13 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
skcf->router_conf = tmcf->router_conf;
skcf->router_conf->count++;
- if (lscf.application.length > 0) {
- skcf->application = nxt_router_listener_application(tmcf,
- &lscf.application);
- nxt_router_app_use(task, skcf->application, 1);
+ if (lscf.pass.length != 0) {
+ skcf->pass = nxt_http_pass_create(task, tmcf, &lscf.pass);
+
+ /* COMPATIBILITY: listener application. */
+ } else if (lscf.application.length > 0) {
+ skcf->pass = nxt_http_pass_application(task, tmcf,
+ &lscf.application);
}
}
@@ -1712,6 +1734,8 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
tmcf->router_conf->access_log = access_log;
}
+ nxt_http_routes_resolve(task, tmcf);
+
nxt_queue_add(&tmcf->deleting, &router->sockets);
nxt_queue_init(&router->sockets);
@@ -1752,7 +1776,7 @@ nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name)
}
-static nxt_app_t *
+nxt_app_t *
nxt_router_listener_application(nxt_router_temp_conf_t *tmcf, nxt_str_t *name)
{
nxt_app_t *app;
@@ -2885,7 +2909,6 @@ nxt_router_listen_event_release(nxt_task_t *task, nxt_listen_event_t *lev,
void
nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint)
{
- nxt_app_t *app;
nxt_socket_conf_t *skcf;
nxt_router_conf_t *rtcf;
nxt_thread_spinlock_t *lock;
@@ -2904,7 +2927,6 @@ nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint)
* be already destroyed by another thread.
*/
skcf = joint->socket_conf;
- app = skcf->application;
rtcf = skcf->router_conf;
lock = &rtcf->router->lock;
@@ -2916,7 +2938,6 @@ nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint)
if (--skcf->count != 0) {
skcf = NULL;
rtcf = NULL;
- app = NULL;
} else {
nxt_queue_remove(&skcf->link);
@@ -2929,6 +2950,10 @@ nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint)
nxt_thread_spin_unlock(lock);
if (skcf != NULL) {
+ if (skcf->pass != NULL) {
+ nxt_http_pass_cleanup(task, skcf->pass);
+ }
+
#if (NXT_TLS)
if (skcf->tls != NULL) {
task->thread->runtime->tls->server_free(task, skcf->tls);
@@ -2936,16 +2961,14 @@ nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint)
#endif
}
- if (app != NULL) {
- nxt_router_app_use(task, app, -1);
- }
-
/* TODO remove engine->port */
/* TODO excude from connected ports */
if (rtcf != NULL) {
nxt_debug(task, "old router conf is destroyed");
+ nxt_http_routes_cleanup(task, rtcf->routes);
+
nxt_router_access_log_release(task, lock, rtcf->access_log);
nxt_mp_thread_adopt(rtcf->mem_pool);
@@ -4432,10 +4455,10 @@ nxt_router_app_port(nxt_task_t *task, nxt_app_t *app, nxt_req_app_link_t *ra)
void
-nxt_router_process_http_request(nxt_task_t *task, nxt_app_parse_ctx_t *ar)
+nxt_router_process_http_request(nxt_task_t *task, nxt_app_parse_ctx_t *ar,
+ nxt_app_t *app)
{
nxt_int_t res;
- nxt_app_t *app;
nxt_port_t *port;
nxt_event_engine_t *engine;
nxt_http_request_t *r;
@@ -4443,13 +4466,6 @@ nxt_router_process_http_request(nxt_task_t *task, nxt_app_parse_ctx_t *ar)
nxt_req_conn_link_t *rc;
r = ar->request;
- app = r->conf->socket_conf->application;
-
- if (app == NULL) {
- nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
engine = task->thread->engine;
rc = nxt_port_rpc_register_handler_ex(task, engine->port,