Skip to content
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

Message->setHeader allowing duplicates #2170

Closed
MGatner opened this issue Aug 28, 2019 · 1 comment
Closed

Message->setHeader allowing duplicates #2170

MGatner opened this issue Aug 28, 2019 · 1 comment

Comments

@MGatner
Copy link
Member

MGatner commented Aug 28, 2019

Describe the bug
Message->setHeader() will accept any case for headers (as it should) but if different ones are supplied it will produce duplicate headers.

CodeIgniter 4 version
Latest develop branch

Affected module(s)
Message, Response, CURLRequest

Expected behavior, and steps to reproduce if appropriate
Consider the following:

$response = service('response');
$response->setHeader('Content-Type', 'application/json');
$response->setHeader('Content-type', 'application/json');
  	
$headers = $response->getHeaders();
var_dump($headers);

This should produce one header, but it produces two with each case-sensitive key.

EDIT - output of example:

array(2) {
  ["Content-Type"]=>
  object(CodeIgniter\HTTP\Header)#52 (2) {
    ["name":protected]=>
    string(12) "Content-Type"
    ["value":protected]=>
    string(16) "application/json"
  }
  ["Content-type"]=>
  object(CodeIgniter\HTTP\Header)#17 (2) {
    ["name":protected]=>
    string(12) "Content-type"
    ["value":protected]=>
    string(16) "application/json"
  }
}
@MGatner
Copy link
Member Author

MGatner commented Aug 28, 2019

The issue is that setHeader() isn't using the case-insensitive version from $headerMap when it checks isset:

		if (! isset($this->headers[$name]))
		{
			$this->headers[$name] = new Header($name, $value);
			$this->headerMap[strtolower($name)] = $name;
			return $this;
		}
		if (! is_array($this->headers[$name]))
		{
			$this->headers[$name] = [$this->headers[$name]];
		}
		if (isset($this->headers[$name]))
		{
			$this->headers[$name] = new Header($name, $value);
		}

There's also something weird here, because it essentially has a logic redundancy: if (! isset($x)) and if (isset($x)), and the middle array check does nothing because it is immediately overwritten by the following statement.

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

No branches or pull requests

2 participants