Skip to content

Implement removeHeader() (curl -H "Host:" ...) #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ $curl->post('https://www.example.com/login/', array(
$curl = new Curl();
$curl->setBasicAuthentication('username', 'password');
$curl->setUserAgent('MyUserAgent/0.0.1 (+https://www.example.com/bot.html)');
$curl->setReferrer('');
$curl->setReferrer('https://www.example.com/url?url=https%3A%2F%2Fwww.example.com%2F');
$curl->setHeader('X-Requested-With', 'XMLHttpRequest');
$curl->setCookie('key', 'value');
$curl->get('https://www.example.com/');

if ($curl->error) {
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage;
} else {
echo $curl->response;
echo 'Response:' . "\n";
var_dump($curl->response);
}

var_dump($curl->requestHeaders);
Expand Down Expand Up @@ -201,6 +202,7 @@ Curl::patch($url, $data = array())
Curl::post($url, $data = array(), $follow_303_with_post = false)
Curl::progress($callback)
Curl::put($url, $data = array())
Curl::removeHeader($key)
Curl::search($url, $data = array())
Curl::setBasicAuthentication($username, $password = '')
Curl::setConnectTimeout($seconds)
Expand All @@ -215,6 +217,7 @@ Curl::setDefaultUserAgent()
Curl::setDefaultXmlDecoder()
Curl::setDigestAuthentication($username, $password = '')
Curl::setHeader($key, $value)
Curl::setHeaders($headers)
Curl::setJsonDecoder($function)
Curl::setMaxFilesize($bytes)
Curl::setOpt($option, $value)
Expand Down Expand Up @@ -248,6 +251,7 @@ MultiCurl::close()
MultiCurl::complete($callback)
MultiCurl::error($callback)
MultiCurl::getOpt($option)
MultiCurl::removeHeader($key)
MultiCurl::setBasicAuthentication($username, $password = '')
MultiCurl::setConcurrency($concurrency)
MultiCurl::setConnectTimeout($seconds)
Expand All @@ -257,6 +261,7 @@ MultiCurl::setCookieJar($cookie_jar)
MultiCurl::setCookieString($string)
MultiCurl::setDigestAuthentication($username, $password = '')
MultiCurl::setHeader($key, $value)
MultiCurl::setHeaders($headers)
MultiCurl::setJsonDecoder($function)
MultiCurl::setOpt($option, $value)
MultiCurl::setOpts($options)
Expand Down
49 changes: 46 additions & 3 deletions src/Curl/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public function options($url, $data = array())
$url = $this->baseUrl;
}
$this->setURL($url, $data);
$this->unsetHeader('Content-Length');
$this->removeHeader('Content-Length');
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS');
return $this->exec();
}
Expand All @@ -517,7 +517,7 @@ public function patch($url, $data = array())
}

if (is_array($data) && empty($data)) {
$this->unsetHeader('Content-Length');
$this->removeHeader('Content-Length');
}

$this->setURL($url);
Expand Down Expand Up @@ -900,6 +900,8 @@ public function setDefaultUserAgent()
/**
* Set Header
*
* Add extra header to include in the request.
*
* @access public
* @param $key
* @param $value
Expand All @@ -914,6 +916,27 @@ public function setHeader($key, $value)
$this->setOpt(CURLOPT_HTTPHEADER, $headers);
}

/**
* Set Headers
*
* Add extra headers to include in the request.
*
* @access public
* @param $headers
*/
public function setHeaders($headers)
{
foreach ($headers as $key => $value) {
$this->headers[$key] = $value;
}

$headers = array();
foreach ($this->headers as $key => $value) {
$headers[] = $key . ': ' . $value;
}
$this->setOpt(CURLOPT_HTTPHEADER, $headers);
}

/**
* Set JSON Decoder
*
Expand Down Expand Up @@ -1058,13 +1081,33 @@ public function success($callback)
/**
* Unset Header
*
* Remove extra header previously set using Curl::setHeader().
*
* @access public
* @param $key
*/
public function unsetHeader($key)
{
$this->setHeader($key, '');
unset($this->headers[$key]);
$headers = array();
foreach ($this->headers as $key => $value) {
$headers[] = $key . ': ' . $value;
}
$this->setOpt(CURLOPT_HTTPHEADER, $headers);
}

/**
* Remove Header
*
* Remove an internal header from the request.
* Using `curl -H "Host:" ...' is equivalent to $curl->removeHeader('Host');.
*
* @access public
* @param $key
*/
public function removeHeader($key)
{
$this->setHeader($key, '');
}

