Skip to content

Commit c4868d6

Browse files
committed
Added response httpHeader support for http client
1 parent 335a7eb commit c4868d6

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/Hprose/Http/Client.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Client extends \Hprose\Client {
4040
private $curlVersionLittleThan720;
4141
private $results = array();
4242
private $curls = array();
43+
private $contexts = array();
4344
public static function keepSession() {
4445
if (isset($_SESSION['HPROSE_COOKIE_MANAGER'])) {
4546
self::$cookieManager = $_SESSION['HPROSE_COOKIE_MANAGER'];
@@ -226,7 +227,7 @@ private function initCurl($curl, $request, $context) {
226227
But PHP 5.3 can't call private method in closure,
227228
so we comment the private keyword.
228229
*/
229-
/*private*/ function getContents($response) {
230+
/*private*/ function getContents($response, $context) {
230231
do {
231232
list($response_headers, $response) = explode("\r\n\r\n", $response, 2);
232233
$http_response_header = explode("\r\n", $response_headers);
@@ -242,6 +243,24 @@ private function initCurl($curl, $request, $context) {
242243
$response_status = "Unknown Error.";
243244
}
244245
} 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;
245264
if ($response_code != '200') {
246265
throw new Exception($response_code . ": " . $response_status . "\r\n\r\n" . $response);
247266
}
@@ -256,7 +275,7 @@ private function syncSendAndReceive($request, stdClass $context) {
256275
if ($errno) {
257276
throw new Exception($errno . ": " . curl_error($curl));
258277
}
259-
$data = $this->getContents($data);
278+
$data = $this->getContents($data, $context);
260279
curl_close($curl);
261280
return $data;
262281
}
@@ -266,6 +285,7 @@ private function asyncSendAndReceive($request, stdClass $context) {
266285
$this->initCurl($curl, $request, $context);
267286
$this->curls[] = $curl;
268287
$this->results[] = $result;
288+
$this->contexts[] = $context;
269289
return $result;
270290
}
271291
protected function sendAndReceive($request, stdClass $context) {
@@ -291,6 +311,8 @@ public function loop() {
291311
$this->curls = array();
292312
$results = $this->results;
293313
$this->results = array();
314+
$contexts = $this->contexts;
315+
$this->contexts = array();
294316
foreach ($curls as $curl) {
295317
curl_multi_add_handle($multicurl, $curl);
296318
}
@@ -304,9 +326,10 @@ public function loop() {
304326
while ($info = curl_multi_info_read($multicurl, $msgs_in_queue)) {
305327
$handle = $info['handle'];
306328
$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) {
308331
if ($info['result'] === CURLM_OK) {
309-
return $self->getContents(curl_multi_getcontent($handle));
332+
return $self->getContents(curl_multi_getcontent($handle), $context);
310333
}
311334
throw new Exception($info['result'] . ": " . curl_error($handle));
312335
}));

0 commit comments

Comments
 (0)