@@ -40,6 +40,7 @@ class Client extends \Hprose\Client {
40
40
private $ curlVersionLittleThan720 ;
41
41
private $ results = array ();
42
42
private $ curls = array ();
43
+ private $ contexts = array ();
43
44
public static function keepSession () {
44
45
if (isset ($ _SESSION ['HPROSE_COOKIE_MANAGER ' ])) {
45
46
self ::$ cookieManager = $ _SESSION ['HPROSE_COOKIE_MANAGER ' ];
@@ -226,7 +227,7 @@ private function initCurl($curl, $request, $context) {
226
227
But PHP 5.3 can't call private method in closure,
227
228
so we comment the private keyword.
228
229
*/
229
- /*private*/ function getContents ($ response ) {
230
+ /*private*/ function getContents ($ response, $ context ) {
230
231
do {
231
232
list ($ response_headers , $ response ) = explode ("\r\n\r\n" , $ response , 2 );
232
233
$ http_response_header = explode ("\r\n" , $ response_headers );
@@ -242,6 +243,24 @@ private function initCurl($curl, $request, $context) {
242
243
$ response_status = "Unknown Error. " ;
243
244
}
244
245
} while (substr ($ response_code , 0 , 1 ) == "1 " );
246
+ $ header = array ();
247
+ foreach ($ http_response_header as $ headerline ) {
248
+ $ pair = explode (': ' , $ headerline , 2 );
249
+ $ name = trim ($ pair [0 ]);
250
+ $ value = (count ($ pair ) > 1 ) ? trim ($ pair [1 ]) : '' ;
251
+ if (array_key_exists ($ name , $ header )) {
252
+ if (is_array ($ header [$ name ])) {
253
+ $ header [$ name ][] = $ value ;
254
+ }
255
+ else {
256
+ $ header [$ name ] = array ($ header [$ name ], $ value );
257
+ }
258
+ }
259
+ else {
260
+ $ header [$ name ] = $ value ;
261
+ }
262
+ }
263
+ $ context ->httpHeader = $ header ;
245
264
if ($ response_code != '200 ' ) {
246
265
throw new Exception ($ response_code . ": " . $ response_status . "\r\n\r\n" . $ response );
247
266
}
@@ -256,7 +275,7 @@ private function syncSendAndReceive($request, stdClass $context) {
256
275
if ($ errno ) {
257
276
throw new Exception ($ errno . ": " . curl_error ($ curl ));
258
277
}
259
- $ data = $ this ->getContents ($ data );
278
+ $ data = $ this ->getContents ($ data, $ context );
260
279
curl_close ($ curl );
261
280
return $ data ;
262
281
}
@@ -266,6 +285,7 @@ private function asyncSendAndReceive($request, stdClass $context) {
266
285
$ this ->initCurl ($ curl , $ request , $ context );
267
286
$ this ->curls [] = $ curl ;
268
287
$ this ->results [] = $ result ;
288
+ $ this ->contexts [] = $ context ;
269
289
return $ result ;
270
290
}
271
291
protected function sendAndReceive ($ request , stdClass $ context ) {
@@ -291,6 +311,8 @@ public function loop() {
291
311
$ this ->curls = array ();
292
312
$ results = $ this ->results ;
293
313
$ this ->results = array ();
314
+ $ contexts = $ this ->contexts ;
315
+ $ this ->contexts = array ();
294
316
foreach ($ curls as $ curl ) {
295
317
curl_multi_add_handle ($ multicurl , $ curl );
296
318
}
@@ -304,9 +326,10 @@ public function loop() {
304
326
while ($ info = curl_multi_info_read ($ multicurl , $ msgs_in_queue )) {
305
327
$ handle = $ info ['handle ' ];
306
328
$ index = array_search ($ handle , $ curls , true );
307
- $ results [$ index ]->resolve (Future \sync (function () use ($ self , $ info , $ handle ) {
329
+ $ context = $ contexts [$ index ];
330
+ $ results [$ index ]->resolve (Future \sync (function () use ($ self , $ info , $ handle , $ context ) {
308
331
if ($ info ['result ' ] === CURLM_OK ) {
309
- return $ self ->getContents (curl_multi_getcontent ($ handle ));
332
+ return $ self ->getContents (curl_multi_getcontent ($ handle ), $ context );
310
333
}
311
334
throw new Exception ($ info ['result ' ] . ": " . curl_error ($ handle ));
312
335
}));
0 commit comments