#include <lwip/httpd/httpd.h>
Public Member Functions | |
tcHttpd (int max_connects) | |
int | run (tsHttpdParams *params) |
void | html_write (tsHttpConn *httpconn, const void *str, int length) |
int | find_cgi_param (tsHttpConn *httpconn, const char *name, char *value, int maxlen) |
This class is the MityDSP web server. Your application will create one tcHttpd object to handle all of the HTTP requests received from web browsers.
tcHttpd::tcHttpd | ( | int | max_connects | ) |
This constructor creates the web server.
max_connects | The maximum number of simultaneous HTTP connections which the server will allow. |
int tcHttpd::run | ( | tsHttpdParams * | params | ) |
This function starts the web server. At this point the tsHttpdParams::user1 parameter, the tsHttpdParams::port parameter and the applications CGI function table (tsHttpdParams::cgi_table) are specified to the HTTPD object.
params | The web server configuration parameters. |
void tcHttpd::html_write | ( | tsHttpConn * | httpconn, | |
const void * | str, | |||
int | length | |||
) |
This function incrementally builds up a web page in response to a GET from the web browser. When an application supplied CGI function is executed, it will enqueue its rendered HTML data into the web page being served by calling this function with the appropriate tsHttpConn pointer, a pointer to the string containing the HTML text and the length of that string.
httpconn | The web server connection which requested the page being rendered. | |
str | The HTML text string to insert into the page. | |
length | The length of the HTML text string. |
int tcHttpd::find_cgi_param | ( | tsHttpConn * | httpconn, | |
const char * | name, | |||
char * | value, | |||
int | maxlen | |||
) |
This function will retrieve posted HTML form data from the last HTML query made to the specified connection. When an HTML form is created each form object has a name and a value. The name identifies the form object and the value identifies the user data entered into the object by the browser user.
To retrieve data submitted from a form you will call this function for each HTML form object. You set the name parameter to the name of the HTML form object that you want to retrieve the data from. If a match is found in the HTML query, the value string that you supply will be filled in. If there is no match, the function will return a non-zero value.
httpconn | The web server connection which requested the page being rendered. | |
name | The name of the form object being retrieved. | |
value | Pointer to the string buffer where you want the value stored. | |
maxlen | The maximum string length that the value buffer will hold. |
Assume we have a user login form with an HTML text object named "user" which a visitor is prompted to enter their user name into. There is also a submit button to invoke the login. The form action points to a web page which references the following CGI function "cgi_login".
The first visit to this page the user will be prompted with "Please Login...", then when they enter a user name and hit submit, the page will refresh with either a welcome message or an invalid user message.
HTML Code for login.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head></head> <body> <%login%><br><br> <form action="login.html" method="get"> <fieldset> <input type="text" name="user" size="50" maxlength="10" value=""/> <input type="submit" name="submit" value="Login"/> </fieldset> </form> </body> </html>
CGI function for <login%>:
void cgi_login (tsHttpConn *httpconn) { char value[20], str[100]; if (httpconn->httpd->find_cgi_param (httpconn, "user", value, 20) != 0) { strcpy (str, "Please login..."); } else if (is_user_valid (value)) { sprintf (str, "Welcome %s !!!", value); } else { strcpy (str, "Invalid user name."); } httpconn->httpd->html_write (httpconn, str, strlen (str)); }