-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
feat: add Message::addHeader() to add header with the same name #8194
Conversation
4059b72
to
5c5675b
Compare
c60bb05
to
8af1e3d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get it. Multiple headers with the same name should be handled with a single Header
class that has multiple values.
Am I missing something?
The question is not strange. It seems PSR-7 specification is like that. But I don't think these are the same:
If we handle multiple headers with the same name with a single Header class that has multiple values, If we were to send three Set-Cookie headers, how would we handle them? |
The main issue I have with this is that code like this: $this->request->header('NAME')?->getValueLine(); is no longer guaranteed to work.
I don't see much difference when it comes to distinguishing between values, because in both cases we are dealing with a simple array. There are no methods that make it easy to work with multiple objects of the As you said, the PSR-7 seems to implement it just like we do right now: https://www.php-fig.org/psr/psr-7/#headers-with-multiple-values
In our case, it would be: $this->request->header('NAME')?->getValue(); |
If $this->message->setHeader('accept', ['json', 'html']);
$this->message->appendHeader('Accept', 'xml');
dd($this->message->header('accept')?->getValueLine());
// "json, html, xml" But the following code does not make sense, because we cannot use $this->message->appendHeader(
'Set-Cookie',
'logged_in=no; Path=/'
);
$this->message->appendHeader(
'Set-Cookie',
'sessid=123456; Path=/'
);
dd($this->message->header('Set-Cookie')?->getValueLine());
// string (43) "logged_in=no; Path=/, sessid=123456; Path=/" In PSR-7, we also cannot use require __DIR__ . '/vendor/autoload.php';
$response = new \Laminas\Diactoros\Response();
$cookie1 = 'user_id=123; Path=/; Expires='
. gmdate('D, d M Y H:i:s T', strtotime('+1 day'));
$cookie2 = 'session_token=abc123; Path=/; Secure; HttpOnly; SameSite=None; Expires='
. gmdate('D, d M Y H:i:s T', strtotime('+7 days'));
$response = $response->withAddedHeader('Set-Cookie', $cookie1);
$response = $response->withAddedHeader('Set-Cookie', $cookie2);
var_dump($response->getHeader('Set-Cookie'));
var_dump($response->getHeaderLine('Set-Cookie'));
|
After all, our question is: |
Yes... you are right. I clearly missed a lot. There is no other way around it. However, I still see some issues.
And that's the problem. If we call: $this->request->addHeader('NAME', 'value1');
$this->request->addHeader('NAME', 'value2'); then it will not work. We will receive an error when we try to use it. This should be at least documented in the changelog. Or even better handled just like you showed in Laminas. // Edit |
8af1e3d
to
b89f51e
Compare
Thanks. To get multiple headers and make them into one line does not make sense. |
Hopefully, others can wave in with a comment about this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the thread around the confusion and I think this is probably the best that we can do. I'm not entirely sure PSR-7 handles this correctly but that's our guide.
PSR-7 cannot handle multiple headers with the same name correctly. |
b89f51e
to
4c55a28
Compare
Rebased and added docs. |
989167f
to
5d55cd0
Compare
Description
For #5041
Message::addHeader()
Message::header()
"return an array of header objects":CodeIgniter4/system/HTTP/MessageTrait.php
Lines 120 to 121 in 0ab4d67
Checklist: