THRIFT-1878 php: THttpClient - Add the possibility to send custom headers
Patch: Laurent Sarrazin
diff --git a/lib/php/lib/Thrift/Transport/THttpClient.php b/lib/php/lib/Thrift/Transport/THttpClient.php
index 87f2871..f46b18a 100644
--- a/lib/php/lib/Thrift/Transport/THttpClient.php
+++ b/lib/php/lib/Thrift/Transport/THttpClient.php
@@ -83,6 +83,13 @@
protected $timeout_;
/**
+ * http headers
+ *
+ * @var array
+ */
+ protected $headers_;
+
+ /**
* Make a new HTTP client.
*
* @param string $host
@@ -100,6 +107,7 @@
$this->buf_ = '';
$this->handle_ = null;
$this->timeout_ = null;
+ $this->headers_ = array();
}
/**
@@ -176,11 +184,15 @@
// 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 @@
}
}
+ public function addHeaders($headers) {
+ $this->headers_ = array_merge($this->headers_, $headers);
+ }
+
}