www.pudn.com > TcpIpOn8051.rar > HTTP.C
#include
#include "public.h"
#include "ether.h"
#include "arp.h"
#include "ip.h"
#include "tcp.h"
#define VERSION "1.0"
/* HTTP response header: 0=none, 1=simple OK, 2=content-type */
#define HTTP_HEAD 2
/* Directory type: 0=text, 1=simple HTML, 2=table, 3=large dir support */
#define HTML_DIR 3
#define HTTP_OK "HTTP/1.0 200 OK\r\n"
#define HTTP_NOFILE "HTTP/1.0 404 Not found\r\n"
#define HTTP_HTM "Content-type: text/html\r\n"
#define HTTP_TXT "Content-type: text/plain\r\n"
#define HTTP_GIF "Content-type: image/gif\r\n"
#define HTTP_XBM "Content-type: image/x-xbitmap\r\n"
#define HTTP_BLANK "\r\n"
#define HTTP_MAXLEN 400
#define ndata 10
char httpreq[HTTP_MAXLEN+1]; // Buffer for first part of HTTP request
//BYTE code html[]=""
// "登陆"
// ""
// ""
// "串口设备网关系统"
// "";
BYTE code html[]=""
""
"登陆"
""
""
"";
BYTE code index1[]=""
""
"更改密码"
""
""
"";
BYTE code changesuccess[]="修改用户名密码成功修改成功";
BYTE code index2[]="ERROR输入不合法";
BYTE code index3[]="SUCCESS修改成功";
BYTE code index4[]="权限错误username or password error";
BYTE code index5[]=""
"ICSD232网关系统"
""
""
"";
/*
BYTE code systemCon[] = ""
"系统配置"
""
""
"";
*/
//BYTE code serialCon[] =
char filepath[10]="";
char filedir[10]="c:/test/";
static WORD III;
char clientcmd[30]; // Pointer to command-line command
BYTE telopts[] =
{
TEL_IAC, TEL_DO, TEL_SGA, // Do suppress go-ahead
TEL_IAC, TEL_WONT, TEL_ECHO, // Won't echo
TEL_IAC, TEL_WONT, TEL_AUTH // Won't authenticate
};
typedef struct {
BYTE *in; /* File I/P pointer */
WORD length; /* State variable/counter */
} APPDATA;
APPDATA appdata[nsocks];
//定义用户权限
IDENTIFY xdata id;
extern mycomp xdata mypara;
extern TCP_SOCK xdata tsock[nsocks];
extern void x24c02_wrcon(BYTE address,BYTE *info,BYTE len);
WORD server_action(TCP_SOCK *ts, CONN_STATE conn);
WORD client_action(TCP_SOCK *ts, CONN_STATE conn);
void ping(ip destip);
char *com_get(char *strTocken,char *strDilimit);
void http_get(TCP_SOCK *ts, char *fname,bit flag);
void http_post(TCP_SOCK *ts, char *fname);
int dir_head(CBUFF *bp, char *path);
void strlwr(char *str);
BYTE *fopen(char *filepath);
void http_data(TCP_SOCK *ts);
extern get_current();
extern delay_ms(BYTE sec);
extern initcpu();
extern void Task_attemper(void);
extern void ip_arpreq(ip destip);
extern WORD buff_in(CBUFF *bp, BYTE *tdata, WORD len);
extern WORD buff_out(CBUFF *bp, BYTE *tdata, WORD maxlen);
extern WORD buff_chrlen(CBUFF *bp, char c);
extern WORD buff_instr(CBUFF *bp, char *str);
extern WORD buff_inprintf(CBUFF *bp, char *str, void *p);
extern WORD buff_freelen(CBUFF *bp);
extern void print(char str[]);
extern void icmp_send(netcard *netbuf,ip destip,BYTE type,BYTE icode,WORD ident,WORD seq,WORD len);
extern BYTE tcp_open(TCP_SOCK *ts,ip destip, WORD destport);
extern BYTE tcp_close(TCP_SOCK *ts);
extern WORD mindata(WORD a, WORD b);
extern DWORD strtoip(char *arg);
extern void set_node(WORD *mac);
extern WORD strtoword(char *str);
extern void strtomac(BYTE *str,WORD mac[3]);
extern WORD check_str(BYTE *mac_str);
extern int check_ip(BYTE *ip_str);
void iptostr(ip sip,char *str);
extern void write_nodeid(void);
extern void write_ip(void);
extern void write_mask(void);
extern void write_gate(void);
void write_24c02(char *ipstr,char *macstr,char *maskstr,char *gatestr);
/* Application-specific storage */
void main()
{
//WORD numnum;
register BYTE i;
initcpu();
//numnum=0;
print("start!\n\r");
ip_arpreq(mypara.mygate);
for (i=0; ilocport;
adp = (APPDATA *)ts->app;
if (port != httpport)
ok = 0;
else if (conn == TCP_OPEN)
{
adp->in = 0;
}
else if (conn == TCP_DATA)
{
if ((len = buff_chrlen(&ts->rxbuf, '\n'))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len] = 0;
// s = strtok(httpreq, " ");
s = com_get(httpreq, " "); // Chop into tokens
if (!strcmp(httpreq, "GET")) // 1st token is 'GET'?
{
name = strtok(s, " ?\r\n"); // 2nd token is filename
http_get(ts, name,(bit)0); // Process filename
}
else if (!strcmp(httpreq, "POST")) // 1st token is 'GET'?
{
name = strtok(s, " ?\r\n"); // 2nd token is filename
http_post(ts, name); // Process filename
}
}
http_data(ts);
}
else if(conn == TCP_NODATA)
{
http_data(ts);
}
//http_data(ts);
return(ok);
}
// Process the filepath from an HTTP 'get' method
void http_get(TCP_SOCK *ts, char *fname,bit flag)
{
APPDATA *adp;
char *s=0;
adp = (APPDATA *)ts->app;
strcpy(filepath, filedir); // Add on base directory
if (*fname) // Copy filename without leading '/'
strcat(filepath, fname+1); //skip "/"
strlwr(filepath); // Force to lower-case
if (strlen(fname) <=1)
{ // If name is only a '/'..
strcpy(filepath, filedir);
strcat(filepath, "index.html"); // ..display directory
}
if ((adp->in = fopen(filepath)) == 0)
{ // ..send message
buff_instr(&ts->txbuf, HTTP_NOFILE HTTP_TXT HTTP_BLANK);
buff_inprintf(&ts->txbuf, "Webserve v%s\r\n", VERSION);
buff_inprintf(&ts->txbuf, "Can't find '%s'\r\n", filepath);
tcp_close(ts);
}
else // File found OK
{
// if(!strcmp(fname,"/index5.html"))
// {
// if(flag==0)
// adp->in=index4;
// }
buff_instr(&ts->txbuf, HTTP_OK HTTP_HTM HTTP_BLANK);
adp->length=strlen(adp->in);
// s = strstr(filepath, ".html") ? HTTP_HTM :
// strstr(filepath, ".txt") ? HTTP_TXT :
// strstr(filepath, ".gif") ? HTTP_GIF :
// strstr(filepath, ".xbm") ? HTTP_XBM : "";
// buff_instr(&ts->txbuf, s);
// buff_instr(&ts->txbuf, HTTP_BLANK);
}
}
// Process the filepath from an HTTP 'post' method
void http_post(TCP_SOCK *ts, char *fname)
{
//APPDATA *adp;
WORD len,temp[3];
char *s=0;
char username[10];
char password[10];
char ipstr[16];
char macstr[16];
char maskstr[16];
char gatestr[16];
char name[20];
char temp1[16];
BYTE httpStr[30] = "http://";
strcpy(name,fname);
if(!strcmp(name,"/index5.html")||!strcmp(name,"/index1.html"))
{
if ((len = buff_chrlen(&ts->rxbuf, '='))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len] = 0;
}
if ((len = buff_chrlen(&ts->rxbuf, '&'))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len-1] = 0;
}
strcpy(username,httpreq);
if ((len = buff_chrlen(&ts->rxbuf, '='))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len] = 0;
}
if ((len = buff_chrlen(&ts->rxbuf, '\r'))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len-1] = 0;
}
strcpy(password,httpreq);
if(!strcmp(username,id.username) &&!strcmp(password,id.password))
http_get(ts, name,(bit)1);
else http_get(ts,"/index4.html",(bit)1);
}
else
{
if(!strcmp(name,"/changesuccess.html"))
{
if ((len = buff_chrlen(&ts->rxbuf, '='))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len] = 0;
}
if ((len = buff_chrlen(&ts->rxbuf, '&'))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len-1] = 0;
}
strcpy(id.username,httpreq);
if ((len = buff_chrlen(&ts->rxbuf, '='))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len] = 0;
}
if ((len = buff_chrlen(&ts->rxbuf, '&'))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len-1] = 0;
}
strcpy(id.password,httpreq);
write_id();
http_get(ts, name,(bit)1);
}
else
{
if(!strcmp(name,"/index3.html"))
{
if ((len = buff_chrlen(&ts->rxbuf, '='))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len] = 0;
}
if ((len = buff_chrlen(&ts->rxbuf, '&'))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len-1] = 0;
}
strcpy(ipstr,httpreq);
if ((len = buff_chrlen(&ts->rxbuf, '='))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len] = 0;
}
if ((len = buff_chrlen(&ts->rxbuf, '&'))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len-1] = 0;
}
strcpy(macstr,httpreq);
if ((len = buff_chrlen(&ts->rxbuf, '='))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len] = 0;
}
if ((len = buff_chrlen(&ts->rxbuf, '&'))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); //Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len-1] = 0;
}
strcpy(maskstr,httpreq);
if ((len = buff_chrlen(&ts->rxbuf, '='))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len] = 0;
}
if ((len = buff_chrlen(&ts->rxbuf, '&'))!=0) // Got request?
{
len = mindata(len+1, HTTP_MAXLEN); // Truncate length
buff_out(&ts->rxbuf, (BYTE *)httpreq, (WORD)len);
httpreq[len-1] = 0;
}
strcpy(gatestr,httpreq);
if(check_str(macstr)&&check_ip(ipstr)&&check_ip(maskstr)&&check_ip(gatestr))
{
write_24c02(ipstr,macstr,maskstr,gatestr);
http_get(ts, name,(bit)1);
}
else http_get(ts,"/index2.html",(bit)1);
}
else http_get(ts, name,(bit)1);
}
}
}
void write_24c02(char *ipstr,char *macstr,char *maskstr,char *gatestr)
{
WORD temp[3],temp_mac[3];
ip temp_ip,temp_mask,temp_gate;
BYTE i;
//写入mac地址
strtomac((BYTE *)macstr,temp);
for(i=0;i<3;i++)
temp_mac[i] = mypara.mynodeid[i].word;
set_node(&temp);
write_nodeid();
for(i=0;i<3;i++)
mypara.mynodeid[i].word = temp_mac[i];
//写入ip
temp_ip = mypara.myip;
mypara.myip = strtoip(ipstr);
write_ip();
mypara.myip = temp_ip;
//写入掩码
temp_mask = mypara.mymask;
mypara.mymask = strtoip(maskstr);
write_mask();
mypara.mymask = temp_mask;
//写入网关
temp_gate = mypara.mygate;
mypara.mygate = strtoip(gatestr);
write_gate();
mypara.mygate = temp_gate;
}
// If there is space in the transmit buffer, send HTTP data
void http_data(TCP_SOCK *ts)
{
APPDATA *adp;
WORD len;
// char *s;
adp = (APPDATA *)ts->app;
if (adp->in && (len = buff_freelen(&ts->txbuf)) > 0)
{ // ..put out as much as possible
len=mindata(len,adp->length);
if (!adp->length || (len = buff_in(&ts->txbuf, adp->in, (WORD)len)) == 0)
{ // If end of file, close it..
//fclose(adp->in);
adp->in = 0;
tcp_close(ts); // ..and start closing connection
}
else
{
if(adp->length<=len)
adp->length=0;
else
adp->length-=len;
adp->in+=len;
}
}
}
// Write out head of HTML file dir, given filepath. Return 0 if error
int dir_head(CBUFF *bp, char *path)
{
return(buff_instr(bp, HTTP_OK HTTP_HTM HTTP_BLANK) &&
buff_instr(bp, html));
//buff_instr(bp, "This is a test刘虹