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

[HttpClient] fix documentation of Cookies #19947

Closed
infinitum11 opened this issue Jun 7, 2024 · 3 comments
Closed

[HttpClient] fix documentation of Cookies #19947

infinitum11 opened this issue Jun 7, 2024 · 3 comments
Labels
bug hasPR A Pull Request has already been submitted for this issue. HttpClient
Milestone

Comments

@infinitum11
Copy link

infinitum11 commented Jun 7, 2024

HttpClient is used for sending HTTP requests, but not for responding to HTTP requests, so the following lines are invalid.

'headers' => [
    'Cookie' => new Cookie('flavor', 'chocolate', strtotime('+1 day')),
    // you can also pass the cookie contents as a string
    'Cookie' => 'flavor=chocolate; expires=Sat, 11 Feb 2023 12:18:13 GMT; Max-Age=86400; path=/'
],

PHP parses and populates the $_COOKIE array as follows which is wrong:

$_COOKIE = [
    "flavor" => "chocolate",
    "expires" => "Sat, 08 Jun 2024 08:34:15 GMT",
    "Max-Age" => "86400",
    "path" => "/",
    "httponly" => "",
    "samesite" => "lax",
];

So, one of the variances is to use the following syntax:

'headers' => [
    'Cookie' => 'foo=bar;baz=zap', 
    // Or an encoded format
    'Cookie' => sprintf("%s=%s", 'foo', rawurlencode('bar'))
],

See:

@javiereguiluz
Copy link
Member

@infinitum11 I think I'm missing something in your issue report. The format that you are proposing (xxx=yyy;aaa=bbb;...) is the same as the second format shown in the example. The only difference is that the Symfony example includes some white spaces. Is that the issue? Thanks.

@infinitum11
Copy link
Author

@javiereguiluz Technically they are the same, but semantically they differ. Let me explain.

When sending a cookie from a server to a client, you use the Set-Cookie HTTP Response header. For this, the Symfony\Component\HttpFoundation\Cookie class is used. It specifies attributes like path, domain, SameSite, etc.

However, when sending a cookie from a client to a server, you use the Cookie HTTP Request header (This is the HTTP Client case). There is no path, domain or other attributes, just key=value pair separated by ";".

So, when you set additional HTTP headers this way:

'headers' => [
    'Cookie' => new Cookie('flavor', 'chocolate', strtotime('+1 day')),
    // you can also pass the cookie contents as a string
    'Cookie' => 'flavor=chocolate; expires=Sat, 11 Feb 2023 12:18:13 GMT; Max-Age=86400; path=/'
],

the 6 (six) cookies are sent from the client to a server: flavor, expires, Max-Age, path, httponly, and samesite. Of course, this is not what we expect to receive on the server side.

@javiereguiluz javiereguiluz added bug hasPR A Pull Request has already been submitted for this issue. and removed Waiting feedback labels Jun 17, 2024
@javiereguiluz javiereguiluz added this to the 5.4 milestone Jun 17, 2024
@javiereguiluz
Copy link
Member

@infinitum11 thanks a lot for the detailed explanation. It's clear to me now. I created #19968 to try to fix this. Thanks!

OskarStark added a commit that referenced this issue Jun 17, 2024
…reguiluz)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[HttpClient] Fix how cookies are defined and sent

Fix #19947.

Commits
-------

0643f16 [HttpClient] Fix how cookies are defined and sent
@xabbuh xabbuh closed this as completed Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug hasPR A Pull Request has already been submitted for this issue. HttpClient
Projects
None yet
Development

No branches or pull requests

4 participants