📄 form_response.c
字号:
// Web Form Demo.
// The Rabbit hosts a Web page containing a form that enables the user to
// enter two temperature values. When the user clicks the form's Submit button,
// the server receives the form data in an HTTP POST request and returns
// a response that redirects the user's browser to a page that acknowledges
// receiving the data.
// This file and other embedded Ethernet and Internet code and resources are
// available from www.Lvr.com.
// Select a network configuration from \lib\tcpip\tcp_config.lib
// in the Dynamic C distribution.
#define TCPCONFIG 1
// This program uses the ServerSpec structure in zserver.lib instead of the
// HttpSpec structure in http.lib. When HttpSpec is unneeded, the
// HTTP_NO_FLASHSPEC directive saves code space.
#define HTTP_NO_FLASHSPEC
// This buffer must be large enough to hold the name, value, and
// four additional bytes for each form variable.
#define FORM_ERROR_BUF 256
// The IP address clients will use to access the Web server.
// _PRIMARY_STATIC_IP is defined in tcp_config.lib.
#define REDIRECTHOST _PRIMARY_STATIC_IP
// On receiving a form, the server redirects the client's browser to this URL.
#define FORM_RESPONSE_REDIRECTTO "http://" REDIRECTHOST "/formresponse.shtml"
// All C functions not declared as root go to extended memory.
#memmap xmem
// The dcrtcp library supports IP and TCP; the http library supports HTTP functions.
#use "dcrtcp.lib"
#use "http.lib"
// ximport loads files from the development PC into the Rabbit's flash memory.
// The symbol form_response_shtml is a macro that gives the file's length and physical address
// in the Rabbit's memory.
// YOU MUST CHANGE THIS PATH to match the location of the file in your system.
// OK to use forward or back slashes as separators.
#ximport "c:\embedded_ethernet_code\rabbit\formresponse\formresponse.shtml" form_response_shtml
// The HttpType structure names the handler to use with specific file extensions.
// The first item in the structure is the handler used when a request
// doesn't name a page or other resource.
// Requests with .html, extensions use the default handler, which
// serves the resource unchanged.
// A request for a resource with the .shtml extension uses shtml_handler, which
// handles requests for HTML pages that include SSI directives.
const HttpType http_types[] = {
{ ".html", "text/html", NULL},
{ ".shtml", "text/html", shtml_handler}
};
int form_response(HttpState* state) {
// On receiving form data, the server returns a response that advises the
// client's browser to request the resource named in FORM_RESPONSE_REDIRECTTO.
cgi_redirectto(state, FORM_RESPONSE_REDIRECTTO );
return 0;
} // end form_response
void main(void) {
// Create the form and handle HTTP requests.
// The FormVar array holds information about the form variables.
FormVar setup[2];
int var;
int form;
int function;
// The form variables
int maximum_temperature;
int minimum_temperature;
// Initialize the form variables.
maximum_temperature = 80;
minimum_temperature = 65;
// Add the file containing the Web page that users see after submitting form data.
sspec_addxmemfile("formresponse.shtml", form_response_shtml, SERVER_HTTP);
// Add the form to zserver's ServerSpec structure.
// "setup.html" is the form's name.
// setup is the FormVar array.
// 2 is the number of entries in FormVar.
// SERVER_HTTP specifies that the HTTP server can serve the form.
form = sspec_addform("setup.html", setup, 2, SERVER_HTTP);
// Set the title the form will display.
sspec_setformtitle(form, "Temperature Alarm Setup");
// Add a function to the list of objects the server recognizes.
// "form_response" is the function's name.
// form_response is a pointer to the function.
// SERVER_HTTP specifies that the HTTP server can access the function.
function = sspec_addfunction("form_response", form_response, SERVER_HTTP);
// Name the function the server will call after receiving form data from a client.
// form is the array index returned by sspec_addform.
// function is the value returned by sppec_addfunction.
sspec_setformepilog(form, function);
// Add a variable to the setup array.
// "maximum_temperature" is the variable's name.
// &maximum_temperature is the variable's location in memory.
// INT16 is the variable type.
// "%d" specifies the output format.
// SERVER_HTTP specifies that the HTTP server can access the variable.
var = sspec_addvariable("maximum_temperature", &maximum_temperature, INT16, "%d", SERVER_HTTP);
// Add the variable (var) to the form (form).
var = sspec_addfv(form, var);
// Set the name the form will display for the variable.
sspec_setfvname(form, var, "Maximum Temperature");
// Set a variable description the form will display.
sspec_setfvdesc(form, var, "Range 0 - 212 °F");
// Set the maximum number of characters required to display the variable's value.
sspec_setfvlen(form, var, 3);
// Set the minimum and maximum allowed values of the variable.
sspec_setfvrange(form, var, 0, 212);
// Add the next variable and set it up for the form in the same way.
var = sspec_addvariable("minimum_temperature", &minimum_temperature, INT16, "%d", SERVER_HTTP);
var = sspec_addfv(form, var);
sspec_setfvname(form, var, "Minimum Temperature");
sspec_setfvdesc(form, var, "Range 0 - 212 °F");
sspec_setfvlen(form, var, 3);
sspec_setfvrange(form, var, 0, 212);
// Enable requesting the form as "index.html" or as the default Web page ("/")
// (as well as by the form's name "setup.html".
sspec_aliasspec(form, "index.html");
sspec_aliasspec(form, "/");
// Initialize the TCP/IP stack and the web server.
sock_init();
http_init();
// For improved performance, reserve port 80 for the Web server.
tcp_reserveport(80);
while (1) {
// Handle HTTP requests.
http_handler();
// Code to perform other tasks can go here.
}
} // end main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -