Lumen help
Expression language was design to perform operations on variables and values. It is used in all fields that expect an expression that evaluate to true or false (for instance matchRule expressions) and also in fields which accept values in the form of interpolated strings.
The syntax differs slightly between expressions and interpolated strings—most notably expressions accept values without a "$" sign and interpolated strings require every variable to be prefixed with a "$" sign. The output value of each is also (typically) different. Expressions must return a boolean value (true or false) and interpolated string are typically expected to return a non-empty string (for instance a new URI to use when requesting data from the origin server).
Operator |
Case-insenstive operator |
“English” operator equivalent |
Case-insensitive “English” operator |
Type |
Allows list argument? |
Result type |
Meaning |
== |
%= |
|
|
infix |
yes |
boolean |
Equality test |
!= |
!%= |
|
|
infix |
yes |
boolean |
Inequality test |
> |
%> |
|
|
infix |
yes |
boolean |
Greater than test |
< |
%< |
|
|
infix |
yes |
boolean |
Less than test |
>= |
%>= |
|
|
infix |
yes |
boolean |
Greater than or equal test |
<= |
%<= |
|
|
infix |
yes |
boolean |
Less than or equality test |
*= |
%*= |
globmatch |
globmatchi |
infix |
yes |
boolean |
Footprint glob match |
!*= |
!%*= |
!globmatch |
!globmatchi |
infix |
yes |
boolean |
Negated Footprint glob match |
/= |
%/= |
rexmatch |
rexmatchi |
infix |
yes |
string |
Footprint rex match |
!/= |
!%/= |
!rexmatch |
!rexmatchi |
infix |
yes |
boolean |
Negated Footprint rex match |
~= |
%~= |
regexmatch |
regexmatchi |
infix |
yes |
boolean |
Simple (PCRE) regex match |
!~= |
!%~= |
!regexmatch |
!regexmatchi |
infix |
yes |
boolean |
Negated Simple (PCRE) regex match |
|
|
ipmatch |
|
infix |
yes |
boolean |
Match against IP address or CIDR |
|
|
!ipmatch |
|
infix |
yes |
boolean |
Negated match against IP address or CIDR (IPv4, IPv6) |
+ |
|
|
|
infix |
no |
numeric |
Addition |
+ |
|
|
|
unary |
no |
numeric |
Positive numeric constant; forces result of expression to be numeric (result could be negative though) |
- |
|
|
|
infix |
no |
numeric |
Subtraction |
- |
|
|
|
unary |
no |
numeric |
Negate numeric constant; forces expression to be numeric and negate it (result could be positive though) |
* |
|
|
|
infix |
no |
numeric |
Multiplication |
/ |
|
|
|
infix |
no |
numeric |
Division |
% |
|
|
|
infix |
no |
unsigned or integer |
Modulus |
& |
|
|
|
|
no |
unsigned |
Bitwise AND |
| |
|
|
|
|
no |
unsigned |
Bitwise OR |
<< |
|
|
|
infix |
no |
unsigned |
Shift left |
>> |
|
|
|
infix |
no |
unsigned |
Shift right |
~ |
|
|
|
|
no |
unsigned |
Bitwise complement |
! |
|
|
|
unary |
no |
boolean |
Logical NOT |
. |
|
|
|
infix |
no |
string |
Concatenation |
? : |
|
|
|
ternary |
no |
* |
Conditional operator: <e> ? <e1> : <e2> Evaluates <e1> if <e> is true, <e2> otherwise. |
( ) |
|
|
|
grouping, function call |
no |
|
Used to override precedence and for function calls |
[ ] |
|
|
|
list |
no |
|
Creates a list of expressions to operate on |
There are two basic types of request and response objects in leapfrogd: "raw" and "cooked".
A "raw" request or response is read-only, and is the request or response as read off the wire. The raw request or response may not be available, e.g., in the case where the request or response was created internally.
The "cooked" request or response may be modified and is generally the result of applying normalization rules to a raw request or response, or the result of creating a request or response internally.
The variable hierarchy allows for access to generic requests (req.*) as well as explicitly the raw request (rreq.*) and the cooked request (creq.*). The same is true for the generic, explicitly raw, and explicitly cooked responses (respectively, resp.*, rresp.*, and cresp.*).
The "generic" request or response object corresponds to the cooked request or response, if present, else, the raw request or response. This is the form that should be used to access elements of requests and responses except in special circumstances.
Generally, accessing a cooked request or response will cause the cooked version of the request or response to be created (from a raw request or response) if necessary (and possible).
req.h.<name> |
rreq.h.<name> |
creq.h.<name> |
Request header <name> |
req.uri |
rreq.uri |
creq.uri |
Request URI |
req.uri.path |
rreq.uri.path |
creq.uri.path |
Request path |
req.uri.pathquery |
rreq.uri.pathquery |
creq.uri.pathquery |
Request path and query string |
req.uri.query |
rreq.uri.query |
creq.uri.query |
Request query string (cooked, raw) |
req.uri.query.<hk> |
rreq.uri.query.<hk> |
creq.uri.query.<hk> |
Request string string components (see below). |
req.uri.queryq |
rreq.uri.queryq |
creq.uri.queryq |
Request query string, including preceding "?", unless empty |
req.method |
rreq.method |
creq.method |
Request method |
resp.h.<name> |
rresp.h.<name> |
cresp.h.<name> |
Response header <name> |
resp.status |
rresp.status |
cresp.status |
Response status |
prop.<name> |
|
|
Transaction property <name> |
In the variable list above, <hk> denotes a "hierarchical key". These keys are used to perform lookups in multiple levels of a table-like structure, or to specify multiple components of a key in general.
The req.uri.query.<hk> variable allows access to individual components of the query string on a request. In this case, one or optional two components of the hierarchical key are processed. The first component is the name of the parameter, and the second is a numeric index that can be used to select the nth instance of the parameter, where a positive index counts from the beginning of the query string and a negative index counts from the end. For example, if the query string is ?foo=foo-1&bar=bar-1&foo=foo-2 then:
req.uri.query.foo evaluates to "foo-1".
req.uri.query.foo#1 evaluates to "foo-1" (first instance of "foo").
req.uri.query.foo#2 evaluates to "foo-2" (second instance of "foo").
req.uri.query.foo#-1 evaluates to "foo-2" (last instance of "foo").
req.uri.query.foo#-2 evaluates to "foo-1" (second to last instance of "foo").
req.uri.query.bar evaluates to "bar-1".
Note that all functions take expressions as arguments and convert the expression results to whatever form is needed.
Function types |
Function name |
Action |
Argument(s) |
Return type |
|
interp(…) |
Performs leapfrogd string interpolation on all of its’ arguments, returns concatenated result. |
Variable |
string |
Type Conversion |
unsigned(e) |
Convert expression to unsigned |
1 |
unsigned |
integer(e) |
Convert expression to integer |
1 |
integer |
|
real(e) |
Convert expression to real |
1 |
real |
|
string(e) |
Convert expression to string |
1 |
string |
|
boolean(e) |
Convert expression to boolean |
1 |
boolean |
|
String Conversion |
upper(e) |
Convert string to upper case. |
1 |
string |
lower(e) |
Convert string to lower case. |
1 |
string |
|
Date Conversion |
date2secs(date) |
Convert HTTP Date string to a Unix epoch timestamp |
1 |
integer |
secs2date(secs) |
Convert Unix epoch timestamp to an HTTP Date string |
1 |
string |
|
Encoding/Decoding |
hex(e [,flag]) |
Return hex-encoded string using lower-case letters. If flag is true, return with upper-case letters instead. |
1 or 2 |
string |
unhex(s) |
Hex-decode string s. |
1 |
string* |
|
base64(s) |
Base64 encode string s. |
1 |
string |
|
unbase64(s) |
Base64 decode string s. |
1 |
string* |
|
urlencode(s) |
Returns the URL-encoded (“percent-encoded”) version of s. |
1 |
string |
|
urlunecode(s) |
Decodes URL-encoded string s and returns the result. |
1 |
string |
|
Cryptography |
md5(e) |
Compute MD5 digest. |
1 |
string* |
sha1(e) |
Compute SHA1 digest. |
1 |
string* |
|
sha224(e) |
Compute SHA224 digest. |
1 |
string* |
|
sha256(e) |
Compute SHA256 digest. |
1 |
string* |
|
sha384(e) |
Compute SHA384 digest. |
1 |
string* |
|
sha512(e) |
Compute SHA512 digest. |
1 |
string* |
|
hmac(type, data, key) |
Compute HMAC. All arguments are strings. typedigest type (md5, sha1, sha224, sha256, sha384, sha512)datacontent for which to create the digestkeysigning key |
3 |
string* |
|
Regular Expression Match and Replace |
match(s, re) |
Match string s against regular expression re and return the matching portion. Returns nil if no match. |
2 |
string or nil |
match_replace(s, re, repl) |
Match string s against regular expression re and replace all matches with repl. Returns input string if nothing matches. |
3 |
string |
|
URL Parsing |
url_path(url) |
Returns path component of url. |
1 |
string |
url_query(url) |
Returns query string component of url. |
1 |
string |
|
url_frag(url) |
Returns fragment component of url. |
1 |
string |
|
url_scheme(url) |
Returns scheme of url. |
1 |
string |
|
url_host(url) |
Returns host of url. |
1 |
string |
|
url_port(url) |
Returns port of url. (Use unsigned() or integer() if the on the return value to get the numeric value of the port.) |
1 |
string |
|
url_user(url) |
Returns the user (from the authority component) of url. |
1 |
string |
|
URL Manipulation |
add_query(url, param [,value]) |
Adds query parameter param to url with optional value value and returns the result. |
2 or 3 |
string |
remove_query(url, param) |
Removes query parameter param (and its value, if any) from url and returns the result. |
2 |
string |
|
Path parsing and manipulation |
path_element(p, n [,m]) |
Returns a path element or elements from the path p. If only n is given, returns the nth element. If m is also given, returns elements n through m (inclusive). If n or m is negative, is names a path element counted from the last element in the path (e.g., -1 is the last element). Both n and m are 1-based, i.e., 1 is the first element, 2 is the second, etc. |
2 or 3 |
string |
path_join(p, [e [,…]]) |
Appends path elements to path p, ensuring that one and only one slash separates them. The result always has a leading slash and never has a trailing slash. |
1 or more |
string |
This page describes the leapfrogd expression language. The expression language features both infix and unary operators including:
Operator |
Case-insenstive operator |
“English” operator equivalent |
Case-insensitive “English” operator |
Type |
Allows list argument? |
Result type |
Meaning |
== |
%= |
|
|
infix |
yes |
boolean |
Equality test |
!= |
!%= |
|
|
infix |
yes |
boolean |
Inequality test |
> |
%> |
|
|
infix |
yes |
boolean |
Greater than test |
< |
%< |
|
|
infix |
yes |
boolean |
Less than test |
>= |
%>= |
|
|
infix |
yes |
boolean |
Greater than or equal test |
<= |
%<= |
|
|
infix |
yes |
boolean |
Less than or equality test |
*= |
%*= |
globmatch |
globmatchi |
infix |
yes |
boolean |
Footprint glob match |
!*= |
!%*= |
!globmatch |
!globmatchi |
infix |
yes |
boolean |
Negated Footprint glob match |
/= |
%/= |
rexmatch |
rexmatchi |
infix |
yes |
string |
Footprint rex match |
!/= |
!%/= |
!rexmatch |
!rexmatchi |
infix |
yes |
boolean |
Negated Footprint rex match |
~= |
%~= |
regexmatch |
regexmatchi |
infix |
yes |
boolean |
Simple (PCRE) regex match |
!~= |
!%~= |
!regexmatch |
!regexmatchi |
infix |
yes |
boolean |
Negated Simple (PCRE) regex match |
|
|
ipmatch |
|
infix |
yes |
boolean |
Match against IP address or CIDR |
|
|
!ipmatch |
|
infix |
yes |
boolean |
Negated match against IP address or CIDR (IPv4, IPv6) |
+ |
|
|
|
infix |
no |
numeric |
Addition |
+ |
|
|
|
unary |
no |
numeric |
Positive numeric constant; forces result of expression to be numeric (result could be negative though) |
- |
|
|
|
infix |
no |
numeric |
Subtraction |
- |
|
|
|
unary |
no |
numeric |
Negate numeric constant; forces expression to be numeric and negate it (result could be positive though) |
* |
|
|
|
infix |
no |
numeric |
Multiplication |
/ |
|
|
|
infix |
no |
numeric |
Division |
% |
|
|
|
infix |
no |
unsigned or integer |
Modulus |
& |
|
|
|
|
no |
unsigned |
Bitwise AND |
| |
|
|
|
|
no |
unsigned |
Bitwise OR |
<< |
|
|
|
infix |
no |
unsigned |
Shift left |
>> |
|
|
|
infix |
no |
unsigned |
Shift right |
~ |
|
|
|
|
no |
unsigned |
Bitwise complement |
! |
|
|
|
unary |
no |
boolean |
Logical NOT |
. |
|
|
|
infix |
no |
string |
Concatenation |
? : |
|
|
|
ternary |
no |
* |
Conditional operator: <e> ? <e1> : <e2> Evaluates <e1> if <e> is true, <e2> otherwise. |
( ) |
|
|
|
grouping, function call |
no |
|
Used to override precedence and for function calls |
[ ] |
|
|
|
list |
no |
|
Creates a list of expressions to operate on |
Additionally, two function-like operators are supported:
list(exprlist) - Indirect list. Each expression in the given expression list is evaluated, and the result of the evaluation is then interpreted as a list. This is useful when the list of items to operate on is being held in a note or other temporary variable.
ident(string-literal) - Arbitrary identifier. The value of the string literal is interpreted as an identifier name. This is useful in cases where an identifier needs to contain non-identifier characters. A good example of this is when using resp.h.something or req.h.something to refer to a header where the header name contains non-identifier characters.
The expression languages supports three types of pattern matching:
glob (aka wildcard) matching: The *= operator and its variations.
Simple PCRE regex matching: The ~= operator and its variations.
“Rex” (PCRE with replacement string and flags) matching: The /= operator and its variations.
Operator: *= and its variations
Glob matching is the simplest and most familiar form of pattern matching supported by the expression language. Globs generally are matched literally, unless the following special characters are used:
* - match any sequence of characters, including none.
? - match a single character.
[ … ] - match any character within the brackets.
[^ … ] - match any character not within the brackets.
+ - match up until the next forward slash. This is useful for matching a portion of a single path component.
c{n,m} - match character c at least n but not more than m times. The form c{m} matches c exactly m times. The form c{m,} matches c at least m times.
For example, to match all paths ending in “.html”:
To match on a path prefix (“/my/prefix” in this example):
To match on single path component, “mypath”, anywhere in the path:
Glob matching works with any variable on the left hand side of the rule, not just req.uri.path. For example, to match “MSIE” anywhere in the User-Agent field of a request:
The expression language supports simple matching on regular expressions using the ~= operator and its variations. With these operators, the right hand side is interpreted as a PCRE (Perl-Compatible Regular Expression).
Regular expressions offer far more powerful pattern matching capabilities than globs, but are generally less efficient (even in relatively simple forms) and can be harder to read. The use of globs is preferred in cases where the glob match functionality can meet the requirements.
This document does not describe the PCRE syntax in any detail. For more information, refer to the PCRE documentation.
Some common pitfalls of using regular expressions:
Regular expressions are not anchored by default. That means that unless explicitly anchored, a regular expression can match anywhere in the subject string (the left hand side of the expression) where a match is possible. For example, the regular expression foo matches “foo”, “foofoo”, “some computer engineers use ‘foo’ a lot but no one knows what it means”, etc.
There are many special characters in regular expressions, far more than there are in globs. This includes dot (“.”), which matches any character. Therefore, the regular expression foo.html matches “foo.html” and “fooFhtml”, etc.
A star (“*”) in a regular expression does not mean the same as it does in a glob. Rather, it modifies the meaning of the prior match to mean “match any number of times, including 0”. So f* matches “f”, and also matches “fff” (anywhere in the subject string). It also matches zero occurrences of “f”. This means that even if the subject string has no “f”s at all, f* will match it.
A common case where a regular expression match is useful is in alternation, that is, matching two or more alternate sequences of characters. For example, the following is true if the path ends in “.jpeg”, “.jpg”, or “.gif”:
This example illustrates some other common pitfalls of regular expressions:
The backslash before the initial dot (“.”) removes the special meaning of dot (which normally means to match any character) and requires matching a literal dot. Backslash escapes are common in regular expressions, but are also the escape character used in a regular string in the expression language, and also in JSON strings that generally carry expressions. This example uses a raw string constructor (the initial r) to avoid requiring a “double backslash”.
Frequently users of regular expressions forget to use appropriate anchors. This regular expression is anchored at the end of the string thanks to the $ special character, and so therefore requires that the string end with “.jpeg”, “.jpg”, or “.gif”. Note that it is not anchored at the beginning of the string (which would be indicated by a ^ character as the first character of the regular expression), which is appropriate because we want to match any initial path components and filenames up to the final suffix.
When all that is wanted is a simple regex match/doesn’t match test, the ~= operator and its variations are the operators to use.
The /= operator and its variations support an extended form of regular expression that includes a replacement string and optional flags. The name for this construct is “rex”. This form requires the use of a delimiter to delimit the various portions of the expression. The canonical form is:
Here, the delimiter character is a slash (“/”). However, any delimiter character may be chosen. The requirement is that the delimiter cannot appear anywhere in the regular expression, the replacement string, or the flags (not even if escaped by a backslash).
The rex form is documented externally here. (This is the documentation for the library facility that implements support for rexes.)
If the given regular expression (a PCRE regular expression, same as is used with the ~= operator) matches, then the result is the value of the replacement string, or an empty string if no replacement string has been specified. If the regular expression does not match, then the result is nil. This means that the /= operator and its variations can be used in boolean context with the same meaning as the ~= operator (but still requiring a delimited rex rather than a simple regular expression).
Note that when negated or when used in lists, the /= form reverts to boolean-only semantics. Only the /= and %/= operators provide access to the replacement string.
By default, the replacement string only replaces the first portion of the subject string that is matched by the regular expression. Therefore, given /foo/bar as a rex, the following holds:
“This is foo” becomes “This is bar”.
“This is foo and here’s another foo” becomes “This is bar and here’s another foo”.
To replace ALL matches, use the g flag: /foo/bar/g:
“This is foo and here’s another foo” becomes “This is bar and here’s another bar”.
Finally, to get only the value of the replacement string, use the t flag: /foo/bar/t
“This is foo” becomes “bar”.
“This is foo and here’s another foo” also becomes “bar”.
The t flag is handy when using subgroups and wanting to evaluate only to the portion matched by a subgroup. For example, a handy rex to retrieve the last path component of a URI is #([^/]*)$#\1#t. Note that use of alternate delimiter (“#”) in this case since the regular expression contains a slash. Without the ‘t’ flag the equivalent expression is a bit less intuitive: #^.*?([^/]*)$#\1#
Finally, note that the b (basic regex) and e (extended regex) flags are suppressed by the expression language. The expression language only supports PCRE.
Keywords
Keyword |
Meaning |
and |
Logical AND |
or |
Logical OR |
not |
Logical NOT |
any |
List modifier, true if any comparisons are true: any [1,2,3] |
all |
List modifier, true if all comparisons are true: all [1,2,3] |
nil |
No value (distinct from empty value |
true |
Boolean constant: true |
false |
Boolean constant: false |
list |
Indirect list operator: Takes a list of expressions in parentheses. Each is evaluated as a string, and then compiled as a list. The concatenation of the resulting lists becomes the list being acted on. Allows any and all before the list keyword. |
Generally speaking, comparisons may be made against lists of values or expressions, as noted above. The type of list may be specified using the "any" and "all" keywords. Generally speaking, positive comparisons (e.g., equality) default to "any" lists, and negative comparisons (e.g., inequality) default to "all" lists. For instance, "resp.status == [ 200, 201 ]" is true if the response status is 200 or 201. "resp.status != [ 200, 201 ]" is true if the response status is not equal to 200 and not equal to 201.
Note that indirect lists (where the list values are contained in some variable, e.g., a transaction note or a header) are possible via the list operator. For example, if there’s an identifier “note.aliaslist” that has the value “‘alias1’, ‘alias2’, ‘alias3’”, then the following statements are equivalent:
Types and promotion rules
The expression language features the following types, in ranked order, from highest to lowest:
Type |
Notes |
From string |
From real |
From integer |
From unsigned |
boolean |
The result of a comparison or a negation |
nil is false, all else are true |
0 false, non-0 true |
0 false, non-0 true |
0 false, non-0 true |
string |
Result of a string operation |
|
%lf C99 printf format |
%jd C99 printf format |
%ju C99 printf format |
real |
Double-precision floating point number |
|
|
Integer (by truncation), sign maintained |
Integer (by truncation), no sign |
integer |
Signed 64-bit integer |
|
|
|
Considered as unsigned (no other conversion, so negative integers become large unsigned values) |
unsigned |
Unsigned 64-bit integer |
|
|
|
|
Generally speaking, comparison tests use the highest ranked type. For example, comparing a string to a boolean results in the string being promoted to boolean first.
There is no automatic conversion to numeric form for the numeric operators. The unary “+” operator may be used to force a string or boolean to a numeric value, however. Strings are converted to the highest required numeric type, and booleans are converted to unsigned (0 for false, 1 for true).
Constants
Constant values of the supported types are specified as follows:
Type |
Constant specification |
boolean |
Keywords: true and false. |
string |
Single- or double-quoted string of characters, with C99-standard escape sequences supported, e.g., “This is a string”, and ‘so is this’, and “This string has “embedded” quotes in it.” |
string |
The letter “r” followed by any non-alphanumeric character (the delimiter), followed by a string of characters not including the delimiter, followed by the delimiter. Escape sequences are NOT supported. Therefore, the delimiter cannot be present in the string, but backslash is not a special character. So r’backslash () is a common escape character’ works as expected, whereas with a single or double-quoted string, you’d have to write: “backslash (\) is a common escape character”. |
string |
Keyword: nil (specifies a missing but not empty string value) |
unsigned |
A sequence of decimal digits not including a decimal point (“.”) and not prefixed with a sign (“+” or “-“), OR “0x” or ” 0X” followed by a sequence of hexadecimal digits (also not prefixed with a sign). |
integer |
A sign (“+” or “-“) followed by an unsigned value. |
real |
A sequence of digits including a decimal point (“.”), and optionally prefixed by a sign. |
Notes:
All decimal numeric types also allow the specification of an exponent. This is indicated by the letter “e” followed by the exponent (a positive or negative integer).
All hexadecimal numbers support a power-of-two exponent. This is indicated by the letter “p” followed by the exponent (a positive or negative integer).
Variables
There are two basic types of request and response objects in leapfrogd: “raw” and “cooked”.
A “raw” request or response is read-only, and is the request or response as read off the wire. The raw request or response may not be available, e.g., in the case where the request or response was created internally.
The “cooked” request or response may be modified and is generally the result of applying normalization rules to a raw request or response, or the result of creating a request or response internally.
The variable hierarchy allows for access to generic requests (req.*) as well as explicitly the raw request (rreq.*) and the cooked request (creq.*). The same is true for the generic, explicitly raw, and explicitly cooked responses (respectively, resp.*, rresp.*, and cresp.*).
The “generic” request or response object corresponds to the cooked request or response, if present, else, the raw request or response. This is the form that should be used to access elements of requests and responses except in special circumstances.
Generally, accessing a cooked request or response will cause the cooked version of the request or response to be created (from a raw request or response) if necessary (and possible).
Function types |
Function name |
Action |
Argument(s) |
Return type |
|
interp(…) |
Performs leapfrogd string interpolation on all of its’ arguments, returns concatenated result. |
Variable |
string |
Type Conversion |
unsigned(e) |
Convert expression to unsigned |
1 |
unsigned |
integer(e) |
Convert expression to integer |
1 |
integer |
|
real(e) |
Convert expression to real |
1 |
real |
|
string(e) |
Convert expression to string |
1 |
string |
|
boolean(e) |
Convert expression to boolean |
1 |
boolean |
|
String Conversion |
upper(e) |
Convert string to upper case. |
1 |
string |
lower(e) |
Convert string to lower case. |
1 |
string |
|
Date Conversion |
date2secs(date) |
Convert HTTP Date string to a Unix epoch timestamp |
1 |
integer |
secs2date(secs) |
Convert Unix epoch timestamp to an HTTP Date string |
1 |
string |
|
Encoding/Decoding |
hex(e [,flag]) |
Return hex-encoded string using lower-case letters. If flag is true, return with upper-case letters instead. |
1 or 2 |
string |
unhex(s) |
Hex-decode string s. |
1 |
string* |
|
base64(s) |
Base64 encode string s. |
1 |
string |
|
unbase64(s) |
Base64 decode string s. |
1 |
string* |
|
urlencode(s) |
Returns the URL-encoded (“percent-encoded”) version of s. |
1 |
string |
|
urlunecode(s) |
Decodes URL-encoded string s and returns the result. |
1 |
string |
|
Cryptography |
md5(e) |
Compute MD5 digest. |
1 |
string* |
sha1(e) |
Compute SHA1 digest. |
1 |
string* |
|
sha224(e) |
Compute SHA224 digest. |
1 |
string* |
|
sha256(e) |
Compute SHA256 digest. |
1 |
string* |
|
sha384(e) |
Compute SHA384 digest. |
1 |
string* |
|
sha512(e) |
Compute SHA512 digest. |
1 |
string* |
|
hmac(type, data, key) |
Compute HMAC. All arguments are strings. typedigest type (md5, sha1, sha224, sha256, sha384, sha512)datacontent for which to create the digestkeysigning key |
3 |
string* |
|
Regular Expression Match and Replace |
match(s, re) |
Match string s against regular expression re and return the matching portion. Returns nil if no match. |
2 |
string or nil |
match_replace(s, re, repl) |
Match string s against regular expression re and replace all matches with repl. Returns input string if nothing matches. |
3 |
string |
|
URL Parsing |
url_path(url) |
Returns path component of url. |
1 |
string |
url_query(url) |
Returns query string component of url. |
1 |
string |
|
url_frag(url) |
Returns fragment component of url. |
1 |
string |
|
url_scheme(url) |
Returns scheme of url. |
1 |
string |
|
url_host(url) |
Returns host of url. |
1 |
string |
|
url_port(url) |
Returns port of url. (Use unsigned() or integer() if the on the return value to get the numeric value of the port.) |
1 |
string |
|
url_user(url) |
Returns the user (from the authority component) of url. |
1 |
string |
|
URL Manipulation |
add_query(url, param [,value]) |
Adds query parameter param to url with optional value value and returns the result. |
2 or 3 |
string |
remove_query(url, param) |
Removes query parameter param (and its value, if any) from url and returns the result. |
2 |
string |
|
Path parsing and manipulation |
path_element(p, n [,m]) |
Returns a path element or elements from the path p. If only n is given, returns the nth element. If m is also given, returns elements n through m (inclusive). If n or m is negative, is names a path element counted from the last element in the path (e.g., -1 is the last element). Both n and m are 1-based, i.e., 1 is the first element, 2 is the second, etc. |
2 or 3 |
string |
path_join(p, [e [,…]]) |
Appends path elements to path p, ensuring that one and only one slash separates them. The result always has a leading slash and never has a trailing slash. |
1 or more |
string |
Lua bindings
Three lua bindings have been created for evaluating expressions:
leapfrog.expr_test(<expression>): Test whether an expression is valid. Returns a flag (true or false) and an error message (or nil if no error).
leapfrog.expr_val(<txn>, <expression>): Evaluate the expression and return the result.
leapfrog.expr_val_bool(<txn>, <expression>): Evaluate the expression, convert the result to a boolean value, and return the result.
Both of the bindings take a transaction object and an expression (as a string). If errors occur during evaluation, a lua error is thrown. the lua pcall() mechanism may be used to do a protected call of an expression evaluation so that any errors can be caught and reported.
Examples
Example |
Effect |
req.h.host == "customer.com" |
Check if Host header on request is ”customer.com” |
resp.status == [ 400, 401, 402, 403, 405 ] |
Check if the response status is 400, 401, 402, 403, or 405 |
req.h.If-Modified-Since != nil |
Check if request has no If-Modified-Since header |
req.h.host = "new-customer.com" |
Assignment: Change host header on request to “new-customer.com” |
req.h.host = note.alias . "-customer.com" |
Assignment: Set the host header to the value of the “alias” transaction note concatenated with “-customer.com” |
2 * 3 + 4 |
Math: Result is 10 |
(2 * 3) + 4 |
Math: Result is 10 |
2 * (3 + 4) |
Math (showing precedence override): Result is 14 |
not (req.h.host == "foo.com" and req.h.user-agent *= "*MSIE*") |
Negated expression, and glob match operator: True unless the Host header is “foo.com” and the User-Agent header contains “MSIE” |
req.h.host = interp("$note.alias") |
Set host header to the value of the “alias” note, using the string interpolation function (legacy leapfrogd string interpolation) |
resp.status >= 400 and resp.status < 500 |
Check if response status is 4xx |
Additional notes
The existence of “English” operators (e.g. "globmatch") leads to complications in parsing , since "globmatch" would also be recognized as an identifier. For the Enlish operators se to be correctly used as an operator, they must be followed by a non-identifier character. This allows for the “first-match-wins” semantics to be used.
It should be noted that whitespace is only necessary to resolve lexical ambiguity across different tokens or expressions.
Example:
“123.45” is a numeric constant
“123 . 45” is a concatenation expression
While that is the case, generally whitespace use is recommended for increased readability within expressions.
Learn more about
Media portal
Explore Media portal
Top 10 articles