www.pudn.com > uCOS_II_uart.rar > httpd.c


#include "lwip/sys.h" 
#include "lwip/api.h" 
 
/* This is the data for the actual web page. 
Most compilers would place this in ROM. */ 
const static char indexdata[] = 
" \ 
A test page \ 
 \ 
This is a small test page. \ 
 \ 
"; 
const static char http_html_hdr[] = 
"Content-type: text/html\r\n\r\n"; 
/* This function processes an incomming connection. */ 
static void 
process_connection(struct netconn *conn) 
{ 
struct netbuf *inbuf; 
char *rq; 
int len; 
/* Read data from the connection into the netbuf inbuf. 
We assume that the full request is in the netbuf. */ 
inbuf = netconn_recv(conn); 
/* Get the pointer to the data in the first netbuf 
fragment which we hope contains the request. */ 
netbuf_data(inbuf, &rq, &len); 
/* Check if the request was an HTTP "GET /\r\n". */ 
if(rq[0] == 'G' && rq[1] == 'E' && 
rq[2] == 'T' && rq[3] == ' ' && 
rq[4] == '/' && rq[5] == '\r' && 
rq[6] == '\n') { 
/* Send the header. */ 
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr),NETCONN_NOCOPY); 
/* Send the actual web page. */ 
netconn_write(conn, indexdata, sizeof(indexdata), 
NETCONN_NOCOPY); 
/* Close the connection. */ 
netconn_close(conn); 
} 
} 
} 
 
void httpd_thread(void *arg) 
{ 
struct netconn *conn, *newconn; 
/* Create a new TCP connection handle. */ 
conn = netconn_new(NETCONN_TCP); 
/* Bind the connection to port 80 on any 
local IP address. */ 
netconn_bind(conn, NULL, 80); 
/* Put the connection into LISTEN state. */ 
netconn_listen(conn); 
/* Loop forever. */ 
while(1) { 
/* Accept a new connection. */ 
newconn = netconn_accept(conn); 
/* Process the incomming connection. */ 
process_connection(newconn); 
/* Deallocate connection handle. */ 
netconn_delete(newconn); 
} 
return 0; 
} 
 
void httpd_init(void) 
{ 
  sys_thread_new(httpd_thread, NULL,TCPIP_THREAD_PRIO);   
}