This hook function, if defined, is called by μC/HTTP-server after an HTTP request has been completely received as shown in the figure Hook Functions. It allows the upper application to do whatever wanted with data received in the request body. The hook function SHOULD NOT be blocking and SHOULD return quickly. A time consuming function will block the processing of the other connections and reduce the HTTP server performance. In case the request processing is time consuming, the Poll hook function SHOULD be enabled to allow the server to periodically verify if the upper application has finished the request processing. If the hook is not required by the upper application, it can be set as DEF_NULL
and no function will be called.
Prototype
Code Block |
---|
CPU_BOOLEAN HTTPs_ReqRdySignalHook (const HTTPs_INSTANCE *p_instance,
HTTPs_CONN *p_conn,
const void *p_hook_cfg,
const HTTPs_KEY_VAL *p_data); |
Arguments
p_instance
Pointer to the instance structure (read only).
...
The listing below is shown to demonstrate the µC/HTTP-server module capabilities. That code analyses each key value pair in the list and try to find control name which is linked to led blinking functionality and change the document returned to display the led state using a data in memory as well.
Code Block |
---|
language | cpp |
---|
title | Listing - CGI Post Hook Function Example Code |
---|
linenumbers | true |
---|
|
static CPU_BOOLEAN HTTPs_ReqRdySignalHook (const HTTPs_INSTANCE *p_instance,
HTTPs_CONN *p_conn,
const void *p_hook_cfg,
const HTTPs_KEY_VAL *p_data)
{
HTTPs_KEY_VAL *p_key_val;
static CPU_CHAR buf[4];
CPU_INT16S str_cmp;
CPU_SIZE_T str_len;
static CPU_BOOLEAN led1_val;
static CPU_BOOLEAN led2_val;
CPU_BOOLEAN led_val;
HTTPs_ERR err_https;
p_key_val = (HTTPs_KEY_VAL *)p_data;
while (p_key_val != DEF_NULL) { (1)
printf("Ctrl Name = %s, Value = %s\n\r",
p_key_val->KeyPtr,
p_key_val->ValPtr);
if (p_key_val->DataType == HTTPs_KEY_VAL_TYPE_PAIR) { (2)
str_cmp = Str_Cmp_N(p_key_val->KeyPtr, "LED", p_key_val->KeyLen); (3)
if (str_cmp == 0) {
str_cmp = Str_Cmp_N(p_key_val->ValPtr, "LED1", p_key_val->ValLen); (4)
if (str_cmp == 0) {
led1_val = !led1_val;
led_val = led1_val;
} else {
str_cmp = Str_Cmp_N(p_key_val->ValPtr, "LED2", p_key_val->ValLen); (5)
if (str_cmp == 0) {
led2_val = !led2_val;
led_val = led2_val;
}
}
if (led_val == DEF_ON) { (6)
str_len = Str_Len("ON");
Str_Copy(&buf[0], "ON");
} else {
str_len = Str_Len("OFF");
Str_Copy(&buf[0], "OFF");
}
HTTPs_RespBodySetParamStaticData(p_instance, (7)
p_conn,
HTTP_CONTENT_TYPE_PLAIN,
&buf[0],
str_len,
DEF_NO,
&err_https);
}
} else if (p_key_val->DataType == HTTPs_KEY_VAL_TYPE_FILE) { (8)
HTTPs_RespBodySetParamFile(p_instance, (9)
p_conn,
p_key_val->ValPtr,
HTTP_CONTENT_TYPE_UNKNOWN,
DEF_YES,
&err_https);
}
p_key_val = p_key_val->DataNextPtr;
}
return (DEF_NO);
} |
Panel |
---|
|
- Analyze each key value pair into the list
- Make sure the key value pair item is a control key and not a file.
- If the control name starts with the key word ‘LED’ than a led should be toggled.
- If the value contains ‘LED1’ than the first led should be toggled.
- Else if the value contains ‘LED2’ than the second led should be toggled.
- Update memory data to transmit based on the led state.
- Set the data to send in the response body.
- If the key value pair is for a file uploaded.
- Change the file to be transmitted equal to the file just received.
|