使用分块编码对PHP7的POST请求无法正确返回结果

mar*_*ark 25 php chunked-encoding php-7

我正在从客户端发送POST请求(使用curl和自定义nodejs脚本测试)并且没有正确地返回响应.整个过程适用于PHP 5.6.

环境

整个事情尽可能减少:

我正在使用的最小nginx站点配置:

server {
  listen 80;
  server_name localhost;
  location / {
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_pass  unix:/var/run/php/php7.0-fpm-api.sock;
    fastcgi_param SCRIPT_FILENAME /vagrant/index.php;
  }
}
Run Code Online (Sandbox Code Playgroud)

示例PHP脚本来自/vagrant/index.php:

<?php
echo str_repeat('.', 512);
flush(); // not necessary, only due testing
Run Code Online (Sandbox Code Playgroud)

我正在使用的卷曲电话: curl -XPOST http://localhost/ -H "Transfer-Encoding: chunked" -d ''

我正在使用的NodeJS脚本:

'use strict';

var http = require('http');
var url = require('url');

var uri = url.parse(process.env.URL);
var options = {
  method: 'POST', protocol: uri.protocol, hostname: uri.hostname,
  port: uri.port, path: uri.path,
};
var data = '';

var httpRequest = http.request(options, function(res) {
    res.on('data', function(chunk) {
      console.log('received data', chunk.length);
      data += chunk;
    });
    res.on('end', function() { console.log('final size', data.length); });
  })
  .on('error', function(err) { console.log(err); });

httpRequest.write('');
httpRequest.end();
Run Code Online (Sandbox Code Playgroud)

将我的测试请求发送到PHP 5.6

$ curl http://localhost/
..........[cut off]

$ curl -XPOST http://localhost/ -H "Transfer-Encoding: chunked" -d ''
..........[cut off]

$ URL=http://localhost/ node php7test.js
received data 512
final size 512
Run Code Online (Sandbox Code Playgroud)

将我的测试请求发送到PHP 7.0

$ curl http://localhost/
..........[cut off]

$ URL=http://localhost/ node php7test.js
final size 0

$ curl -XPOST http://localhost/ -H "Transfer-Encoding: chunked" -d ''
curl: (18) transfer closed with outstanding read data remaining
Run Code Online (Sandbox Code Playgroud)

我为什么要使用分块编码?

没有商业理由这样做,但是我使用的是一个非常相似的NodeJS代码,它默认为分块编码,在切换到PHP7时突然停止工作.

我已经从nodejs端发现了以下内容:显式设置Content-Length标头会删除NodeJS Transfer-Encoding: chunked发送的隐式标头,因此适用于两个PHP版本.

但是,我想了解为什么PHP7在这里表现不同,以及我是在错误还是在这里发生了什么.

更新1:

  • 我比较了sapi/fpm/5.6和7.0之间的来源,除了由于PHP内部变化引起的变化之外,我几乎没有差异
  • php -S所有测试都不会影响内置服务器()

更新2:

我将PHP源分成两部分,并能够确定行为何时发生变化:

在其间,输出git bisect,提交我无法编译:

$ git bisect skip
There are only 'skip'ped commits left to test.
The first bad commit could be any of:
ba5ecf355fe792a5a2a8e6582d5e081d02b16fbf
e383cb4493031a7cd952cfcaed3297e583149c07
fef18f4bea1980a59a9283c2197bd090aaf500cb
18cf4e0a8a574034f60f4d123407c173e57e54ec
We cannot bisect more!
Run Code Online (Sandbox Code Playgroud)

感觉这可能是一个错误,我把它写到内部,也许他们有一些见解:https://marc.info/? l = php-internals &m = 145090900217798&w = 2

mar*_*ark 0

这是/曾经是 PHP7 中的一个错误,最近已通过https://github.com/php/php-src/pull/1745修复。它尚未包含在任何正式版本中,但最终会在某个时候出现。

在上述 PR 之前,还报告了一个错误,描述了类似的问题:https://bugs.php.net/bug.php?id =71466