我正在编写一个 Java CGI 客户端来与 PHP-CGI 对话,但我遇到了一个障碍,我的 php $_POST
没有填充任何数据,而我肯定会发送一些数据。我不知道为什么会这样做,而且我在任何地方都找不到这个问题。
我直接使用 windows.php.net 中的干净 php 二进制文件没有任何编辑。
这是我现在用来测试的代码:
public static void main(String[] args)
{
HashMap<String, String> map = new HashMap<String, String>();
String body = "data=Foo+Bar";
String queryString = "yes=no&a=b";
String requestMethod = "POST";
String contentType = "application/x-www-form-urlencoded";
String contentLength = Integer.toString( body.length() );
String docRoot = "D:/http test";
String scriptName = "/index.php";
String scriptFileName = docRoot + scriptName;
String pathInfo = "";
String pathTranslated = docRoot + pathInfo;
String requestUri = scriptName + pathInfo + ("?" + queryString);
String documentUri = scriptName + pathInfo;
String serverProtocol = "HTTP/1.1";
String gatewayInterface = "CGI/1.1";
map.put( "QUERY_STRING" , queryString);
map.put( "REQUEST_METHOD" , requestMethod);
map.put( "CONTENT_TYPE" , contentType);
map.put( "CONTENT_LENGTH" , contentLength);
map.put( "SCRIPT_FILENAME" , scriptFileName);
map.put( "SCRIPT_NAME" , scriptName);
map.put( "PATH_INFO" , pathInfo);
map.put( "PATH_TRANSLATED" , pathTranslated);
map.put( "REQUEST_URI" , requestUri);
map.put( "DOCUMENT_URI" , documentUri);
map.put( "DOCUMENT_ROOT" , docRoot);
map.put( "SERVER_NAME" , "localhost" );
map.put( "SERVER_PROTOCOL" , serverProtocol);
map.put( "GATEWAY_INTERFACE" , gatewayInterface);
Client c = new Client( "127.0.0.1" , 8090 );
System.out.println("\n" + c.doRequest( map , body ));
}
客户端.java:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Map.Entry;
public class Client
{
private Socket socket;
public Client( String host, int port ) throws UnknownHostException, IOException
{
socket = new Socket( host, port );
}
public String doRequest( Map<String, String> params, String content ) throws IOException
{
ByteArrayOutputStream paramBytes = new ByteArrayOutputStream();
for ( Entry<String, String> param: params.entrySet() )
paramBytes.write( nvpair( param.getKey() , param.getValue() ) );
Packet beginRequest = new Packet( FCGI.BEGIN_REQUEST, FCGI.NULL_REQUEST_ID, new byte[] { 0, FCGI.RESPONDER, FCGI.KEEP_CONN, 0, 0, 0, 0, 0 } );
Packet requestParams = new Packet( FCGI.PARAMS, FCGI.NULL_REQUEST_ID, paramBytes.toByteArray() );
Packet requestContent = new Packet( FCGI.STDIN, FCGI.NULL_REQUEST_ID, content.getBytes( "UTF-8" ) );
OutputStream stream = socket.getOutputStream();
stream.write( beginRequest.getBytes() );
stream.write( requestParams.getBytes() );
stream.write( requestContent.getBytes() );
return readResponse();
}
private String readResponse() throws IOException
{
InputStream stream = socket.getInputStream();
// TODO buffering
String out = null;
for ( Packet p = new Packet( stream ); p.getType() != FCGI.END_REQUEST; p = new Packet( stream ) )
{
System.out.print( p.getType() + ", " );
if ( p.getType() == FCGI.STDOUT )
out = new String( p.getContent() );
}
return out;
}
public byte[] nvpair( String name, String value )
{
try
{
int nl = name.length();
int vl = value.length();
ByteArrayOutputStream bytes = new ByteArrayOutputStream( nl + vl + 10 );
if ( nl < 128 )
bytes.write( b( nl ) );
else
bytes.write( new byte[] { b( nl >> 24 ), b( nl >> 16 ), b( nl >> 8 ), b( nl ) } );
if ( vl < 128 )
bytes.write( b( vl ) );
else
bytes.write( new byte[] { b( vl >> 24 ), b( vl >> 16 ), b( vl >> 8 ), b( vl ) } );
bytes.write( name.getBytes( "UTF-8" ) );
bytes.write( value.getBytes( "UTF-8" ) );
return bytes.toByteArray();
}
catch( IOException e )
{
e.printStackTrace();
}
return null;
}
public byte b( int i )
{
return (byte) i;
}
}
index.php:
<pre><?php
echo "Hello World\n";
echo "REQUEST = "; print_r($_REQUEST);
echo "GET = "; print_r($_GET);
echo "POST = "; print_r($_POST);
echo php_ini_loaded_file(), "\n";
echo file_get_contents("php://input"), "\n";
echo php_sapi_name();
?></pre>
他的结果是我得到的:
6,
X-Powered-By: PHP/5.6.3
Content-type: text/html; charset=UTF-8
<pre>Hello World
REQUEST = Array
(
[yes] => no
[a] => b
)
GET = Array
(
[yes] => no
[a] => b
)
POST = Array
(
)
D:\Programs\PHP 5.6.3 x64 TS\php.ini
cgi-fcgi</pre>
我想要的是 $_POST
包含 Array( [data] => Foo Bar )
(这就是我要发送的内容)。
有谁知道如何修复它,以便 $_POST
也填充数据?
最佳答案
好吧,如果你看看 FastCGI-spec chapter 3.3 under the subtitle "Types of Record Types"它说:
A stream record is part of a stream, i.e. a series of zero or more non-empty records (length != 0) of the stream type, followed by an empty record (length == 0) of the stream type.
这意味着正确的流由至少一个非空数据包组成,并由相同流类型的空数据包终止。
您还可以查看Appendix B (Typical Protocol Message Flow)了解它应该是什么样子。
这与您的问题有什么关系?
PHP 期望一个或多个非空“写入”到 FCGI_PARAMS
流,然后标记 params-stream 的结尾,一个空的 FCGI_PARAMS
数据包。
如果您查看您的 doRequest
方法,您将编写一个非空的 FCGI_PARAMS
数据包,然后紧接着编写您的 FCGI_STDIN
数据包。
PHP 的作用是读取您的第一个 FCGI_PARAMS
记录,然后如果您的 FCGI_STDIN
数据包到达,它仅读取 header 并停止。因此,下一个 fcgi_read
读取无效数据并默默失败 - 但 PHP 继续使用空 STDIN 流处理请求。或者,如果您的请求正文太小(例如 body = "a"),PHP 将永远阻塞。
如果您修改 doRequest
方法并以空写入终止流(我将其称为 EOF 数据包),它应该可以工作:
public String doRequest( Map<String, String> params, String content ) throws IOException
{
ByteArrayOutputStream paramBytes = new ByteArrayOutputStream();
for ( Entry<String, String> param: params.entrySet() )
paramBytes.write( nvpair( param.getKey() , param.getValue() ) );
Packet beginRequest = new Packet( FCGI.BEGIN_REQUEST, FCGI.NULL_REQUEST_ID, new byte[] { 0, FCGI.RESPONDER, FCGI.KEEP_CONN, 0, 0, 0, 0, 0 } );
Packet requestParams = new Packet( FCGI.PARAMS, FCGI.NULL_REQUEST_ID, paramBytes.toByteArray() );
Packet requestParamsEOF = new Packet( FCGI.PARAMS, FCGI.NULL_REQUEST_ID, new byte[] {} );
Packet requestContent = new Packet( FCGI.STDIN, FCGI.NULL_REQUEST_ID, content.getBytes( "UTF-8" ) );
Packet requestContentEOF = new Packet( FCGI.STDIN, FCGI.NULL_REQUEST_ID, new byte[] {} );
OutputStream stream = socket.getOutputStream();
stream.write( beginRequest.getBytes() );
stream.write( requestParams.getBytes() );
stream.write( requestParamsEOF.getBytes() );
stream.write( requestContent.getBytes() );
stream.write( requestContentEOF.getBytes() );
return readResponse();
}
这样做的原因是您可以将大流分成多个数据包(因为每个数据包只能容纳 64 KiB 的有效负载)。理论上,您必须检查请求正文的长度并将其分成 64 KiB(2^16 字节)的 block 。因此,即使这个版本的 doRequest
也可以改进。
关于java - PHP-CGI 帖子空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27457543/