Get Error Document

This hook function, if defined, is called by μC/HTTP-server instance every time the response status code has been changed to a value that is associated with an error to allow the upper application to send the appropriate error page as shown in the figure Hook Functions. The change could be the result of the request processing in the Connection request hook function or the result of an internal error in the uC/HTTP-server core. This function is intended to set the name of the file which will be sent with the response message. If no file is set, a default status page will be sent including the status code number and the reason phrase.

Prototype

void  HTTPs_ErrFileGetHook (const  void                   *p_hook_cfg,
                                   HTTP_STATUS_CODE        status_code,
                                   CPU_CHAR               *p_file_str,
                                   CPU_INT32U              file_len_max,
                                   HTTPs_BODY_DATA_TYPE   *p_file_type,
                                   HTTP_CONTENT_TYPE      *p_content_type,
                                   void                  **p_data,
                                   CPU_INT32U             *p_data_len);

Arguments

p_hook_cfg

Pointer to hook application data.

status_code

Status code number of the response message.

p_file_str

Pointer to the string buffer where the filename string must be copied.

file_len_max

Maximum string length of the filename.

p_file_type

Pointer to the variable holding the file type, can be modified by this function when data must be transmitted instead of a file:

HTTPs_BODY_DATA_TYPE_FILE
HTTPs_BODY_DATA_TYPE_STATIC_DATA
HTTPs_BODY_DATA_TYPE_NONE

p_content_type

Content type of the body. If the data is a File, the content type doesn't need to be set. It will be set according to the file extension. If the data is Static Data, the parameter MUST be set.

p_data

Pointer to the data memory block,
Must set if file type is configured as HTTPs_BODY_DATA_TYPE_STATIC_DATA.
DEF_NULL, otherwise.

p_data_len

Pointer to variable holding the length of the data,
Must set if file type is configured as HTTPs_FILE_TYPE_STATIC_DATA.
DEF_NULL, otherwise

Return Values

None.

Required Configuration

See section Hook Configuration.

Notes / Warnings

If the configured file doesn't exist the instance will transmit the default web page instead, defined by “HTTPs_CFG_HTML_DFLT_ERR_PAGE” in http-s_cfg.h.

Example Template

The listing below demonstrates the µC/HTTP-server module capabilities. It uses a file for 404 error and the default document for any other error.

Listing - Connection Error Hook Function Example Code
#define  HTTPs_CFG_INSTANCE_STR_FILE_ERR_404              "404.html"
          
static  void  HTTPs_ErrFileGetHook (const  void                   *p_hook_cfg,
                                           HTTPs_STATUS_CODE       status_code,
                                           CPU_CHAR               *p_file_str,
                                           CPU_INT32U              file_len_max,
                                           HTTPs_BODY_DATA_TYPE   *p_file_type,
                                           HTTP_CONTENT_TYPE      *p_content_type,
                                           void                  **p_data,
                                           CPU_INT32U             *p_data_len)
{
	switch (status_code) {
    	case HTTP_STATUS_NOT_FOUND:
             Str_Copy_N(p_file_str, HTTPs_CFG_INSTANCE_STR_FILE_ERR_404, file_len_max);
            *p_file_type = HTTPs_BODY_DATA_TYPE_FILE;
             return;
          
        default:
            *p_data         = HTTPs_CFG_HTML_DFLT_ERR_PAGE;
            *p_data_len     = HTTPs_HTML_DLFT_ERR_LEN;
            *p_file_type    = HTTPs_BODY_DATA_TYPE_STATIC_DATA;
            *p_content_type = HTTP_CONTENT_TYPE_HTML;
             return;
	}
}