File tree 3 files changed +36
-1
lines changed
src/Symfony/Component/HttpClient
3 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,8 @@ class StreamWrapper
37
37
/** @var resource|null */
38
38
private $ handle ;
39
39
40
+ private $ blocking = true ;
41
+ private $ timeout ;
40
42
private $ eof = false ;
41
43
private $ offset = 0 ;
42
44
@@ -150,7 +152,7 @@ public function stream_read(int $count)
150
152
return $ data ;
151
153
}
152
154
153
- foreach ($ this ->client ->stream ([$ this ->response ]) as $ chunk ) {
155
+ foreach ($ this ->client ->stream ([$ this ->response ], $ this -> blocking ? $ this -> timeout : 0 ) as $ chunk ) {
154
156
try {
155
157
$ this ->eof = true ;
156
158
$ this ->eof = !$ chunk ->isTimeout ();
@@ -181,6 +183,19 @@ public function stream_read(int $count)
181
183
return '' ;
182
184
}
183
185
186
+ public function stream_set_option (int $ option , int $ arg1 , ?int $ arg2 ): bool
187
+ {
188
+ if (STREAM_OPTION_BLOCKING === $ option ) {
189
+ $ this ->blocking = (bool ) $ arg1 ;
190
+ } elseif (STREAM_OPTION_READ_TIMEOUT === $ option ) {
191
+ $ this ->timeout = $ arg1 + $ arg2 / 1e6 ;
192
+ } else {
193
+ return false ;
194
+ }
195
+
196
+ return true ;
197
+ }
198
+
184
199
public function stream_tell (): int
185
200
{
186
201
return $ this ->offset ;
Original file line number Diff line number Diff line change @@ -75,4 +75,20 @@ public function testToStream404()
75
75
$ response = $ client ->request ('GET ' , 'http://localhost:8057/404 ' );
76
76
$ stream = $ response ->toStream ();
77
77
}
78
+
79
+ public function testNonBlockingStream ()
80
+ {
81
+ $ client = $ this ->getHttpClient (__FUNCTION__ );
82
+ $ response = $ client ->request ('GET ' , 'http://localhost:8057/timeout-body ' );
83
+ $ stream = $ response ->toStream ();
84
+
85
+ $ this ->assertTrue (stream_set_blocking ($ stream , false ));
86
+ $ this ->assertSame ('<1> ' , fread ($ stream , 8192 ));
87
+ $ this ->assertFalse (feof ($ stream ));
88
+
89
+ $ this ->assertTrue (stream_set_blocking ($ stream , true ));
90
+ $ this ->assertSame ('<2> ' , fread ($ stream , 8192 ));
91
+ $ this ->assertSame ('' , fread ($ stream , 8192 ));
92
+ $ this ->assertTrue (feof ($ stream ));
93
+ }
78
94
}
Original file line number Diff line number Diff line change @@ -171,6 +171,10 @@ protected function getHttpClient(string $testCase): HttpClientInterface
171
171
172
172
return $ client ;
173
173
174
+ case 'testNonBlockingStream ' :
175
+ $ responses [] = new MockResponse ((function () { yield '<1> ' ; yield '' ; yield '<2> ' ; })(), ['response_headers ' => $ headers ]);
176
+ break ;
177
+
174
178
case 'testMaxDuration ' :
175
179
$ mock = $ this ->getMockBuilder (ResponseInterface::class)->getMock ();
176
180
$ mock ->expects ($ this ->any ())
You can’t perform that action at this time.
0 commit comments