diff options
author | Valentin Bartenev <vbart@nginx.com> | 2020-03-21 01:39:00 +0300 |
---|---|---|
committer | Valentin Bartenev <vbart@nginx.com> | 2020-03-21 01:39:00 +0300 |
commit | c63b498f9416d26c1288a86ae4fc0b6007a16142 (patch) | |
tree | 7fe911579cc7f3aac50a0a649236195127cf767e /src | |
parent | 35d6f84426cfaa27587456a8ebb81b13f60e697a (diff) | |
download | unit-c63b498f9416d26c1288a86ae4fc0b6007a16142.tar.gz unit-c63b498f9416d26c1288a86ae4fc0b6007a16142.tar.bz2 |
Implemented "location" option for "return" action.
This allows to specify redirects:
{
"action": {
"return": 301,
"location": "https://www.example.com/"
}
}
Diffstat (limited to 'src')
-rw-r--r-- | src/nxt_conf_validation.c | 5 | ||||
-rw-r--r-- | src/nxt_h1proto.c | 2 | ||||
-rw-r--r-- | src/nxt_http.h | 2 | ||||
-rw-r--r-- | src/nxt_http_return.c | 15 | ||||
-rw-r--r-- | src/nxt_http_route.c | 38 |
5 files changed, 60 insertions, 2 deletions
diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c index ad921a7e..3227a7e9 100644 --- a/src/nxt_conf_validation.c +++ b/src/nxt_conf_validation.c @@ -362,6 +362,11 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_return_action_members[] = { &nxt_conf_vldt_return, NULL }, + { nxt_string("location"), + NXT_CONF_VLDT_STRING, + NULL, + NULL }, + NXT_CONF_VLDT_END }; diff --git a/src/nxt_h1proto.c b/src/nxt_h1proto.c index abc92dd4..5e3b2f82 100644 --- a/src/nxt_h1proto.c +++ b/src/nxt_h1proto.c @@ -1103,6 +1103,8 @@ static const nxt_str_t nxt_http_redirection[] = { nxt_string("HTTP/1.1 302 Found\r\n"), nxt_string("HTTP/1.1 303 See Other\r\n"), nxt_string("HTTP/1.1 304 Not Modified\r\n"), + nxt_string("HTTP/1.1 307 Temporary Redirect\r\n"), + nxt_string("HTTP/1.1 308 Permanent Redirect\r\n"), }; diff --git a/src/nxt_http.h b/src/nxt_http.h index a86b77f9..638affc8 100644 --- a/src/nxt_http.h +++ b/src/nxt_http.h @@ -23,6 +23,8 @@ typedef enum { NXT_HTTP_FOUND = 302, NXT_HTTP_SEE_OTHER = 303, NXT_HTTP_NOT_MODIFIED = 304, + NXT_HTTP_TEMPORARY_REDIRECT = 307, + NXT_HTTP_PERMANENT_REDIRECT = 308, NXT_HTTP_BAD_REQUEST = 400, NXT_HTTP_FORBIDDEN = 403, diff --git a/src/nxt_http_return.c b/src/nxt_http_return.c index 770f5289..c466cc25 100644 --- a/src/nxt_http_return.c +++ b/src/nxt_http_return.c @@ -14,6 +14,7 @@ nxt_http_action_t * nxt_http_return_handler(nxt_task_t *task, nxt_http_request_t *r, nxt_http_action_t *action) { + nxt_http_field_t *field; nxt_http_status_t status; status = action->u.return_code; @@ -27,6 +28,20 @@ nxt_http_return_handler(nxt_task_t *task, nxt_http_request_t *r, r->status = status; r->resp.content_length_n = 0; + + if (action->name.length > 0) { + field = nxt_list_zero_add(r->resp.fields); + if (nxt_slow_path(field == NULL)) { + nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); + return NULL; + } + + nxt_http_field_name_set(field, "Location"); + + field->value = action->name.start; + field->value_length = action->name.length; + } + r->state = &nxt_http_return_send_state; nxt_http_request_header_send(task, r, NULL, NULL); diff --git a/src/nxt_http_route.c b/src/nxt_http_route.c index 6403a005..ee22f48d 100644 --- a/src/nxt_http_route.c +++ b/src/nxt_http_route.c @@ -42,6 +42,7 @@ typedef enum { typedef struct { nxt_conf_value_t *pass; nxt_conf_value_t *ret; + nxt_str_t location; nxt_conf_value_t *share; nxt_conf_value_t *proxy; nxt_conf_value_t *fallback; @@ -582,6 +583,11 @@ static nxt_conf_map_t nxt_http_route_action_conf[] = { offsetof(nxt_http_route_action_conf_t, ret) }, { + nxt_string("location"), + NXT_CONF_MAP_STR, + offsetof(nxt_http_route_action_conf_t, location) + }, + { nxt_string("share"), NXT_CONF_MAP_PTR, offsetof(nxt_http_route_action_conf_t, share) @@ -606,6 +612,7 @@ nxt_http_route_action_create(nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *cv, nxt_mp_t *mp; nxt_int_t ret; nxt_str_t name, *string; + nxt_uint_t encode; nxt_conf_value_t *conf; nxt_http_route_action_conf_t accf; @@ -619,9 +626,38 @@ nxt_http_route_action_create(nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *cv, nxt_memzero(action, sizeof(nxt_http_action_t)); + mp = tmcf->router_conf->mem_pool; + if (accf.ret != NULL) { action->handler = nxt_http_return_handler; action->u.return_code = nxt_conf_get_integer(accf.ret); + + if (accf.location.length > 0) { + if (nxt_is_complex_uri_encoded(accf.location.start, + accf.location.length)) + { + string = nxt_str_dup(mp, &action->name, &accf.location); + if (nxt_slow_path(string == NULL)) { + return NXT_ERROR; + } + + } else { + string = &action->name; + + encode = nxt_encode_complex_uri(NULL, accf.location.start, + accf.location.length); + string->length = accf.location.length + encode * 2; + + string->start = nxt_mp_nget(mp, string->length); + if (nxt_slow_path(string->start == NULL)) { + return NXT_ERROR; + } + + nxt_encode_complex_uri(string->start, accf.location.start, + accf.location.length); + } + } + return NXT_OK; } @@ -637,8 +673,6 @@ nxt_http_route_action_create(nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *cv, nxt_conf_get_string(conf, &name); - mp = tmcf->router_conf->mem_pool; - string = nxt_str_dup(mp, &action->name, &name); if (nxt_slow_path(string == NULL)) { return NXT_ERROR; |