Bundles¶
While developing Cripts, we realized that Cripts often repeat the same, common patterns of code. To minimize such duplications across 100's or even 1000's of scripts, we introduced the concept of a bundle.
A bundle is a collection of functions, classes, and other definitions
turning these common patterns into easily reusable components. A bundle
must be activated in the do_create_instance()
hook of a Cript. This
does not exclude doing additional hooks in the Cript itself.
注釈
The member variables of a bundle are always lower case, and not Pascal case like methods. This is because even though they technically are functions, they act more like variables with a value.
The following bundles are available in the core today:
Bundle |
Description |
---|---|
|
For DSCP and an overridable Cache-Control header. |
|
Log sampling, TCPInfo and per-remap metrics. |
|
For removing or adding headers. |
|
Various cache controlling behavior. |
This example shows how a Cript would enable both of these bundles with all features:
#include <cripts/Preamble.hpp>
#include <cripts/Bundles/Common.hpp>
#include <cripts/Bundles/LogsMetrics.hpp>
do_create_instance()
{
cripts::Bundle::Common::Activate().dscp(10)
.via_header("client", "basic")
.set_config({{"proxy.config.srv_enabled", 0},
{"proxy.config.http.response_server_str", "ATS"});
cripts::Bundle::LogsMetrics::Activate().logsample(100)
.tcpinfo(true)
.propstats("example.com");
cripts::Bundle::Caching::Activate().cache_control("max-age=259200")
.disable(true)
}
The set_config()
function can also take a single configuration and value, without the need
to make a list.
注釈
You don't have to activate all components of a Bundle, just leave it out if you don't need it.
注釈
The bundles are not enabled by default. You have to explicitly activate them in your Cript, with the appropriate include directives. This is because the list of Bundles may grow over time, as well as the build system allowing for custom bundles locally.
Via Header¶
The cripts::Bundle::Common
bundle has a function called via_header()
that adds a Via header to the
client response or the origin request. The first argument is client
or origin
, and the second
argument is the type of Via header to be used:
Type |
Description |
---|---|
|
No Via header added. |
|
Add the basic protocol and proxy identifier. |
|
Add basic transaction codes. |
|
Add detailed transaction codes. |
|
Add full user agent connection protocol tags. |
Headers¶
Even though adding or removing headers in Cripts is very straight forward, we've added the
cripts::Bundle::Headers
for not only convenience, but also for easier integration and
migration with existing configurations. There are two main functions in this bundle:
rm_headers()
: Add a header to the request or response.add_headers()
: Remove a header from the request or response.
The rm_headers()
function takes a list of headers to remove, while the add_headers()
function takes a list
of key-value pairs to add. The header values here are strings, but they can also be strings with the special
operators from the header_rewrite
plugin. For example:
#include <cripts/Preamble.hpp>
#include <cripts/Bundles/Headers.hpp>
do_create_instance()
{
cripts::Bundle::Headers::Activate().rm_headers({"X-Header1", "X-Header2"})
.add_headers({{"X-Header3", "value3"},
{"X-Header4", "%{FROM-URL:PATH}"},
{"X-Header5", "%{ID:UNIQUE}"} });
}