Commit Graph

9 Commits

Author SHA1 Message Date
Alejandro Colomar
1b05161107 Removed the unsafe nxt_memcmp() wrapper for memcmp(3).
The casts are unnecessary, since memcmp(3)'s arguments are 'void *'.
It might have been necessary in the times of K&R, where 'void *' didn't
exist.  Nowadays, it's unnecessary, and _very_ unsafe, since casts can
hide all classes of bugs by silencing most compiler warnings.

The changes from nxt_memcmp() to memcmp(3) were scripted:

$ find src/ -type f \
  | grep '\.[ch]$' \
  | xargs sed -i 's/nxt_memcmp/memcmp/'

Reviewed-by: Andrew Clayton <a.clayton@nginx.com>
Signed-off-by: Alejandro Colomar <alx@nginx.com>
2022-11-04 00:30:27 +01:00
Zhidao HONG
7aa6b06298 HTTP: added a $request_time variable. 2022-10-12 08:21:02 +08:00
Andrew Clayton
eebaff42ea Var: added a $dollar variable that translates to a '$'.
Allow $dollar (or ${dollar}) to translate to a literal $ to allow
support for sub-delimiters in URIs.

It is possible to have URLs like

  https://example.com/path/15$1588/9925$2976.html

and thus it would be useful to be able to specify them in various bits
of the unit config such as the location setting.

However this hadn't been possible due to $ being used to denote
variables for substitution. E.g $host.

As was noted in the below GitHub issue it was suggested by @VBart to
use $sign to represent a literal $, however I feel $dollar is more
appropriate so we have a variable named after the thing it represents,
also @tippexs found[0] that &dollar is used in HTML to represent a $, so
there is some somewhat related precedent.

(The other idea to use $$ was rejected in my original pull-request[1]
 for this issue.)

This means the above URL could be specified as

  https://example.com/path/15${dollar}1588/9925${dollar}2976.html

in the unit config.

This is done by adding a variable called 'dollar' which is loaded into
the variables hash table which translates into a literal $.

This is then handled in nxt_var_next_part() where variables are parsed
for lookup and $dollar is set for substitution by a literal '$'. Actual
variable substitution happens in nxt_var_query_finish().

[0]: https://github.com/nginx/unit/pull/693#issuecomment-1130412323
[1]: https://github.com/nginx/unit/pull/693

Closes: https://github.com/nginx/unit/issues/675
2022-07-20 23:28:02 +01:00
Zhidao HONG
8c5e2d5ce5 HTTP: added more variables.
This commit adds the following variables:
$remote_addr, $time_local, $request_line, $status,
$body_bytes_sent, $header_referer, $header_user_agent.
2022-07-14 04:34:05 +08:00
Zhidao HONG
45b89e3257 Var: dynamic variables support.
This commit adds the variables $arg_NAME, $header_NAME, and $cookie_NAME.
2022-07-14 04:32:49 +08:00
Zhidao HONG
0d2d40e231 Summary: Var: removing all async stuff.
No functional changes.
2022-06-02 09:36:35 +08:00
Alejandro Colomar
9bf614cd08 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>
2022-05-31 12:40:02 +02:00
Valentin Bartenev
70c2a4645e Vars: added $host.
This closes #407 issue on GitHub.
2020-08-28 19:34:49 +03:00
Valentin Bartenev
93146616cf Basic variables support. 2020-08-13 02:46:54 +03:00