Token Replacement
µC/HTTP-server allows dynamic content to be inserted in HTML and plain text documents by using special tokens being substituted before the page is actually sent to the web browser. There are two type of token: internal and external. The value of external token must be provided by the upper application via a hook function. Internal tokens are defined to print some data related to HTTP such as status lines and error codes.
For more information on Dynamic Content, please refer to section Dynamic Content of the HTTP Reference Guide.
Configuration
Compile-time Configuration
The macro configuration HTTPs_CFG_TOKEN_PARSE_EN
inside the http-s_cfg.h file must be set to DEF_ENABLED
to enable the token substitution feature.
See section Dynamic Content Configuration for more details on compile-time configuration.
Run-time Configuration
The run-time configuration for Token Replacement is defined as a HTTPs_TOKEN_CFG
structure. Your application must declare an HTTPs_TOKEN_CFG
object and include a pointer to this object in the HTTP server instance configuration HTTPs_CFG
object where the TokenCfgPtr
parameter is defined.
See section Token Configuration for additional details.
Hook Configuration
The HTTP Server Instance configuration structure (HTTPs_CFG
) contains a pointer to an HTTPs_HOOK_CFG
object. This object indexes all the hook functions required by your application.
For additional details on the configuration of hook functions, see section Hook Configuration.
To visualize when each hook function is called by the HTTP server core, see section Hook Functions.
The hook function OnRespTokenHook
must be used to set the value that will be used to replace the token found. Each time a token is found in the HTML/text file the hook function will be called.
Usage
External Token
External tokens are represented in the HTML/text files as:
${TOKEN_NAME}
Assuming we have an HTML page that look like this:
<html><body><center>
This system's IP address is ${My_IP_Address}
</center></body></html>
When a web client requests this file, µC/HTTP-server will parse the file, find ${My_IP_Address}
token, and pass the string "My_IP_Address
" into the OnRespTokenHook
hook function. That function will copy the token value then the µC/HTTP-server instance will send the token value instead of the token:
<html><body><center>
This system's IP address is 135.17.115.215
</center></body></html>
Note that if the upper application doesn’t copy a valid data the token is automatically replaced by ‘~’ characters. See Response Token Transmit Hook for more information on how the callback function having to be implemented to support this.
Internal Token
Internal tokens are represented in the HTML files as:
#{TOKEN_NAME}
For every HTML document that must be transmitted µC/HTTP-server parse the document and replace all internal token automatically without calling any callback function. Internal token can be use in any HTML/text documents. Here is the list of built-in token replacement:
Token | Value |
---|---|
| Replaced by status code number of the connection. |
| Replaced by the reason phrase based on the status code of the connection |