Implemented "location" option for "return" action.

This allows to specify redirects:

  {
      "action": {
          "return": 301,
          "location": "https://www.example.com/"
      }
  }
This commit is contained in:
Valentin Bartenev
2020-03-21 01:39:00 +03:00
parent 35d6f84426
commit c63b498f94
5 changed files with 60 additions and 2 deletions

View File

@@ -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
};

View File

@@ -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"),
};

View File

@@ -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,

View File

@@ -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);

View File

@@ -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;
@@ -581,6 +582,11 @@ static nxt_conf_map_t nxt_http_route_action_conf[] = {
NXT_CONF_MAP_PTR,
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,
@@ -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;