@@ -59,7 +59,7 @@ public function request(string $method, string $url, array $options = []): Respo
59
59
return new AsyncResponse ($ this ->client , $ method , $ url , $ options , function (ChunkInterface $ chunk , AsyncContext $ context ) use ($ method , $ url , $ options , &$ retryCount , &$ content , &$ firstChunk ) {
60
60
$ exception = null ;
61
61
try {
62
- if ($ chunk ->isTimeout () || null !== $ chunk ->getInformationalStatus ()) {
62
+ if ($ chunk ->isTimeout () || null !== $ chunk ->getInformationalStatus () || $ context -> getInfo ( ' canceled ' ) ) {
63
63
yield $ chunk ;
64
64
65
65
return ;
@@ -76,23 +76,14 @@ public function request(string $method, string $url, array $options = []): Respo
76
76
}
77
77
78
78
if (false === $ shouldRetry ) {
79
- $ context ->passthru ();
80
- if (null !== $ firstChunk ) {
81
- yield $ firstChunk ;
82
- yield $ context ->createChunk ($ content );
83
- yield $ chunk ;
84
- } else {
85
- yield $ chunk ;
86
- }
87
- $ content = '' ;
79
+ yield from $ this ->passthru ($ context , $ firstChunk , $ content , $ chunk );
88
80
89
81
return ;
90
82
}
91
83
}
92
84
} elseif ($ chunk ->isFirst ()) {
93
85
if (false === $ shouldRetry = $ this ->strategy ->shouldRetry ($ context , null , null )) {
94
- $ context ->passthru ();
95
- yield $ chunk ;
86
+ yield from $ this ->passthru ($ context , $ firstChunk , $ content , $ chunk );
96
87
97
88
return ;
98
89
}
@@ -105,9 +96,9 @@ public function request(string $method, string $url, array $options = []): Respo
105
96
return ;
106
97
}
107
98
} else {
108
- $ content .= $ chunk ->getContent ();
109
-
110
99
if (!$ chunk ->isLast ()) {
100
+ $ content .= $ chunk ->getContent ();
101
+
111
102
return ;
112
103
}
113
104
@@ -116,10 +107,7 @@ public function request(string $method, string $url, array $options = []): Respo
116
107
}
117
108
118
109
if (false === $ shouldRetry ) {
119
- $ context ->passthru ();
120
- yield $ firstChunk ;
121
- yield $ context ->createChunk ($ content );
122
- $ content = '' ;
110
+ yield from $ this ->passthru ($ context , $ firstChunk , $ content , $ chunk );
123
111
124
112
return ;
125
113
}
@@ -159,4 +147,22 @@ private function getDelayFromHeader(array $headers): ?int
159
147
160
148
return null ;
161
149
}
150
+
151
+ private function passthru (AsyncContext $ context , ?ChunkInterface $ firstChunk , string &$ content , ChunkInterface $ lastChunk ): \Generator
152
+ {
153
+ $ context ->passthru ();
154
+
155
+ if (null !== $ firstChunk ) {
156
+ yield $ firstChunk ;
157
+ }
158
+
159
+ if ('' !== $ content ) {
160
+ $ chunk = $ context ->createChunk ($ content );
161
+ $ content = '' ;
162
+
163
+ yield $ chunk ;
164
+ }
165
+
166
+ yield $ lastChunk ;
167
+ }
162
168
}
0 commit comments