Skip to content

Commit

Permalink
Simplify how Lua scripts can be specified in rewrite conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
lpereira committed Sep 28, 2021
1 parent 214b355 commit 9b89cdd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 73 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,26 +519,26 @@ It's also possible to specify conditions to trigger a rewrite. To specify one,
open a `condition` block, specify the condition type, and then the parameters
for that condition to be evaluated:

|Condition|Parameters|Description|
|Condition|Can use subst. syntax|Parameters|Description|
|---------|----------|-----------|
|`cookie` | A single `key` = `value`| Checks if request has cookie `key` has value `value` |
|`query` | A single `key` = `value`| Checks if request has query variable `key` has value `value` |
|`post` | A single `key` = `value`| Checks if request has post data `key` has value `value` |
|`header` | A single `key` = `value`| Checks if request header `key` has value `value` |
|`environment` | A single `key` = `value`| Checks if environment variable `key` has value `value` |
|`stat` | `path`, `is_dir`, `is_file` | Checks if `path` exists in the filesystem, and optionally checks if `is_dir` or `is_file` |
|`lua` | `script` | Runs Lua function `matches(req)` inside `script` and checks if it returns `true` or `false` |
|`encoding` | `deflate`, `gzip`, `brotli`, `zstd`, `none` | Checks if client accepts responses in a determined encoding (e.g. `deflate = yes` for Deflate encoding) |
|`proxied`| Boolean | Checks if request has been proxied through PROXY protocol |
|`http_1.0`| Boolean | Checks if request is made with a HTTP/1.0 client |
|`has_query_string`| Boolean | Checks if request has a query string (even if empty) |
|`method` | Method name | Checks if HTTP method is the one specified |

The `value` in all conditions, with the exception of those marked with ``,
can reference the matched pattern using the same substitution syntax used
for the `rewrite as` or `redirect to` actions. For instance, `condition
cookie { some-cookie-name = foo-%1-bar }` will substitute `%1` with the
first match from the pattern this condition is related to.
|`cookie` | Yes | A single `key` = `value`| Checks if request has cookie `key` has value `value` |
|`query` | Yes | A single `key` = `value`| Checks if request has query variable `key` has value `value` |
|`post` | Yes | A single `key` = `value`| Checks if request has post data `key` has value `value` |
|`header` | Yes | A single `key` = `value`| Checks if request header `key` has value `value` |
|`environment` | Yes | A single `key` = `value`| Checks if environment variable `key` has value `value` |
|`stat` | Yes | `path`, `is_dir`, `is_file` | Checks if `path` exists in the filesystem, and optionally checks if `is_dir` or `is_file` |
|`encoding` | No | `deflate`, `gzip`, `brotli`, `zstd`, `none` | Checks if client accepts responses in a determined encoding (e.g. `deflate = yes` for Deflate encoding) |
|`proxied` | No | Boolean | Checks if request has been proxied through PROXY protocol |
|`http_1.0`| No | Boolean | Checks if request is made with a HTTP/1.0 client |
|`has_query_string`| No | Boolean | Checks if request has a query string (even if empty) |
|`method`|No | Method name | Checks if HTTP method is the one specified |
|`lua`|No| String | Runs Lua function `matches(req)` inside String and checks if it returns `true` or `false` |

*Can use subst. syntax* refers to the ability to reference the matched
pattern using the same substitution syntax used for the `rewrite as` or
`redirect to` actions. For instance, `condition cookie { some-cookie-name =
foo-%1-bar }` will substitute `%1` with the first match from the pattern
this condition is related to.

Conditions marked with `` do not require a section, and can be written, for
instance, as `condition has_query_string = yes`.
Expand Down
64 changes: 10 additions & 54 deletions src/lib/lwan-mod-rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,54 +699,6 @@ static void parse_condition_accept_encoding(struct pattern *pattern,
}
}

#ifdef HAVE_LUA
static void parse_condition_lua(struct pattern *pattern,
struct config *config,
const struct config_line *line)
{
char *script = NULL;

while ((line = config_read_line(config))) {
switch (line->type) {
case CONFIG_LINE_TYPE_SECTION:
config_error(config, "Unexpected section: %s", line->key);
goto out;

case CONFIG_LINE_TYPE_SECTION_END:
if (!script) {
config_error(config, "Script not specified");
goto out;
}

pattern->condition.lua.script = script;
pattern->flags |= PATTERN_COND_LUA;
return;

case CONFIG_LINE_TYPE_LINE:
if (streq(line->key, "script")) {
if (script) {
config_error(config, "Script already specified");
goto out;
}
script = strdup(line->value);
if (!script) {
config_error(config, "Could not copy script");
goto out;
}
} else {
config_error(config, "Unexpected key: %s", line->key);
goto out;
}

break;
}
}

out:
free(script);
}
#endif

static bool get_method_from_string(struct pattern *pattern, const char *string)
{
#define GENERATE_CMP(upper, lower, mask, constant) \
Expand Down Expand Up @@ -792,11 +744,6 @@ static void parse_condition(struct pattern *pattern,
if (streq(line->value, "encoding")) {
return parse_condition_accept_encoding(pattern, config);
}
#ifdef HAVE_LUA
if (streq(line->value, "lua")) {
return parse_condition_lua(pattern, config, line);
}
#endif

config_error(config, "Condition `%s' not supported", line->value);
}
Expand Down Expand Up @@ -852,7 +799,16 @@ static bool rewrite_parse_conf_pattern(struct private_data *pd,
goto out;
}
pattern->flags |= PATTERN_COND_METHOD;
} else {
} else
#ifdef HAVE_LUA
if (streq(line->key, "condition_lua")) {
pattern->condition.lua.script = strdup(line->value);
if (!pattern->condition.lua.script)
lwan_status_critical("Couldn't copy Lua script");
pattern->flags |= PATTERN_COND_LUA;
} else
#endif
{
config_error(config, "Unexpected key: %s", line->key);
goto out;
}
Expand Down

0 comments on commit 9b89cdd

Please sign in to comment.