C ++ CGI library GNU cgicc and Linux

Source: Internet
Author: User
Tags dbx

[Size = medium] download, installation, configuration:

Download: ftp://ftp.gnu.org/gnu/cgicc/

Unpack: Tar xzf cgicc-X.X.X.tar.gz

Build libraries:

* D cgicc-X.X.X/

*./Configure -- prefix =/usr (default or/OPT. Make sure you have write priviges to the directory .)

If compiling to create a 32 bit library on a 64 bit athelon:

Cxxflags = "-M32" cflags = "-M32" ldflags = "-M32"

If installed in/opt then you will need to include:

O include path defined in the compile statement:-I/opt/include

O link command reference:-L/opt/lib

Or use ld_run_path or/etc/lD. So. conf

I prefer to install into/usr to eliminate the configuration headaches.

* Make install

This will create and install include files, documentation and libraries:

* Libraries:

/Usr/lib/libcgicc.

Libcgicc. La

Libcgicc. So (softlink to libcgicc. so.1.3.0)

Libcgicc. so.1 (softlink to libcgicc. so.1.3.0)

Libcgicc. so.1.3.0

* Include files:/usr/include/cgicc /...

* Documentation:/usr/doc/cgicc-X.X.X/index.html

Also available at http://www.gnu.org/software/cgicc/doc/

Example:

The web page:

Test cgicc form

Value 1:

Value 2:

Value 3: button1 button2

HTML form Source:

<HTML>

<Head> <title> test cgicc form </title>
<Body bgcolor = "# cccccc" text = "#000000">

<H2> test cgicc form </H2>

<P>

<Form method = "Post" Action = "/cgi-bin/testcgi">

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>

C ++ CGI Source:

# 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

// Print: <! Doctype HTML public "-// W3C // dtd html 4.0 // en" "http://www.w3.org/TR/REC-html40/strict.dtd">

Cout

// Print: <HTML lang = "en" dir = "LTR">

Cout <HTML (). Set ("Lang", "en"). Set ("dir", "LTR") <Endl;

// Set up the HTML document

Cout <HTML ()
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.

}

Compile:

* Compile and static link: (size: 1063688)

O if installed in/opt/: G ++-O testcgi-I/opt/include testcgi. CPP/opt/lib/libcgicc.

O if installed in/usr/: G ++-O testcgi. CPP/usr/lib/libcgicc.

* Dynamic Link (at run time): (size: 48465)

O if installed in/opt/: G ++-O testcgi-I/opt/include testcgi. cpp-L/opt/lib-lcgicc

O if installed in/usr/: G ++-O testcgi. cpp-lcgicc

Run:

The following paths are for a red hat 7.x installation.

1. Place web page in:/var/www/html/testcgi.html

2. Place CGI in:/var/www/cgi-bin/testcgi

3. Start Apache: Service httpd start

4. Test: http: // localhost/testcgi.html

Debugging:

Debugging a "post" method CGI:

* Print environment for transaction:

Code snipet to include in cgicc CGI program:

// Function declaration

Void dumpenvironment (const cgienvironment & env );

...

Const cgienvironment & Env = CGI. getenvironment ();



Cout <env. getrequestmethod () <Endl;

... Env. getpathinfo ()...

... Env. getpathtranslated ()

Env. getreferrer ()

Env. getcontentlength ()

Env. getpostdata ()

...

Also: ENV. getrequestmethod (), ENV. getpathinfo (),
Env. getpathtranslated (), ENV. getreferrer (), ENV. getcontentlength (),
Env. getpostdata (), ENV. getremotehost (), ENV. getremoteaddr (),
Env. getauthtype (), ENV. getremoteuser (), ENV. getremoteident (),
Env. getcontenttype (), ENV. getaccept (), ENV. getuseragent (),
Env. getserversoftware (), ENV. getservername (), ENV. getgatewayinterface (),
Env. getserverprotocol (), ENV. getserverport (), ENV. usinghttps (),
Env. getredirectrequest (), ENV. getredirecturl (), ENV. getredirectstatus (),
Env. getcookies (), ENV. getquerystring (), ENV. getscriptname ().

* Set environment variables:. debug-env

File: Debug-env

Export http_user_agent = "Mozilla/5.0 (X11; U; Linux x86_64; en-US; RV: 1.7.3) Gecko/20041020"

Export server_port = 80

Export http_host = localhost

Export document_root = "/var/www/html"

Export http_accept_charset = "iso-8859-1, *, UTF-8"

Export script_filename = "/var/www/cgi-bin/env. Sh"

Export request_uri = "/cgi-bin/env. Sh"

Export script_name = "/cgi-bin/program. cgi"

Export http_referrer = "http: // localhost/web-page.html"

Export http_connection = keep-alive ""

Export remote_port = 32984

Export Path = "/sbin:/usr/sbin:/bin:/usr/x11r6/bin"

Export Pwd = "/var/www/cgi-bin"

Export server_admin = "root @ localhost"

Export http_accept_language = en

Export
Http_accept = 'text/XML, application/XML, application/XHTML + XML, text/html; q = 0.9, text/plain; q = 0.8, image/PNG, */*; q = 0.5'

Export remote_addr = '2017. 0.0.1'

Export shlvl = 1

Export SERVER_NAME = localhost

Export server_software = 'apache/2.0.52 (Fedora )'

Export QUERY_STRING =

Export server_addr = '2017. 0.0.1'

Export gateway_interface = 'cgi/123'

Export server_protocol = 'HTTP/1.1'

Export http_accept_encoding = Gzip

Export request_method = post

Export content_length = 47

Export content_type = 'application/X-WWW-form-urlencoded'

Export post_data = 'formval1 = value1 & formval2 = value2 & formval3 = value3'

* Create File for (standard input) stdin: Echo $ post_data> debug-stdin

* Run in DBX:

O dbx program. cgi

O break #### (set breakpoints)

O run <debug-stdin

Or

If not debugging, just run: Echo $ post_data |./program. cgi

Links:

* GNU cgicc Home Page

O cgicc documentation

* Yolinux C ++ tutorials and Info

* More source code examples:

O generic test: Test. cpp

O upload a file: Upload. cpp

O drop a cookie: Cookie. cpp

Http://www.lslnet.com/linux/f/docs1/i34/big5261271.htm

Http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CGI.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.