@@ -32,14 +32,13 @@ final class NativeResponse implements ResponseInterface
32
32
private $ onProgress ;
33
33
private $ remaining ;
34
34
private $ buffer ;
35
- private $ inflate ;
36
35
private $ multi ;
37
36
private $ debugBuffer ;
38
37
39
38
/**
40
39
* @internal
41
40
*/
42
- public function __construct (NativeClientState $ multi , $ context , string $ url , $ options , bool $ gzipEnabled , array &$ info , callable $ resolveRedirect , ?callable $ onProgress , ?LoggerInterface $ logger )
41
+ public function __construct (NativeClientState $ multi , $ context , string $ url , array $ options , array &$ info , callable $ resolveRedirect , ?callable $ onProgress , ?LoggerInterface $ logger )
43
42
{
44
43
$ this ->multi = $ multi ;
45
44
$ this ->id = (int ) $ context ;
@@ -50,27 +49,17 @@ public function __construct(NativeClientState $multi, $context, string $url, $op
50
49
$ this ->info = &$ info ;
51
50
$ this ->resolveRedirect = $ resolveRedirect ;
52
51
$ this ->onProgress = $ onProgress ;
53
- $ this ->content = $ options ['buffer ' ] ? fopen ('php://temp ' , 'w+ ' ) : null ;
52
+ $ this ->inflate = !isset ($ options ['normalized_headers ' ]['accept-encoding ' ]);
53
+ $ this ->shouldBuffer = $ options ['buffer ' ] ?? true ;
54
54
55
- // Temporary resources to dechunk/inflate the response stream
55
+ // Temporary resource to dechunk the response stream
56
56
$ this ->buffer = fopen ('php://temp ' , 'w+ ' );
57
- $ this ->inflate = $ gzipEnabled ? inflate_init (ZLIB_ENCODING_GZIP ) : null ;
58
57
59
58
$ info ['user_data ' ] = $ options ['user_data ' ];
60
59
++$ multi ->responseCount ;
61
60
62
61
$ this ->initializer = static function (self $ response ) {
63
- if (null !== $ response ->info ['error ' ]) {
64
- throw new TransportException ($ response ->info ['error ' ]);
65
- }
66
-
67
- if (null === $ response ->remaining ) {
68
- foreach (self ::stream ([$ response ]) as $ chunk ) {
69
- if ($ chunk ->isFirst ()) {
70
- break ;
71
- }
72
- }
73
- }
62
+ return null === $ response ->remaining ;
74
63
};
75
64
}
76
65
@@ -165,7 +154,7 @@ private function open(): void
165
154
stream_set_blocking ($ h , false );
166
155
$ this ->context = $ this ->resolveRedirect = null ;
167
156
168
- // Create dechunk and inflate buffers
157
+ // Create dechunk buffers
169
158
if (isset ($ this ->headers ['content-length ' ])) {
170
159
$ this ->remaining = (int ) $ this ->headers ['content-length ' ][0 ];
171
160
} elseif ('chunked ' === ($ this ->headers ['transfer-encoding ' ][0 ] ?? null )) {
@@ -175,10 +164,6 @@ private function open(): void
175
164
$ this ->remaining = -2 ;
176
165
}
177
166
178
- if ($ this ->inflate && 'gzip ' !== ($ this ->headers ['content-encoding ' ][0 ] ?? null )) {
179
- $ this ->inflate = null ;
180
- }
181
-
182
167
$ this ->multi ->handlesActivity [$ this ->id ] = [new FirstChunk ()];
183
168
184
169
if ('HEAD ' === $ context ['http ' ]['method ' ] || \in_array ($ this ->info ['http_code ' ], [204 , 304 ], true )) {
@@ -188,7 +173,7 @@ private function open(): void
188
173
return ;
189
174
}
190
175
191
- $ this ->multi ->openHandles [$ this ->id ] = [$ h , $ this ->buffer , $ this ->inflate , $ this -> content , $ this -> onProgress , &$ this ->remaining , &$ this ->info ];
176
+ $ this ->multi ->openHandles [$ this ->id ] = [$ h , $ this ->buffer , $ this ->onProgress , &$ this ->remaining , &$ this ->info ];
192
177
}
193
178
194
179
/**
@@ -228,15 +213,15 @@ private static function perform(NativeClientState $multi, array &$responses = nu
228
213
$ multi ->handles = [];
229
214
}
230
215
231
- foreach ($ multi ->openHandles as $ i => [$ h , $ buffer , $ inflate , $ content , $ onProgress ]) {
216
+ foreach ($ multi ->openHandles as $ i => [$ h , $ buffer , $ onProgress ]) {
232
217
$ hasActivity = false ;
233
- $ remaining = &$ multi ->openHandles [$ i ][5 ];
234
- $ info = &$ multi ->openHandles [$ i ][6 ];
218
+ $ remaining = &$ multi ->openHandles [$ i ][3 ];
219
+ $ info = &$ multi ->openHandles [$ i ][4 ];
235
220
$ e = null ;
236
221
237
222
// Read incoming buffer and write it to the dechunk one
238
223
try {
239
- while ($ remaining && '' !== $ data = (string ) fread ($ h , 0 > $ remaining ? 16372 : $ remaining )) {
224
+ if ($ remaining && '' !== $ data = (string ) fread ($ h , 0 > $ remaining ? 16372 : $ remaining )) {
240
225
fwrite ($ buffer , $ data );
241
226
$ hasActivity = true ;
242
227
$ multi ->sleep = false ;
@@ -264,16 +249,8 @@ private static function perform(NativeClientState $multi, array &$responses = nu
264
249
rewind ($ buffer );
265
250
ftruncate ($ buffer , 0 );
266
251
267
- if (null !== $ inflate && false === $ data = @inflate_add ($ inflate , $ data )) {
268
- $ e = new TransportException ('Error while processing content unencoding. ' );
269
- }
270
-
271
- if ('' !== $ data && null === $ e ) {
252
+ if (null === $ e ) {
272
253
$ multi ->handlesActivity [$ i ][] = $ data ;
273
-
274
- if (null !== $ content && \strlen ($ data ) !== fwrite ($ content , $ data )) {
275
- $ e = new TransportException (sprintf ('Failed writing %d bytes to the response buffer. ' , \strlen ($ data )));
276
- }
277
254
}
278
255
}
279
256
0 commit comments