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[]="" 
				"" 
				"登陆" 
				"" 
				"" 
    			"" 
				"" 
				"" 
  				"
ICSD232网关系统 

   
" "

" "" "" "" "" "" "
用户名:      密码:

"; 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网关系统" "" "" "
" "" "" "" "" "" "" "" "" "" "" // "" "" "" "
ICSD232网关系统 

ip地址
mac地址
子网掩码
网关

"; /* BYTE code systemCon[] = "" "系统配置" "" "" "" "" "
ICSD232系统配置 

   
" "

" "" "" "" "
" "

   

" "
" "

" "
"; */ //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刘虹

\r\n")); //buff_instr(bp, "\r\n")); } /* // Write out HTML file dir entry, given name. Return 0 if buffer full int dir_entry(CBUFF *bp, char *name) { #if HTML_DIR > 1 return(buff_inprintf(bp, "", name, name) && buff_inprintf(bp, "\r\n", find_filesize())); #else return(buff_inprintf(bp, "%s
\r\n", name, name)); #endif } */ // Write out tail of HTML file dir. Return length, 0 if error int dir_tail(CBUFF *bp) { #if HTML_DIR > 1 return(buff_instr(bp, "
FileSize
%s%lu
\r\n")); #else return(buff_instr(bp, "\r\n")); #endif } BYTE *fopen(char *filepath) { BYTE *result; int temp1 = 0; char xdata temp[16]; result=0; // temp1 = strlen(index5); if(!strcmp(filepath,"c:/test/index.html")) result=html; else if(!strcmp(filepath,"c:/test/index1.html")) result=index1; else if(!strcmp(filepath,"c:/test/index2.html")) result=index2; else if(!strcmp(filepath,"c:/test/index3.html")) result=index3; else if(!strcmp(filepath,"c:/test/index4.html")) result=index4; else if(!strcmp(filepath,"c:/test/index5.html")) result=index5; else if(!strcmp(filepath,"c:/test/changesuccess.html")) result=changesuccess; return(result); } char *com_get(char *strTocken,char *strDilimit) { register BYTE i; BYTE slen,dlen; char *s; slen=strlen(strDilimit); dlen=strlen(strTocken); s=strTocken; for(i=0;i<=dlen-slen;i++) { if(!strncmp(s++,strDilimit,slen)) break; } s--; *s=0; return(s+slen); } // void strlwr(char *str) { while(*str) { if(*str>='A' && *str<='Z') *str=*str+0x20; str++; } } // WORD client_action(TCP_SOCK *ts, CONN_STATE conn) { char ret[]="\r\n"; if (conn == TCP_OPEN) { if (ts->remport == telport || ts->remport == 1024) // If login, send Telnet opts buff_in(&ts->txbuf, telopts, sizeof(telopts)); if (clientcmd) { buff_in(&ts->txbuf, clientcmd,sizeof(clientcmd));// Send command-line string buff_in(&ts->txbuf, ret,strlen(ret)); } } return(1); } /* //server upcall WORD server_action(TCP_SOCK *ts, CONN_STATE conn) { WORD ok=1; WORD port, len; BYTE temps[30]; // time_t t; // temps[1]='\0'; port = ts->locport; // Connection being opened if (conn == TCP_OPEN) { ok = port==echoport || port==httpport || port==1024; //if (port == echoport || port==1024) // Echo server? //ts->connflags = TPUSH; // ..use PUSH flag // else if (port == HTTPORT) // HTTP server ? // { // ..load my page // buff_in(&ts->txb, (BYTE *)MYPAGE, sizeof(MYPAGE)); // } } else if (conn == TCP_CONN) // Connected { //if (port==DAYPORT || port==HTTPORT) // If daytime or HTTP // ok = 0; // ..close straight away } else if (conn == TCP_DATA) // Received data { if (port == echoport || port==1024) // If Echo { // ..echo it back! len=buff_out(&ts->rxbuf, temps, sizeof(temps)); temps[1]='\0'; if(len!=0) print(&temps[0]); //temps[0]=0x41; buff_in(&ts->txbuf, &temps[0], 1); } } return(ok); } */ void ping(ip destip) { netcard xdata netbuf;//缓冲区,注意:可能需要修改 print("pinging 202.118.19.174!\n\r"); icmp_send(&netbuf,destip,8,0x00,0x0200,III,60); III++; } /* EOF */