Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coroutine\Http\Client could not unpack response normally with the request header "Expect: 100-continue" #3299

Closed
Littlesqx opened this issue May 7, 2020 · 4 comments

Comments

@Littlesqx
Copy link

Littlesqx commented May 7, 2020

Please answer these questions before submitting your issue. Thanks!

  1. What did you do? If possible, provide a simple script for reproducing the error.
Swoole\Coroutine::create(function () {
    // the target server can response "Expect: 100-continue" correctly, for example nginx
    $http = new \Swoole\Coroutine\Http\Client($host, $port);
    $http->setMethod('POST');
    $http->setHeaders([
        'Content-type' => 'application/json',
        "Expect" => '100-continue',
    ]);
    $http->setData(\json_encode(['id' => 0]));
    $http->execute('/test_api'); // the request will block for a long time

    var_dump($http);
});
  1. What did you expect to see?

the print result:

object(Swoole\Coroutine\Http\Client)#2 (18) {
  ["errCode"]=>
  int(0)
  ["errMsg"]=>
  string(0) ""
  ["connected"]=>
  bool(true)
  ["host"]=>
  string(12) "host"
  ["port"]=>
  int(80)
  ["ssl"]=>
  bool(false)
  ["setting"]=>
  NULL
  ["requestMethod"]=>
  string(4) "POST"
  ["requestHeaders"]=>
  array(1) {
    ["Content-type"]=>
    string(16) "application/json"
  }
  ["requestBody"]=>
  string(8) "{"id":0}"
  ["uploadFiles"]=>
  NULL
  ["downloadFile"]=>
  NULL
  ["downloadOffset"]=>
  int(0)
  ["statusCode"]=>
  int(200)
  ["headers"]=>
  array(7) {
    ["server"]=>
    string(12) "nginx/1.15.8"
    ["content-type"]=>
    string(16) "application/json"
    ["transfer-encoding"]=>
    string(7) "chunked"
    ["connection"]=>
    string(10) "keep-alive"
    ["cache-control"]=>
    string(17) "no-cache, private"
    ["date"]=>
    string(29) "Thu, 07 May 2020 07:19:57 GMT"
  }
  ["body"]=>
  string(20) "{"data":"","code":0}"
}

  1. What did you see instead?

the print result:

object(Swoole\Coroutine\Http\Client)#2 (18) {
  ["errCode"]=>
  int(0)
  ["errMsg"]=>
  string(0) ""
  ["connected"]=>
  bool(true)
  ["host"]=>
  string(12) "host"
  ["port"]=>
  int(80)
  ["ssl"]=>
  bool(false)
  ["setting"]=>
  NULL
  ["requestMethod"]=>
  string(4) "POST"
  ["requestHeaders"]=>
  array(2) {
    ["Content-type"]=>
    string(16) "application/json"
    ["Expect"]=>
    string(12) "100-continue"
  }
  ["requestBody"]=>
  string(8) "{"id":0}"
  ["uploadFiles"]=>
  NULL
  ["downloadFile"]=>
  NULL
  ["downloadOffset"]=>
  int(0)
  ["statusCode"]=>
  int(100)
  ["headers"]=>
  NULL
  ["set_cookie_headers"]=>
  NULL
  ["cookies"]=>
  NULL
  ["body"]=>
  string(910) "HTTP/1.1 200 OK
Server: nginx/1.15.8
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: no-cache, private
Date: Thu, 07 May 2020 07:17:57 GMT

14
{"data":"","code":0}
0

"
}

If requesting without header "Expect: 100-continue", the response will be parse correctly

And request via curl is correct:

curl -H "Content-type: application/json" -H "Expect: 100-continue" -X POST -d '{"id":100}' http://host/test -v           
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 192.168.10.10...
* TCP_NODELAY set
* Connected to host (192.168.10.10) port 80 (#0)
> POST /test HTTP/1.1
> Host: larabbs.test
> User-Agent: curl/7.54.0
> Accept: */*
> Content-type: application/json
> Expect: 100-continue
> Content-Length: 10
> 
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Server: nginx/1.15.8
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
< Cache-Control: no-cache, private
< Date: Thu, 07 May 2020 07:26:16 GMT
< 
* Connection #0 to host xxx.xxx left intact
{"data":"","code":0}              
  1. What version of Swoole are you using (show your php --ri swoole)?
swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 4.4.6
Built => Sep 22 2019 23:36:25
coroutine => enabled
kqueue => enabled
rwlock => enabled
http2 => enabled
pcre => enabled
zlib => enabled
brotli => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => Off => Off
swoole.unixsock_buffer_size => 262144 => 262144

  1. What is your machine environment used (including version of kernel & php & gcc) ?
PHP 7.3.5 (cli) (built: May  2 2019 12:40:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.5, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.5, Copyright (c) 1999-2018, by Zend Technologies
@twose
Copy link
Member

twose commented May 7, 2020

Not supported, and we have no plans to support it
Why use it?

@Littlesqx
Copy link
Author

我用中文了,我是用了 hyperf/guzzle 发现的,里面的 CoroutineHandler 是基于 swoole http 协程客户端做的,guzzle 底层逻辑好像是当提交内容很大的时候,Expect:100-continue 头是会自动带上的,这好像是规范来的

@Littlesqx
Copy link
Author

我用中文了,我是用了 hyperf/guzzle 发现的,里面的 CoroutineHandler 是基于 swoole http 协程客户端做的,guzzle 底层逻辑好像是当提交内容很大的时候,Expect:100-continue 头是会自动带上的,这好像是规范来的

https://github.com/guzzle/guzzle/blob/master/src/PrepareBodyMiddleware.php#L98-L100

@twose
Copy link
Member

twose commented May 8, 2020

常见的浏览器不会发送 Expect 消息头
有些服务器默认也不支持100 continue
一般来说这个没什么用 最好是关了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants