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 <http://example.com/foo%23bar?baz> is requested, the server
redirects to <https://example.com/foo%23bar?baz>.

===

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]: </*foo%2Abar?baz>
This commit is contained in:
Alejandro Colomar
2022-05-26 14:38:42 +02:00
parent 8027e7ce0f
commit 9bf614cd08
2 changed files with 26 additions and 0 deletions

View File

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