Skip to content

Commit

Permalink
Added support for cookie management. Can now add and delete cookies - #…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnnnyw committed Nov 16, 2016
1 parent 65564e8 commit 78887ba
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 30 deletions.
70 changes: 70 additions & 0 deletions src/JonnyW/PhantomJs/Http/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ abstract class AbstractRequest
*/
protected $settings;

/**
* Cookies
*
* @var array
* @access protected
*/
protected $cookies;

/**
* Request data
*
Expand Down Expand Up @@ -118,6 +126,11 @@ public function __construct($url = null, $method = RequestInterface::METHOD_GET,
$this->viewportWidth = 0;
$this->viewportHeight = 0;

$this->cookies = array(
'add' => array(),
'delete' => array()
);

$this->setMethod($method);
$this->setTimeout($timeout);

Expand Down Expand Up @@ -419,6 +432,63 @@ public function getSettings()
return $this->settings;
}

/**
* Add cookie.
*
* @access public
* @param string $name
* @param mixed $value
* @param string $path
* @param string $domain
* @param bool $httpOnly (default: true)
* @param bool $secure (default: false)
* @param int $expires (default: null)
* @return \JonnyW\PhantomJs\Http\AbstractRequest
*/
public function addCookie($name, $value, $path, $domain, $httpOnly = true, $secure = false, $expires = null)
{
$filter = function ($value) {
return !is_null($value);
};

$this->cookies['add'][] = array_filter(array(
'name' => $name,
'value' => $value,
'path' => $path,
'domain' => $domain,
'httponly' => $httpOnly,
'secure' => $secure,
'expires' => $expires
), $filter);

return $this;
}

/**
* Delete cookie.
*
* @access public
* @param string $name
* @return \JonnyW\PhantomJs\Http\AbstractRequest
*/
public function deleteCookie($name)
{
$this->cookies['delete'][] = $name;

return $this;
}

/**
* Get cookies
*
* @access public
* @return array
*/
public function getCookies()
{
return $this->cookies;
}

/**
* Set body styles
*
Expand Down
16 changes: 16 additions & 0 deletions src/JonnyW/PhantomJs/Http/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ public function addHeaders(array $headers);
*/
public function getHeaders();

/**
* Get settings
*
* @access public
* @return array|string
*/
public function getSettings();

/**
* Get cookies
*
* @access public
* @return array|string
*/
public function getCookies();

/**
* Set body styles
*
Expand Down
6 changes: 5 additions & 1 deletion src/JonnyW/PhantomJs/Resources/procedures/http_default.proc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var page = require('webpage').create(),
*/
[[ engine.load('page_viewport_size') ]]


/**
* Define custom headers.
*/
Expand All @@ -43,6 +42,11 @@ var page = require('webpage').create(),
*/
[[ engine.load('page_settings') ]]

/**
* Page cookies
*/
[[ engine.load('page_cookies') ]]

/**
* On resource timeout
*/
Expand Down
6 changes: 5 additions & 1 deletion src/JonnyW/PhantomJs/Resources/procedures/http_lazy.proc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var page = require('webpage').create(),
*/
[[ engine.load('page_viewport_size') ]]


/**
* Define custom headers.
*/
Expand All @@ -45,6 +44,11 @@ var page = require('webpage').create(),
*/
[[ engine.load('page_settings') ]]

/**
* Page cookies
*/
[[ engine.load('page_cookies') ]]

/**
* On resource timeout
*/
Expand Down
23 changes: 23 additions & 0 deletions src/JonnyW/PhantomJs/Resources/procedures/page_cookies.partial
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

var cookies = {{ input.getCookies()|json_encode() }};

cookies.delete.forEach(function(name) {

if(name == '*') {
page.clearCookies();
debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Deleted all cookies');
} else {

if(page.deleteCookie(name)) {
debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Deleted cookie ' + name);
}
}
});


cookies.add.forEach(function(cookie) {

if(page.addCookie(cookie)) {
debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Added cookie ' + cookie.name + '=' + cookie.value);
}
});
Loading

0 comments on commit 78887ba

Please # to comment.