/**
Expand Down
50 changes: 40 additions & 10 deletions src/Curl/MultiCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function addOptions($url, $data = array())
}
$curl = new Curl();
$curl->setURL($url, $data);
$curl->unsetHeader('Content-Length');
$curl->removeHeader('Content-Length');
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS');
$this->queueHandle($curl);
return $curl;
Expand All @@ -182,7 +182,7 @@ public function addPatch($url, $data = array())
}
$curl = new Curl();
$curl->setURL($url);
$curl->unsetHeader('Content-Length');
$curl->removeHeader('Content-Length');
$curl->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH');
$curl->setOpt(CURLOPT_POSTFIELDS, $data);
$this->queueHandle($curl);
Expand Down Expand Up @@ -212,7 +212,7 @@ public function addPost($url, $data = array(), $follow_303_with_post = false)
$curl = new Curl();

if (is_array($data) && empty($data)) {
$curl->unsetHeader('Content-Length');
$curl->removeHeader('Content-Length');
}

$curl->setURL($url);
Expand Down Expand Up @@ -456,6 +456,8 @@ public function setCookieJar($cookie_jar)
/**
* Set Header
*
* Add extra header to include in the request.
*
* @access public
* @param $key
* @param $value
Expand All @@ -465,6 +467,21 @@ public function setHeader($key, $value)
$this->headers[$key] = $value;
}

/**
* Set Headers
*
* Add extra headers to include in the request.
*
* @access public
* @param $headers
*/
public function setHeaders($headers)
{
foreach ($headers as $key => $value) {
$this->headers[$key] = $value;
}
}

/**
* Set JSON Decoder
*
Expand Down Expand Up @@ -643,15 +660,30 @@ public function success($callback)
/**
* Unset Header
*
* Remove extra header previously set using Curl::setHeader().
*
* @access public
* @param $key
*/
public function unsetHeader($key)
{
$this->setHeader($key, '');
unset($this->headers[$key]);
}

/**
* Remove Header
*
* Remove an internal header from the request.
* Using `curl -H "Host:" ...' is equivalent to $curl->removeHeader('Host');.
*
* @access public
* @param $key
*/
public function removeHeader($key)
{
$this->setHeader($key, '');
}

/**
* Verbose
*
Expand Down Expand Up @@ -714,15 +746,13 @@ private function initHandle($curl)
$curl->complete($this->completeFunction);
}

foreach ($this->options as $option => $value) {
$curl->setOpt($option, $value);
}
foreach ($this->headers as $key => $value) {
$curl->setHeader($key, $value);
}
$curl->setOpts($this->options);
$curl->setHeaders($this->headers);

foreach ($this->cookies as $key => $value) {
$curl->setCookie($key, $value);
}

$curl->setJsonDecoder($this->jsonDecoder);
$curl->setXmlDecoder($this->xmlDecoder);

Expand Down
21 changes: 21 additions & 0 deletions tests/PHPCurlClass/PHPCurlClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2979,4 +2979,25 @@ public function testBuildUrlArgSeparator()
$this->assertEquals($expected_url, $actual_url);
}
}

public function testUnsetHeader()
{
$request_key = 'X-Request-Id';
$request_value = '1';
$data = array(
'test' => 'server',
'key' => 'HTTP_X_REQUEST_ID',
);

$curl = new Curl();
$curl->setHeader($request_key, $request_value);
$curl->get(Test::TEST_URL, $data);
$this->assertEquals($request_value, $curl->response);

$curl = new Curl();
$curl->setHeader($request_key, $request_value);
$curl->unsetHeader($request_key);
$curl->get(Test::TEST_URL, $data);
$this->assertEquals('', $curl->response);
}
}
25 changes: 25 additions & 0 deletions tests/PHPCurlClass/PHPMultiCurlClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2383,4 +2383,29 @@ public function testAlternativeStandardErrorOutput()

$this->assertNotEmpty($stderr);
}

public function testUnsetHeader()
{
$request_key = 'X-Request-Id';
$request_value = '1';
$data = array(
'test' => 'server',
'key' => 'HTTP_X_REQUEST_ID',
);

$multi_curl = new MultiCurl();
$multi_curl->setHeader($request_key, $request_value);
$multi_curl->addGet(Test::TEST_URL, $data)->complete(function ($instance) use ($request_value) {
PHPUnit_Framework_Assert::assertEquals($request_value, $instance->response);
});
$multi_curl->start();

$multi_curl = new MultiCurl();
$multi_curl->setHeader($request_key, $request_value);
$multi_curl->unsetHeader($request_key);
$multi_curl->addGet(Test::TEST_URL, $data)->complete(function ($instance) {
PHPUnit_Framework_Assert::assertEquals('', $instance->response);
});
$multi_curl->start();
}
}
7 changes: 6 additions & 1 deletion tests/PHPCurlClass/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
}
}

$test = isset($_SERVER['HTTP_X_DEBUG_TEST']) ? $_SERVER['HTTP_X_DEBUG_TEST'] : '';
$test = '';
if (isset($_SERVER['HTTP_X_DEBUG_TEST'])) {
$test = $_SERVER['HTTP_X_DEBUG_TEST'];
} elseif (isset($_GET['test'])) {
$test = $_GET['test'];
}
$key = isset($data_values['key']) ? $data_values['key'] : '';

if ($test === 'http_basic_auth') {
Expand Down