Skip to content

Commit

Permalink
Fix cookie session handling in profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
evaisse committed Jul 21, 2015
1 parent ccfc9bf commit 792a1e8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 124 deletions.
6 changes: 3 additions & 3 deletions DataCollector/ProfilerDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public function onResponse(FilterResponseEvent $event)
public function finishEvent($key)
{
$this->calls[$key]['stopWatchEvent']->stop();
// dump('stop' . $key, $this->calls[$key]['stopWatchEvent']);
unset($this->calls[$key]['stopWatchEvent']);
}

Expand Down Expand Up @@ -258,11 +257,12 @@ public function fetchResponseInfos(Response $response)
$data['cookies'][$c->getName()] = array(
"value" => $c->getValue(),
"domain" => $c->getDomain(),
"expires" => date('Y-m-d H:i:s', $c->getExpiresTime()),
"expires" => $c->getExpiresTime() === 0 ? 'on session close' : date('Y-m-d H:i:s', $c->getExpiresTime()),
"path" => $c->getPath(),
"secure" => $c->isSecure(),
"httpOnly" => $c->isHttpOnly(),
"cleared" => $c->isCleared(),
"cleared" => $c->getExpiresTime() !== 0
&& time() > $c->getExpiresTime(),
);
}

Expand Down
5 changes: 4 additions & 1 deletion Http/Exception/TransportException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ abstract class TransportException extends HttpException
*/
public function __construct($message = null, $code = 0, \Exception $previous = null)
{
parent::__construct(0, $message, $previous, array(), $code);
/*
* @see https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
*/
parent::__construct(599, $message, $previous, array(), $code);
}
}
120 changes: 0 additions & 120 deletions Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ public function handleMultiInfoEvent(MultiInfoEvent $e)
$headersCollector->retrieve()
);

$this->loadCookiesFromCookieFile($request, $response);

foreach ($headersCollector->getCookies() as $cookie) {
$response->headers->setCookie($cookie);
}
Expand Down Expand Up @@ -188,8 +186,6 @@ public function execute(array $stmts)

$this->services = array();

$this->setTmpCookieFile(tempnam(sys_get_temp_dir(), 'curl-cookie-file'));

foreach ($stmts as $stmt) {

$request = $stmt->getRequest();
Expand Down Expand Up @@ -227,9 +223,6 @@ public function execute(array $stmts)
// for the "non blocking" multi manager, we need to trigger the destructor
unset($mm);

$this->deleteTmpCookieFile();


return $this;

}
Expand Down Expand Up @@ -320,14 +313,11 @@ protected function prepareRawCurlHandler(Statement $stmt)

$curl = $this->getCurlRequest();

$cookieFile = $this->getTmpCookieFile();

$curl->setOptionArray(array(
CURLOPT_URL => $request->getUri(),
CURLOPT_COOKIE => $this->buildCookieString($request->cookies),
CURLINFO_HEADER_OUT => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIEJAR => $cookieFile,
));

// Set timeout
Expand Down Expand Up @@ -522,115 +512,5 @@ protected function setEventDispatcher(EventDispatcher $eventDispatcher)
return $this;
}

/**
* Extract any cookies found from the cookie file. This function expects to get
* a string containing the contents of the cookie file which it will then
* attempt to extract and return any cookies found within.
*
* @example
*
* # Netscape HTTP Cookie File
* # http://curl.haxx.se/rfc/cookie_spec.html
* # This file was generated by libcurl! Edit at your own risk.
*
* www.example.com FALSE / FALSE 1338534278 cookiename value
*
*
* # The first few lines are comments and can therefore be ignored. The cookie data consists of the following items (in the order they appear in the file.
* #
* # domain - The domain that created and that can read the variable.
* # flag - A TRUE/FALSE value indicating if all machines within a given domain can access the variable. This value is set automatically by the browser, depending on the value you set for domain.
* # path - The path within the domain that the variable is valid for.
* # secure - A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.
* # expiration - The UNIX time that the variable will expire on.
* # name - The name of the variable.
* # value - The value of the variable.
*
* @param string $string The contents of the cookie file.
*
* @return array The array of cookies as extracted from the string.
*
*/
protected function loadCookiesFromCookieFile(Request $request, Response $response)
{
$cookies = array();

if (!file_exists($this->getTmpCookieFile())) {
return;
}

$lines = explode("\n", file_get_contents($this->getTmpCookieFile()));

// iterate over lines
foreach ($lines as $line) {

// we only care for valid cookie def lines
if (isset($line[0]) && substr_count($line, "\t") == 6) {

// get tokens in an array
$tokens = explode("\t", $line);

// trim the tokens
$tokens = array_map('trim', $tokens);

$infos = array();

// Extract the data
$infos['domain'] = $tokens[0];
$infos['flag'] = (strtolower($tokens[1]) == "TRUE");
$infos['path'] = $tokens[2];
$infos['secure'] = (strtolower($tokens[3]) == "TRUE");

// Convert date to a readable format
$infos['expiration'] = new \DateTime(date('Y-m-d h:i:s', $tokens[4]));

$infos['name'] = $tokens[5];
$infos['value'] = $tokens[6];

// Record the cookie.
$cookie = new Cookie($infos['name'],
$infos['value'],
$infos['expiration'],
$infos['path'],
$infos['domain'],
$infos['secure'],
$infos['flag']);

$response->headers->setCookie($cookie);
}
}

return $cookies;
}



/**
* Get the cookie file path used to allow curl to persist the cookie jar session when multicurl parse response.
* @return string
*/
protected function getTmpCookieFile()
{
return $this->tmpCookieFile;
}

/**
* Set the cookie file path used to allow curl to persist the cookie jar session when multicurl parse response.
* @param string $tmpCookieFile a real path to the cookie file used to allow curl to persist the cookie session
*/
protected function setTmpCookieFile($tmpCookieFile)
{
$this->tmpCookieFile = $tmpCookieFile;
}

/**
* Delete tmp cookie jar file
*/
protected function deleteTmpCookieFile()
{
if (file_exists($this->tmpCookieFile)) {
unlink($this->tmpCookieFile);
}
}

}

0 comments on commit 792a1e8

Please sign in to comment.