|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Http\Client\Socket; |
|
4
|
|
|
|
|
5
|
|
|
use Http\Client\Exception\NetworkException; |
|
6
|
|
|
use Http\Message\MessageFactory; |
|
7
|
|
|
use Psr\Http\Message\RequestInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Method for reading response |
|
12
|
|
|
* |
|
13
|
|
|
* Mainly used by SocketHttpClient |
|
14
|
|
|
* |
|
15
|
|
|
* @author Joel Wurtz <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
trait ResponseReader |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var MessageFactory |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $messageFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Read a response from a socket |
|
26
|
|
|
* |
|
27
|
|
|
* @param RequestInterface $request |
|
28
|
|
|
* @param resource $socket |
|
29
|
|
|
* |
|
30
|
|
|
* @throws NetworkException When the response cannot be read |
|
31
|
|
|
* |
|
32
|
|
|
* @return ResponseInterface |
|
33
|
|
|
*/ |
|
34
|
59 |
|
protected function readResponse(RequestInterface $request, $socket) |
|
35
|
|
|
{ |
|
36
|
59 |
|
$headers = []; |
|
37
|
59 |
|
$reason = null; |
|
38
|
|
|
|
|
39
|
59 |
|
while (($line = fgets($socket)) !== false) { |
|
40
|
58 |
|
if (rtrim($line) === '') { |
|
41
|
58 |
|
break; |
|
42
|
|
|
} |
|
43
|
58 |
|
$headers[] = trim($line); |
|
44
|
58 |
|
} |
|
45
|
|
|
|
|
46
|
59 |
|
$metadatas = stream_get_meta_data($socket); |
|
47
|
|
|
|
|
48
|
59 |
|
if (array_key_exists('timed_out', $metadatas) && true === $metadatas['timed_out']) { |
|
49
|
|
|
throw new NetworkException("Error while reading response, stream timed out", $request); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
59 |
|
$parts = explode(' ', array_shift($headers), 3); |
|
53
|
|
|
|
|
54
|
59 |
|
if (count($parts) <= 1) { |
|
55
|
1 |
|
throw new NetworkException('Cannot read the response', $request); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
58 |
|
$protocol = substr($parts[0], -3); |
|
59
|
58 |
|
$status = $parts[1]; |
|
60
|
|
|
|
|
61
|
58 |
|
if (isset($parts[2])) { |
|
62
|
58 |
|
$reason = $parts[2]; |
|
63
|
58 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Set the size on the stream if it was returned in the response |
|
66
|
58 |
|
$responseHeaders = []; |
|
67
|
|
|
|
|
68
|
58 |
|
foreach ($headers as $header) { |
|
69
|
58 |
|
$headerParts = explode(':', $header, 2); |
|
70
|
|
|
|
|
71
|
58 |
|
if (!array_key_exists(trim($headerParts[0]), $responseHeaders)) { |
|
72
|
58 |
|
$responseHeaders[trim($headerParts[0])] = []; |
|
73
|
58 |
|
} |
|
74
|
|
|
|
|
75
|
58 |
|
$responseHeaders[trim($headerParts[0])][] = isset($headerParts[1]) |
|
76
|
58 |
|
? trim($headerParts[1]) |
|
77
|
58 |
|
: ''; |
|
78
|
58 |
|
} |
|
79
|
|
|
|
|
80
|
58 |
|
$response = $this->messageFactory->createResponse($status, $reason, $responseHeaders, null, $protocol); |
|
81
|
58 |
|
$stream = $this->createStream($socket, $response); |
|
82
|
|
|
|
|
83
|
58 |
|
return $response->withBody($stream); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Create the stream |
|
88
|
|
|
* |
|
89
|
|
|
* @param $socket |
|
90
|
|
|
* @param ResponseInterface $response |
|
91
|
|
|
* |
|
92
|
|
|
* @return Stream |
|
93
|
|
|
*/ |
|
94
|
58 |
|
protected function createStream($socket, ResponseInterface $response) |
|
95
|
|
|
{ |
|
96
|
58 |
|
$size = null; |
|
97
|
|
|
|
|
98
|
58 |
|
if ($response->hasHeader('Content-Length')) { |
|
99
|
|
|
$size = (int)$response->getHeaderLine('Content-Length'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
58 |
|
return new Stream($socket, $size); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|