Skip to content

Commit

Permalink
fixes to cookies not setting properly
Browse files Browse the repository at this point in the history
  • Loading branch information
donwilson committed Nov 22, 2023
1 parent 0195688 commit 97c7e9e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 69 deletions.
30 changes: 29 additions & 1 deletion src/Magnetar/Http/CookieJar/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
namespace Magnetar\Http\CookieJar;

class Cookie {
/**
* Whether the cookie has been sent
* @var bool
*/
protected bool $sent = false;

/**
* Cookie constructor
*/
Expand Down Expand Up @@ -53,6 +59,28 @@ public function __construct(

}

/**
* Set the cookie using the PHP setcookie() function
* @return void
*/
public function send(): void {
if($this->sent) {
return;
}

setcookie(
$this->name,
$this->value,
(time() + $this->expires_seconds),
$this->path,
$this->domain,
$this->secure,
$this->httponly
);

$this->sent = true;
}

/**
* Get the name of the cookie
* @return string
Expand Down Expand Up @@ -104,7 +132,7 @@ public function getExpires(): int|null {
* @param int $expires_seconds The expiration time in seconds. If null, the default will be used
* @return self
*/
public function setExpires(int|null $expires_seconds=null): self {
public function setExpires(int $expires_seconds): self {
$this->expires_seconds = $expires_seconds;

return $this;
Expand Down
15 changes: 8 additions & 7 deletions src/Magnetar/Http/CookieJar/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
declare(strict_types=1);

namespace Magnetar\Http\CookieJar;

use Magnetar\Http\Request;

use Magnetar\Application;
use Magnetar\Http\Request;
use Magnetar\Http\CookieJar\Cookie;

class CookieJar {
Expand Down Expand Up @@ -144,11 +145,11 @@ public function set(
$this->cookie_queue[ $name ] = new Cookie(
$name,
(string)$value,
$expires_seconds,
$path,
$domain,
$secure,
$httponly
$expires_seconds ?? $this->default_expires_seconds,
$path ?? $this->default_path,
$domain ?? $this->default_domain,
$secure ?? $this->default_secure,
$httponly ?? $this->default_httponly
);

return $this;
Expand Down
4 changes: 3 additions & 1 deletion src/Magnetar/Http/CookieJar/CookieJarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public function register(): void {
return (new CookieJar)->setDefaults(
$config['expires_seconds'] ?? 3600,
$config['path'] ?? '',
$config['domain'] ?? ''
$config['domain'] ?? '',
$config['secure'] ?? false,
$config['httponly'] ?? false
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,8 @@ public function __construct(
public function handle(Request $request, Closure $next): Response {
$response = $next($request);

//print('<pre>'. print_r([
// 'called_from' => 'AddPendingCookiesToResponse::handle()',
// //'response' => $response,
// 'pending_cookies' => $this->cookieJar->getQueuedCookies(),
// 'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
//], true) .'</pre>');

foreach($this->cookieJar->getQueuedCookies() as $cookie) {
// @TODO
$response->setCookie(
$cookie,
$this->cookieJar->getDefaultExpiresSeconds(),
$this->cookieJar->getDefaultPath(),
$this->cookieJar->getDefaultDomain(),
$this->cookieJar->getDefaultSecure(),
$this->cookieJar->getDefaultHttpOnly()
);
$response->setCookie($cookie);
}

return $response;
Expand Down
7 changes: 0 additions & 7 deletions src/Magnetar/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ public function process(Request $request): Response {

$this->sortMiddleware();

//die("<pre>". print_r([
// 'called_from' => 'Kernel::process()',
// 'middleware' => $this->middleware,
// 'request' => $request,
// 'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
//], true) ."</pre>");

try {
$response = (new Pipeline($this->app))
->send($request)
Expand Down
55 changes: 19 additions & 36 deletions src/Magnetar/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@ class Response {
protected int $response_code = 200;

/**
* The response body
* @var string
* Array of Cookie instances to send with the respones
* @var array
*/
protected string $body = '';
protected array $cookies = [];

/**
* Whether the response headers have been sent
* @var bool
*/
protected bool $sent_headers = false;

/**
* The response body
* @var string
*/
protected string $body = '';

/**
* Whether the response has been sent (headers and body)
* @var bool
Expand Down Expand Up @@ -85,42 +91,14 @@ public function sendBody(): self {
}

/**
* Set a cookie
* @param string $name The cookie name
* @param string|null $value The cookie value
* @param int $expires The cookie expiration time
* 0 = expire at end of session
* >0 = expire in $expires seconds
* <0 = expire in abs($expires) seconds
* @param string $path The cookie path
* @param string $domain The cookie domain
* @param bool $secure Whether the cookie should only be sent over HTTPS
* @param bool $httponly Whether the cookie should only be accessible over HTTP
* @return Response
* @note This method is a wrapper for PHP's setcookie() function
*
* @TODO needs a cookie management class
* Send a cookie through the response. Not intended to be used directly, use cookie() instead
* @param Cookie $cookie
* @return self
*/
/* public function setCookie(
string $name,
string|null $value=null,
int $expires=0,
string $path='',
string $domain='',
bool $secure=false,
bool $httponly=false
): self {
setcookie($name, $value, $expires, $path, $domain, $secure, $httponly);
return $this;
} */

public function setCookie(
string $name,
string|null $value=null,
int|null $expires=null
Cookie $cookie
): self {
// @TODO
$this->cookies[ $cookie->getName() ] = $cookie;

return $this;
}
Expand Down Expand Up @@ -156,6 +134,11 @@ public function sendHeaders(): self {
// send headers
$this->headers->send();

// send cookies
foreach($this->cookies as $cookie) {
$cookie->send();
}

// mark headers as sent
$this->sent_headers = true;

Expand Down
2 changes: 1 addition & 1 deletion src/Magnetar/_autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function cookie(
return app('cookie')->remove($name);
}

return app('cookie')->get(
return app('cookie')->set(
$name,
$value,
$expires_seconds,
Expand Down

0 comments on commit 97c7e9e

Please sign in to comment.