//http_state结构体,增加两个元素:
char *response_file;//处理后返回的uri
int http_post_payload_len; //
struct http_state {
struct fs_file *handle; //一个指向fs_file的指针
char *file; /*指向buf中第一个未被发送的字节 Pointer to first unsent byte in buf. */
int cgi_handler_index; //为-1表示无CGI handler索引
char *response_file;//处理后返回的uri
int http_post_payload_len; //
代码:
#define LWIP_HTTPD_POST_MAX_PAYLOAD_LEN 512
static char http_post_payload[LWIP_HTTPD_POST_MAX_PAYLOAD_LEN];
static u16_t http_post_payload_len=0;
err_t httpd_post_begin(void *connection, const char *uri, const char *http_request,
u16_t http_request_len, int content_len, char *response_uri,
u16_t response_uri_len, u8_t *post_auto_wnd){
#if LWIP_HTTPD_CGI
int i=0;
#endif
struct http_state *hs=(struct http_state *)connection;
if(!uri || (uri[0]=='\0')){
return ERR_ARG;
}
hs->cgi_handler_index=-1;//此变量本人在struct http_state 添加 用于保存CGI handler 索引,为-1表示无CGI handler索引
hs->response_file=NULL;
#if LWIP_HTTPD_CGI
if(g_iNumCGIs && g_pCGIs){
for(i=0;i<g_iNumCGIs;i++){
for(i=0;i<g_iNumCGIs;i++){
if(strcmp(uri,g_pCGIs[i].pcCGIName)==0){
hs->cgi_handler_index=i;//找到相应的CGI handler讲其保存在cgi_handler_index以便在httpd_post_data中使用
break;
}
}
}
if(i==g_iNumCGIs){
return ERR_ARG; //未找到CGI
}
#endif
return ERR_OK;
}
}
err_t httpd_post_receive_data(void *connection,struct pbuf *p){
struct http_state *hs=(struct http_state*)connection;
struct pbuf *q=p;
int count;
u32_t http_post_payload_full_flag=0;
while(q!=NULL){//缓存接受到的数据至http_post_payload
if(http_post_payload_len + q->len <=LWIP_HTTPD_POST_MAX_PAYLOAD_LEN){
MEMCPY(http_post_payload + http_post_payload_len,q->payload,q->len);
hs->http_post_payload_len+=q->len;}
else{//缓存溢出标志位
http_post_payload_full_flag=1;
break;
}
q=q->next;
}
pbuf_free(p);//释放pbuf;
if(http_post_payload_full_flag)//缓存溢出,则丢弃数据
{
http_post_payload_full_flag=0;
http_post_payload_len=0;
hs->cgi_handler_index=-1;
hs->response_file=NULL;
}else{
if(hs->post_content_len_left==0){//POST数据已经接受完毕
if(hs->cgi_handler_index!=-1){
count=extract_uri_parameters(hs,http_post_payload);//解析
hs->response_file=(char *)g_pCGIs[hs->cgi_handler_index].pfnCGIHandler(hs->cgi_handler_index,count,hs->params,hs->params);//调用解析函数
http_post_payload_len=0;
}else{
hs->response_file=NULL;
http_post_payload_len=0;
}
}
return ERR_OK;
}
}
void httpd_post_finished(void *connection,char *response_uri,u16_t response_uri_len){
struct http_state *hs=(struct http_state*)connection;
if(hs->response_file !=NULL){
strncpy(response_uri,hs->response_file,response_uri_len);//拷贝uri用于给浏览器相应的请求
}
}