HTTP_CGI.c文件中接口函数的实现
KEIL官网有提供HTTPCGI的接口文件,名为HTTP_CGI.c。我们就是在这个文件上修改实现,主要是函数cgi_func,其它函数未使用。具体实现内容如下:
#include
#include
#include
#include "bsp.h"
/* ---------------------------------------------------------------------------
* The HTTP server provides a small scripting language.
*
* The script language is simple and works as follows. Each script line starts
* with a command character, either "i", "t", "c", "#" or ".".
* "i" - command tells the script interpreter to "include" a file from the
* virtual file system and output it to the web browser.
* "t" - command should be followed by a line of text that is to be output
* to the browser.
* "c" - command is used to call one of the C functions from the this file.
* It may be followed by the line of text. This text is passed to
* 'cgi_func()' as a pointer to environment variable.
* "#' - command is a comment line and is ignored (the "#" denotes a comment)
* "." - denotes the last script line.
*
* --------------------------------------------------------------------------*/
/* at_System.c */
extern LOCALM localm[];
#define LocM localm[NETIF_ETH]
/* Net_Config.c */
extern struct tcp_cfg tcp_config;
extern struct http_cfg http_config;
#define tcp_NumSocks tcp_config.NumSocks
#define tcp_socket tcp_config.Scb
#define http_EnAuth http_config.EnAuth
#define http_auth_passw http_config.Passw
extern volatile int16_t iTemp;
extern RTC_TimeTypeDef RTC_TimeStructure;
/* Local variables. */
U8 LEDControl;
/* My structure of CGI status U32 variable. This variable is private for */
/* each HTTP Session and is not altered by HTTP Server. It is only set to */
/* zero when the cgi_func() is called for the first time. */
typedef struct {
U16 xcnt;
U16 unused;
} MY_BUF;
#define MYBUF(p) ((MY_BUF *)p)
/*----------------------------------------------------------------------------
* HTTP Server Common Gateway Interface Functions
*---------------------------------------------------------------------------*/
/*--------------------------- cgi_process_var -------------------------------*/
void cgi_process_var (U8 *qs) {
/* This function is called by HTTP server to process the Querry_String */
/* for the CGI Form GET method. It is called on SUBMIT from the browser. */
/*.The Querry_String.is SPACE terminated. */
U8 *var;
int s[4];
var = (U8 *)alloc_mem (40);
do {
/* Loop through all the parameters. */
qs = http_get_var (qs, var, 40);
/* Check the returned string, 'qs' now points to the next. */
if (var[0] != 0) {
/* Returned string is non 0-length. */
if (str_scomp (var, "ip=") == __TRUE) {
/* My IP address parameter. */
sscanf ((const char *)&var[3], "%d.%d.%d.%d",&s[0],&s[1],&s[2],&s[3]);
LocM.IpAdr[0] = s[0];
LocM.IpAdr[1] = s[1];
LocM.IpAdr[2] = s[2];
LocM.IpAdr[3] = s[3];
}
else if (str_scomp (var, "msk=") == __TRUE) {
/* Net mask parameter. */
sscanf ((const char *)&var[4], "%d.%d.%d.%d",&s[0],&s[1],&s[2],&s[3]);
LocM.NetMask[0] = s[0];
LocM.NetMask[1] = s[1];
LocM.NetMask[2] = s[2];
LocM.NetMask[3] = s[3];
}
else if (str_scomp (var, "gw=") == __TRUE) {
/* Default gateway parameter. */
sscanf ((const char *)&var[3], "%d.%d.%d.%d",&s[0],&s[1],&s[2],&s[3]);
LocM.DefGW[0] = s[0];
LocM.DefGW[1] = s[1];
LocM.DefGW[2] = s[2];
LocM.DefGW[3] = s[3];
}
else if (str_scomp (var, "pdns=") == __TRUE) {
/* Default gateway parameter. */
sscanf ((const char *)&var[5], "%d.%d.%d.%d",&s[0],&s[1],&s[2],&s[3]);
LocM.PriDNS[0] = s[0];
LocM.PriDNS[1] = s[1];
LocM.PriDNS[2] = s[2];
LocM.PriDNS[3] = s[3];
}
else if (str_scomp (var, "sdns=") == __TRUE) {
/* Default gateway parameter. */
sscanf ((const char *)&var[5], "%d.%d.%d.%d",&s[0],&s[1],&s[2],&s[3]);
LocM.SecDNS[0] = s[0];
LocM.SecDNS[1] = s[1];
LocM.SecDNS[2] = s[2];
LocM.SecDNS[3] = s[3];
}
}
}while (qs);
free_mem ((OS_FRAME *)var);
}
/*--------------------------- cgi_process_data ------------------------------*/
void cgi_process_data (U8 code, U8 *dat, U16 len) {
/* This function is called by HTTP server to process the returned Data */
/* for the CGI Form POST method. It is called on SUBMIT from the browser. */
/* Parameters: */
/* code - callback context code */
/* 0 = www-url-encoded form data */
/* 1 = filename for file upload (0-terminated string) */
/* 2 = file upload raw data */
/* 3 = end of file upload (file close requested) */
/* 4 = any xml encoded POST data (single or last stream) */
/* 5 = the same as 4, but with more xml data to follow */
/* Use http_get_content_type() to check the content type */
/* dat - pointer to POST received data */
/* len - received data length */
U8 *var;
switch (code) {
case 0:
/* Url encoded form data received. */
break;
case 1:
/* Filename for file upload received as encoded by the browser. */
/* It might contain an absolute path to a file from the sending */
/* host. Open a file for writing. */
return;
case 2:
/* File content data received. Write data to a file. */
/* This function will be called several times with */
/* code 2 when a big file is being uploaded. */
return;
case 3:
/* File upload finished. Close a file. */
return;
case 4:
/* XML encoded content type, last packet. */
// pType = http_get_content_type ();
/* check the content type for CGX file request. */
/* pType is a pointer to a 0-terminated string */
/* For example: text/xml; charset=utf-8 */
return;
case 5:
/* XML encoded as under 4, but with more to follow. */
return;
default:
/* Ignore all other codes. */
return;
}
if (len == 0) {
/* No data or all items (radio, checkbox) are off. */
return;
}
var = (U8 *)alloc_mem (40);
do {
/* Parse all returned parameters. */
dat = http_get_var (dat, var, 40);
if (var[0] != 0) {
}
}while (dat);
free_mem ((OS_FRAME *)var);
}
/*--------------------------- cgi_func --------------------------------------*/
U16 cgi_func (U8 *env, U8 *buf, U16 buflen, U32 *pcgi) {
/* This function is called by HTTP server script interpreter to make a */
/* formated output for 'stdout'. It returns the number of bytes written */
/* to the output buffer. Hi-bit of return value (len is or-ed with 0x8000)*/
/* is a repeat flag for the system script interpreter. If this bit is set */
/* to 1, the system will call the 'cgi_func()' again for the same script */
/* line with parameter 'pcgi' pointing to a 4-byte buffer. This buffer */
/* can be used for storing different status variables for this function. */
/* It is set to 0 by HTTP Server on first call and is not altered by */
/* HTTP server for repeated calls. This function should NEVER write more */
/* than 'buflen' bytes to the buffer. */
/* Parameters: */
/* env - environment variable string */
/* buf - HTTP transmit buffer */
/* buflen - length of this buffer (500-1400 bytes - depends on MSS) */
/* pcgi - pointer to session local buffer used for repeated loops */
/* This is a U32 variable - size is 4 bytes. Value is: */
/* - on 1st call = 0 */
/* - 2nd call = as set by this function on first call */
U32 len = 0;
switch (env[0]) {
/* Analyze the environment string. It is the script 'c' line starting */
/* at position 2. What you write to the script file is returned here. */
/* BEEP Input - xml file 'beep.cgx' */
case 'q':
switch (env[1]) {
case '1':
BEEP_Start(5, 1, 2);
break;
}
len = 0;
break;
case 'g':
/* DS18B20 Input - xml file 'ds180b20.cgi' */
switch (env[2]) {
case '1':
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
len = sprintf((char *)buf,(const char *)&env[4], RTC_TimeStructure.RTC_Hours,
RTC_TimeStructure.RTC_Minutes,
RTC_TimeStructure.RTC_Seconds);
break;
case '2':
len = sprintf((char *)buf,(const char *)&env[4],(float)iTemp/16);
break;
}
break;
case 'p':
/* DS18B20 Input - xml file 'ds180b20.cgx' */
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
len = sprintf((char *)buf,(const char *)&env[1], RTC_TimeStructure.RTC_Hours,
RTC_TimeStructure.RTC_Minutes,
RTC_TimeStructure.RTC_Seconds);
break;
case 'x':
/* DS18B20 Input - xml file 'ds180b20.cgx' */
len = sprintf((char *)buf,(const char *)&env[1], (float)iTemp/16);
break;
}
return ((U16)len);
}
/*--------------------------- cgx_content_type ------------------------------*/
#if 0
U8 *cgx_content_type (void) {
/* User configurable Content-type for CGX script files. If this function */
/* is missing, or returns a NULL pointer, the default 'text/xml' content */
/* type from the library is used for xml-script file types. */
return ("text/xml; charset=utf-8");
}
#endif
/*--------------------------- http_encoding ---------------------------------*/
#if 0
U8 *http_encoding (void) {
/* User configurable character encoding for text types. If this function */
/* is missing, or returns a NULL pointer, web browser uses a default */
/* character encoding, which is set in the browser configuration. */
return ("utf-8");
}
#endif
/*--------------------------- http_accept_host ------------------------------*/
#if 0
BOOL http_accept_host (U8 *rem_ip, U16 rem_port) {
/* This function checks if a connection from remote host is accepted or */
/* not. If this function is missing, all remote hosts are accepted. */
if (rem_ip[0] == 192 &&
rem_ip[1] == 168 &&
rem_ip[2] == 1 &&
rem_ip[3] == 1) {
/* Accept a connection. */
return (__TRUE);
}
/* Deny a connection. */
return (__FALSE);
}
#endif
/*----------------------------------------------------------------------------
* end of file
*---------------------------------------------------------------------------*/ |