This hook function, if defined, is called by μC/HTTP-server before starting to transmit the response message as shown on Hook Functions. It allows the upper application to add header field to the response by calling a specific API function with header field type and value.
Code Block |
---|
CPU_BOOLEAN HTTPs_RespHdrTxHook (const HTTPs_INSTANCE *p_instance,
HTTPs_CONN *p_conn,
const void *p_hook_cfg); |
p_instance
Pointer to the instance structure (read only).
...
The listing below is shown to demonstrate the µC/HTTP-server module capabilities. That code simply add It adds two header fields to a response message for an HTML document.
Code Block |
---|
language | cpp |
---|
title | Listing - Add Header Field Hook Function Example Code |
---|
|
static CPU_BOOLEAN HTTPs_RespHdrTxHook (const HTTPs_INSTANCE *p_instance,
HTTPs_CONN *p_conn,
const void *p_hook_cfg)
{
#if (HTTPs_CFG_HDR_EN == DEF_ENABLED)
HTTPs_HDR_BLK *p_resp_hdr_blk;
const HTTPs_CFG *p_cfg;
CPU_CHAR *str_data;
CPU_SIZE_T str_len;
HTTPs_ERR http_err;
p_cfg = p_instance->CfgPtr;
switch (p_conn->StatusCode) {
case HTTPs_STATUS_OK: (1)
if (p_conn->FileContentType == HTTPs_CONTENT_TYPE_HTML) { (2)
p_resp_hdr_blk = HTTPs_RespHdrGet(p_instance, (3)
p_conn,
HTTPs_HDR_FIELD_SET_COOKIE,
HTTPs_HDR_VAL_TYPE_STR_DYN,
&http_err);
if (p_resp_hdr_blk == DEF_NULL) {
return(DEF_FAIL);
}
str_data = "user=micrium";
str_len = Str_Len_N(str_data, p_cfg->RespHdrStrLenMax);
Str_Copy_N(p_resp_hdr_blk->ValPtr, (4)
str_data,
str_len);
p_resp_hdr_blk->ValLen = str_len;
p_resp_hdr_blk = HTTPs_RespHdrGet(p_instance, (5)
p_conn,
HTTPs_HDR_FIELD_SERVER,
HTTPs_HDR_VAL_TYPE_STR_DYN,
&http_err);
if (p_resp_hdr_blk == DEF_NULL) {
return(DEF_FAIL);
}
str_data = "uC-HTTPs V2.00.00";
str_len = Str_Len_N(str_data, p_cfg->RespHdrStrLenMax); (6)
Str_Copy_N(p_resp_hdr_blk->ValPtr,
str_data,
str_len);
p_resp_hdr_blk->ValLen = str_len;
}
break;
default:
break;
}
#endif
return (DEF_YES);
} |
Panel |
---|
|
- Ensure that we don’t add header field to a response with an error
- Make sure to add header field only on HTML document.
- Acquire and add a first header field block of type ‘Cookie’ and with a string value type to the connection
- Set the value string value (cookie content) and the string length.
- Acquire and add a second header field block of type ‘Server’ and with a string value type to the connection
- Set the value string value (server name) and the string length.
|