libslax Default Extension Libraries: bit, cURL, and xutil
libslax bit Extension Library
The bit extension library contains functions that create and manipulate bit strings. The functions support 64-bit integer arguments. To incorporate functions from the bit extension library into SLAX scripts, include the namespace statement for that library in the script.
Call the bit extension functions using the bit prefix and the function name. For example:
version 1.1 ; ns bit extension = "http://xml.libslax.org/bit"; var $a = 63; var $b = { expr "10111"; } match / { <out> { <bit-and> { <a1> bit:and("101100", "100101"); <a2> bit:and($a, $b); <a3> bit:and($a, number($b)); } <bit-or> { <a1> bit:or("101100", "100101"); <a2> bit:or($a, $b); <a3> bit:or($a, number($b)); } <bit-mask> { <a1> bit:mask(0); <a2> bit:mask(8, 32); } <ops> { <a1> bit:to-int("10101"); } } }
Table 1 lists the functions available in the bit extension library and which are supported in SLAX 1.1 scripts.
Table 1: Functions in the bit Extension Library
Function and Arguments | Description | Example |
---|---|---|
bit:and(b1, b2) | Return the logical AND of two bit strings. | bit:and("101100", "100101") |
bit:clear(b1,bitnum) | Set the specified bit in the bit string to zero and return the new bit string. Bits are numbered starting from zero. If the integer argument is greater than the bit string length, the bit string is extended. | bit:clear("11111", 0) bit:clear("11111", 6) |
bit:compare(value1, value2) | Compare two values and return an integer less than, equal to, or greater than zero, if the first argument is found to be less than, equal to, or greater than the second argument, respectively. | bit:compare("10000", 16) bit:compare("11111", "10000") |
bit:from-hex(string, len?) | Return the value of the hex argument as a bit string. The optional second argument pads the bit string with leading 0’s until it is the specified length. | bit:from-hex("0x45", 8) |
bit:from-int(integer, len?) | Return the value of the integer argument as a bit string. The optional second argument pads the bit string with leading 0’s until it is the specified length. | bit:from-int(65,8) |
bit:mask(count, len?) | Return a bit string with count low-order bits set to one. The optional second argument pads the bit string with leading 0’s until it is the specified length. | bit:mask(4, 8) |
bit:nand(b1, b2) | Return the logical NAND of two bit strings. | bit:nand("101100", "100101") |
bit:nor(b1, b2) | Return the logical NOR of two bit strings. | bit:nor("101100", "100101") |
bit:not(b1) | Return the inversion (NOT) of a bit string. | bit:not("101100") |
bit:or(b1, b2) | Return the logical OR of two bit strings. | bit:or("101100", "100101") |
bit:set(b1, bitnum) | Set the specified bit in the bit string and return the new bit string. Bits are numbered starting from zero. If the integer argument is greater than the bit string length, the bit string is extended. | bit:set("1001", 2) bit:set("1001", 6) |
bit:to-int(b1) | Return the value of the bit string argument as an integer. | bit:to-int("101100") |
bit:to-hex(b1) | Return the value of the bit string argument as a string representation of the hex value. | bit:to-hex("101100") |
bit:xor(b1, b2) | Return the logical XOR of two bit strings. | bit:xor("101100", "100101") |
bit:xnor(b1, b2) | Return the logical XNOR of two bit strings. | bit:xnor("101100", "100101") |
libslax cURL Extension Library
- Understanding the cURL Extension Library
- curl:close
- curl:open
- curl:perform
- curl:set
- curl:single
- cURL Examples
Understanding the cURL Extension Library
cURL is a command-line tool that uses the libcurl library and permits data transfers using a number of protocols, including FTP, FTPS, HTTP, HTTPS, SCP, and SMTP. For more information about cURL, see the cURL website at http://curl.haxx.se/.
To incorporate functions from the cURL extension library into SLAX scripts, include the namespace statement for that library in the script.
Call the cURL extension functions using the curl prefix and the function name. cURL operations are directed using a set of elements passed to the cURL extension functions.
Table 2 lists the supported operations in the cURL extension library and includes a description of each function. Table 3 and Table 4 list the supported elements and include the syntax and a description of each element. Table 3 lists elements used for web services operations, and Table 4 lists the elements used for e-mail operations.
Table 2: Functions in the cURL Extension Library
Function | Description |
---|---|
curl:close | Close an open connection. Further operations cannot be performed over the connection. See curl:close. |
curl:open | Open a connection to a remote server, allowing multiple operations over a single connection. See curl:open. |
curl:perform | Perform simple transfers using a persistent connection handle provided by curl:open. See curl:perform. |
curl:set | Record a set of parameters that persists for the lifespan of a connection. See curl:set. |
curl:single | Perform transfer operations without using a persistent connection. See curl:single. |
Table 3: Web Services Elements in the cURL Extension Library
Element | Description | Syntax |
---|---|---|
<content-type> | Provide the MIME type for the transfer payload. | <content-type> "mime/type"; |
<fail-on-error> | Indicate that the transfer should fail if any errors, including insignificant ones, are detected. | <fail-on-error>; |
<format> | Specify the expected format of returned results, allowing the cURL extension to automatically make the content available in the native format. Formats include "html", "text", and "xml". | <format> "xml"; |
<header> | Provide additional header fields for the request. | <header name="name"> "value"; |
<insecure> | Indicate a willingness to tolerate insecure communications operations. Specifically, allow SSL Certs without checking the common name. | <insecure>; |
<method> | Set the method used to transfer data. This controls the HTTP request type, as well as triggering other transfer mechanisms. Acceptable method names include "get", "post", "delete", "head", "email", "put", and "upload". The "get" method is the default. | <method> "get"; |
<param> | Provide additional parameter values for the request. These parameters are typically encoded into the URL. | <param name="x"> "y"; |
<password> | Set the user's password for the transfer. | <password> "password"; |
<secure> | Request the use of the secure version of a protocol, including HTTPS and FTPS. | <secure>; |
<upload> | Indicate this is a file upload request. | <upload>; |
<url> | Set the base URL for the request. | <url> "target-url"; |
<username> | Set the username to use for the transfer. | <username> "username"; |
<verbose> | Request detailed debug information about the operations and communication of the cURL transfer. | <verbose>; |
Table 4: E-mail Elements in the cURL Extension Library
Element | Description | Syntax |
---|---|---|
<cc> | Set the "Cc" address for e-mail (SMTP) requests. For multiple addresses, use multiple <cc> elements. | <cc> "cc-user@email.example.com"; |
<contents> | Specify the contents to be transferred. | <contents> "multi-\nline\ncontents\n"; |
<from> | Set the "From" address for e-mail (SMTP) requests. | <from> "source-user@email.example.com"; |
<header> | Provide additional header fields for the request. | <header name="name"> "value"; |
<local> | Set the local hostname for e-mail (SMTP) requests. | <local> "local host name"; |
<server> | Set the outgoing SMTP server name. Currently, MX records are not processed. | <server> "email-server.example.com"; |
<subject> | Set the "Subject" field for e-mail (SMTP) requests. | <subject> "email subject string"; |
<to> | Set the "To" address for e-mail (SMTP) requests. For multiple addresses, use multiple <to> elements. | <to> "to-user@email.examplecom"; |
<verbose> | Request detailed debug information about the operations and communication of the cURL transfer. | <verbose>; |
The libcurl elements closely mimic the options used by the native C libcurl API in libcurl's curl_easy_setopt() function. Once the options are set, a call to curl_easy_perform() performs the requested transfer. For more information about the curl_easy_setopt() function, see http://curl.haxx.se/libcurl/c/curl_easy_setopt.html .
In the SLAX cURL extension library, the libcurl API options are represented as individual elements. For example, the <url> element is mapped to the CURLOPT_URL option , the <method> element is mapped to the CURLOPT_CUSTOMREQUEST option, and so forth.
These elements can be used in three ways:
- The curl:single() extension function permits a set of options to be used in a single transfer operation with no persistent connection handle.
- The curl:perform() extension function permits a set of options to be used with a persistent connection handle. The handle is returned from the curl:open() extension function and can be closed with the curl:close() extension function.
- The curl:set() extension function records a set of options for a connection handle and keeps those options active for the lifetime of the connection. For example, if the script needs to transfer a number of files, it can record the <username> and <password> options and avoid repeating them in every curl:perform() call.
curl:close
The curl:close() extension function closes an open connection. Further operations cannot be performed over the connection once it is closed.
The syntax is:
The argument is the connection handle to close.
curl:open
The curl:open() extension function opens a connection to a remote server, allowing multiple operations over a single connection.
The syntax is:
The returned object is a connection handle that can be passed to curl:perform() or curl:close().
curl:perform
The curl:perform() extension function performs simple transfers using a persistent connection handle provided by curl:open().
The syntax is:
The arguments are the connection handle and a set of option elements. Supported cURL extension library elements are defined in Table 3 and Table 4.
The returned object is an XML hierarchy containing the results of the transfer. Table 5 lists the possible elements in the reply, and Table 6 lists the possible elements contained within the <header> element.
Table 5: curl:perform Reply Elements
Element | Contents |
---|---|
<curl-success> | Empty element which Indicates success |
<data> | Parsed data |
<error> | Error message text, if any |
<header> | Parsed header fields |
<raw-data> | Raw data from the reply |
<raw-headers> | Raw header fields from the reply |
<url> | Requested URL |
Table 6: curl:perform <header> Elements
Element | Contents |
---|---|
<code> | HTTP reply code |
<field> | HTTP reply field (with @name and value) |
<message> | HTTP reply message |
<version> | HTTP reply version string |
The following example shows the <header> element with header fields parsed into <field> elements:
<header> <version>HTTP/1.1</version> <code>404</code> <message>Not Found</message> <field name="Content-Type">text/html</field> <field name="Content-Length">345</field> <field name="Date">Mon, 08 Aug 2011 03:40:21 GMT</field> <field name="Server">lighttpd/1.4.28 juisebox</field> </header>
curl:set
The curl:set() extension function records a set of parameters that persist for the lifespan of a connection.
The syntax is:
The arguments are the connection handle and a set of option elements. Supported cURL extension library elements are defined in Table 3 and Table 4.
curl:single
The curl:single() extension function performs transfer operations without using a persistent connection.
The syntax is:
The returned object is identical in structure to the one returned by curl:perform(). Refer to curl:perform for additional information.
cURL Examples
The following examples show SLAX version 1.1 scripts that use the cURL extension library functions to perform operations.
The following SLAX script performs a simple GET operation to retrieve a web page. The script specifies the header field for the HTTP header and a parameter that is incorporated into the requested URL.
version 1.1; ns curl extension = "http://xml.libslax.org/curl"; param $url = "https://www.juniper.net"; match / { <op-script-results> { var $options = { <header name="client"> "slaxproc"; <param name="smokey"> "bandit"; } var $results = curl:single($url, $options); message "completed: " _ $results/headers/message; <curl> { copy-of $results; } } }
The following SLAX script takes a username and password and uses the Google login services to translate them into an "Authorization" string:
version 1.1; ns curl extension = "http://xml.libslax.org/curl"; param $url = "https://www.google.com/accounts/ClientLogin"; param $username; param $password; var $auth-params := { <url> $url; <method> "post"; <insecure>; <param name="Email"> $username; <param name="Passwd"> $password; <param name="accountType"> "GOOGLE"; <param name="service"> "wise"; <param name="source"> "test-app"; } match / { var $curl = curl:open(); var $auth-cred = curl:perform($curl, $auth-params); <options> { for-each(slax:break-lines( $auth-cred/raw-data )) { if(starts-with(.,"Auth")) { <header name="GData-Version"> "3.0"; <header name="Authorization"> "GoogleLogin " _ .; } } } expr curl:close($curl); }
The following SLAX script sends an e-mail by way of a server, which is provided as a parameter:
version 1.1; ns curl extension = "http://xml.libslax.org/curl"; param $server; match / { <out> { var $info = { <method> "email"; <server> $server; <from> "muffin@example.com"; <to> "phil@example.net"; <subject> "Testing..."; <contents> "Hello, This is an email. Thanks, Phil "; } var $res = curl:single($info); <res> { copy-of $res; } } }
libslax xutil Extension Library
The xutil extension library contains functions that convert between strings and XML node sets. To incorporate functions from the xutil extension library into SLAX scripts, include the namespace statement for that library in the script.
Call the xutil extension functions using the xutil prefix and the function name. Table 7 lists the functions available in the xutil extension library and which are supported in SLAX 1.1 scripts.
Table 7: Functions in the xutil Extension Library
Function and Arguments | Description |
---|---|
xutil:string-to-xml(string+) | Return a node set of the concatenated string arguments. |
xutil:xml-to-string(node-set+) | Return a string representation of the XML hierarchies provided as arguments. |