Headers
Just like the URL objects, all header objects are managed and owned by the Cripts system, and must always be borrowed. The pattern for this is as follows:
borrow req = cripts::Client::Request::Get();
auto foo = req["X-Foo"];
There are four types of headers that can be borrowed:
Header Object |
Description |
---|---|
|
The client request headers. Always available. |
|
The client response headers. |
|
The server request headers. |
|
The server response headers. |
|
The cached response headers (immutable and conditional). |
注釈
For all of these headers, except the cripts::Client::Request
, the headers are not
available until the respective hook is called. For example, the cripts::Client::Response
headers
are not available until the response headers are received from the origin server or cache lookup.
The cripts::Cache::Response
header is also immutable, and can only be used after a successful
cache lookup.
Assigning the empty value (""
) to a header will remove it from the header list. For example:
borrow req = cripts::Client::Request::Get();
req["X-Foo"] = "bar"; // Set the header
req["X-Fie"] = ""; // Remove the header
A header can also be removed by using the Erase
method, which is a little more explicit:
borrow req = cripts::Client::Request::Get();
req.Erase("X-Foo");
注釈
There is also a Cripts Bundle for headers, see Bundles.
Iterators
A common use pattern with the header values is to iterate (loop) over all of them. This is easily done with a pattern such as the following example:
borrow req = cripts::Client::Request::Get();
for (auto header : req) {
CDebug("Header: {}: {}", header, req[header]); // This will print all headers and their values
}
The request object implements a standard C++ style iterator, so any type of loop that works with standard C++ iterators will work with the request object.
Methods
In addition to holding all the request headers, the request objects also holds the method
verb.
For the cripts::Client::Request
object, this is the client request method (GET, POST, etc.). For the
cripts::Server::Request
object, this is the method that will be sent to the origin server (GET, POST, etc.).
Cripts provides the following convenience symbols for common methods (and performance):
Method |
Description |
---|---|
|
The GET method. |
|
The POST method. |
|
The PUT method. |
|
The DELETE method. |
|
The HEAD method. |
|
The OPTIONS method. |
|
The CONNECT method. |
|
The TRACE method. |
These symbols can be used to compare against the method in the request object. For example:
borrow req = cripts::Client::Request::Get();
if (req.method == cripts::Method::GET) {
// Do something
}
Status
The two response objects also hold the status code of the response. This includes:
Member |
Description |
---|---|
|
The status code of the response. E.g. |
|
The reason phrase of the response. E.g. |
Lookup Status
The Cache::Response
header also hold the status of the cache lookup, in a member named
cachelookup
. This is an integer value that represents the status of the cache lookup.
The possible values are:
Value |
Description |
---|---|
|
No lookup status available. |
|
The response was not found in the cache. |
|
The response was found in the cache, but it was stale. |
|
The response was found in the cache and is fresh. |
|
We skipped cache lookup completely |
A string representation of the cache lookup status is also available in in the lookupstatus
member,
via an implicit conversion to a string
Value |
Description |
---|---|
|
No lookup status available. |
|
The response was not found in the cache. |
|
The response was found in the cache, but it was stale. |
|
The response was found in the cache and is fresh. |
|
We skipped cache lookup completely |
This status can be used to determine if the response was found in the cache, and if so, if it was fresh or stale. Example usage of the cache lookup status:
do_read_response() {
borrow cached = cripts::Cache::Response::Get();
if (cached.lookupstatus == cripts::LookupStatus::MISS) {
// Do something
}
}