Network Component
Version 7.19.0
MDK Middleware for IPv4 and IPv6 Networking
|
Functions reacting on CGI requests to the HTTP server. More...
Functions | |
void | netCGI_ProcessQuery (const char *qstr) |
Process query string received by GET or POST request. [user-provided]. More... | |
void | netCGI_ProcessData (uint8_t code, const char *data, uint32_t len) |
Process data received by POST request. [user-provided]. More... | |
uint32_t | netCGI_Script (const char *env, char *buf, uint32_t buf_len, uint32_t *pcgi) |
Generate dynamic web data based on a CGI script. [user-provided]. More... | |
const char * | netCGI_GetEnvVar (const char *env, char *ansi, uint32_t max_len) |
Process environment variables and convert to ANSI format. [thread-safe]. More... | |
const char * | netCGI_Charset (void) |
Override default character encoding in HTML documents. [user-provided]. More... | |
const char * | netCGI_ContentType (const char *file_ext) |
Add custom MIME type for unsupported file types. [user-provided]. More... | |
const char * | netCGI_CustomHeader (void) |
Add custom HTTP response header. [user-provided]. More... | |
const char * | netCGI_Redirect (const char *file_name) |
Redirect resource URL address to a new location. [user-provided]. More... | |
void | netCGI_ProcessRequest (const char *method, const char *uri, const char *header, uint32_t header_len) |
Process HTTP request. [user-provided]. More... | |
void | netCGI_ProcessCookie (const char *method, const char *uri, const char *cookie, uint32_t cookie_len) |
Process HTTP cookie. [user-provided]. More... | |
netStatus | netCGI_SetCookie (const char *cookie) |
Set HTTP cookie to send to the client. [thread-safe]. More... | |
const char * | netCGX_ContentType (void) |
Override default Content-Type for CGX script files. [user-provided]. More... | |
Functions reacting on CGI requests to the HTTP server.
To process a query string that was received by a GET request, the function netCGI_ProcessQuery is used while serving a POST request is done with netCGI_ProcessData. The function netCGI_Script generates a HTML web page based on CGI scripting. Processing and conversion of environment variables is done using netCGI_GetEnvVar, while the preferred character set (language) of a browser can be retrieved with netCGI_Charset.
const char * netCGI_Charset | ( | void | ) |
Override default character encoding in HTML documents. [user-provided].
The function netCGI_Charset 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 umlaut characters on German web pages.
The netCGI_Charset function is in the HTTP_Server_CGI.c module. The prototype is defined in rl_net.h.
Code Example
const char * netCGI_ContentType | ( | const char * | file_ext | ) |
Add custom MIME type for unsupported file types. [user-provided].
[in] | file_ext | filename extension, a null-terminated string. |
The function netCGI_ContentType allows you to add new MIME types for the Content-Type HTTP header in the response to the web service application requests. The function returns a pointer to the Content-Type HTTP header. You can use this function to add new file types to the Network Component library.
The argument file_ext specifies the file extension of a requested file, which is not supported in 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 netCGI_ContentType function does not exist in the project or if it returns a NULL pointer.
The netCGI_ContentType function is in the HTTP_Server_CGI.c module. The prototype is defined in rl_net.h.
Code Example
const char * netCGI_CustomHeader | ( | void | ) |
Add custom HTTP response header. [user-provided].
The function netCGI_CustomHeader allows you to add custom HTTP headers to web server response. If this function is implemented and returns a non-NULL pointer, the web server adds custom HTTP headers. The function returns a pointer to one or more custom HTTP headers. Each header must be terminated with the "\r\n".
With this function you can implement HTTP security headers to prevent web server vulnerabilities.
Code Example
const char * netCGI_GetEnvVar | ( | const char * | env, |
char * | ansi, | ||
uint32_t | max_len | ||
) |
Process environment variables and convert to ANSI format. [thread-safe].
[in] | env | pointer to environment variables. |
[out] | ansi | output buffer to write converted variable to. |
[in] | max_len | maximum length of environment variable. |
The function netCGI_GetEnvVar 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 max_len 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 max_len to fit it into the buffer.
Code Example (see netCGI_ProcessQuery)
void netCGI_ProcessCookie | ( | const char * | method, |
const char * | uri, | ||
const char * | cookie, | ||
uint32_t | cookie_len | ||
) |
Process HTTP cookie. [user-provided].
[in] | method | requested http method, a null-terminated string. |
[in] | uri | resource identifier, a null-terminated string. |
[in] | cookie | http request cookie. |
[in] | cookie_len | length of http request cookie. |
The function netCGI_ProcessCookie allows you to access HTTP cookies received in an HTTP request. The function is called when received an HTTP request and before opening the resource file. You can use this function to process session cookies provided by a the WEB browser.
The argument method specifies the HTTP request method used in the request, such as: GET, POST, HEADER, and so on. The method is a pointer to the null-terminated string.
The argument uri specifies the HTTP request path, which is a pointer to a null-terminated string. Arguments are not included. The path is pre-processed and all percent encoded characters are already decoded before the function is called.
The argument cookie is a pointer to HTTP cookie header contained in the request. The cookie header may contain more than one cookie. Cookies are separated by a semicolon.
The argument cookie_len specifies the length of the HTTP cookie header.
Code Example
void netCGI_ProcessData | ( | uint8_t | code, |
const char * | data, | ||
uint32_t | len | ||
) |
Process data received by POST request. [user-provided].
[in] | code | callback context:
|
[in] | data | pointer to POST data. |
[in] | len | length of POST data. |
The function netCGI_ProcessData 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 null-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.
Code Example
The following example is available in the user code template file HTTP_Server_CGI.c. Customize it to the application's needs.
void netCGI_ProcessQuery | ( | const char * | qstr | ) |
Process query string received by GET or POST request. [user-provided].
[in] | qstr | pointer to the query string. |
The function netCGI_ProcessQuery processes the environment variable QUERY_STRING returned by the method GET or by the method POST of the CGI form. The HTTP server calls this function when a user clicks the SUBMIT button on the input form. The function is not called if no URL query string arguments are specified.
The argument qstr points to the QUERY_STRING that is encoded in the URL of the request. The query string is terminated by a space character.
Code Example
The following example is available in the user code template file HTTP_Server_CGI.c. Customize it to the application's needs.
void netCGI_ProcessRequest | ( | const char * | method, |
const char * | uri, | ||
const char * | header, | ||
uint32_t | header_len | ||
) |
Process HTTP request. [user-provided].
[in] | method | requested http method, a null-terminated string. |
[in] | uri | resource identifier, a null-terminated string. |
[in] | header | http request header. |
[in] | header_len | length of http request header. |
The function netCGI_ProcessRequest allows you to access HTTP request parameters. The function is called when an HTTP request is received and before the resource file is opened. You can use this function to extend the existing functionality of the HTTP server with additional options.
The argument method specifies the HTTP request method used in the request, such as: GET, POST, HEADER, and so on. The method is a pointer to the null-terminated string.
The argument uri specifies the HTTP request path, which is a pointer to a null-terminated string. Arguments are not included. The path is pre-processed and all percent encoded characters are already decoded before the function is called.
The argument header is a pointer to HTTP headers contained in the request.
The argument header_len specifies the length of the HTTP request headers.
Code Example
const char * netCGI_Redirect | ( | const char * | file_name | ) |
Redirect resource URL address to a new location. [user-provided].
[in] | file_name | resource filename, a null-terminated string. |
The function netCGI_Redirect allows you to redirect URL address of a missing resource to a new location. The function returns a pointer to the Location HTML header. Web server calls this function, if the requested resource is not found on the server.
When netCGI_Redirect function returns valid URL location, Web server returns 301 Moved Permanently error code with HTML header containing a new location in the response to the web service application request.
The argument file_name specifies the full path name of a requested resource.
The netCGI_ContentType function is in the HTTP_Server_CGI.c module. The prototype is defined in rl_net.h.
Code Example
uint32_t netCGI_Script | ( | const char * | env, |
char * | buf, | ||
uint32_t | buf_len, | ||
uint32_t * | pcgi | ||
) |
Generate dynamic web data based on a CGI script. [user-provided].
[in] | env | environment string. |
[out] | buf | output data buffer. |
[in] | buf_len | size of output buffer (from 536 to 1440 bytes). |
[in,out] | pcgi | pointer to a session's local buffer of 4 bytes.
|
The function netCGI_Script is what the script interpreter calls when interpreting the script to output the dynamic part of the HTTP response. The script interpreter calls netCGI_Script for each line in the script that begins with the command c. You must customize the netCGI_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 netCGI_Script function must write the HTTP response.
The argument buf_len 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 netCGI_Script function. You might use this to store:
or
the return value from this function with 1U<<30.Code Example
The following example is available in the user code template file HTTP_Server_CGI.c. Customize it to the application's needs.
netStatus netCGI_SetCookie | ( | const char * | cookie | ) |
Set HTTP cookie to send to the client. [thread-safe].
[in] | cookie | cookie definition, a null-terminated string. |
The function netCGI_SetCookie sets the cookie that shall be returned to the client. The cookie is composed of the cookie_name and cookie_value.
The argument cookie is a pointer to a cookie that needs to be set, which is a null-terminated string. The function copies the contents of the cookie to the internal memory. If more than one cookie needs to be set, you can call the function several times to set all the required cookies.
You enable the processing of cookies by specifying the netCGI_ProcessCookie function in the code. If the function is not available, cookies are automatically disabled on the web server.
Possible netStatus return values:
Code Example
const char * netCGX_ContentType | ( | void | ) |
Override default Content-Type for CGX script files. [user-provided].
The function netCGX_ContentType 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 netCGX_ContentType function does not exist in the project or if it returns a NULL pointer.
The netCGX_ContentType function is in the HTTP_Server_CGI.c module. The prototype is defined in rl_net.h.
Code Example