From 1f9717d192137d06927846cc2f2f7e380e5da834 Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Sat, 23 Mar 2013 16:03:38 +0100 Subject: [PATCH] THRIFT-1878 php: THttpClient - Add the possibility to send custom headers Patch: Laurent Sarrazin --- lib/php/lib/Thrift/Transport/THttpClient.php | 26 ++++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/php/lib/Thrift/Transport/THttpClient.php b/lib/php/lib/Thrift/Transport/THttpClient.php index 87f2871a..f46b18a9 100644 --- a/lib/php/lib/Thrift/Transport/THttpClient.php +++ b/lib/php/lib/Thrift/Transport/THttpClient.php @@ -82,6 +82,13 @@ class THttpClient extends TTransport { */ protected $timeout_; + /** + * http headers + * + * @var array + */ + protected $headers_; + /** * Make a new HTTP client. * @@ -100,6 +107,7 @@ class THttpClient extends TTransport { $this->buf_ = ''; $this->handle_ = null; $this->timeout_ = null; + $this->headers_ = array(); } /** @@ -176,11 +184,15 @@ class THttpClient extends TTransport { // God, PHP really has some esoteric ways of doing simple things. $host = $this->host_.($this->port_ != 80 ? ':'.$this->port_ : ''); - $headers = array('Host: '.$host, - 'Accept: application/x-thrift', - 'User-Agent: PHP/THttpClient', - 'Content-Type: application/x-thrift', - 'Content-Length: '.TStringFuncFactory::create()->strlen($this->buf_)); + $headers = array(); + $defaultHeaders = array('Host' => $host, + 'Accept' => 'application/x-thrift', + 'User-Agent' => 'PHP/THttpClient', + 'Content-Type' => 'application/x-thrift', + 'Content-Length' => TStringFuncFactory::create()->strlen($this->buf_)); + foreach (array_merge($defaultHeaders, $this->headers_) as $key => $value) { + $headers[] = "$key: $value"; + } $options = array('method' => 'POST', 'header' => implode("\r\n", $headers), @@ -202,4 +214,8 @@ class THttpClient extends TTransport { } } + public function addHeaders($headers) { + $this->headers_ = array_merge($this->headers_, $headers); + } + } -- 2.17.1