当前位置: 首页 > 工具软件 > Nanoweb > 使用案例 >

为了给自己开发一个支持 fastcgi 的 http server 做准备。剥离了 nanoweb 的 fastcgi 接口部分代码。测试了下。 成功了...

卫甫
2023-12-01

<?php define("FCGI_VERSION_1", 1); define("FCGI_BEGIN_REQUEST", 1); define("FCGI_ABORT_REQUEST", 2); define("FCGI_END_REQUEST", 3); define("FCGI_PARAMS", 4); define("FCGI_STDIN", 5); define("FCGI_STDOUT", 6); define("FCGI_STDERR", 7); define("FCGI_DATA", 8); define("FCGI_GET_VALUES", 9); define("FCGI_GET_VALUES_RESULT", 10); define("HTTP_VERSION", "HTTP/1.1"); if (!$sck=fsockopen('127.0.0.1', 9000, $errno, $errstr, 5)) { $rq_err=500; $tmperr="mod_fcgi: unable to contact application server ($errno : $errstr)."; $add_errmsg.=($tmperr."<br><br>"); techo("WARN: ".$tmperr, NW_EL_WARNING); return (false); }else{ echo 'ok'; } $begin_rq_packet=chr(0).chr(1).chr(0).chr(0).chr(0).chr(0).chr(0).chr(0); fwrite($sck, build_fcgi_packet(FCGI_BEGIN_REQUEST, $begin_rq_packet)); $fcgi_params_packet = ''; $fcgi_params_packet.=build_fcgi_nvpair("GATEWAY_INTERFACE", "FastCGI/1.0"); $nsv["SERVER_SOFTWARE"]='xxnioo'; $nsv["SERVER_NAME"]='xx'; $nsv["SERVER_PROTOCOL"]=HTTP_VERSION; $nsv["SERVER_PORT"]= '9000'; $nsv["SERVER_ADDR"]='127.0.0.1'; $nsv["SERVER_API"]='2.1'; $nsv["SERVER_ADMIN"]='my'; $nsv["REQUEST_METHOD"]='GET'; $nsv["PATH_TRANSLATED"]=''; $nsv["SCRIPT_NAME"]='info.php'; $nsv["PATH_TRANSLATED"]='info.php'; $nsv["QUERY_STRING"]=''; $nsv["REMOTE_HOST"]=''; $nsv["REMOTE_ADDR"]=''; $nsv["REMOTE_PORT"]=''; $nsv["AUTH_TYPE"]=''; $nsv["DOCUMENT_ROOT"]='/tmp/'; $nsv["SCRIPT_FILENAME"]='/tmp/info.php'; foreach($nsv as $key=>$var) $fcgi_params_packet.=build_fcgi_nvpair($key, $var); // Send params fwrite($sck, build_fcgi_packet(FCGI_PARAMS, $fcgi_params_packet)); fwrite($sck, build_fcgi_packet(FCGI_PARAMS, "")); // Build and send stdin flow $stdin_content = ''; if ($stdin_content) fwrite($sck, build_fcgi_packet(FCGI_STDIN, $stdin_content)); fwrite($sck,build_fcgi_packet(FCGI_STDIN, "")); $content=""; while (($p1=strpos($content, "\r\n\r\n"))===false) { $tmpp=decode_fcgi_packet($packet=fread($sck, 8)); $tl=$tmpp["length"]%8; $tadd=($tl?(8-$tl):0); $resp=decode_fcgi_packet($packet.fread($sck, $tmpp["length"]+$tadd)); if ($valid_pck=($resp["type"]==FCGI_STDOUT || $resp["type"]==FCGI_STDERR)) $content.=$resp["content"]; if ($resp["type"]==FCGI_STDERR) echo( NW_EL_WARNING); } echo($content); function build_fcgi_packet($type, $content) { $clen=strlen($content); $packet=chr(FCGI_VERSION_1); $packet.=chr($type); $packet.=chr(0).chr(1); // Request id = 1 $packet.=chr((int)($clen/256)).chr($clen%256); // Content length $packet.=chr(0).chr(0); // No padding and reserved $packet.=$content; return($packet); } function build_fcgi_nvpair($name, $value) { $nlen = strlen($name); $vlen = strlen($value); if ($nlen < 128) { $nvpair = chr($nlen); } else { $nvpair = chr(($nlen >> 24) | 0x80) . chr(($nlen >> 16) & 0xFF) . chr(($nlen >> 8) & 0xFF) . chr($nlen & 0xFF); } if ($vlen < 128) { $nvpair .= chr($vlen); } else { $nvpair .= chr(($vlen >> 24) | 0x80) . chr(($vlen >> 16) & 0xFF) . chr(($vlen >> 8) & 0xFF) . chr($vlen & 0xFF); } return $nvpair . $name . $value; } function decode_fcgi_packet($data) { $ret["version"]=ord($data{0}); $ret["type"]=ord($data{1}); $ret["length"]=(ord($data{4}) << 8)+ord($data{5}); $ret["content"]=substr($data, 8, $ret["length"]); return($ret); } ?>

关键在于

$nsv["SCRIPT_FILENAME"]='/tmp/info.php';

这个脚本环境变量的 发送。。 否则 php-cgi 会现实

Content-type:text/htmlNoinputfilespecified

 类似资料: