Variables¶
Cripts supports a wide variety of variable types, and they can be used in many places. In
many cases, the variable types are automatically converted to the correct type, but in some
cases you may need to explicitly declare the variable. Most commonly you will declare a
variable using either auto
or borrow
. The latter is used when working with a
complex object, like a URL or a header, that is managed by ATS itself.
For auto
variables, these are the most common types:
Type |
Description |
---|---|
|
A 64-bit signed integer. |
|
A 64-bit floating point number. |
|
A boolean value. |
|
A string. |
|
A string view. |
Of these, string_view
is perhaps the least familiar type, but it is the most commonly used
type of strings within ATS itself. As such, they are incredibly efficient to use. Again, most of the
time just declare your variables as auto
and let the compiler figure out the type.
Strings¶
String and string_views
are probably the types you will work with most. These types
have a number of methods that can be used to manipulate the string. Strings here are secretly
wrapped in standard C++ strings, and have all the methods that come with them. As an example, to
get the length of a string, you can use the size()
method:
borrow req = cripts::Client::Request::Get();
if (req["Host"].size() > 3) {
// Do something
}
Here's a list of methods available on strings:
Method |
Description |
---|---|
|
Clear (empty) the string. |
|
Check if the string is empty or not, returns |
|
Return the length of the string. Also available as |
|
Check if the string starts with a given string. |
|
Check if the string ends with a given string. |
|
Find a string within the string. |
|
Find a string within the string, starting from the end. |
|
Check if the string contains a given string. |
|
Get a substring of the string, arguments are |
|
Split the string into a list of strings, using a delimiter. Returns a list. |
|
Trim whitespace from the string. |
|
Trim whitespace from the left of the string. |
|
Trim whitespace from the right of the string. |
|
Remove a prefix string from the string. This does not return a new string. |
|
Remove a suffix string from the string. This does not return a new string. |
|
Convert the string to a float. |
|
Convert the string to a boolean. |
|
Convert the string to an integer. |
|
Convert the string to a float. |
In addition to this, there's a number of matching features in Cripts, which can be used together
with strings. These are covered in more detail in the Matchers section. Of course,
regular comparisons such as ==
and !=
are also available.
注釈
The find()
and rfind()
methods return the position of the string within the string, or
cripts::string_view::npos
if the string is not found.
Configuration Variable¶
ATS has a flexible set of configurations, many of which can be accessed and modified using Cripts.
These configurations are, in ATS terms, what's called overridable. They all have a default value,
that is set globally via records.yaml
, but they are also overridable per transaction.
Cript exposes this via the global proxy
object, which is a map of all the configurations.
Best way to understand this is to look at an example:
auto cache_on = proxy.config.http.cache.http.Get();
if (cache_on > 0) {
proxy.config.http.ignore_server_no_cache.Set(1);
}
This is a pretty artificial example, but shows the name space of these configurations, and how they match the documented ATS configuration names.
The configurations can also be access via a more advanced API, typically used for embedding existing
control planes with Cripts. This is done using the Records
object, for example:
do_remap() {
auto http_cache = cripts::cripts::Records("proxy.config.http.cache.http");
if (AsInteger(http_cache.Get()) > 0) {
CDebug("HTTP Cache is on");
}
}
Control Variable¶
ATS plugins (and Cripts) have access to a number of control functions that can be used to control
some behavior of ATS. This is exposed via the global control
object. This object has a number of
variables that can be used to control the behavior of ATS.
Variable |
Description |
---|---|
|
Control the cache behavior for the request. |
|
Control the cache behavior for the response. |
|
Control the cache behavior for the no-store. |
|
Turn logging on or off. |
|
Turn incepts on or off. |
|
Enable debugging (rarely used) |
|
Indicate whether remap matching is required or not. |
All of these are controlled via a boolean value, and can be set to either true
or false
,
using the same Get()
and Set()
as for configuration variables. As an example, lets randomly
turn off logging for some percentage of requests:
do_remap() {
if (cripts::random(1000) > 99) {
control.logging.Set(false); // 10% log sampling
}
}
Versions¶
Cripts provides a way to get the version of ATS and Cripts at runtime. The following global variables are available:
Variable |
Description |
---|---|
versions.major |
The major version of ATS. |
versions.minor |
The minor version of ATS. |
versions.patch |
The patch version of ATS. |
Other Variables¶
There are a number of other variables that are available in Cripts. They are generally closely tied to another object, and are therefore documented in various chapters within here. However, here's a quick list of some of the more common ones: