Web (vii)---FASTCGI Advanced (Request and response) Official example

Source: Internet
Author: User

In the fast-cgi source code of the examples folder there are many examples, the following gives the Echo example, the compilation run method above several sections.

fast-cgi API for Google.

Http://fossies.org/dox/fcgi-2.4.0/fcgiapp_8h.html#a32f6950798054a70404ce24c22ea28b9

Echo-cpp.cpp

#include <stdlib.h>#ifdef _win32#include<process.h>#else#include<unistd.h>extern Char**environ;#endif#include"fcgio.h"#include"fcgi_config.h"  //Have_iostream_withassign_streambufusing namespacestd;//Maximum number of bytes allowed to is read from stdinStatic ConstUnsignedLongStdin_max =1000000;Static voidPenv (Const Char*Const*envp) {cout<<"<pre>\n";  for(; *envp; + +)envp) {cout<< *ENVP <<"\ n"; } cout<<"</pre>\n";}Static LongGstdin (Fcgx_request * Request,Char**content) {    Char* Clenstr = Fcgx_getparam ("content_length", request->envp); unsignedLongClen =Stdin_max; if(clenstr) {Clen= Strtol (Clenstr, &clenstr,Ten); if(*clenstr) {Cerr<<"can ' t parse \ "Content_length="<< Fcgx_getparam ("content_length", request->envp)<<"\ "\ n"; Clen=Stdin_max; }        //*always* put a cap on the amount of data so would be read        if(Clen > Stdin_max) clen =Stdin_max; *content =New Char[Clen]; Cin.read (*content, Clen); Clen=Cin.gcount (); }    Else    {        //*never* Read stdin when content_length is missing or unparsable*content =0; Clen=0; }    //Chew up to remaining stdin-this shouldn ' t be necessary//But is because mod_fastcgi doesn ' t handle it correctly. //ignore () doesn ' t set the EOF bit in some versions of Glibc++//So use Gcount () instead of EOF () ...     DoCin.ignore (1024x768); while(Cin.gcount () = =1024x768); returnClen;}intMain (void){    intCount =0; LongPID =Getpid (); Streambuf* Cin_streambuf =Cin.rdbuf (); Streambuf* Cout_streambuf =Cout.rdbuf (); Streambuf* Cerr_streambuf =Cerr.rdbuf ();    Fcgx_request Request;    Fcgx_init (); Fcgx_initrequest (&request,0,0);  while(Fcgx_accept_r (&request) = =0)    {        //Note that the default bufsize (0) would cause the use of iostream//methods that require positioning (such as peek (), Seek (),//Unget () and putback ()) to-fail (in favour of more efficient IO).Fcgi_streambuf Cin_fcgi_streambuf (Request.inch); Fcgi_streambuf cout_fcgi_streambuf (Request. out); Fcgi_streambuf cerr_fcgi_streambuf (request.err);#ifHave_iostream_withassign_streambufCin= &Cin_fcgi_streambuf; cout= &Cout_fcgi_streambuf; Cerr= &Cerr_fcgi_streambuf;#elseCin.rdbuf (&cin_fcgi_streambuf); Cout.rdbuf (&cout_fcgi_streambuf); Cerr.rdbuf (&cerr_fcgi_streambuf);#endif        //Although FastCGI supports writing before reading,//many HTTP clients (browsers) don ' t support it//The connection deadlocks until a timeout expires!).        Char*content; unsignedLongClen = Gstdin (&request, &content); cout<<"content-type:text/html\r\n"                "\ r \ n"                "<title>echo-cpp</title>\n"                ""                ""<< PID <<""                ""<< ++count <<""; cout<<"";        Penv (REQUEST.ENVP); cout<<"";        Penv (environ); cout<<""<<Clen; if(Clen = = Stdin_max) cout <<"(Stdin_max)"; cout<<"bytes"; if(Clen) cout.write (content, Clen); if(content) Delete []content; //If the output streambufs had Non-zero bufsizes and//were constructed outside of the accept loop (i.e. //their destructor won ' t be called here), they would//There is a flushed here.    }#ifHave_iostream_withassign_streambufCin=Cin_streambuf; cout=Cout_streambuf; Cerr=Cerr_streambuf;#elsecin.rdbuf (CIN_STREAMBUF);    Cout.rdbuf (COUT_STREAMBUF); Cerr.rdbuf (CERR_STREAMBUF);#endif    return 0;}

Web (vii)---FASTCGI Advanced (Request and response) Official example

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.