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>
100 lines
1.7 KiB
C
100 lines
1.7 KiB
C
|
|
/*
|
|
* Copyright (C) NGINX, Inc.
|
|
*/
|
|
|
|
#include <nxt_router.h>
|
|
#include <nxt_http.h>
|
|
|
|
|
|
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,
|
|
nxt_str_t *str, void *ctx);
|
|
|
|
|
|
static nxt_var_decl_t nxt_http_vars[] = {
|
|
{ nxt_string("method"),
|
|
&nxt_http_var_method,
|
|
0 },
|
|
|
|
{ nxt_string("request_uri"),
|
|
&nxt_http_var_request_uri,
|
|
0 },
|
|
|
|
{ nxt_string("uri"),
|
|
&nxt_http_var_uri,
|
|
0 },
|
|
|
|
{ nxt_string("host"),
|
|
&nxt_http_var_host,
|
|
0 },
|
|
};
|
|
|
|
|
|
nxt_int_t
|
|
nxt_http_register_variables(void)
|
|
{
|
|
return nxt_var_register(nxt_http_vars, nxt_nitems(nxt_http_vars));
|
|
}
|
|
|
|
|
|
static nxt_int_t
|
|
nxt_http_var_method(nxt_task_t *task, nxt_var_query_t *query, nxt_str_t *str,
|
|
void *ctx)
|
|
{
|
|
nxt_http_request_t *r;
|
|
|
|
r = ctx;
|
|
|
|
*str = *r->method;
|
|
|
|
return NXT_OK;
|
|
}
|
|
|
|
|
|
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)
|
|
{
|
|
nxt_http_request_t *r;
|
|
|
|
r = ctx;
|
|
|
|
*str = *r->path;
|
|
|
|
return NXT_OK;
|
|
}
|
|
|
|
|
|
static nxt_int_t
|
|
nxt_http_var_host(nxt_task_t *task, nxt_var_query_t *query, nxt_str_t *str,
|
|
void *ctx)
|
|
{
|
|
nxt_http_request_t *r;
|
|
|
|
r = ctx;
|
|
|
|
*str = r->host;
|
|
|
|
return NXT_OK;
|
|
}
|