1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Client\Socket; |
4
|
|
|
|
5
|
|
|
use Http\Client\Socket\Exception\StreamException; |
6
|
|
|
use Http\Client\Socket\Exception\TimeoutException; |
7
|
|
|
use Psr\Http\Message\StreamInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Stream implementation for Socket Client |
11
|
|
|
* |
12
|
|
|
* This implementation is used to have a Stream which react better to the Socket Client behavior. |
13
|
|
|
* |
14
|
|
|
* The main advantage is you can get the response of a request even if it's not finish, the response is available |
15
|
|
|
* as soon as all headers are received, this stream will have the remaining socket used for the request / response |
16
|
|
|
* call. |
17
|
|
|
* |
18
|
|
|
* It is only readable once, if you want to read the content multiple times, you can store contents of this |
19
|
|
|
* stream into a variable or encapsulate it in a buffered stream. |
20
|
|
|
* |
21
|
|
|
* Writing and seeking is disable to avoid weird behaviors. |
22
|
|
|
* |
23
|
|
|
* @author Joel Wurtz <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Stream implements StreamInterface |
26
|
|
|
{ |
27
|
|
|
/** @var resource Underlying socket */ |
28
|
|
|
private $socket; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool Is stream detached |
32
|
|
|
*/ |
33
|
|
|
private $isDetached = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int|null Size of the stream, so we know what we must read, null if not available (i.e. a chunked stream) |
37
|
|
|
*/ |
38
|
|
|
private $size; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int Size of the stream readed, to avoid reading more than available and have the user blocked |
42
|
|
|
*/ |
43
|
|
|
private $readed = 0; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create the stream |
47
|
|
|
* |
48
|
|
|
* @param resource $socket |
49
|
|
|
* @param integer $size |
50
|
|
|
*/ |
51
|
70 |
|
public function __construct($socket, $size = null) |
52
|
|
|
{ |
53
|
70 |
|
$this->socket = $socket; |
54
|
70 |
|
$this->size = $size; |
55
|
70 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
1 |
|
public function __toString() |
61
|
|
|
{ |
62
|
1 |
|
return (string)$this->getContents(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
4 |
|
public function close() |
69
|
|
|
{ |
70
|
4 |
|
fclose($this->socket); |
71
|
4 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
1 |
|
public function detach() |
77
|
|
|
{ |
78
|
1 |
|
$this->isDetached = true; |
79
|
1 |
|
$socket = $this->socket; |
80
|
1 |
|
$this->socket = null; |
81
|
|
|
|
82
|
1 |
|
return $socket; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
57 |
|
public function getSize() |
89
|
|
|
{ |
90
|
57 |
|
return $this->size; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
*/ |
96
|
1 |
|
public function tell() |
97
|
|
|
{ |
98
|
1 |
|
return ftell($this->socket); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritDoc} |
103
|
|
|
*/ |
104
|
1 |
|
public function eof() |
105
|
|
|
{ |
106
|
1 |
|
return feof($this->socket); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
1 |
|
public function isSeekable() |
113
|
|
|
{ |
114
|
1 |
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
*/ |
120
|
1 |
|
public function seek($offset, $whence = SEEK_SET) |
121
|
|
|
{ |
122
|
1 |
|
throw new StreamException("This stream is not seekable"); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritDoc} |
127
|
|
|
*/ |
128
|
1 |
|
public function rewind() |
129
|
|
|
{ |
130
|
1 |
|
throw new StreamException("This stream is not seekable"); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritDoc} |
135
|
|
|
*/ |
136
|
1 |
|
public function isWritable() |
137
|
|
|
{ |
138
|
1 |
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritDoc} |
143
|
|
|
*/ |
144
|
1 |
|
public function write($string) |
145
|
|
|
{ |
146
|
1 |
|
throw new StreamException("This stream is not writable"); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
*/ |
152
|
1 |
|
public function isReadable() |
153
|
|
|
{ |
154
|
1 |
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritDoc} |
159
|
|
|
*/ |
160
|
3 |
|
public function read($length) |
161
|
|
|
{ |
162
|
3 |
|
if (null === $this->getSize()) { |
163
|
|
|
return fread($this->socket, $length); |
164
|
|
|
} |
165
|
|
|
|
166
|
3 |
|
if ($this->getSize() < ($this->readed + $length)) { |
167
|
|
|
throw new StreamException("Cannot read more than %s", $this->getSize() - $this->readed); |
168
|
|
|
} |
169
|
|
|
|
170
|
3 |
|
if ($this->getSize() === $this->readed) { |
171
|
1 |
|
return ""; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// Even if we request a length a non blocking stream can return less data than asked |
175
|
3 |
|
$read = fread($this->socket, $length); |
176
|
|
|
|
177
|
3 |
|
if ($this->getMetadata('timed_out')) { |
178
|
|
|
throw new TimeoutException("Stream timed out while reading data"); |
179
|
|
|
} |
180
|
|
|
|
181
|
3 |
|
$this->readed += strlen($read); |
182
|
|
|
|
183
|
3 |
|
return $read; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritDoc} |
188
|
|
|
*/ |
189
|
57 |
|
public function getContents() |
190
|
|
|
{ |
191
|
57 |
|
if (null === $this->getSize()) { |
192
|
54 |
|
return stream_get_contents($this->socket); |
193
|
|
|
} |
194
|
|
|
|
195
|
3 |
|
$contents = ""; |
196
|
|
|
|
197
|
|
|
do { |
198
|
3 |
|
$contents .= $this->read($this->getSize() - $this->readed); |
199
|
3 |
|
} while ($this->readed < $this->getSize()); |
200
|
|
|
|
201
|
3 |
|
return $contents; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritDoc} |
206
|
|
|
*/ |
207
|
4 |
|
public function getMetadata($key = null) |
208
|
|
|
{ |
209
|
4 |
|
$meta = stream_get_meta_data($this->socket); |
210
|
|
|
|
211
|
4 |
|
if (null === $key) { |
212
|
|
|
return $meta; |
213
|
|
|
} |
214
|
|
|
|
215
|
4 |
|
return $meta[$key]; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|