| 
 
新手上路 
 
	积分36金钱36 注册时间2017-11-15在线时间4 小时 | 
 
2金钱 
| 我买的是F429的板子, 然后通过光盘附送的代码, 然后进行通过LWIP搭建web_server, 原始的demo能够通, 但是只能支持cgi的get, 我想用post。web的html没有使用原始的。 我看里面有一个
 #define LWIP_HTTPD_SUPPORT_POST   0
 然后我将值更改成1,
 然后进行编译,发现httpd_post_begin(), httpd_post_receive_data(), httpd_post_finished()只有声明, 没有实现代码,然后我在网上找了下,
 将代码抄了下。
 链接地址为:
 http://bbs.csdn.net/topics/390967955
 http://blog.csdn.net/lijing198997/article/details/25987193
 但是web可能有问题, 连接上网页后会发送两次cgi请求(主要是cgi响应函数里面的打印出现了两次,认为PC上的浏览器打开网页后,发送了两次,本来应该连接一次,后面再查找原因),
 web_server在第二次cgi请求后,会出现死机的现象。 不知道什么原因造成,我进行跟踪,在http_recv() 函数里面最后一个return返回的时候加的打印信息,可以看见这个打印信息,
 因此可以判断是http_recv()执行完了后面的代码造成的,具体是那个地方还没找到,因为http_recv是一个注册的回调函数。
 不知道哪里出了问题。
 
 httpd_post的begin, recvieve_data, finished三个函数代码如下:
 代码如下:
 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; // 此变量为本人自己在struct http_state 添加 用于保存 CGI handler 处理完后返回的响应uri.
 
 #if LWIP_HTTPD_CGI
 if (g_iNumCGIs && g_pCGIs) {
 for (i = 0; i < g_iNumCGIs; i++) {
 if (strcmp(uri, g_pCGIs.pcCGIName) == 0) {
 
 hs->cgi_handler_index = i; // 找到响应的 CGI handler 将其保存在cgi_handler_index 以便在httpd_post_receive_data中使用
 break;
 }
 }
 }
 
 
 if(i == g_iNumCGIs) {
 return ERR_ARG; // 未找到CGI handler
 }
 #endif
 
 return ERR_OK;
 }
 
 #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_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);
 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 = g_pCGIs[hs->cgi_handler_index].pfnCGIHandler(hs->cgi_handler_index, count, hs->params,
 hs->param_vals); // 调用解析函数
 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 用于给浏览器响应相应的请求
 }
 }
 
 
 | 
 |