|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author jan huang <[email protected]> |
|
4
|
|
|
* @copyright 2017 |
|
5
|
|
|
* |
|
6
|
|
|
* @see https://www.github.com/janhuang |
|
7
|
|
|
* @see http://www.fast-d.cn/ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace FastD\Servitization\Client; |
|
11
|
|
|
|
|
12
|
|
|
use FastD\Http\Response; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Consumer. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Client extends \FastD\Swoole\Client |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @return string |
|
21
|
|
|
*/ |
|
22
|
|
|
public function getProtocol() |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->scheme; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $data |
|
29
|
|
|
* |
|
30
|
|
|
* @return Response |
|
31
|
|
|
*/ |
|
32
|
|
|
public function send($data = '') |
|
33
|
|
|
{ |
|
34
|
|
|
$response = parent::send($data); |
|
35
|
|
|
|
|
36
|
|
|
return $this->wrapResponse($response); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param $response |
|
41
|
|
|
* |
|
42
|
|
|
* @return Response |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function wrapResponse($response) |
|
45
|
|
|
{ |
|
46
|
|
|
$statusCode = 200; |
|
47
|
|
|
$headers = []; |
|
48
|
|
|
|
|
49
|
|
|
if (false !== (strpos($response, "\r\n\r\n")) && false !== strpos($this->scheme, 'http')) { |
|
50
|
|
|
list($responseHeaders, $response) = explode("\r\n\r\n", $response, 2); |
|
51
|
|
|
$responseHeaders = preg_split('/\r\n/', $responseHeaders, null, PREG_SPLIT_NO_EMPTY); |
|
52
|
|
|
|
|
53
|
|
|
$code = array_shift($responseHeaders); |
|
54
|
|
|
list(, $statusCode) = explode(' ', $code); |
|
55
|
|
|
$headers = []; |
|
56
|
|
|
array_map(function ($headerLine) use (&$headers) { |
|
57
|
|
|
list($key, $value) = explode(':', $headerLine, 2); |
|
58
|
|
|
$headers[$key] = trim($value); |
|
59
|
|
|
unset($headerLine, $key, $value); |
|
60
|
|
|
}, $responseHeaders); |
|
61
|
|
|
|
|
62
|
|
|
if (isset($headers['Content-Encoding'])) { |
|
63
|
|
|
$response = zlib_decode($response); |
|
64
|
|
|
} |
|
65
|
|
|
unset($responseHeaders, $code); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return new Response($response, $statusCode, $headers); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|