lwip的sample_http.c
void
httpd_init(void)
{
struct tcp_pcb *pcb;
pcb = tcp_new();
tcp_bind(pcb, NULL, 80);
tcp_listen(pcb);
tcp_accept(pcb, http_accept);
}
static err_t
http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
struct http_state *hs;
tcp_setprio(pcb, TCP_PRIO_MIN);
/* Allocate memory for the structure that holds the state of the
connection. */
hs = mem_malloc(sizeof(struct http_state));
if(hs == NULL) {
return ERR_MEM;
}
/* Initialize the structure. */
hs->file = NULL;
hs->left = 0;
hs->retries = 0;
/* Tell TCP that this is the structure we wish to be passed for our
callbacks. */
tcp_arg(pcb, hs);
/* Tell TCP that we wish to be informed of incoming data by a call
to the http_recv() function. */
tcp_recv(pcb, http_recv);
tcp_err(pcb, conn_err);
tcp_poll(pcb, http_poll, 4);
return ERR_OK;
}
static err_t
http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
int i;
char *data;
struct http_state *hs;
//added by millin, 2008.01
u8_t hour,min,sec;
u16_t day;
hs = arg;
if(err == ERR_OK && p != NULL) {
/* Inform TCP that we have taken the data. */
tcp_recved(pcb, p->tot_len);
if(hs->file == NULL) {
data = p->payload;
if(*data =='G') {
for(i = 0; i < 40; i++) {
if( ((char *)data + 4)[i] == ' ' || ((char *)data + 4)[i] == '\r' || ((char *)data + 4)[i] == '\n') {
((char *)data + 4)[i] = 0;
}// if
}//for
// if it was GET dsp.jpg
if ((*(data+5) =='E') && (*(data+6) =='V') && (*(data+7) =='B')) {
hs->file = (char *)&jpeg;
hs->left = sizeof(jpeg)+1;
}
// GET other files
else {
hs->file = (char *)&demo;
hs->left = sizeof(demo)+1;
}
pbuf_free(p);
send_data(pcb, hs);
/* Tell TCP that we wish be to informed of data that has been
successfully sent by a call to the http_sent() function. */
tcp_sent(pcb, http_sent);
} //if(*data =='G')
else {
pbuf_free(p);
close_conn(pcb, hs);
} //else if(*data =='G')
} //if(hs->file == NULL)
else {
pbuf_free(p);
}
}
if(err == ERR_OK && p == NULL) {
close_conn(pcb, hs);
}
return ERR_OK;
}