lwip1.4.0之http server實現及POST 實現
一、HTTP SERVER的實現
lwip默認的http server 在 apps/httpserver_raw 主要核心文件爲 fs.c fs.h(讀取相關html相關資源), httpd.c httpd.h httpd_structs.h 爲http協議核心文件
首先在LWIP協議棧正常運行後 需要在main函數中調用httpd_init() 初始化Http 正常情況下般還需要實現SSI和CGI回調函數的初始工作 本人寫在一個函數中 如下:
void http_start(void)
{
http_set_ssi_handler(SSIHandler, g_pcConfigSSITags, 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 具體命令如下:
echo off
makefsfile -i web_pages -o ../lwip-1.4.0/src/apps/httpserver_raw/fsdata.h -r -h
echo on
其中 web_pages爲所包含的網頁文件夾 產生的網頁數據放在fsdata.h中 用於跟工程文件一起編譯, -r 表示每次編譯網頁時重寫fsdata.h -h 表示產生的網頁數據中不包含http協議頭部 因爲本人在HTTP中使用的是動態產生HTTP協議頭。
至此 網頁完成 在瀏覽器中敲入板子的IP地址 便可以看到網頁了
二、POST方案實現
LWIP HTTP 協議中默認只支持GET方法 但是一般提交表單時都用POST方法 而LWIPPOST方案需要自己實現 不過LWIP已經需要實現的函數申明在httpd.h中了
首先將宏 LWIP_HTTPD_SUPPORT_POST 設置成1 表示支持HTTP POST 方法, 需要實現的函數分別爲: httpd_post_begin(當接收到一個POST請求時會調用此函數), httpd_post_receive_data(接收HTTP POST 數據), httpd_post_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[i].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 用於給瀏覽器響應相應的請求
}
}
至此 HTTP SERVER 和 HTTP POST 方法實現完成