天天看點

cgicc使用CgiCc 使用

CgiCc 使用

一、下載下傳

下載下傳位址:

http://ftp.gnu.org/gnu/cgicc/  
ftp://ftp.gnu.org/gnu/cgicc/            

二、配置、編譯、安裝

  • 下載下傳完成後解壓:

    shell tar xzf cgicc-X.X.X.tar.gz

  • 進入解壓的目錄:

    `

    shell cd cgicc-X.X.X/
  • 配置
./configure --prefix=/opt           

--prefix 參數指定安裝目錄,預設安裝目錄為/usr

  • 編譯
make           
  • 安裝
make install           

三、例程

3.1 如果在64bit平台編譯32位庫:

CXXFLAGS="-m32" CFLAGS="-m32" LDFLAGS="-m32"           

3.2 如果安裝目錄為/opt:

  • 編譯時指定包含路徑:

    shell -I/opt/include

  • 編譯時指定引用路徑:

    shell -L/opt/lib

3.3 HTML Demo

testcgi.html

<html>
<head><title>Test CGIcc form</title></head>
<body bgcolor="#cccccc" text="#000000">
<h2>Test CGIcc form</h2>
<p>
<form method="post" action="/cgi-bin/testcgi.cgi">
Value 1 :
<input type="text" name="value1">
<p>
Value 2 :
<select name="value2">
   <option value="option1">Option 1
   <option value="option2">Option 2
   <option value="option3">Option 3
</select>
<P>
Value 3 :
<input type="radio" name="value3" value="button1" checked="checked">Button1
<input type="radio" name="value3" value="button2">Button2

<input type="hidden" name="value4" value="data4">
<p>
<input type="submit" value="Submit">
</form>
</body>
</html>           

3.4 C++ CGI Demo

testcgi.cpp

#include <iostream>
#include <vector>
#include <string>

#include "cgicc/CgiDefs.h"
#include "cgicc/Cgicc.h"
#include "cgicc/HTTPHTMLHeader.h"
#include "cgicc/HTMLClasses.h"

#include <stdio.h>
#include <stdlib.h>

using namespace std;
using namespace cgicc; // Or reference as cgicc::Cgicc formData; below in object instantiation.

int main(int argc, char **argv)
{
    try {
       Cgicc formData;

       // Send HTTP header: Content-type: text/html
       cout << HTTPHTMLHeader() << endl;

       // Print: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
       cout << HTMLDoctype(HTMLDoctype::eStrict) << endl;

       // Print: <html lang="en" dir="LTR">
       cout << html().set("lang", "EN").set("dir", "LTR") << endl;

       // Set up the HTML document
       cout << html() << head() << title("Cgicc example") << head() << endl;
       cout << body().set("bgcolor","#cccccc").set("text","#000000").set("link","#0000ff").set("vlink","#000080") << endl;

       cout << h1("This is a demonstration of the GNU CgiCC library") << endl;

       form_iterator fvalue1 = formData.getElement("value1");
       if( !fvalue1->isEmpty() && fvalue1 != (*formData).end()) {
          cout << "Value1: " << **fvalue1 << endl;
       }
       else
          cout << "No text entered for value1" << endl;

       cout << p();

       form_iterator fvalue2 = formData.getElement("value2");
       if( !fvalue2->isEmpty() && fvalue2 != (*formData).end()) {
          // Note this is just a different way to access the string class.
          // See the YoLinux GNU string class tutorial.
          cout << "Value2: " << (**fvalue2).c_str() << endl;
       }

       cout << p();

       form_iterator fvalue3 = formData.getElement("value3");
       if( !fvalue3->isEmpty() && fvalue3 != (*formData).end()) {
          cout << "Value3: " << **fvalue3 << endl;
       }

       cout << p();

       form_iterator fvalue4 = formData.getElement("value4");
       if( !fvalue4->isEmpty() && fvalue4 != (*formData).end()) {
          cout << "Value4: " << **fvalue4 << endl;
       }

       // Close the HTML document
       cout << body() << html();
    }
    catch(exception& e) {
       // handle any errors here.
       cout << "ERROR!!" << endl;
    }
    return 0; // To avoid Apache errors.
}
                 

3.5 編譯c++ CGI demo

  • 使用靜态庫編譯

如果安裝在/opt/:

g++ -o testcgi.cgi -I/opt/include testcgi.cpp /opt/lib/libcgicc.a

如果安裝在/usr/:

g++ -o testcgi.cgi testcgi.cpp /usr/lib/libcgicc.a

  • 使用動态庫編譯

g++ -o testcgi.cgi -I/opt/include testcgi.cpp -L/opt/lib -lcgicc

g++ -o testcgi.cgi testcgi.cpp -lcgicc

3.6 運作

  • testcgi.html

    拷貝到

    /var/www/html/

  • testcgi.cgi

    /var/www/cgi-bin/

  • 本機測試 :

    http://127.0.0.1/testcgi.html