From 9bf614cd0874e0ef9fda3145faacd3ef3a5fb0ed Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Thu, 26 May 2022 14:38:42 +0200 Subject: Var: Added $request_uri (as in NGINX). This supports a new variable $request_uri that contains the path and the query (See RFC 3986, section 3). Its contents are percent encoded. This is useful for example to redirect HTTP to HTTPS: { "return": "301", "location": "https://$host$request_uri" } When is requested, the server redirects to . === Testing: //diff --git a/src/nxt_http_return.c b/src/nxt_http_return.c //index 82c9156..adeb3a1 100644 //--- a/src/nxt_http_return.c //+++ b/src/nxt_http_return.c //@@ -196,6 +196,7 @@ nxt_http_return_send_ready(nxt_task_t *task, void *obj, void *data) // field->value = ctx->encoded.start; // field->value_length = ctx->encoded.length; // } //+ fprintf(stderr, "ALX: target[%1$i]: <%2$.*1$s>\n", (int)r->target.length, r->target.start); // // r->state = &nxt_http_return_send_state; // { "listeners": { "*:81": { "pass": "routes/ru" } }, "routes": { "ru": [{ "action": { "return": 301, "location": "$request_uri" } }] } } $ curl -i http://localhost:81/*foo%2Abar?baz#arg HTTP/1.1 301 Moved Permanently Location: /*foo%2Abar?baz Server: Unit/1.27.0 Date: Mon, 30 May 2022 16:04:30 GMT Content-Length: 0 $ sudo cat /usr/local/unit.log | grep ALX ALX: target[15]: --- src/nxt_http_variables.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/nxt_http_variables.c') diff --git a/src/nxt_http_variables.c b/src/nxt_http_variables.c index 1c0b561d..b765e177 100644 --- a/src/nxt_http_variables.c +++ b/src/nxt_http_variables.c @@ -9,6 +9,8 @@ static nxt_int_t nxt_http_var_method(nxt_task_t *task, nxt_var_query_t *query, nxt_str_t *str, void *ctx); +static nxt_int_t nxt_http_var_request_uri(nxt_task_t *task, + nxt_var_query_t *query, nxt_str_t *str, void *ctx); static nxt_int_t nxt_http_var_uri(nxt_task_t *task, nxt_var_query_t *query, nxt_str_t *str, void *ctx); static nxt_int_t nxt_http_var_host(nxt_task_t *task, nxt_var_query_t *query, @@ -20,6 +22,10 @@ static nxt_var_decl_t nxt_http_vars[] = { &nxt_http_var_method, 0 }, + { nxt_string("request_uri"), + &nxt_http_var_request_uri, + 0 }, + { nxt_string("uri"), &nxt_http_var_uri, 0 }, @@ -51,6 +57,20 @@ nxt_http_var_method(nxt_task_t *task, nxt_var_query_t *query, nxt_str_t *str, } +static nxt_int_t +nxt_http_var_request_uri(nxt_task_t *task, nxt_var_query_t *query, + nxt_str_t *str, void *ctx) +{ + nxt_http_request_t *r; + + r = ctx; + + *str = r->target; + + return NXT_OK; +} + + static nxt_int_t nxt_http_var_uri(nxt_task_t *task, nxt_var_query_t *query, nxt_str_t *str, void *ctx) -- cgit