檔案上傳後, 網頁伺服器會呼叫 CGI 處理, 需要在 CGI 端加入自訂的處理函式.
程式要放在
mint_cm/mcm_cgi/mcm_cgi_upload_module
沒有限制函式的放置方式, 可以依據不同的功能將各種函式分類放在不同的檔案.
步驟-01 : 加入基本的標頭檔 :
#include <stdio.h>
#include "mcm_lib/mcm_lheader/mcm_return.h"
#include "../mcm_cgi_common_extern.h"
#include "mcm_cgi_module_debug.h"
如果需要使用 MintCM 函式則改加入 (以及在 Makefile 中加入鏈結 MintCM 的函式庫) :
#include <stdio.h>
#include "mcm_lib/mcm_lheader/mcm_type.h"
#include "mcm_lib/mcm_lheader/mcm_size.h"
#include "mcm_lib/mcm_lheader/mcm_connect.h"
#include "mcm_lib/mcm_lheader/mcm_return.h"
#include "mcm_lib/mcm_lheader/mcm_data_exinfo_auto.h"
#include "mcm_lib/mcm_lulib/mcm_lulib_api.h"
#include "../mcm_cgi_common_extern.h"
#include "mcm_cgi_module_debug.h"
步驟-02 : 加入處函式.
函式格式 :
void $(function_name)(
struct part_info_t *part_list)
{
...
}
$(function_name)
自定義的函式名稱.
參數的用途 :
| 參數 |
說明 |
struct part_info_t *
part_list
|
上傳的 form 裡面的每一個 input 元素內容, 雙向串列結構
(紀錄 callback 的元素不會加入到此表)
[詳細]
|
[struct part_info_t *part_list]
結構成員
char *
name_tag
|
元素的 name [詳細]
|
char *
filename_tag
|
上傳的檔案的檔案名稱
必須是 file 類型的元素而且有選擇檔案上傳才會有值
|
char *
data_con
|
元素的 value 或上傳的檔案的資料內容
|
MCM_DTYPE_USIZE_TD
data_len
|
資料的長度
|
struct part_info_t *
prev_part
|
前一個節點
|
struct part_info_t *
next_part
|
後一個節點
|
步驟-03 : 在處理的函式內將處理結果回應給瀏覽器.
函式 :
mcm_cgi_fill_response_header
填充回應資料的開頭部分.
| 參數 |
說明 |
MCM_DTYPE_BOOL_TD
fill_content_text
|
是否在回應資料的 HTTP header 中填入
Content-Type: text/plain
0 : 否
1 : 是
|
MCM_DTYPE_BOOL_TD
fill_connection_close
|
是否在回應資料的 HTTP header 中填入
Connection: close
0 : 否
1 : 是
|
int
ret_code
|
回應資料的代碼部分
(mcm_jslib_upload() 的 $.rep_code)
|
|
注意事項 :
| 01. |
mcm_cgi_fill_response_header() 只會填充開頭部分的資料. |
| 02. |
主要回應資料 (mcm_jslib_upload() 的 $.rep_data) 直接使用
printf() 填充.
|
| 03. |
至少要用 mcm_cgi_fill_response_header() 回應處理結果, 主要回應資料可有可無.
|
其他 : 在 console 顯示除錯訊息.
在 CGI 使用 printf() 會被網頁伺服器導向輸出給瀏覽器, 訊息要寫入 console 設備才能顯示.
在處理函式的開頭加入打開 console 設備 :
#if MCM_CUMEMODE | MCM_CUMDMODE
dbg_console_fd = open(MCM_DBG_CONSOLE, O_WRONLY);
if(dbg_console_fd == -1)
return;
#endif
在離開處理函式時加入關閉 console 設備 :
#if MCM_CUMEMODE | MCM_CUMDMODE
close(dbg_console_fd);
#endif
[MCM_CUMEMODE]
定義, 是否要顯示錯誤訊息.
開關的值的定義在
mint_cm/mcm_lib/mcm_lheader/mcm_debug.h.
[MCM_CUMDMODE]
定義, 是否要顯示除錯訊息.
開關的值的定義在
mint_cm/mcm_lib/mcm_lheader/mcm_debug.h.
[MCM_DBG_CONSOLE]
定義, console 設備的路徑.
路徑的值定義在
mint_cm/mcm_lib/mcm_lheader/mcm_debug.h.
[int dbg_console_fd]
變數, 紀錄開啟的設備的檔案編號.
變數的宣告定義在
mcm_cgi_module_debug.h.
[char dbg_msg_buf[MCM_DBG_BUFFER_SIZE]]
變數, 儲存訊息的緩衝.
變數的宣告定義在
mcm_cgi_module_debug.h.
緩衝大小的定義在
mint_cm/mcm_lib/mcm_lheader/mcm_debug.h.
[MCM_CUMEMSG(msg_fmt, msg_args...)]
巨集, 顯示訊息 (錯誤類), 用法同
printf().
巨集的定義在
mcm_cgi_module_debug.h.
[MCM_CUMDMSG(msg_fmt, msg_args...)]
巨集, 顯示訊息 (除錯類), 用法同
printf().
巨集的定義在
mcm_cgi_module_debug.h.
範例 :
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "mcm_lib/mcm_lheader/mcm_return.h"
#include "../mcm_cgi_common_extern.h"
#include "mcm_cgi_module_debug.h"
void upload_handle(
struct part_info_t *part_list)
{
struct part_info_t *tmp_part;
#if MCM_CUMEMODE | MCM_CUMDMODE
dbg_console_fd = open(MCM_DBG_CONSOLE, O_WRONLY);
if(dbg_console_fd == -1)
return;
#endif
mcm_cgi_fill_response_header(1, 1, MCM_RCODE_PASS);
printf("alert(");
for(tmp_part = part_list; tmp_part != NULL; tmp_part = tmp_part->next_part)
{
MCM_CUMDMSG("name = %s", tmp_part->name_tag);
MCM_CUMDMSG("filename = %s", tmp_part->filename_tag);
MCM_CUMDMSG("data = [" MCM_DTYPE_USIZE_PF "][%p]",
tmp_part->data_len, tmp_part->data_con);
printf("\"name = %s\\n\" + ", tmp_part->name_tag);
printf("\"filename = %s\\n\" + ", tmp_part->filename_tag);
printf("\"data = [" MCM_DTYPE_USIZE_PF "][%p]\"",
tmp_part->data_len, tmp_part->data_con);
if(tmp_part->next_part != NULL)
printf(" + \"\\n\" + \"\\n\" + ");
}
printf(");\n");
#if MCM_CUMEMODE | MCM_CUMDMODE
close(dbg_console_fd);
#endif
return;
}