Network Component
Version 6.6
MDK-Professional Middleware for IP Networking
|
HTTP Web Server routines are used to run and configure services of the built-in web server. More...
Functions | |
uint8_t | http_check_account (const char *username, const char *password) |
Check if an user account exist in the user database. | |
uint8_t | http_get_user_id (void) |
Retrieve the user identification. | |
bool | http_accept_client (const uint8_t *ip_addr, uint16_t port) |
Accept or deny connection from remote HTTP client. | |
bool | http_file_access (uint8_t user_id, const char *fname) |
Check if remote user is allowed to access a file on HTTP server. | |
char * | http_server_fgets (void *file, char *buf, uint32_t size) |
Read a string from a file in HTTP server. | |
void | http_server_fclose (void *file) |
Close a file previously open in HTTP server. | |
void * | http_server_fopen (const char *fname) |
Open a file for reading in HTTP server. | |
uint32_t | http_server_fread (void *file, uint8_t *buf, uint32_t len) |
Read block of data from a file in HTTP server. | |
uint32_t | http_server_ftime (const char *fname) |
Retrieve last modification time of a file. | |
uint32_t | http_utc_time (uint8_t hr, uint8_t min, uint8_t sec, uint8_t day, uint8_t mon, uint16_t year) |
Convert generic time to UTC time format. | |
const char * | http_get_env_var (const char *env, char *ansi, uint32_t maxlen) |
Process environment variables and convert to ANSI format. | |
const char * | http_server_get_content_type (void) |
Get Content-Type HTML header, received in XML post request. | |
const char * | http_server_get_lang (void) |
Retrieve the preferred language setting from the browser. | |
int32_t | http_server_get_session (void) |
Get current session number of HTTP server. | |
netStatus | http_server_get_client (uint8_t *ip_addr, uint8_t *mac_addr) |
Get IP and MAC address of connected remote machine. | |
void | cgi_process_data (uint8_t code, const char *data, uint32_t len) |
Process data received by POST request. | |
void | cgi_process_query (const char *qstr) |
Process query string received by GET request. | |
uint32_t | cgi_script (const char *env, char *buf, uint32_t buflen, uint32_t *pcgi) |
Generate dynamic web data from a script line. | |
const char * | cgi_content_type (const char *file_ext) |
Add custom MIME type for unsupported file types. | |
const char * | cgx_content_type (void) |
Override default Content-Type for CGX script files. | |
const char * | http_encoding (void) |
Override default character encoding in html documents. | |
HTTP Web Server routines are used to run and configure services of the built-in web server.
A web server is used to host websites and to deliver the web pages to clients using the Hypertext Transfer Protocol. There are two types of web pages which are stored on a web server and sent to a web client on request:
The Network Component's HTTP server supports both types of web pages. Static web pages are generally stored in the virtual ROM file system. The files are converted into C-code by the FCARM file converter and compiled into code.
The Web Server supports the usage of many advanced web technologies:
The following source code is part of the HTTP_Server_CGI.c template file. The application specific behaviour of the Script Language may be implemented using this code template:
The following source code is part of the HTTP_Server_Multiuser.c template file, which handles Multi-user Web Authentication for the HTTP server:
The following source code is part of the HTTP_Server_Error.c template file. Application specific error pages are implemented using this code template:
const char * cgi_content_type | ( | const char * | file_ext | ) |
Add custom MIME type for unsupported file types.
[in] | file_ext | filename extension, a null-terminated string. |
The function cgi_content_type allows you to add new MIME types for the Content-Type html header in the response to the web service application requests. The function returns a pointer to the Content-Type html header. You can use this function to add new file types to the Network Component library.
The default content type for unsupported file types is:
This header is used if the file type is not supported in Web server and cgi_content_type function does not exist in the project or if it returns a NULL pointer.
The cgi_content_type function is in the HTTP_Server_CGI.c module. The prototype is defined in rl_net.h.
Code Example
void cgi_process_data | ( | uint8_t | code, |
const char * | data, | ||
uint32_t | len | ||
) |
Process data received by POST request.
[in] | code | callback context:
|
[in] | data | pointer to POST data. |
[in] | len | length of POST data. |
The function cgi_process_data processes the data returned from the POST method of the CGI form. The HTTP server calls this function when a user clicks the SUBMIT button of an input form.
The argument code can have the following types:
Code | Data Type | Meaning of *data | Meaning of len |
---|---|---|---|
0 | Form data | Pointer to data string returned from the POST method. | Length of data string. |
1 | Filename | Pointer to a Filename for the http file upload. Filename is a 0-terminated string. | Length of a filename. |
2 | File data | Pointer to data packet received from the host. | Length of data packet. |
3 | End of file | NULL | Don't care. |
4 | Other data | Pointer to data string returned from the POST method. A single packet or last packet in data stream. | Length of data string. |
5 | Other data | Pointer to data string returned from the POST method. The same as under 4, but with more data to follow. | Length of data string. |
The argument data point to the data that get processed. The argument len is the data length in bytes.
The function is in the HTTP_Server_CGI.c module.
Code Example
void cgi_process_query | ( | const char * | qstr | ) |
Process query string received by GET request.
[in] | qstr | pointer to the query string. |
The function cgi_process_query processes the environmental variable QUERY_STRING that is returned by the GET method of the CGI form. The HTTP server calls this function when a user clicks the SUBMIT button on the input form.
The argument qstr points to the QUERY_STRING that is returned by the GET method. The query string is terminated by a space character.
The function is in the HTTP_Server_CGI.c module.
Code Example
uint32_t cgi_script | ( | const char * | env, |
char * | buf, | ||
uint32_t | buflen, | ||
uint32_t * | pcgi | ||
) |
Generate dynamic web data from a script line.
[in] | env | environment string. |
[out] | buf | output data buffer. |
[in] | buflen | size of output buffer (from 536 to 1460 bytes). |
[in,out] | pcgi | pointer to a session's local buffer of 4 bytes.
|
The function cgi_script is what the script interpreter calls when interpreting the script to output the dynamic part of the HTTP response. The script interpreter calls cgi_script for each line in the script that begins with the command c. You must customize the cgi_script function so that it can understand and use the input string from the script.
The argument env is a pointer to the input string to create the dynamic response. It is the same string of bytes that is specified in the CGI script code using the c command.
The argument buf is a pointer to the output buffer where the cgi_script function must write the HTTP response.
The argument buflen specifies the length of the output buffer in bytes.
The argument pcgi is a pointer to a variable that never gets altered by the HTTP Server. Hence, you can use it to store parameters for successive calls of the cgi_script function. You might use this to store:
The cgi_script function is in the HTTP_Server_CGI.c module.
or
the return value from this function with 1U<<30.Code Example
const char * cgx_content_type | ( | void | ) |
Override default Content-Type for CGX script files.
The function cgx_content_type allows you to change the Content-Type html header in the response to the web service application requests. The function returns a pointer to the new Content-Type html header. You can use this function to override the default content type header from the Network Component library. This content type header is used in cgx script responses.
The default content type header in cgx script response is:
This header is used if the cgx_content_type function does not exist in the project or if it returns a NULL pointer.
The cgx_content_type function is in the HTTP_Server_CGI.c module. The prototype is defined in rl_net_lib.h.
Code Example
bool http_accept_client | ( | const uint8_t * | ip_addr, |
uint16_t | port | ||
) |
Accept or deny connection from remote HTTP client.
[in] | ip_addr | IP address of the remote HTTP client. |
[in] | port | port number of the remote HTTP client. |
The function http_accept_client checks if a connection from a remote client is allowed or not. This enables remote client filtering. You can selectively decide which clients are allowed to connect to the web server.
The argument ip_addr points to a buffer containing the four octets that make up the IP address of the remote machine.
The argument port specifies the port on the remote machine.
The function is in the HTTP_Server_Access.c module.
Code Example
uint8_t http_check_account | ( | const char * | username, |
const char * | password | ||
) |
Check if an user account exist in the user database.
[in] | username | pointer to username. |
[in] | password | pointer to password. |
The function http_check_account checks if a user account exist in the user database for the provided credentials. It is called from the Network Component's Web server to check if the user with provided credentials is allowed to access the web pages or not.
The argument username points to a 0-terminated string representing the user name that was typed in from the browser.
The argument password points to a 0-terminated string representing the password.
The function is in the HTTP_Server_Multiuser.c module.
Code Example
const char * http_encoding | ( | void | ) |
Override default character encoding in html documents.
The function http_encoding overrides the default character encoding in a web browser. If this function is implemented and returns a non-NULL pointer, the web server adds a character encoding HTTP header. The browser then uses this information to correctly decode the character set.
If this function is not existing (or returns NULL), then the ‘charset’ parameter is not generated in web server's response. The browser then uses the default character encoding. This might cause troubles in displaying web pages that are encoded in different character sets. Example: an English user might not see the umlauts characters on German web pages.
Code Example
The example above will instruct the web server to generate the following HTTP header:
bool http_file_access | ( | uint8_t | user_id, |
const char * | fname | ||
) |
Check if remote user is allowed to access a file on HTTP server.
[in] | user_id | user identification number. |
[in] | fname | name of a file to access. |
The function http_file_access checks if a file access is allowed for a specified user. This enables access protection of sensitive web pages. Protected web pages will not be displayed for unprivileged users. Instead, the Web server shows Error page 403 - Forbidden.
The argument user_id is an user identification number as returned by http_check_account. It identifies the user who is trying to access the specified file.
The argument fname points to a buffer containing the file name of a file, which the user is trying to access. The file name is a 0-terminated string.
The function is in the HTTP_Server_Multiuser.c module.
Code Example
const char * http_get_env_var | ( | const char * | env, |
char * | ansi, | ||
uint32_t | maxlen | ||
) |
Process environment variables and convert to ANSI format.
[in] | env | pointer to environment variables. |
[out] | ansi | output buffer to write converted variable to. |
[in] | maxlen | maximum length of environment variable. |
The function http_get_env_var processes the string env, which contains the environment variables, and identifies where the first variable ends. The function obtains and stores the first variable and its value in the buffer pointed by ansi, in ANSI format.
The argument maxlen specifies the maximum length that can be stored in the ansi buffer. If the decoded environment variable value is longer than this limit, then the function truncates it to maxlen to fit it into the buffer.
Code Example
Referenced by cgi_process_data(), and cgi_process_query().
uint8_t http_get_user_id | ( | void | ) |
Retrieve the user identification.
The function http_get_user_id retrieves the user identification number when the web pages are protected with user authentication. It can be used to generate a web page, whose content is based on the connected user. This function is normally called from the cgi_script function.
The http_get_user_id is a system function that is in the Network Component library.
Code Example
void http_server_fclose | ( | void * | file | ) |
Close a file previously open in HTTP server.
[in] | file | pointer to the file to close. |
The function http_server_fclose closes the file identified by the file stream pointer in the function argument.
The function interfaces to the File System Component and will be called by the Network Component automatically. If another file system will be used, refer to the reference implementation in the HTTP_Server_FS.c module.
char * http_server_fgets | ( | void * | file, |
char * | buf, | ||
uint32_t | size | ||
) |
Read a string from a file in HTTP server.
[in] | file | pointer to the file to read from. |
[out] | buf | output buffer to write data to. |
[in] | size | size of output buffer. |
The function http_server_fgets reads up to size bytes from the file identified by the file stream pointer in the function argument.
The argument buf is a pointer to the buffer which stores the data.
The function interfaces to the File System Component and will be called by the Network Component automatically. If another file system will be used, refer to the reference implementation in the HTTP_Server_FS.c module.
void * http_server_fopen | ( | const char * | fname | ) |
Open a file for reading in HTTP server.
[in] | fname | name of the file to open. |
The function http_server_fopen opens a file for reading.
The argument fname specifies the name of the file to open. If the file does not exist, the function fails and returns NULL.
The argument fname is percent-encoded. This means that all reserved characters in a file name are escaped.
The function interfaces to the File System Component and will be called by the Network Component automatically. If another file system will be used, refer to the reference implementation in the HTTP_Server_FS.c module.
uint32_t http_server_fread | ( | void * | file, |
uint8_t * | buf, | ||
uint32_t | len | ||
) |
Read block of data from a file in HTTP server.
[in] | file | pointer to the file to read from. |
[out] | buf | block of memory to write data to. |
[in] | len | length of data to read in bytes. |
The function http_server_fread reads len bytes from the file identified by the file stream pointer in the function argument.
The argument buf is a pointer to the buffer where the function stores the read data.
The function interfaces to the File System Component and will be called by the Network Component automatically. If another file system will be used, refer to the reference implementation in the HTTP_Server_FS.c module.
uint32_t http_server_ftime | ( | const char * | fname | ) |
Retrieve last modification time of a file.
[in] | fname | name of the file. |
The function http_server_ftime reads the time when the file identified by the fname was last modified.
The function interfaces to the File System Component and will be called by the Network Component automatically. If another file system will be used, refer to the reference implementation in the HTTP_Server_FS.c module.
netStatus http_server_get_client | ( | uint8_t * | ip_addr, |
uint8_t * | mac_addr | ||
) |
Get IP and MAC address of connected remote machine.
[out] | ip_addr | pointer to IP address. |
[out] | mac_addr | pointer to MAC address. |
The function http_server_get_client obtains information about the remote machine and records the IP address and MAC address in memory.
The argument ip_addr is the remote machine IP address.
The argument mac_addr is the remote machine MAC address.
Code Example
const char * http_server_get_content_type | ( | void | ) |
Get Content-Type HTML header, received in XML post request.
The function http_server_get_content_type returns a pointer to the Content-Type html header, which was received with a XML or any other type POST request. You can use this function to check for the content type, which was submitted by a web service application.
Code Example
const char * http_server_get_lang | ( | void | ) |
Retrieve the preferred language setting from the browser.
The function http_server_get_lang retrieves the preferred language setting from the browser. You can use this information to implement automatic language selection for your embedded web pages.
Code Example
int32_t http_server_get_session | ( | void | ) |
Get current session number of HTTP server.
The function http_server_get_session returns the current session number of the HTTP server running on the Network Component. The session number can be any value between 0 and HTTP_SERVER_NUM_SESSIONS, defined in Net_Config_HTTP_Server.h.
Code Example
uint32_t http_utc_time | ( | uint8_t | hr, |
uint8_t | min, | ||
uint8_t | sec, | ||
uint8_t | day, | ||
uint8_t | mon, | ||
uint16_t | year | ||
) |
Convert generic time to UTC time format.
[in] | hr | hours [0..23]. |
[in] | min | minutes [0..59]. |
[in] | sec | seconds [0..59]. |
[in] | day | day [1..31]. |
[in] | mon | month [1..12]. |
[in] | year | year [1980..2107]. |
The function http_utc_time converts the time arguments into a UTC time format.
Code Example
Referenced by http_server_ftime().