Skip to content

Conversation

KTP95
Copy link

@KTP95 KTP95 commented Aug 25, 2015

There has a bug in unsetHeader()


The unsetHeader bad work,
for example

$curl->setHeader('Host','example.com');
$curl->unsetHeader('Host');
$curl->get('http://127.0.0.1/');


This request is make with Header "Host" = '' (empty string), because unsetHeader bad work

with the my fix the header 'Host' with be deleted and curl auto-retrive the right header.
horever is already possible make request with header 'Host' = '' (empty string) with $curl->setHeader('Host', '' );

also remove a bad comment about fuction

  • \* @return string
    
    in setHeader()

The unsetHeader bad work, 
for example
$curl->setHeader('Host','example.com');
$curl->unsetHeader('Host');
$curl->get('http://127.0.0.1/');
---------
This request is make with Header "Host" = '' (empty string), because unsetHeader bad work

with the my fix the header 'Host' with be deleted and curl auto-retrive the right header.
horever is already possible make request with header 'Host' = '' (empty string) with $curl->setHeader('Host', '' );

also remove a bad comment about fuction
   *		
-     * @return string
in setHeader()
@zachborboa
Copy link
Member

Hi. unsetHeader() correctly sets the header key without a value causing the header to not be included in the request headers. From the man page:

Remove an internal header by giving a replacement without content on the right side of the colon, as in: -H "Host:". If you send the custom header with no-value then its header must be terminated with a semicolon, such as -H "X-Custom-Header;" to send "X-Custom-Header:".

http://curl.haxx.se/docs/manpage.html#-H

@zachborboa
Copy link
Member

Example test:

// index.php
echo $_SERVER['HTTP_X_FOO'];
$curl = new Curl();
$curl->setHeader('X-FOO', 'BAR');
$curl->post('http://127.0.0.1:8000');
echo $curl->rawResponse . "\n"; // returns 'BAR'
$curl = new Curl();
$curl->setHeader('X-FOO', 'BAR');
$curl->unsetHeader('X-FOO');
$curl->post('http://127.0.0.1:8000');
echo $curl->rawResponse . "\n"; // returns 'Notice: Undefined index: HTTP_X_FOO'

@KTP95
Copy link
Author

KTP95 commented Aug 26, 2015

OK, you are right, I Dont know curl 100%, i'm sorry. but there are a problem:

By Default Curl send the following headers with a value auto-retrieve:

Request-Line , Host , Accept

So if i force another value, for example for "Host" with

$curl->setHeader('Host', 'example.com');

Don't exist a method, in your class for restore to 'Host' a default value that is auto-retrived

I thought that unsetHeader will be planned for do this, but it is planned for Remove header, not for restore, so is missing a method: setDefaultHeader

@KTP95
Copy link
Author

KTP95 commented Aug 26, 2015

Horever in your class there're a bug, please try this

$curl = new Curl();
$curl->unsetHeader('Host');
$curl->setHeader('X-FOO', 'BAR');
$curl->get('http://example.com:8080/');

the header 'Host' will be reset to default value and it is present in the request

unsetHeader() function must be edit and remove the unset

/**
 * Unset Header
 *
 * @access public
 * @param  $key
 */
public function unsetHeader($key)
{
    $this->setHeader($key, '');
}

@KTP95
Copy link
Author

KTP95 commented Aug 26, 2015

i was write the setDefaultHeader() method , please let see if everything is ok

public function setDefaultHeader($key)
{
    unset($this->headers[$key]);
    $this->buildHeaders();
}

$curl = new Curl();
$curl->setHeader('Host', 'example.com');
$curl->get('http://127.0.0.1:8000/');


 [requestHeaders] => CaseInsensitiveArray Object
    (
        [container:CaseInsensitiveArray:private] => Array
            (
                [Request-Line] => GET /test/ HTTP/1.1
                [User-Agent] => PHP-Curl-Class/4.6.9 (+https://github.com/php-curl-class/php-curl-class) PHP/5.4.16 curl/7.29.0
                [Accept] => */*
                [Host] => example.com
            )

    )

$curl = new Curl();
$curl->setHeader('Host', '');
$curl->get('http://127.0.0.1:8000/');


[requestHeaders] => CaseInsensitiveArray Object
    (
        [container:CaseInsensitiveArray:private] => Array
            (
                [Request-Line] => GET /test/ HTTP/1.1
                [User-Agent] => PHP-Curl-Class/4.6.9 (+https://github.com/php-curl-class/php-curl-class) PHP/5.4.16 curl/7.29.0
                [Accept] => */*
            )

    )

$curl = new Curl();
$curl->setHeader('Host', 'example.com');
$curl->setDefaultHeader('Host');
$curl->get('http://another-example.com/');


  [requestHeaders] => CaseInsensitiveArray Object
    (
        [container:CaseInsensitiveArray:private] => Array
            (
                [Request-Line] => GET /test/ HTTP/1.1
                [User-Agent] => PHP-Curl-Class/4.6.9 (+https://github.com/php-curl-class/php-curl-class) PHP/5.4.16 curl/7.29.0
                [Host] => another-example.com
                [Accept] => */*
            )

    )

I Thinks is all right now,

Confirm ?

@zachborboa
Copy link
Member

Can you provide a use case for implementing Curl::setDefaultHeader?

@zachborboa
Copy link
Member

As mentioned in #237 (comment), unsetHeader() unsets the header you previously set. It does not reset any header. A use case is needed for implementing Curl::setDefaultHeader.

@zachborboa zachborboa closed this Aug 18, 2016
@zachborboa
Copy link
Member

Additional note, think of unsetHeader() as removeHeader() which will remove an internal header.

@zachborboa
Copy link
Member

Reopening to implement separate unsetHeader() and removeHeader().

@zachborboa
Copy link
Member

Thanks for the suggestion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants