Next to the section, the last section only responds---cout, this section gets the query_string of the address bar, and then responds.
I. Req_resp.cpp.
#include <stdlib.h>#include<string.h>#include<unistd.h>#include"fcgio.h"#include"fcgi_config.h"using namespacestd;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{fcgi_streambuf cin_fcgi_streambuf (request).inch); Fcgi_streambuf cout_fcgi_streambuf (Request. out); Fcgi_streambuf cerr_fcgi_streambuf (Request.err); Cin.rdbuf (&cin_fcgi_streambuf); Cout.rdbuf (&cout_fcgi_streambuf); Cerr.rdbuf (&cerr_fcgi_streambuf); cout<<"content-type:text/html\r\n" "\ r \ n" ""<< PID <<"" ""<< ++count <<""; cout<<""; for(inti =0; Request.envp[i]; ++i) {Char* Findquerystr =NULL; Findquerystr= Strstr (Request.envp[i],"query_string"); if(findquerystr) {cout<< Request.envp[i] <<'\ n'; }}} cin.rdbuf (CIN_STREAMBUF); Cout.rdbuf (COUT_STREAMBUF); Cerr.rdbuf (CERR_STREAMBUF); return 0;}
Two. Compile and run.
#g + o req_resp req_resp.cpp-i/usr/local/include-l/usr/local/lib-lfcgi-lstdc++-lfcgi++
#./Req_resp
If there is a compile or runtime error (typically a library problem), refer to the previous section of the workaround.
Three. Copy the CGI to Ngix (the target path is not created manually)
CP ***/req_resp/usr/local/nginx/cgibin
Four. Start spawn-fcgi.
/usr/local/nginx/sbin/spawn-fcgi-a 127.0.0.1-p 8088-f/usr/local/nginx/cgibin/req_resp
Five. Access.
Note: Python index.py is already running in the case.
Browser Address bar: localhost/req_resp.cgi?his_is_from_brower_input&hello=1&world=2
Note that the query_string displayed is the content after the Address bar question mark.
The browser appears as:
pid:4072
Request Number:2
Request Environment
query_string=this_is_from_brower_input&hello=1&world=2
Web (vi)---fastcgi advanced (Request and response)