lwip1.4.0之http-server实现及POST-实现

上传人:206****923 文档编号:91574391 上传时间:2019-06-30 格式:DOCX 页数:7 大小:10.97KB
返回 下载 相关 举报
lwip1.4.0之http-server实现及POST-实现_第1页
第1页 / 共7页
lwip1.4.0之http-server实现及POST-实现_第2页
第2页 / 共7页
lwip1.4.0之http-server实现及POST-实现_第3页
第3页 / 共7页
lwip1.4.0之http-server实现及POST-实现_第4页
第4页 / 共7页
lwip1.4.0之http-server实现及POST-实现_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《lwip1.4.0之http-server实现及POST-实现》由会员分享,可在线阅读,更多相关《lwip1.4.0之http-server实现及POST-实现(7页珍藏版)》请在金锄头文库上搜索。

1、lwip1.4.0之httpserver实现及POST实现一、HTTPSERVER的实现lwip默认的httpserver在apps/httpserver_raw主要核心文件为fs.cfs.h(读取相关html相关资源),httpd.chttpd.hhttpd_structs.h为http协议核心文件首先在LWIP协议栈正常运行后需要在main函数中调用httpd_init()初始化Http正常情况下般还需要实现SSI和CGI回调函数的初始工作本人写在一个函数中如下:voidhttp_start(void)http_set_ssi_handler(SSIHandler,g_pcConfigSS

2、ITags,sizeof(g_pcConfigSSITags)/sizeof(char*);http_set_cgi_handlers(g_psConfigCGIURIs,sizeof(g_psConfigCGIURIs)/sizeof(tCGI);然在httpd_init()下调用http_start()完成初始化SSI和CGI的工作。其次要使用makefsfile.exe对网页进行编译这个小工具可以从网上下载一个,本人将编译命令写在一个makefsfile.bat批处理文件中每次编译只要运行一下makefsfile.bat具体命令如下:echooffmakefsfile-iweb_page

3、s-o./lwip-1.4.0/src/apps/httpserver_raw/fsdata.h-r-hechoon其中web_pages为所包含的网页文件夹产生的网页数据放在fsdata.h中用于跟工程文件一起编译,-r表示每次编译网页时重写fsdata.h-h表示产生的网页数据中不包含http协议头部因为本人在HTTP中使用的是动态产生HTTP协议头。至此网页完成在浏览器中敲入板子的IP地址便可以看到网页了二、POST方案实现LWIPHTTP协议中默认只支持GET方法但是一般提交表单时都用POST方法而LWIPPOST方案需要自己实现不过LWIP已经需要实现的函数申明在httpd.h中了首

4、先将宏LWIP_HTTPD_SUPPORT_POST设置成1表示支持HTTPPOST方法,需要实现的函数分别为:httpd_post_begin(当接收到一个POST请求时会调用此函数),httpd_post_receive_data(接收HTTPPOST数据),httpd_post_finished(接收完成后调用此函数)具体实现如下:err_thttpd_post_begin(void*connection,constchar*uri,constchar*http_request,u16_thttp_request_len,intcontent_len,char*response_uri,

5、u16_tresponse_uri_len,u8_t*post_auto_wnd)#ifLWIP_HTTPD_CGIinti=0;#endifstructhttp_state*hs=(structhttp_state*)connection;if(!uri|(uri0=0)returnERR_ARG;hs-cgi_handler_index=-1;/此变量为本人自己在structhttp_state添加用于保存CGIhandler索引为-1表示无CGIhandler索引hs-response_file=NULL;/此变量为本人自己在structhttp_state添加用于保存CGIhandle

6、r处理完后返回的响应uri.#ifLWIP_HTTPD_CGIif(g_iNumCGIs&g_pCGIs)for(i=0;icgi_handler_index=i;/找到响应的CGIhandler将其保存在cgi_handler_index以便在httpd_post_receive_data中使用break;if(i=g_iNumCGIs)returnERR_ARG;/未找到CGIhandler#endifreturnERR_OK;#defineLWIP_HTTPD_POST_MAX_PAYLOAD_LEN512staticcharhttp_post_payloadLWIP_HTTPD_POS

7、T_MAX_PAYLOAD_LEN;staticu16_thttp_post_payload_len=0;err_thttpd_post_receive_data(void*connection,structpbuf*p)structhttp_state*hs=(structhttp_state*)connection;structpbuf*q=p;intcount;u32_thttp_post_payload_full_flag=0;while(q!=NULL)/缓存接收的数据至http_post_payloadif(http_post_payload_len+q-lenpayload,q-

8、len);http_post_payload_len+=q-len;else/缓存溢出置溢出标志位http_post_payload_full_flag=1;break;q=q-next;pbuf_free(p);/释放pbufif(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;elseif(hs-post_content_len_left=0)/POST数据已经接收

9、完毕则处理if(hs-cgi_handler_index!=-1)count=extract_uri_parameters(hs,http_post_payload);/解析hs-response_file=g_pCGIshs-cgi_handler_index.pfnCGIHandler(hs-cgi_handler_index,count,hs-params,hs-param_vals);/调用解析函数http_post_payload_len=0;elsehs-response_file=NULL;http_post_payload_len=0;returnERR_OK;voidhttpd_post_finished(void*connection,char*response_uri,u16_tresponse_uri_len)structhttp_state*hs=(structhttp_state*)connection;if(hs-response_file!=NULL)strncpy(response_uri,hs-response_file,response_uri_len);/拷贝uri用于给浏览器响应相应的请求至此HTTPSERVER和HTTPPOST方法实现完成

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号