php / php/php-src

Cannot stream request using CURLOPT_READFUNCTION without chunked encoding

未关闭
#8,165 4 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Extension: curl Feature Status: Verified
主要语言
C
星标
40.4k
派生
8.1k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

Description

Consider this curl code in C:

int counter = 0;
size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
	if (counter++ == 1) {
		return 0;
	}

	ptr[0] = 'W';
	ptr[1] = 'H';
	ptr[2] = 'A';
	ptr[3] = 'T';
	return 4;
}

int main() {
	CURL* handle = curl_easy_init(); 
	CURLcode res;
	curl_easy_setopt(handle, CURLOPT_URL, "http://localhost:9090");
	curl_easy_setopt(handle, CURLOPT_READFUNCTION, read_callback);
	curl_easy_setopt(handle, CURLOPT_POST, 1);
	curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, 4);
	res = curl_easy_perform(handle);
	curl_easy_cleanup(handle);

	return 0;
}

That results in the following HTTP request

POST / HTTP/1.1
Host: localhost:9090
Accept: */*
Content-Length: 4
Content-Type: application/x-www-form-urlencoded
WHAT

When removing the CURLOPT_POSTFIELDSIZE options, the following request is issued:

POST /iserv/helloworld HTTP/1.1
Host: localhost:982
Accept: */*
Transfer-Encoding: chunked
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

WHAT

That is analogous to its PHP implementation:

$counter = 0;
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://localhost:9090');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_READFUNCTION, static function ($ch, $fd, $length) use (&$counter) {
        if ($counter++ > 0) {
            return "";
        }

        return "WHAT";
});

$output = curl_exec($ch);
curl_close($ch);

However, PHP does not expose the constant CURLOPT_POSTFIELDSIZE, i.e. I have no option to tell curl the length of the request. As a result, I cannot stream a request body using CURLOPT_READFUNCTION without chunked encoding.

PHP Version

8.0.12

Operating System

Ubuntu 21.10

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 PHP 对 CURLOPT_READFUNCTION 的 curl 选项处理以及缺失的 CURLOPT_POSTFIELDSIZE 绑定开始,然后将这些入口点与 libcurl 中对应的选项进行比较。重现 issue 中所示的请求,并验证流式 body 可以在不使用 chunked encoding 的情况下提供其长度。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, php
领域
networking
Issue 类型
功能
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。