@@ -52,6 +52,7 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
52
52
53
53
$ this ->id = $ id = (int ) $ ch ;
54
54
$ this ->logger = $ logger ;
55
+ $ this ->shouldBuffer = $ options ['buffer ' ] ?? true ;
55
56
$ this ->timeout = $ options ['timeout ' ] ?? null ;
56
57
$ this ->info ['http_method ' ] = $ method ;
57
58
$ this ->info ['user_data ' ] = $ options ['user_data ' ] ?? null ;
@@ -65,50 +66,25 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
65
66
curl_setopt ($ ch , CURLOPT_PRIVATE , \in_array ($ method , ['GET ' , 'HEAD ' , 'OPTIONS ' , 'TRACE ' ], true ) && 1.0 < (float ) ($ options ['http_version ' ] ?? 1.1 ) ? 'H2 ' : 'H0 ' ); // H = headers + retry counter
66
67
}
67
68
68
- if (null === $ content = &$ this ->content ) {
69
- $ content = null === $ options || true === $ options ['buffer ' ] ? fopen ('php://temp ' , 'w+ ' ) : (\is_resource ($ options ['buffer ' ]) ? $ options ['buffer ' ] : null );
70
- } else {
71
- // Move the pushed response to the activity list
72
- $ buffer = $ options ['buffer ' ];
73
-
74
- if ('H ' !== curl_getinfo ($ ch , CURLINFO_PRIVATE )[0 ]) {
75
- if ($ options ['buffer ' ] instanceof \Closure) {
76
- try {
77
- [$ content , $ buffer ] = [null , $ content ];
78
- [$ content , $ buffer ] = [$ buffer , $ options ['buffer ' ]($ headers )];
79
- } catch (\Throwable $ e ) {
80
- $ multi ->handlesActivity [$ id ][] = null ;
81
- $ multi ->handlesActivity [$ id ][] = $ e ;
82
- [$ content , $ buffer ] = [$ buffer , false ];
83
- }
84
- }
85
-
86
- if (ftell ($ content )) {
87
- rewind ($ content );
88
- $ multi ->handlesActivity [$ id ][] = stream_get_contents ($ content );
89
- }
90
- }
91
-
92
- if (\is_resource ($ buffer )) {
93
- $ content = $ buffer ;
94
- } elseif (true !== $ buffer ) {
95
- $ content = null ;
96
- }
97
- }
98
-
99
- curl_setopt ($ ch , CURLOPT_HEADERFUNCTION , static function ($ ch , string $ data ) use (&$ info , &$ headers , $ options , $ multi , $ id , &$ location , $ resolveRedirect , $ logger , &$ content ): int {
100
- return self ::parseHeaderLine ($ ch , $ data , $ info , $ headers , $ options , $ multi , $ id , $ location , $ resolveRedirect , $ logger , $ content );
69
+ curl_setopt ($ ch , CURLOPT_HEADERFUNCTION , static function ($ ch , string $ data ) use (&$ info , &$ headers , $ options , $ multi , $ id , &$ location , $ resolveRedirect , $ logger ): int {
70
+ return self ::parseHeaderLine ($ ch , $ data , $ info , $ headers , $ options , $ multi , $ id , $ location , $ resolveRedirect , $ logger );
101
71
});
102
72
103
73
if (null === $ options ) {
104
74
// Pushed response: buffer until requested
105
- curl_setopt ($ ch , CURLOPT_WRITEFUNCTION , static function ($ ch , string $ data ) use (&$ content ): int {
106
- return fwrite ($ content , $ data );
75
+ curl_setopt ($ ch , CURLOPT_WRITEFUNCTION , static function ($ ch , string $ data ) use ($ multi , $ id ): int {
76
+ $ multi ->handlesActivity [$ id ][] = $ data ;
77
+ curl_pause ($ ch , CURLPAUSE_RECV );
78
+
79
+ return \strlen ($ data );
107
80
});
108
81
109
82
return ;
110
83
}
111
84
85
+ $ this ->inflate = !isset ($ options ['normalized_headers ' ]['accept-encoding ' ]);
86
+ curl_pause ($ ch , CURLPAUSE_CONT );
87
+
112
88
if ($ onProgress = $ options ['on_progress ' ]) {
113
89
$ url = isset ($ info ['url ' ]) ? ['url ' => $ info ['url ' ]] : [];
114
90
curl_setopt ($ ch , CURLOPT_NOPROGRESS , false );
@@ -128,33 +104,16 @@ public function __construct(CurlClientState $multi, $ch, array $options = null,
128
104
});
129
105
}
130
106
131
- curl_setopt ($ ch , CURLOPT_WRITEFUNCTION , static function ($ ch , string $ data ) use (& $ content , $ multi , $ id ): int {
107
+ curl_setopt ($ ch , CURLOPT_WRITEFUNCTION , static function ($ ch , string $ data ) use ($ multi , $ id ): int {
132
108
$ multi ->handlesActivity [$ id ][] = $ data ;
133
109
134
- return null !== $ content ? fwrite ( $ content , $ data ) : \strlen ($ data );
110
+ return \strlen ($ data );
135
111
});
136
112
137
113
$ this ->initializer = static function (self $ response ) {
138
- if (null !== $ response ->info ['error ' ]) {
139
- throw new TransportException ($ response ->info ['error ' ]);
140
- }
141
-
142
114
$ waitFor = curl_getinfo ($ ch = $ response ->handle , CURLINFO_PRIVATE );
143
115
144
- if ('H ' === $ waitFor [0 ] || 'D ' === $ waitFor [0 ]) {
145
- try {
146
- foreach (self ::stream ([$ response ]) as $ chunk ) {
147
- if ($ chunk ->isFirst ()) {
148
- break ;
149
- }
150
- }
151
- } catch (\Throwable $ e ) {
152
- // Persist timeouts thrown during initialization
153
- $ response ->info ['error '] = $ e ->getMessage ();
154
- $ response ->close ();
155
- throw $ e ;
156
- }
157
- }
116
+ return 'H ' === $ waitFor [0 ] || 'D ' === $ waitFor [0 ];
158
117
};
159
118
160
119
// Schedule the request in a non-blocking way
@@ -241,6 +200,7 @@ public function __destruct()
241
200
*/
242
201
private function close (): void
243
202
{
203
+ $ this ->inflate = null ;
244
204
unset($ this ->multi ->openHandles [$ this ->id ], $ this ->multi ->handlesActivity [$ this ->id ]);
245
205
curl_setopt ($ this ->handle , CURLOPT_PRIVATE , '_0 ' );
246
206
@@ -419,22 +379,6 @@ private static function parseHeaderLine($ch, string $data, array &$info, array &
419
379
}
420
380
421
381
curl_setopt ($ ch , CURLOPT_PRIVATE , $ waitFor );
422
-
423
- try {
424
- if (!$ content && $ options ['buffer ' ] instanceof \Closure && $ content = $ options ['buffer ' ]($ headers ) ?: null ) {
425
- $ content = \is_resource ($ content ) ? $ content : fopen ('php://temp ' , 'w+ ' );
426
- }
427
-
428
- if (null !== $ info ['error ' ]) {
429
- throw new TransportException ($ info ['error ' ]);
430
- }
431
- } catch (\Throwable $ e ) {
432
- $ multi ->handlesActivity [$ id ] = $ multi ->handlesActivity [$ id ] ?? [new FirstChunk ()];
433
- $ multi ->handlesActivity [$ id ][] = null ;
434
- $ multi ->handlesActivity [$ id ][] = $ e ;
435
-
436
- return 0 ;
437
- }
438
382
} elseif (null !== $ info ['redirect_url ' ] && $ logger ) {
439
383
$ logger ->info (sprintf ('Redirecting: "%s %s" ' , $ info ['http_code ' ], $ info ['redirect_url ' ]));
440
384
}
0 commit comments