@@ -34,15 +34,19 @@ class Response implements ResponseInterface {
34
34
/** @var null|callable */
35
35
private $ exitCallback ;
36
36
private Deferred $ deferred ;
37
+ private CurlInterface $ curl ;
37
38
38
39
public function __construct (
39
40
private ?int $ statusCode = null ,
40
41
?ResponseHeaders $ headers = null ,
41
42
private readonly ?Request $ request = null ,
42
- private readonly ?CurlInterface $ curl = null ,
43
+ ?CurlInterface $ curl = null ,
43
44
) {
44
45
$ this ->headers = $ headers ?? new ResponseHeaders ();
45
46
$ this ->stream = new Stream ();
47
+ if ($ curl ) {
48
+ $ this ->curl = $ curl ;
49
+ }
46
50
}
47
51
48
52
/** @phpstan-ignore-next-line */
@@ -169,6 +173,42 @@ public function getResponseHeaders():ResponseHeaders {
169
173
return $ this ->headers ;
170
174
}
171
175
176
+ public function startDeferredResponse (
177
+ CurlInterface $ curl
178
+ ):Deferred {
179
+ $ this ->deferred = new Deferred ();
180
+ $ this ->curl = $ curl ;
181
+ return $ this ->deferred ;
182
+ }
183
+
184
+ public function endDeferredResponse (?string $ integrity = null ):void {
185
+ $ position = $ this ->stream ->tell ();
186
+ $ this ->stream ->rewind ();
187
+ $ contents = $ this ->stream ->getContents ();
188
+ $ this ->stream ->seek ($ position );
189
+ $ this ->checkIntegrity ($ integrity , $ contents );
190
+ $ this ->deferred ->resolve ($ contents );
191
+ }
192
+
193
+ private function checkIntegrity (?string $ integrity , string $ contents ):void {
194
+ if (is_null ($ integrity )) {
195
+ return ;
196
+ }
197
+
198
+ [$ algo , $ hash ] = explode ("- " , $ integrity );
199
+
200
+ $ availableAlgos = hash_algos ();
201
+ if (!in_array ($ algo , $ availableAlgos )) {
202
+ throw new InvalidIntegrityAlgorithmException ($ algo );
203
+ }
204
+
205
+ $ hashedContents = hash ($ algo , $ contents );
206
+
207
+ if ($ hashedContents !== $ hash ) {
208
+ throw new IntegrityMismatchException ();
209
+ }
210
+ }
211
+
172
212
/**
173
213
* Takes the Response's stream and reads it to completion. Returns a Promise which resolves with the result
174
214
* as a Gt\Http\ArrayBuffer.
0 commit comments