call php-cgi from c++ with REQUEST_METHOD="POST" -
i want convert shell script c++. please me.
this shell script file. worked well, when move c++, not work correctly
test.sh
#!/bin/sh request_data="var_1=val_1&var_2=val_2" export gateway_interface="cgi/1.1" export server_protocol="http/1.1" export query_string="test=querystring" export redirect_status="200" export script_filename="/test.php" export request_method="post" export content_length=${#request_data} export content_type="application/x-www-form-urlencoded;charset=utf-8" echo $request_data | /usr/bin/php-cgi
and test.php
is
<?php print_r($_post); ?>
when run sh test.sh
, got response:
x-powered-by: php/5.6.13 set-cookie: phpsessid=f4ntbasno365p08tf94drl7026; path=/ expires: thu, 19 nov 1981 08:52:00 gmt cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 pragma: no-cache content-type: text/html; charset=utf-8 array ( [var_1] => val_1 [var_2] => val_2 )
when trying convert c++
char **syscline = null; char **sysenv = null; std::string sdata = "var_1=val_1&var_2=val_2"; std::vector<std::string> aargs; aargs.push_back("/usr/bin/php-cgi"); aargs.push_back("/test.php"); syscline = new char*[aargs.size() + 1]; (i=0; i<aargs.size(); i++) { syscline[i] = new char[aargs[i].size()+1]; ::strncpy(syscline[i], aargs[i].c_str(), aargs[i].size()+1); } syscline[aargs.size()] = null; // construct environment std::vector<std::string> aenv; aenv.push_back("gateway_interface=cgi/1.1"); aenv.push_back("server_protocol=http/1.1"); aenv.push_back("query_string=test=querystring"); aenv.push_back("redirect_status=200"); aenv.push_back("request_method=post"); aenv.push_back("content_type=application/x-www-form-urlencoded;charset=utf-8"); aenv.push_back("script_filename=/test.php"); aenv.push_back("content_length="+ to_string(sdata.length()) ); sysenv = new char*[aenv.size() + 1]; (i=0; i<aenv.size(); i++) { sysenv[i] = new char[aenv[i].size()+1]; ::strncpy(sysenv[i], aenv[i].c_str(), aenv[i].size() + 1); } sysenv[aenv.size()] = null; execve(syscline[0], syscline, sysenv);
when run, got response:
x-powered-by: php/5.6.13 set-cookie: phpsessid=2vdp947ghlg0h8st66c9ll3vt6; path=/ expires: thu, 19 nov 1981 08:52:00 gmt cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 pragma: no-cache content-type: text/html; charset=utf-8 array ( )
it should same response sh test.sh
you need create pipe in order pass post data script:
char *echocmd[] = {"echo", "var_1=val_1&var_2=val_2", null}; int pp[2]; int pid, res; pipe(pp); if ((pid = fork()) != 0) { dup2(pp[1], 1); close(pp[0]); res = execvp(echocmd[0], echocmd); } else { dup2(pp[0], 0); close(pp[1]); res = execve(syscline[0], syscline, sysenv); } close(pp[1]); return res;
Comments
Post a Comment