-
Notifications
You must be signed in to change notification settings - Fork 6
Fix #67 Fixed Cookie creation in cookie plugin. #68
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
Changes from all commits
ac127fb
facbfca
cfcaf96
8609533
8f6c70c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace Http\Client\Plugin; | ||
|
||
use Http\Client\Exception\TransferException; | ||
use Http\Message\Cookie; | ||
use Http\Message\CookieJar; | ||
use Psr\Http\Message\RequestInterface; | ||
|
@@ -86,6 +87,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl | |
* @param $setCookie | ||
* | ||
* @return Cookie|null | ||
* | ||
* @throws \Http\Client\Exception\TransferException | ||
*/ | ||
private function createCookie(RequestInterface $request, $setCookie) | ||
{ | ||
|
@@ -97,7 +100,8 @@ private function createCookie(RequestInterface $request, $setCookie) | |
|
||
list($name, $cookieValue) = $this->createValueKey(array_shift($parts)); | ||
|
||
$expires = 0; | ||
$maxAge = null; | ||
$expires = null; | ||
$domain = $request->getUri()->getHost(); | ||
$path = $request->getUri()->getPath(); | ||
$secure = false; | ||
|
@@ -109,11 +113,22 @@ private function createCookie(RequestInterface $request, $setCookie) | |
|
||
switch (strtolower($key)) { | ||
case 'expires': | ||
$expires = \DateTime::createFromFormat(DATE_COOKIE, $value); | ||
$expires = \DateTime::createFromFormat(\DateTime::COOKIE, $value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looking at the test failure on hhvm, i wonder if hhvm can't handle dates that far in the future? which in turn leads me to think we should check the return value as \DateTime::createFromFormat is one of those stupid functions that returns false instead of throwing an exception on failure. if we check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And what exception type would you like this to be? It probably needs to implement the name namespace exception interface. Can it be a TransferException? Or since there is a response a HttpException? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say it is not an HTTP related exception, so we should just fail with one of the SPL exceptions. UnexpectedValue maybe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if i am not mistaken, HttpException is to express a HTTP error code. so it would be TransferException and we say that the server sent unexpected data. looking at DateTime::COOKIE having "l, d-M-Y H:i:s T" and Y meaning 4 digit year, i have no idea what hhvm is doing here. but if it works for all versions with a two digit year lets go with that - its just mock data after all. and lets add a test that triggers the exception, with a date value of "this is not a date" or something. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say it is not an HTTP related exception, so we should just fail
with one of the SPL exceptions. UnexpectedValue maybe?
well, its kinda inverse 400: the server sent an invalid response... so i
think TransferException sounds right.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, fine from my side. |
||
|
||
if (true !== ($expires instanceof \DateTime)) { | ||
throw new TransferException( | ||
sprintf( | ||
'Cookie header `%s` expires value `%s` could not be converted to date', | ||
$name, | ||
$value | ||
) | ||
); | ||
} | ||
|
||
break; | ||
|
||
case 'max-age': | ||
$expires = (new \DateTime())->add(new \DateInterval('PT'.(int) $value.'S')); | ||
$maxAge = (int) $value; | ||
break; | ||
|
||
case 'domain': | ||
|
@@ -134,7 +149,7 @@ private function createCookie(RequestInterface $request, $setCookie) | |
} | ||
} | ||
|
||
return new Cookie($name, $cookieValue, $expires, $domain, $path, $secure, $httpOnly); | ||
return new Cookie($name, $cookieValue, $maxAge, $domain, $path, $secure, $httpOnly, $expires); | ||
} | ||
|
||
/** | ||
|
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.
the styleci complaint is here: we require a blank line between the
@return
and an@throws