cgic 寫CGI程式

來源:互聯網
上載者:User

標籤:

CGIC是C語言CGI庫函數,用於編寫CGI程式

CGIC 主要完成以下功能

* 對資料進行文法分析 

* 接收以 GET 和 PSOT 兩種方式發送的資料 

* 把 FORM 中的不同域串連成連續的串 

* 為檢索 FORM 資料而提供字串 , 整數 , 浮點以及單項和多項選擇功能 

* 為數字欄位提供邊界檢測 

* 把 CGI 環境變數載入到非空的 C 串中 

* 為調試而捕捉 CGI 狀態 

 

如何寫CGIC應用程式

任何cgic 應用程式必須串連到cgic.c,如果在Linux 下可以用Makefile 做這個工作。

因為CGI 程式需要做一些初始化操作,如擷取環境變數,分析資料等,所以cgic 庫提供了自己的main()函數,那自己寫的cgic程式不能再包含main()函數,而必須以cgiMain()函數開始,cgic.c會調用這個函數。當然這個函數名是可以更改的.還要注意的是,自己編寫的cgic 程式必須包含#include<cigi.h>

 也就是說cgic,已經做了一些相關的解析工作,在自己的main函數中只要調用cigic 中相應的函數便可。

以下是cigic 庫裡內建的cgic test程式的cgiMain()

 

[cpp] view plain copy  print?
  1. int cgiMain() {  
  2. #ifdef DEBUG  
  3.   LoadEnvironment();  
  4. #endif /* DEBUG */  
  5.   /* Load a previously saved CGI scenario if that button 
  6.     has been pressed. */  
  7.   if (cgiFormSubmitClicked("loadenvironment") == cgiFormSuccess) {  
  8.     LoadEnvironment();  
  9.   }  
  10.   /* Set any new cookie requested. Must be done *before* 
  11.     outputting the content type. */  
  12.   CookieSet();  
  13.   /* Send the content type, letting the browser know this is HTML */  
  14.   cgiHeaderContentType("text/html");  
  15.   /* Top of the page */  
  16.   fprintf(cgiOut, "<HTML><HEAD>\n");  
  17.   fprintf(cgiOut, "<TITLE>cgic test</TITLE></HEAD>\n");  
  18.   fprintf(cgiOut, "<BODY><H1>cgic test</H1>\n");  
  19.   /* If a submit button has already been clicked, act on the  
  20.     submission of the form. */  
  21.   if ((cgiFormSubmitClicked("testcgic") == cgiFormSuccess) ||  
  22.     cgiFormSubmitClicked("saveenvironment") == cgiFormSuccess)  
  23.   {  
  24.     HandleSubmit();  
  25.     fprintf(cgiOut, "<hr>\n");  
  26.   }  
  27.   /* Now show the form */  
  28.   ShowForm();  
  29.   /* Finish up the page */  
  30.   fprintf(cgiOut, "</BODY></HTML>\n");  
  31.   return 0;  
  32. }  


 

[cpp] view plain copy  print?
  1. #ifdef DEBUG  
  2.     LoadEnvironment();  
  3. #endif /* DEBUG */  


一般採用以上方式,調用環境變數用作調試。 可以插入語句:"#define DEBUG 1 將DEBUG模式開啟。

 cgiHeaderContentType("text/html") 這個函數主要是輸出Content Type Header,告訴瀏覽器返回的是一個html頁面,當然也可以是"image/gif", "image/jpeg" and "audio/wav

 

外一點就是,cgi程式輸出HTML頁面的方式都是使用printf把頁面一行一行地列印出來,比如cgictest.c中的這一段代碼:
fprintf(cgiOut, "<textarea NAME=\"address\" ROWS=4 COLS=40>\n");
fprintf(cgiOut, "Default contents go here. \n");
fprintf(cgiOut, "</textarea>\n");
上面這段代碼的運行結果就是在頁面上輸出一個textarea。 第一個參數cgiOut實際上就是stdin,所以我們可以直接使用printf,而不必使用fprintf。不過在調試的時候會用到fprintf來重新導向輸出

 

CGI 調試起來比較麻煩,CGIC提供了一個函數可以將環境變數輸入到檔案,方便調試。

 

[cpp] view plain copy  print?
    1. cgiWriteEnvironment("/etc/conf.d/c0/capcgi.dat");  

cgic 寫CGI程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.