Add Response Header Field

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.

Prototype

CPU_BOOLEAN  HTTPs_RespHdrTxHook (const  HTTPs_INSTANCE  *p_instance,
                                         HTTPs_CONN      *p_conn,
                                  const  void            *p_hook_cfg);

Arguments

p_instance

Pointer to the instance structure (read only).

p_conn

Pointer to the connection structure (read only).

p_hook_cfg

Pointer to hook application data.

Return Value

DEF_YES

If all header fields are added without error.

DEF_NO

otherwise.

Required Configuration

HTTPs_CFG_HDR_EN must be enabled in the http-s_cfg.h file (see section Module Configuration).

The header feature must also be enabled in the Instance configuration (see section Instance Parameters Configuration).

See section Hook Configuration.

Notes / Warnings

  • The instance structure is for read-only. It must not be modified.
  • Connection structure must not be modified by this function since the response is mostly ready to be transmitted.

Example Template

The listing below is shown to demonstrate the µC/HTTP-server module capabilities. It adds two header fields to a response message for an HTML document.

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);
}
  1. Ensure that we don’t add header field to a response with an error
     
  2. Make sure to add header field only on HTML document.
     
  3. Acquire and add a first header field block of type ‘Cookie’ and with a string value type to the connection
     
  4. Set the value string value (cookie content) and the string length.
     
  5. Acquire and add a second header field block of type ‘Server’ and with a string value type to the connection
     
  6. Set the value string value (server name) and the string length.