A handy and convenient FastCgi C + + library-fastcgi++

Source: Internet
Author: User
Tags apache error log

I don't know if you've ever been worried about using the FASTCGI library to develop fastcgi programs with C + + and to conflict with C + + STL code, or if you're still used to cout rather than PRINGF, then this article will learn about a less used fastcgi C + + library--fastcgi++/Fastcgipp.

Development environment:

Os:centos 7

  Compiler: GCC 4.8.3

Get ready:

1. My yum source did not find fastcgi++, and, as I used to, I still prefer to compile the source code:)

fastcgi++ Official Introduction: http://www.nongnu.org/fastcgipp/

Download Source: The latest version is 2.1 version http://download.savannah.nongnu.org/releases/fastcgipp/fastcgi++-2.1.tar.bz2

2. The boost library will be used in most open source libraries abroad, so it is unavoidable to install Libboost-devel. Refer to the installation of the Boost library in MongoDB. or direct yum install boost-devel, just rely on this non-standard library, so there is no need to install other.

3. Since it is fastcgi application, choose a webserver to verify his implementation. Select Nginx, my nginx version is 1.6.0

Compile:

1. There is really nothing to say in this paragraph.

TAR-XVJF fastcgi++-2.1.tar.bz2

./configure--disable-shared--enable-static

Make && make install

If you do not specify the path of the prefix, the GCC compiler will go to the default path to find the header file and the library file. Here, I chose to compile the form of a static library, no database-related operations, no need to compile.

Basic use:

Start using:

   

/**
* fastcgi++ Test by KK
* Main.cpp
*/

#include <boost/date_time/posix_time/posix_time.hpp>#include<fstream>#include<fastcgi++/request.hpp>#include<fastcgi++/manager.hpp>voidError_log (Const Char*msg) { using namespacestd; using namespaceboost; StaticOfstream error; if(!Error.is_open ()) {Error.open ("/tmp/errlog", Ios_base:: out|Ios_base::app); Error.imbue (Locale (Error.getloc (),NewPosix_time::time_facet ())); } Error<<'['<< posix_time::second_clock::local_time () <<"] "<< msg <<Endl;}classHelloWorld: PublicFastcgipp::request<wchar_t>{ BOOLresponse () {wchar_t russian[]={0x041f,0x0440,0x0438,0x0432,0x0435,0x0442,0x0020,0x043c,0x0438,0x0440,0x0000 }; wchar_t chinese[]={0x4e16,0x754c,0x60a8,0x597d,0x0000 }; wchar_t greek[]={0x0393,0x03b5,0x03b9,0x03b1,0x0020,0X03C3,0x03b1,0x03c2,0x0020,0x03ba,0x03cc,0X03C3,0X03BC,0X03BF,0x0000 }; wchar_t japanese[]={0x4eca,0x65e5,0x306f,0x4e16,0x754c,0x0000 }; wchar_t runic[]={0x16ba,0x16d6,0x16da,0X16DF,0x0020,0x16b9,0X16DF,0x16c9,0x16da,0x16de,0x0000 }; out<<"content-type:text/html; charset=utf-8\r\n\r\n"; out<<""; out<<"<title>fastcgi++: Hello World in utf-8</title>"; out<<"English:hello world<br/>"; out<<"Russian:"<< Russian <<"<br/>"; out<<"Greek:"<< Greek <<"<br/>"; out<<"Chinese:"<< Chinese <<"<br/>"; out<<"Japanese:"<< Japanese <<"<br/>"; out<<"runic 中文版?:"<< Runic <<"<br/>"; out<<"</body>"; Err<<"Hello Apache error log"; return true; }};intMain () {Try{Fastcgipp::manager<HelloWorld>fcgi; Fcgi.handler (); } Catch(std::exception&e) {error_log (E.what ()); }}

Compile it with gcc: g++-o Main-lboost_system-mt-lboost_thread-mt-lfastcgipp main.cpp

Verify:

Because it is a fastcgi program, so you need a LIGHTHTTPD project in a fastcgi initiator spawn-fastcgi (OK, the first launcher so called, I would like in the following article source specific exploration of spawn-fastcgi implementation process)

There are many tutorials on spawn-fastcgi online, and it's not difficult to compile them. Paste the source code download path is good: http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz

Change nginx config file nginx.conf add fastcgi configuration to server:

  

Location ~\.fcgi$ {                        Fastcgi_pass 127.0.0.1:9000;                        Fastcgi_index index.fcgi;                        Fastcgi_param script_filename/$fastcgi _script_name;                        Include Fastcgi_params;                }

Reboot the Nginx reload configuration file.

To run the SPAWN-FASTCGI program:

SPAWN-FASTCGI-A 127.0.0.1-c 20-p 9000 Main

Successful tip: Spawn-fcgi:child spawned successfully:pid:13404

Now open the browser and enter localhost/test.fcgi to see the value of the C + + code output.

Postscript:

Previously used fastcgi library, but this is a C language library, using C + + to develop use is extremely inconvenient, and there will be some unknown errors. Fastcgi++ uses the OOP design to develop FASTCGI programs perfectly using C + +. The following article will describe in detail how fastcgi++ is used.  :) Work happily

A handy and convenient FastCgi C + + library-fastcgi++

Related Article

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.